-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmod.ts
58 lines (51 loc) · 1.7 KB
/
mod.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
import type { PluginBuild } from "https://deno.land/x/[email protected]/mod.d.ts";
import { StringReader, readLines } from "https://deno.land/[email protected]/io/mod.ts";
// @deno-types="./generate.d.ts"
import { generate } from "https://esm.sh/[email protected]";
async function countLines(s: string) {
const reader = new StringReader(s)
let count = 0
for await (const _ of readLines(reader, { encoding: 'utf8' })) {
count++
}
return count
}
async function deleteBanner(code: string, banner: string) {
const bannerCount = await countLines(banner);
const reader = new StringReader(code)
let _count = 0
let _code = ''
for await (const line of readLines(reader, { encoding: 'utf8' })) {
_count++
if (_count <= bannerCount) continue
_code += `${line}\n`
}
return _code
}
export const GasPlugin = {
name: "gas-plugin",
setup({ onEnd, initialOptions }: PluginBuild) {
onEnd(async () => {
if (initialOptions.outfile === undefined) {
throw Error(
'"outfile" is required. Note that "write: false" is not available.',
);
}
const jsBanner = initialOptions.banner?.js;
const code = await Deno.readTextFile(initialOptions.outfile);
const gas = generate(code, { comment: true });
if (jsBanner === undefined) {
await Deno.writeTextFile(
initialOptions.outfile,
`let global = this;\n${gas.entryPointFunctions}\n${code}`,
);
} else {
const bannerDeleted = await deleteBanner(code, jsBanner);
await Deno.writeTextFile(
initialOptions.outfile,
`${jsBanner}\nlet global = this;\n${gas.entryPointFunctions}${bannerDeleted}`,
);
}
});
},
};