-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
cmd.cjs
executable file
·165 lines (146 loc) · 4.52 KB
/
cmd.cjs
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
#!/usr/bin/env node
// This file intentionally uses older code conventions to be as friendly
// as possible with error messaging to folks on older runtimes.
const pkg = require("./package.json");
require("please-upgrade-node")(pkg, {
message: function (requiredVersion) {
return (
"Eleventy " +
pkg.version +
" requires Node " +
requiredVersion +
". You will need to upgrade Node to use Eleventy!"
);
},
});
const minimist = require("minimist");
const debug = require("debug")("Eleventy:cmd");
(async function () {
const { EleventyErrorHandler } = await import("./src/Errors/EleventyErrorHandler.js");
class SimpleError extends Error {
constructor(...args) {
super(...args);
this.skipOriginalStack = true;
}
}
try {
const argv = minimist(process.argv.slice(2), {
string: ["input", "output", "formats", "config", "pathprefix", "port", "to", "incremental", "loader"],
boolean: [
"quiet",
"version",
"watch",
"dryrun",
"help",
"serve",
"ignore-initial",
],
default: {
quiet: null,
"ignore-initial": false,
"to": "fs",
},
unknown: function (unknownArgument) {
throw new Error(
`We don’t know what '${unknownArgument}' is. Use --help to see the list of supported commands.`,
);
},
});
debug("command: eleventy %o", argv);
const { Eleventy } = await import("./src/Eleventy.js");
let ErrorHandler = new EleventyErrorHandler();
process.on("unhandledRejection", (error, promise) => {
ErrorHandler.fatal(error, "Unhandled rejection in promise");
});
process.on("uncaughtException", (error) => {
ErrorHandler.fatal(error, "Uncaught exception");
});
process.on("rejectionHandled", (promise) => {
ErrorHandler.warn(promise, "A promise rejection was handled asynchronously");
});
if (argv.version) {
console.log(Eleventy.getVersion());
return;
} else if (argv.help) {
console.log(Eleventy.getHelp());
return;
}
let elev = new Eleventy(argv.input, argv.output, {
source: "cli",
// --quiet and --quiet=true both resolve to true
quietMode: argv.quiet,
configPath: argv.config,
pathPrefix: argv.pathprefix,
runMode: argv.serve ? "serve" : argv.watch ? "watch" : "build",
dryRun: argv.dryrun,
loader: argv.loader,
});
// reuse ErrorHandler instance in Eleventy
ErrorHandler = elev.errorHandler;
// Before init
elev.setFormats(argv.formats);
// careful, we can’t use async/await here to error properly
// with old node versions in `please-upgrade-node` above.
elev
.init()
.then(() => {
if (argv.to === "json" || argv.to === "ndjson") {
// override logging output
elev.setIsVerbose(false);
}
// Only relevant for watch/serve
elev.setIgnoreInitial(argv["ignore-initial"]);
if(argv.incremental) {
elev.setIncrementalFile(argv.incremental);
} else if(argv.incremental !== undefined) {
elev.setIncrementalBuild(argv.incremental === "" || argv.incremental);
}
if (argv.serve || argv.watch) {
if(argv.to === "json" || argv.to === "ndjson") {
throw new SimpleError("--to=json and --to=ndjson are not compatible with --serve or --watch.");
}
elev
.watch()
.then(() => {
if (argv.serve) {
elev.serve(argv.port);
}
}, error => {
// A build error occurred and we aren’t going to --serve
ErrorHandler.once("error", error, "Eleventy Error (watch CLI)");
});
process.on("SIGINT", async () => {
await elev.stopWatch();
process.exitCode = 0;
});
} else {
if (!argv.to || argv.to === "fs") {
elev.write().catch(error => {
ErrorHandler.once("fatal", error, "Eleventy Error (fs CLI)");
});
} else if (argv.to === "json") {
elev.toJSON().then(function (result) {
console.log(JSON.stringify(result, null, 2));
}, error => {
ErrorHandler.once("fatal", error, "Eleventy Error (json CLI)");
});
} else if (argv.to === "ndjson") {
elev.toNDJSON().then(function (stream) {
stream.pipe(process.stdout);
}, error => {
ErrorHandler.once("fatal", error, "Eleventy Error (ndjson CLI)");
});
} else {
throw new SimpleError(
`Invalid --to value: ${argv.to}. Supported values: \`fs\` (default), \`json\`, and \`ndjson\`.`,
);
}
}
}).catch(error => {
ErrorHandler.fatal(error, "Eleventy Error (CLI initialization)");
});
} catch (error) {
let ErrorHandler = new EleventyErrorHandler();
ErrorHandler.fatal(error, "Eleventy Fatal Error (CLI)");
}
})();