generated from Richienb/node-module-boilerplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
136 lines (119 loc) · 3.13 KB
/
cli.js
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
#!/usr/bin/env node
"use strict"
const { rollup: build, watch } = require("rollup")
const chalk = require("chalk")
const pathExists = require("path-exists")
const meow = require("meow")
const ora = require("ora")
const path = require("path")
const updateNotifier = require("update-notifier")
const exitHook = require("exit-hook")
const getPort = require("get-port")
const { babel } = require("@rollup/plugin-babel")
const closure = require("@ampproject/rollup-plugin-closure-compiler")
const { nodeResolve } = require("@rollup/plugin-node-resolve")
const commonjs = require("@rollup/plugin-commonjs")
const typescript = require("@rollup/plugin-typescript")
const serve = require("rollup-plugin-serve")
const { input, pkg, showHelp } = meow(`
Usage
$ scratcher [build|watch] <source>
Examples
$ scratcher build
√ ${chalk.green("Successfully built extension!")}
$ scratcher watch
${chalk.cyan("/")} Waiting for changes
`)
updateNotifier({ pkg }).notify()
const [
action,
source = path.resolve("source", "index.ts"),
outputFile = path.resolve("dist", "index.min.js")
] = input
const commonPlugins = [
commonjs({ extensions: [".js", ".ts"] }),
nodeResolve({ browser: true }),
typescript({ target: "esnext", esModuleInterop: true }),
babel({
presets: [
["@babel/preset-env", {
targets: {
browsers: ["last 3 versions", "Safari >= 8", "iOS >= 8"]
}
}]
],
plugins: [
["@babel/plugin-transform-runtime", {
regenerator: true
}]
],
babelHelpers: "runtime"
})
]
const commonOutputOptions = {
format: "iife"
}
module.exports = (async () => {
if (!await pathExists(source)) {
console.log(chalk.redBright("The input file does not exist!"))
return
}
if (action === "build") {
const spinner = ora(`Building ${source}`).start()
const bundle = await build({
input: source,
plugins: [
...commonPlugins,
closure({ externs: path.resolve(__dirname, "utils", "closure-externs.js") })
]
})
spinner.text = `Writing to ${outputFile}`
await bundle.write({
file: outputFile,
...commonOutputOptions
})
spinner.succeed(chalk.greenBright("Successfully built extension!"))
} else if (action === "watch") {
const spinner = ora("Preparing watcher").start()
const watcher = watch({
input: source,
plugins: [
serve({
open: true,
openPage: `?extension=${path.basename(outputFile)}`,
verbose: false,
port: await getPort({ port: 8080 }),
contentBase: [
path.dirname(outputFile),
path.dirname(require.resolve("scratch-gui/lib.min"))
],
headers: {
"Access-Control-Allow-Origin": "*"
}
}),
...commonPlugins
],
output: {
file: outputFile,
...commonOutputOptions,
sourcemap: true
}
})
watcher.on("event", ({ code: event, error }) => {
if (event === "START") {
spinner.text = "Starting watcher"
} else if (event === "BUNDLE_START") {
spinner.text = "Building extension"
} else if (event === "END") {
spinner.text = "Waiting for changes"
} else if (event === "ERROR") {
spinner.text = chalk.red(`Build error: ${error.message}`)
}
})
exitHook(() => {
watcher.close()
})
} else {
showHelp()
}
})()