-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathcli.ts
385 lines (360 loc) · 10.3 KB
/
cli.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import {
blueBright,
bold,
gray,
green,
red,
reset,
underline,
} from "ansi-colors";
import { prompt } from "enquirer";
import * as fs from "fs-extra";
import * as path from "path";
import * as yargs from "yargs";
import type { CheckResult } from "./lib/core/actionsAndTransformers";
import type { MigrationContextBase } from "./lib/core/migrationContextBase";
import {
Answers,
Question,
QuestionGroup,
questionGroups,
testCondition,
} from "./lib/core/questions";
import { createFiles, File, writeFiles } from "./lib/createAdapter";
import { LocalMigrationContext } from "./lib/localMigrationContext";
import { fetchPackageVersion } from "./lib/packageVersions";
import { error, executeCommand, getOwnVersion, isWindows } from "./lib/tools";
export type ConditionalTitle = (
answers: Record<string, any>,
) => string | undefined;
/** Define command line arguments */
const argv = yargs
.env("CREATE_ADAPTER")
.strict()
.usage("ioBroker adapter creator\n\nUsage: $0 [options]")
.alias("h", "help")
.alias("v", "version")
.options({
target: {
alias: "t",
type: "string",
desc: "Output directory for adapter files\n(default: current directory)",
},
skipAdapterExistenceCheck: {
alias: "x",
type: "boolean",
default: false,
desc: "Skip check if an adapter with the same name already exists on npm",
},
replay: {
alias: "r",
type: "string",
desc: "Replay answers from the given .create-adapter.json file",
},
migrate: {
alias: "m",
type: "string",
desc: "Use answers from an existing adapter directory (must be the base directory of an adapter where you find io-package.json)",
},
noInstall: {
alias: "n",
type: "boolean",
default: false,
desc: "Skip installation of dependencies",
},
install: {
alias: "i",
hidden: true,
type: "boolean",
default: false,
desc: "Force installation of dependencies",
},
})
.parseSync();
/** Where the output should be written */
const rootDir = path.resolve(argv.target || process.cwd());
async function checkAdapterExistence(name: string): Promise<CheckResult> {
try {
await fetchPackageVersion(`iobroker.${name}`);
return `The adapter ioBroker.${name} already exists!`;
} catch (e) {
return true;
}
}
const creatorOptions = {
checkAdapterExistence:
!argv.skipAdapterExistenceCheck && !argv.migrate
? checkAdapterExistence
: undefined,
};
/** Asks a series of questions on the CLI */
async function ask(): Promise<Answers> {
let answers: Record<string, any> = { cli: true, target: "directory" };
let migrationContext: MigrationContextBase | undefined;
if (!!argv.replay) {
const replayFile = path.resolve(argv.replay);
const json = await fs.readFile(replayFile, "utf8");
answers = JSON.parse(json);
answers.replay = replayFile;
}
if (!!argv.migrate) {
try {
const migrationDir = path.resolve(argv.migrate);
const ctx = new LocalMigrationContext(migrationDir);
console.log(`Migrating from ${migrationDir}`);
await ctx.load();
migrationContext = ctx;
} catch (error) {
console.error(error);
throw new Error(
"Please ensure that --migrate points to a valid adapter directory",
);
}
if (await migrationContext.fileExists(".create-adapter.json")) {
// it's just not worth trying to figure out things if the adapter was already created with create-adapter
throw new Error(
"Use --replay instead of --migrate for an adapter created with a recent version of create-adapter.",
);
}
}
async function askQuestion(q: Question): Promise<void> {
if (testCondition(q.condition, answers)) {
if (q.replay) {
q.replay(answers);
}
// Make properties dependent on previous answers
if (typeof q.initial === "function") {
q.initial = q.initial(answers);
}
if (migrationContext && q.migrate) {
let migrated = q.migrate(migrationContext, answers, q);
if (migrated instanceof Promise) {
migrated = await migrated;
}
q.initial = migrated;
}
while (true) {
let answer: Record<string, any>;
if (answers.hasOwnProperty(q.name as string)) {
// answer was loaded using the "replay" feature
answer = { [q.name as string]: answers[q.name as string] };
} else {
if (
answers.expert !== "yes" &&
q.expert &&
q.initial !== undefined
) {
// In non-expert mode, prefill the default answer for expert questions
answer = { [q.name as string]: q.initial };
} else {
// Ask the user for an answer
try {
answer = await prompt(q);
// Cancel the process if necessary
if (answer[q.name as string] == undefined)
throw new Error();
} catch (e) {
error(
(e as Error).message ||
"Adapter creation canceled!",
);
return process.exit(1);
}
}
// Apply an optional transformation
if (typeof q.resultTransform === "function") {
const transformed = q.resultTransform(
answer[q.name as string],
);
answer[q.name as string] =
transformed instanceof Promise
? await transformed
: transformed;
}
// Test the result
if (q.action != undefined) {
const testResult = await q.action(
answer[q.name as string],
creatorOptions,
);
if (typeof testResult === "string") {
error(testResult);
continue;
}
}
}
// And remember it
answers = { ...answers, ...answer };
break;
}
}
}
const questionsAndText: (QuestionGroup | string | ConditionalTitle)[] = [
"",
green.bold("====================================================="),
green.bold(
` Welcome to the ioBroker adapter creator v${getOwnVersion()}!`,
),
green.bold("====================================================="),
"",
gray(`You can cancel at any point by pressing Ctrl+C.`),
(answers) => (!!answers.replay ? green(`Replaying file`) : undefined),
(answers) => (!!answers.replay ? green(answers.replay) : undefined),
...questionGroups,
"",
underline(
"That's it. Please wait a minute while I get this working...",
),
];
for (const entry of questionsAndText) {
if (typeof entry === "string") {
// Headlines
console.log(entry);
} else if (typeof entry === "function") {
// Conditional headlines
const text = entry(answers);
if (text !== undefined) {
console.log(text);
}
} else {
// only print the headline if any of the questions are necessary
if (
entry.questions.find((qq) =>
testCondition(qq.condition, answers),
)
) {
console.log();
console.log(underline(entry.headline));
}
for (const qq of entry.questions) {
await askQuestion(qq);
}
}
}
return answers as Answers;
}
let currentStep = 0;
let maxSteps = 1;
function logProgress(message: string): void {
console.log(blueBright(`[${++currentStep}/${maxSteps}] ${message}...`));
}
/** Whether dependencies should be installed */
const installDependencies = !argv.noInstall || !!argv.install;
/** Whether an initial build should be performed */
let needsBuildStep: boolean;
/** Whether the initial commit should be performed automatically */
let gitCommit: boolean;
/** Whether dev-server should be installed */
let devServer: boolean;
/** CLI-specific functionality for creating the adapter directory */
async function setupProject_CLI(
answers: Answers,
files: File[],
): Promise<void> {
const rootDirName = path.basename(rootDir);
// make sure we are working in a directory called ioBroker.<adapterName>
const targetDir =
rootDirName.toLowerCase() ===
`iobroker.${answers.adapterName.toLowerCase()}`
? rootDir
: path.join(rootDir, `ioBroker.${answers.adapterName}`);
await writeFiles(targetDir, files);
if (installDependencies) {
logProgress("Installing dependencies");
await executeCommand(
isWindows ? "npm.cmd" : "npm",
["install", "--loglevel", "error", "--audit=false", "--fund=false"],
{ cwd: targetDir },
);
if (needsBuildStep) {
logProgress("Compiling source files");
await executeCommand(
isWindows ? "npm.cmd" : "npm",
["run", "build"],
{ cwd: targetDir, stdout: "ignore" },
);
}
}
if (devServer) {
logProgress("Installing dev-server");
await executeCommand(
isWindows ? "npm.cmd" : "npm",
[
"install",
"--loglevel",
"error",
"--global",
"--audit=false",
"--fund=false",
"@iobroker/dev-server",
],
{ cwd: targetDir },
);
await executeCommand(
isWindows ? "iobroker-dev-server.cmd" : "iobroker-dev-server",
["setup", "--adminPort", `${answers.devServerPort}`],
{ cwd: targetDir },
);
}
if (gitCommit) {
logProgress("Initializing git repo");
// As described here: https://help.github.com/articles/adding-an-existing-project-to-github-using-the-command-line/
const gitUrl =
answers.gitRemoteProtocol === "HTTPS"
? `https://github.com/${answers.authorGithub}/ioBroker.${answers.adapterName}`
: `[email protected]:${answers.authorGithub}/ioBroker.${answers.adapterName}.git`;
const gitCommandArgs = [
["init", "-b", answers.defaultBranch || "main"],
["add", "."],
["commit", "-m", "Initial commit"],
["remote", "add", "origin", gitUrl],
];
for (const args of gitCommandArgs) {
await executeCommand("git", args, {
cwd: targetDir,
stdout: "ignore",
stderr: "ignore",
});
}
}
console.log();
console.log();
console.log(blueBright("All done! Have fun programming! ") + red("♥"));
console.log(
blueBright(`Just open `) +
bold(reset(targetDir)) +
blueBright(` in your favorite editor.`),
);
console.log();
console.log(
gray(
"Hint: try CTRL-clicking the path if you have the editor open already.",
),
);
}
// Enable CI testing without stalling
if (process.env.TEST_STARTUP) {
console.log(green("Startup test succeeded - exiting..."));
throw process.exit(0);
}
(async function main() {
const answers = await ask();
if (installDependencies) {
maxSteps++;
needsBuildStep =
answers.language === "TypeScript" ||
answers.adminUi === "react" ||
answers.tabReact === "yes";
if (needsBuildStep) maxSteps++;
}
devServer = answers.devServer === "yes";
if (devServer) maxSteps++;
gitCommit = answers.gitCommit === "yes";
if (gitCommit) maxSteps++;
logProgress("Generating files");
const files = await createFiles(answers);
await setupProject_CLI(answers, files);
})().catch((error) => console.error(error));
process.on("exit", () => {
if (fs.pathExistsSync("npm-debug.log")) fs.removeSync("npm-debug.log");
});