-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcli.ts
More file actions
125 lines (110 loc) · 3.59 KB
/
cli.ts
File metadata and controls
125 lines (110 loc) · 3.59 KB
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
import process from "node:process";
import { Command } from "commander";
import pkg from "../package.json" with { type: "json" };
import { Build, Config, Release, Serve, Test } from "./index.js";
import { checkGitIgnore } from "./utils/gitignore.js";
import { logger } from "./utils/logger.js";
import { updateNotifier } from "./utils/updater.js";
async function main() {
const { name, version } = pkg;
updateNotifier(name, version);
// Env variables are initialized to dev, but can be overridden by each command
// For example, "zotero-plugin build" overrides them to "production"
process.env.NODE_ENV ??= "development";
const cli = new Command();
cli
.version(version)
.usage("<command> [options]");
// .option("--config-cwd <config>", "The cwd for search configuration file");
cli
.command("build")
.description("Build the plugin")
.option("--dev", "Builds the plugin in dev mode")
.option("--dist <dir>", "The relative path for the new output directory (default: build)")
.action((options) => {
process.env.NODE_ENV = options.dev ? "development" : "production";
Config.loadConfig({
dist: options.dist,
}).then(ctx => new Build(ctx).run());
});
cli
.command("serve")
.alias("dev")
.description("Start development server")
// .option(
// "--skip-build",
// "skip building website before deploy it (default: false)",
// )
// .option(
// "--only-start",
// "skip building website before deploy it (default: false)",
// )
.action((_options) => {
Config.loadConfig({}).then(ctx => new Serve(ctx).run());
});
cli.command("test")
.description("Run tests")
.option("--abort-on-fail", "Abort the test suite on first failure")
.option("--exit-on-finish", "Exit the test suite after all tests have run")
.option("--no-watch", "Exit the test suite after all tests have run")
.action((options) => {
process.env.NODE_ENV = "test";
Config.loadConfig({
test: {
abortOnFail: options.abortOnFail,
watch: !options.exitOnFinish && options.watch,
},
}).then((ctx) => {
new Test(ctx).run();
});
});
cli
.command("create")
.description("Create the plugin template")
.action((_options: any) => {
logger.error("The create not yet implemented");
// new Create().run();
});
cli
.command("release")
.description("Release the plugin")
.argument("[version]", "Target version: major, minor, patch, pre*, or specify version")
.option("--preid <preid>", "ID for prerelease")
.option("-y, --yes", "Skip confirmation")
.action(async (version, options) => {
process.env.NODE_ENV = "production";
Config.loadConfig({
release: {
bumpp: {
release: version,
preid: options.preid,
confirm: !options.yes,
},
},
}).then(ctx => new Release(ctx).run());
});
cli.arguments("<command>").action((cmd) => {
cli.outputHelp();
logger.error(`Unknown command name "${cmd}".`);
});
cli.parse();
// globalOpts = cli.optsWithGlobals();
}
export default async function mainWithErrorHandler() {
main()
.then(() => {
checkGitIgnore();
})
.catch(onError);
process.on("uncaughtException", onError);
}
function onError(err: Error) {
logger.error(err);
// For tinyexec - bumpp
// @ts-expect-error tinyexec's NonZeroExitError has output.stderr
if (err.output) {
// @ts-expect-error tinyexec's NonZeroExitError has output.stderr
logger.log(err.output.stderr);
}
process.exit(1);
}