-
Notifications
You must be signed in to change notification settings - Fork 103
/
Copy pathcli.ts
115 lines (110 loc) · 3.9 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
#!/usr/bin/env node
import path from "path";
import yargs from "yargs";
import { CliOptions } from "./cli-options";
import init from "./commands/init";
import install from "./commands/install";
import prepare from "./commands/prepare";
import run from "./commands/run";
import syncExpected from "./commands/sync-expected";
import compare from "./commands/compare";
import publish from "./commands/publish";
import getRegCore from "./get-reg-core";
const version = require(path.resolve(__dirname, "../package.json")).version as string;
function createOptions() {
yargs
.usage("Usage: $0 [options] <command>")
.help()
.option("h", { alias: "help", group: "Global Options:" })
.option("c", {
alias: "config",
desc: "Configuration file path.",
default: "regconfig.json",
group: "Global Options:",
})
.option("t", {
alias: "test",
desc: "Perform a trial with no changes.",
boolean: true,
default: false,
group: "Global Options:",
})
.option("v", {
alias: "verbose",
desc: "Display debug logging messages.",
boolean: true,
default: false,
group: "Global Options:",
})
.option("q", {
alias: "quiet",
desc: "Suppress logging messages",
boolean: true,
default: false,
group: "Global Options:",
})
.option("version", { desc: "Print version number.", group: "Global Options:" })
.version(version)
.boolean("use-dev-core") // This option is used for cli developers only, so does not need help.
.command("init", "Install and set up reg-suit and plugins into your project.", {
useYarn: { desc: "Whether to use yarn as npm client.", boolean: true, default: false },
useYarnWs: { desc: "Whether to use yarn workspace.", boolean: true, default: false },
})
.command("prepare", "Configure installed plugin", {
p: { alias: "plugin", array: true, desc: "Plugin name(s) you want to set up(e.g. slack-notify)." },
})
.command("run", "Run all procedure regression testing.")
.command("sync-expected", "Fetch expected images into working directory.")
.command("compare", "Compare actual images with expected images and creates report.")
.command("publish", "Publish the latest comparison result in working directory.", {
n: { alias: "notification", desc: "Send notifications with publishing", boolean: true, default: false },
})
.wrap(120)
.locale("en");
const { config, verbose, quiet, test, useYarn, useYarnWs, plugin, useDevCore, notification } = yargs.argv;
const command = yargs.argv._[0];
const logLevel = verbose ? "verbose" : quiet ? "silent" : "info";
const npmClient = useYarn || useYarnWs ? (useYarnWs ? "yarn workspace" : "yarn") : "npm";
const plugins = (plugin || []) as string[];
const noInstallCore = !!useDevCore;
return {
command,
logLevel,
configFileName: config,
noEmit: test,
npmClient,
plugins,
noInstallCore,
notification,
} as CliOptions;
}
function cli(): Promise<any> {
const options = createOptions();
const core = getRegCore(options);
core.logger.info(`${core.logger.colors.magenta("version")}: ${version}`);
if (options.command === "run" || options.command === "run-all") {
return run(options);
} else if (options.command === "sync-expected") {
return syncExpected(options);
} else if (options.command === "compare") {
return compare(options);
} else if (options.command === "publish") {
return publish(options);
} else if (options.command === "install") {
return install(options);
} else if (options.command === "prepare") {
return prepare(options);
} else if (options.command === "init") {
return init(options);
} else {
yargs.showHelp();
return Promise.resolve();
}
}
cli()
.then(() => process.exit(0))
.catch((reason: any) => {
// eslint-disable-next-line no-console
console.error(reason);
process.exit(1);
});