forked from utags/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild-all.mjs
More file actions
95 lines (76 loc) · 2.61 KB
/
Copy pathbuild-all.mjs
File metadata and controls
95 lines (76 loc) · 2.61 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
import fs from 'node:fs'
import path from 'node:path'
import process from 'node:process'
import * as esbuild from 'esbuild'
import { getBuildOptions, logger } from '../common.mjs'
const target = 'userscript'
const tag = process.argv.includes('--staging') ? 'staging' : 'prod'
const packagesDir = 'src/packages'
const config = JSON.parse(fs.readFileSync('package.json', 'utf8'))
const log = logger(target)
const packages = fs
.readdirSync(packagesDir, { withFileTypes: true })
.filter((d) => d.isDirectory())
.map((d) => d.name)
if (packages.length === 0) {
console.error('No packages found in ' + packagesDir)
process.exit(1)
}
for (const packageName of packages) {
const entryPath = path.join(packagesDir, packageName, 'index.ts')
const bannerPath = path.join(packagesDir, packageName, 'banner.txt')
if (!fs.existsSync(entryPath)) {
log(`Skip ${packageName}: missing index.ts`)
continue
}
if (!fs.existsSync(bannerPath)) {
log(`Skip ${packageName}: missing banner.txt`)
continue
}
let banner = fs.readFileSync(bannerPath, 'utf8')
if (tag !== 'prod') {
banner = banner.replaceAll(/(@name(:[\w-]+)?\s.+)/gm, `$1 - ${tag}`)
}
const buildOptions = {
...getBuildOptions(target, tag, `packages/${packageName}/index`),
banner: { js: banner },
legalComments: 'none',
outfile:
tag === 'prod'
? `${packageName}/${packageName}.user.js`
: `${packageName}/${packageName}-${tag}.user.js`,
}
buildOptions.alias = {
...buildOptions.alias,
'browser-extension-storage': 'browser-extension-storage/userscript',
'browser-extension-utils': 'browser-extension-utils/userscript',
}
log(`Building ${packageName}...`)
await esbuild.build(buildOptions)
let text = fs.readFileSync(buildOptions.outfile, 'utf8')
if (config.bugs && config.bugs.url) {
text = text.replace('{bugs.url}', config.bugs.url)
}
const keys = banner
.split('\n')
.map((v) => /{([\w\-.:]+)}/.exec(v))
.filter(Boolean)
.map((v) => v[1])
for (const key of keys) {
text = text.replace('{' + key + '}', config[key])
}
const matched = new Set()
text.replaceAll(/(GM[_.]\w+)/gm, (match) => {
matched.add(match)
})
const grants = [...matched]
.map((v) => `// @grant${' '.repeat(16)}${v}`)
.join('\n')
text = text.replace('// ==/UserScript==', `${grants}\n// ==/UserScript==`)
text = text.replace('{', '{\n "use strict";')
text = text.replaceAll(/^\s*\/\/ [^=@].*$/gm, '')
text = text.replaceAll(/\n+/gm, '\n')
fs.writeFileSync(buildOptions.outfile, text)
log(`Built ${packageName} -> ${buildOptions.outfile}`)
}
log('All packages built.')