-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·115 lines (112 loc) · 3.15 KB
/
cli.js
File metadata and controls
executable file
·115 lines (112 loc) · 3.15 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
#!/usr/bin/env node
import yargs from 'yargs';
import { hideBin } from 'yargs/helpers';
import path, { sep } from "path";
import { isMain } from './utils.js';
import { bundle } from "./index.js";
/**
* Invokes the bcore cli parser to handle terminal commands.
* @param {*} meta The current import.meta where this function is invoked from.
* @param {function} configure A callback function that is called just before bundling to allow configuration overrides.
* @param {object} externalMap An externals to globals maping object. Internally passed to the esbuild-plugin-external-global plugin.
*/
export async function cli(meta, configure, externalMap) {
return yargs(hideBin(process.argv))
.command('bundle [path]', 'Bundle a stack', (yargs) => {
return yargs
.positional("path", {
type: "string",
desc: "One or more stack paths to create bundles from",
default: "."
})
.option("production", {
type: "boolean",
desc: "Creates all bundles in ./dict path. Otherwise all bundles will be put in ./build",
default: false
})
.option("format", {
alias: "f",
type: "string",
desc: "Sets the compilation format. Options: esm, cjs",
default: "esm"
})
.option("minify", {
alias: "m",
type: "boolean",
desc: "When true minifies bundles"
})
.option("analyze", {
alias: "a",
type: "boolean",
desc: "Outputs human readable bundle analysis. Displaying which packages were bundled and how much space they take.",
});
}, async (argv) => {
if (argv.verbose) console.info(`[Bundling] ${argv.path}`)
try {
await bundle({
stackPath: path.resolve(argv.path),
watch: false,
production: argv.production,
format: argv.format,
analyze: argv.analyze,
minify: argv.minify,
configure: configure,
externalMap
});
} catch(err) {
console.error(err);
process.exit(1)
}
})
.command('watch [path]', 'Watches and bundles a stack', (yargs) => {
return yargs
.positional("path", {
type: "string",
desc: "One or more stack paths to create bundles from",
default: "."
})
.option("production", {
type: "boolean",
desc: "Creates all bundles in ./dict path. Otherwise all bundles will be put in ./build",
default: false
})
.option("minify", {
alias: "m",
type: "boolean",
desc: "When true minifies bundles"
})
.option("format", {
alias: "f",
type: "string",
desc: "Sets the compilation format. Options: esm, cjs",
default: "esm"
})
}, async (argv) => {
if (argv.verbose) console.info(`[Watching] ${argv.path}`)
try {
await bundle({
stackPath: path.resolve(argv.path),
watch: true,
production: argv.production,
format: argv.format,
analyze: false,
minify: argv.minify,
configure: configure,
externalMap
});
} catch(err) {
console.error(err);
process.exit(1)
}
})
.option('verbose', {
alias: 'v',
type: 'boolean',
description: 'Run with verbose logging'
})
.parse();
}
// only trigger cli if we are explicitly the main app running.
if (isMain(import.meta)) {
await cli();
}