-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.ts
142 lines (134 loc) · 4.64 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env node
/**
* Main entry file for itemize
* @module
*/
// COPY OF SERVER-RESOLVE file
//
const replacers = {
"@onzag/itemize-text-engine": "@onzag/itemize-text-engine/nodejs",
"@onzag/itemize": "@onzag/itemize/nodejs",
};
const replacersKeys = Object.keys(replacers);
const Module = require("module");
const originalResolveFilename = Module._resolveFilename;
Module._resolveFilename = function (request: string, _parent: any) {
for (let key of replacersKeys) {
if (request.startsWith(key)) {
const replaced = request.replace(key, replacers[key]);
const modifiedArguments = [replaced, ...[].slice.call(arguments, 1)];
return originalResolveFilename.apply(this, modifiedArguments);
}
}
return originalResolveFilename.apply(this, arguments);
};
//
import setup from "./setup";
import { start, stop } from "./dev-environment";
import colors from "colors";
import buildData from "./builder";
import buildDatabase from "./dbbuilder";
import getDeployable from "./getdeployable";
import runConTest from "./general/connection-test";
// the action we are asked to execute is the thrird argument 0 is node, 1 is itemize
const action = process.argv[2];
// if this argument is help for the specific process
const wantsSpecificHelp = process.argv[3] === "--help" || process.argv[3] === "help";
// the remaining args
const remainingArgs = process.argv.slice(3);
// now this is our action registry for the actions we want to execute
const actionRegistry: {
[fn: string]: {
fn: (...remainingArgs: string[]) => Promise<void>;
description: string;
usage: string;
needsArgs: number;
arbitraryArgs?: boolean;
}
} = {
"setup": {
fn: setup,
description: "run the initial setup, you can run this utility over again to re-setup",
usage: "itemize setup (step)",
needsArgs: 0,
arbitraryArgs: true,
},
"get-deployable": {
fn: getDeployable,
description: "Provides a deployable for all clusters based on the config",
usage: "itemize get-deployable [development|production]",
needsArgs: 1,
},
"start-dev-environment": {
fn: start,
description: "Starts the development environment, as configured",
usage: "itemize start-dev-environment [development|production] (with-kibana)",
needsArgs: 1,
arbitraryArgs: true,
},
"stop-dev-environment": {
fn: stop,
description: "Stops the development environment",
usage: "itemize stop-dev-environment [development|production]",
needsArgs: 1,
},
"build-data": {
fn: buildData,
description: "Processes the itemize resources and initializes a new build number",
usage: "itemize build-data",
needsArgs: 0,
},
"build-database": {
fn: buildDatabase,
description: "Builds the database (warning you must run build-data before this) " +
"pass the argument development or production in order to specify which config to use " +
"if using a development environment, remember to run start-development-environment",
usage: "itemize build-database [development|production] (build|dump|load-dump)",
needsArgs: 1,
arbitraryArgs: true,
},
"connection-test": {
fn: runConTest,
description: "Runs a connection test to the databases specified by the configuration",
usage: "itemize connection-test [development|production]",
needsArgs: 1,
},
};
// and we trigger these in an async function
(async () => {
// so if our action is registered
if (actionRegistry[action]) {
// if we are tasked with getting specific help, or if the amount of args we need do not match
if (
wantsSpecificHelp ||
(
actionRegistry[action].needsArgs > remainingArgs.length &&
actionRegistry[action].arbitraryArgs
) || (
actionRegistry[action].needsArgs !== remainingArgs.length &&
!actionRegistry[action].arbitraryArgs
)
) {
// we show the specific usage
console.log(actionRegistry[action].description);
console.log("usage: " + colors.yellow(actionRegistry[action].usage))
} else {
// otherwise we try to execute
try {
await actionRegistry[action].fn(...remainingArgs);
} catch (err) {
// if something failed during the process we show the error stack and exit with status 1
console.log(colors.red(err.stack));
process.exit(1);
}
}
} else {
// otherwise we just show the help information
console.log(colors.green("Welcome to itemize build tool"));
// we get into the action registry
Object.keys(actionRegistry).forEach((action) => {
// and explain each action
console.log(colors.yellow(action) + "\n\t" + actionRegistry[action].description);
});
}
})();