-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
executable file
·252 lines (247 loc) · 14.3 KB
/
main.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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env sh
"\"",`$(echo --% ' |out-null)" >$null;function :{};function dv{<#${/*'>/dev/null )` 2>/dev/null;dv() { #>
echo "1.41.3"; : --% ' |out-null <#'; }; version="$(dv)"; deno="$HOME/.deno/$version/bin/deno"; if [ -x "$deno" ]; then exec "$deno" run -q -A --no-lock --no-config "$0" "$@"; elif [ -f "$deno" ]; then chmod +x "$deno" && exec "$deno" run -q -A --no-lock --no-config "$0" "$@"; fi; bin_dir="$HOME/.deno/$version/bin"; exe="$bin_dir/deno"; has () { command -v "$1" >/dev/null; } ; if ! has unzip; then if ! has apt-get; then has brew && brew install unzip; else if [ "$(whoami)" = "root" ]; then apt-get install unzip -y; elif has sudo; then echo "Can I install unzip for you? (its required for this command to work) ";read ANSWER;echo; if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "yes" ] || [ "$ANSWER" = "Y" ]; then sudo apt-get install unzip -y; fi; elif has doas; then echo "Can I install unzip for you? (its required for this command to work) ";read ANSWER;echo; if [ "$ANSWER" = "y" ] || [ "$ANSWER" = "yes" ] || [ "$ANSWER" = "Y" ]; then doas apt-get install unzip -y; fi; fi; fi; fi; if ! has unzip; then echo ""; echo "So I couldn't find an 'unzip' command"; echo "And I tried to auto install it, but it seems that failed"; echo "(This script needs unzip and either curl or wget)"; echo "Please install the unzip command manually then re-run this script"; exit 1; fi; repo="denoland/deno"; if [ "$OS" = "Windows_NT" ]; then target="x86_64-pc-windows-msvc"; else :; case $(uname -sm) in "Darwin x86_64") target="x86_64-apple-darwin" ;; "Darwin arm64") target="aarch64-apple-darwin" ;; "Linux aarch64") repo="LukeChannings/deno-arm64" target="linux-arm64" ;; "Linux armhf") echo "deno sadly doesn't support 32-bit ARM. Please check your hardware and possibly install a 64-bit operating system." exit 1 ;; *) target="x86_64-unknown-linux-gnu" ;; esac; fi; deno_uri="https://github.com/$repo/releases/download/v$version/deno-$target.zip"; exe="$bin_dir/deno"; if [ ! -d "$bin_dir" ]; then mkdir -p "$bin_dir"; fi; if ! curl --fail --location --progress-bar --output "$exe.zip" "$deno_uri"; then if ! wget --output-document="$exe.zip" "$deno_uri"; then echo "Howdy! I looked for the 'curl' and for 'wget' commands but I didn't see either of them. Please install one of them, otherwise I have no way to install the missing deno version needed to run this code"; exit 1; fi; fi; unzip -d "$bin_dir" -o "$exe.zip"; chmod +x "$exe"; rm "$exe.zip"; exec "$deno" run -q -A --no-lock --no-config "$0" "$@"; #>}; $DenoInstall = "${HOME}/.deno/$(dv)"; $BinDir = "$DenoInstall/bin"; $DenoExe = "$BinDir/deno.exe"; if (-not(Test-Path -Path "$DenoExe" -PathType Leaf)) { $DenoZip = "$BinDir/deno.zip"; $DenoUri = "https://github.com/denoland/deno/releases/download/v$(dv)/deno-x86_64-pc-windows-msvc.zip"; [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; if (!(Test-Path $BinDir)) { New-Item $BinDir -ItemType Directory | Out-Null; }; Function Test-CommandExists { Param ($command); $oldPreference = $ErrorActionPreference; $ErrorActionPreference = "stop"; try {if(Get-Command "$command"){RETURN $true}} Catch {Write-Host "$command does not exist"; RETURN $false}; Finally {$ErrorActionPreference=$oldPreference}; }; if (Test-CommandExists curl) { curl -Lo $DenoZip $DenoUri; } else { curl.exe -Lo $DenoZip $DenoUri; }; if (Test-CommandExists curl) { tar xf $DenoZip -C $BinDir; } else { tar -Lo $DenoZip $DenoUri; }; Remove-Item $DenoZip; $User = [EnvironmentVariableTarget]::User; $Path = [Environment]::GetEnvironmentVariable('Path', $User); if (!(";$Path;".ToLower() -like "*;$BinDir;*".ToLower())) { [Environment]::SetEnvironmentVariable('Path', "$Path;$BinDir", $User); $Env:Path += ";$BinDir"; } }; & "$DenoExe" run -q -A --no-lock --no-config "$PSCommandPath" @args; Exit $LastExitCode; <#
# */0}`;
// import { build } from "https://deno.land/x/esbuild@v0.18.17/mod.js"
import { build, stop, context } from "https://deno.land/x/esbuild@v0.24.0/mod.js"
// import { BuildOptions } from "https://deno.land/x/esbuild@v0.18.17/mod.js"
// import { denoPlugins } from "https://deno.land/x/esbuild_deno_loader@0.9.0/mod.ts"
import { denoPlugins } from "https://esm.sh/jsr/@duesabati/esbuild-deno-plugin@0.2.4/mod.ts"
import { parse } from "https://deno.land/std@0.168.0/flags/mod.ts"
import { Console, clearAnsiStylesFrom, black, white, red, green, blue, yellow, cyan, magenta, lightBlack, lightWhite, lightRed, lightGreen, lightBlue, lightYellow, lightMagenta, lightCyan, blackBackground, whiteBackground, redBackground, greenBackground, blueBackground, yellowBackground, magentaBackground, cyanBackground, lightBlackBackground, lightRedBackground, lightGreenBackground, lightYellowBackground, lightBlueBackground, lightMagentaBackground, lightCyanBackground, lightWhiteBackground, bold, reset, dim, italic, underline, inverse, strikethrough, gray, grey, lightGray, lightGrey, grayBackground, greyBackground, lightGrayBackground, lightGreyBackground, } from "https://deno.land/x/quickr@0.6.38/main/console.js"
import { FileSystem, glob } from "https://deno.land/x/quickr@0.6.72/main/file_system.js"
// TODO:
// plugins from CLI
const nodeBuiltinImports = [
"node:assert",
"node:buffer",
"node:child_process",
"node:cluster",
"node:crypto",
"node:dgram",
"node:dns",
"node:domain",
"node:events",
"node:fs",
"node:http",
"node:https",
"node:net",
"node:os",
"node:path",
"node:punycode",
"node:querystring",
"node:readline",
"node:stream",
"node:string_decoder",
"node:timers",
"node:tls",
"node:tty",
"node:url",
"node:util",
"node:v8",
"node:vm",
"node:zlib",
]
const flags = parse(Deno.args, {
boolean: [
"help",
"minify",
"allowOverwrite",
"splitting",
"preserveSymlinks",
"metafile",
"write",
"watch",
],
string: [
"charset",
],
default: {
charset: "utf8",
},
})
const normal = flags._
delete flags._
if (flags.help) {
console.log(`
${green.bold`options`}:
${cyan`outfile`}: ${yellow`string`}
Documentation: https://esbuild.github.io/api/#outfile
${cyan`entryPoints`}: ${yellow`string[] | Record<string, string> | { in: string, out: string }[]`}
Documentation: https://esbuild.github.io/api/#entry-points
${cyan`entryNames`}: ${yellow`string`}
Documentation: https://esbuild.github.io/api/#entry-names
${cyan`allowOverwrite`}: ${yellow`boolean`}
Documentation: https://esbuild.github.io/api/#allow-overwrite
${cyan`splitting`}: ${yellow`boolean`}
Documentation: https://esbuild.github.io/api/#splitting
${cyan`preserveSymlinks`}: ${yellow`boolean`}
Documentation: https://esbuild.github.io/api/#preserve-symlinks
${cyan`metafile`}: ${yellow`boolean`}
Documentation: https://esbuild.github.io/api/#metafile
${cyan`outdir`}: ${yellow`string`}
Documentation: https://esbuild.github.io/api/#outdir
${cyan`outbase`}: ${yellow`string`}
Documentation: https://esbuild.github.io/api/#outbase
${cyan`external`}: ${yellow`string[]`}
Documentation: https://esbuild.github.io/api/#external
${cyan`packages`}: ${yellow`'external'`}
Documentation: https://esbuild.github.io/api/#packages
${cyan`alias`}: ${yellow`Record<string, string>`}
Documentation: https://esbuild.github.io/api/#alias
${cyan`loader`}: ${yellow`{ [ext: string]: Loader }`}
Documentation: https://esbuild.github.io/api/#loader
${cyan`resolveExtensions`}: ${yellow`string[]`}
Documentation: https://esbuild.github.io/api/#resolve-extensions
${cyan`mainFields`}: ${yellow`string[]`}
Documentation: https://esbuild.github.io/api/#main-fields
${cyan`conditions`}: ${yellow`string[]`}
Documentation: https://esbuild.github.io/api/#conditions
${cyan`write`}: ${yellow`boolean`}
Documentation: https://esbuild.github.io/api/#write
${cyan`tsconfig`}: ${yellow`string`}
Documentation: https://esbuild.github.io/api/#tsconfig
${cyan`outExtension`}: ${yellow`{ [ext: string]: string }`}
Documentation: https://esbuild.github.io/api/#out-extension
${cyan`publicPath`}: ${yellow`string`}
Documentation: https://esbuild.github.io/api/#public-path
${cyan`chunkNames`}: ${yellow`string`}
Documentation: https://esbuild.github.io/api/#chunk-names
${cyan`assetNames`}: ${yellow`string`}
Documentation: https://esbuild.github.io/api/#asset-names
${cyan`inject`}: ${yellow`string[]`}
Documentation: https://esbuild.github.io/api/#inject
${cyan`banner`}: ${yellow`{ [type: string]: string }`}
Documentation: https://esbuild.github.io/api/#banner
${cyan`footer`}: ${yellow`{ [type: string]: string }`}
Documentation: https://esbuild.github.io/api/#footer
${""
// ${cyan`stdin`}: ${yellow`StdinOptions`}
// Documentation: https://esbuild.github.io/api/#stdin
// ${cyan`plugins`}: ${yellow`Plugin[]`}
// Documentation: https://esbuild.github.io/plugins/
}${cyan`absWorkingDir`}: ${yellow`string`}
Documentation: https://esbuild.github.io/api/#working-directory
${cyan`nodePaths`}: ${yellow`string[]; // The "NODE_PATH" variable from Node.js`}
Documentation: https://esbuild.github.io/api/#node-paths
${green.bold`notes`}:
Some options (ex: aliases) don't really work directly from the CLI
Feel free to open up a github issue to get your option prioritized:
https://github.com/jeff-hykin/deno_bundle/issues/new
${green.bold`examples`}:
${green`deno_bundle`} my_file.js
${green`deno_bundle`} my_file.js --minify
${green`deno_bundle`} my_file.js ${cyan`--outfile`} my_file.bundle.js
${green`deno_bundle`} my_file.js ${cyan`--mainFields`} '["field1", "field2"]'
${green`deno_bundle`} my_file.js ${cyan`--charset`} ascii
`)
} else {
const stringArrays = [
"external",
"resolveExtensions",
"mainFields",
"conditions",
"inject",
"entryPoints",
"nodePaths",
"alias",
]
for (const [key, value] of Object.entries(stringArrays)) {
if (typeof flags[key] == 'string') {
flags[key] = JSON.parse(value)
}
}
const { watch } = flags
delete flags.help
delete flags.watch
if (!watch) {
await build({
bundle: true,
entryPoints: normal,
jsxFactory: "h",
format: "esm",
plugins: [
{
"name": "handle-on-build",
"setup": (build) => {
build.onEnd((result) => {
if (result.outputFiles.length == 1) {
const eachOutput = result.outputFiles[0]
const sendToStdout = eachOutput.path == "<stdout>" && !(flags.outfile || flags.outdir)
if (sendToStdout) {
Deno.stdout.write(eachOutput.contents)
} else {
FileSystem.ensureIsFolder(FileSystem.dirname(eachOutput.path)).then(
()=>Deno.writeFile(eachOutput.path, eachOutput.contents).catch(console.error)
)
}
} else {
Promise.all([
result.outputFiles.map(
eachOutput=>FileSystem.ensureIsFolder(FileSystem.dirname(eachOutput.path)).then(
Deno.writeFile(eachOutput.path, eachOutput.contents).catch(console.error)
)
)
]).catch(console.error)
}
stop().catch()
Deno.exit(result.errors.length)
})
},
},
...denoPlugins()
],
...flags,
external: [
// ...nodeBuiltinImports,
...(flags?.external||[])
]
})
} else {
if (!(flags.outfile || flags.outdir)) {
throw Error(`When using the --watch flag, there needs to either be an --outfile or --outdir argument`)
}
const ctx = await context({
bundle: true,
entryPoints: normal,
jsxFactory: "h",
format: "esm",
plugins: [
{
"name": "handle-on-build",
"setup": (build) => {
build.onEnd((result) => {
console.log(`(re)building`)
if (result.outputFiles.length == 1) {
const eachOutput = result.outputFiles[0]
const sendToStdout = eachOutput.path == "<stdout>" && !(flags.outfile || flags.outdir)
if (sendToStdout) {
Deno.stdout.write(eachOutput.contents)
} else {
FileSystem.ensureIsFolder(FileSystem.dirname(eachOutput.path)).then(
()=>Deno.writeFile(eachOutput.path, eachOutput.contents).catch(console.error)
)
}
} else {
Promise.all([
result.outputFiles.map(
eachOutput=>FileSystem.ensureIsFolder(FileSystem.dirname(eachOutput.path)).then(
Deno.writeFile(eachOutput.path, eachOutput.contents).catch(console.error)
)
)
]).catch(console.error)
}
})
},
},
...denoPlugins()
],
...flags,
external: [
// ...nodeBuiltinImports,
...(flags?.external||[])
]
})
await ctx.watch()
}
}