/
cli.ts
114 lines (108 loc) 路 2.77 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
import meow from "meow";
import init from "./init";
import validate from "./validate";
import build from "./build";
import watch from "./build/watch";
import fix from "./fix";
import dev from "./dev";
import { error, info, log } from "./logger";
import {
FatalError,
FixableError,
UnexpectedBuildError,
ScopelessError,
BatchError,
} from "./errors";
// tricking static analysis is fun
// @ts-ignore
process["e" + "nv"].NODE_ENV = "production";
let { input } = meow(
`
Usage
$ preconstruct [command]
Commands
init initialise a project
build build the project
watch start a watch process to build the project
validate validate the project
fix infer as much information as possible and fix the project
dev create links so entrypoints can be imported
`,
{}
);
let errors = {
commandNotFound: "Command not found",
};
class CommandNotFoundError extends Error {}
(async () => {
if (input.length === 1) {
switch (input[0]) {
case "init": {
await init(process.cwd());
return;
}
case "validate": {
await validate(process.cwd());
return;
}
case "build": {
await build(process.cwd());
return;
}
case "watch": {
await watch(process.cwd());
return;
}
case "fix": {
await fix(process.cwd());
return;
}
case "dev": {
await dev(process.cwd());
return;
}
default: {
throw new CommandNotFoundError();
}
}
} else {
throw new CommandNotFoundError();
}
})().catch((err) => {
let hasFixableError = false;
if (err instanceof FixableError) {
hasFixableError = true;
error(err.message, err.scope);
} else if (err instanceof FatalError) {
error(err.message, err.scope);
} else if (err instanceof BatchError) {
for (let fatalError of err.errors) {
if (fatalError instanceof FixableError) {
hasFixableError = true;
error(fatalError.message, fatalError.scope);
} else {
error(fatalError.message, fatalError.scope);
}
}
} else if (err instanceof CommandNotFoundError) {
error(errors.commandNotFound);
} else if (err instanceof UnexpectedBuildError) {
error(err.message, err.scope);
} else if (err instanceof ScopelessError) {
log(err.message);
} else {
error(err);
}
if (hasFixableError) {
info(
"Some of the errors above can be fixed automatically by running preconstruct fix"
);
}
info(
"If you want to learn more about the above error, check https://preconstruct.tools/errors"
);
info(
"If the error is not there and you want to learn more about it, open an issue at https://github.com/preconstruct/preconstruct/issues/new"
);
process.exit(1);
});