forked from mxx1111/mdlook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage-utools.mjs
More file actions
98 lines (80 loc) · 3.23 KB
/
Copy pathpackage-utools.mjs
File metadata and controls
98 lines (80 loc) · 3.23 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
#!/usr/bin/env node
import { spawn } from 'node:child_process'
import fs from 'node:fs'
import { cp, mkdir, readFile, rm, writeFile, access } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import archiver from 'archiver'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const rootDir = path.resolve(__dirname, `..`)
const utoolsDir = path.join(rootDir, `apps`, `utools`)
const distDir = path.join(utoolsDir, `dist`)
const releaseDir = path.join(utoolsDir, `release`)
const iconSource = path.join(rootDir, `public`, `mpmd`, `icon-256.png`)
const iconTarget = path.join(utoolsDir, `logo.png`)
const manifestPath = path.join(utoolsDir, `plugin.json`)
function run(command, args, options = {}) {
return new Promise((resolve, reject) => {
const spawnOptions = {
stdio: `inherit`,
shell: true,
...options,
}
const child = spawn(command, args, spawnOptions)
child.on(`close`, (code) => {
if (code === 0)
resolve(code)
else
reject(new Error(`${[command, ...args].join(` `)} exited with code ${code}`))
})
child.on(`error`, reject)
})
}
async function ensureFileExists(filePath, friendlyName) {
try {
await access(filePath, fs.constants.F_OK)
}
catch (error) {
throw new Error(`${friendlyName ?? filePath} 不存在,请确认路径是否正确。`)
}
}
async function main() {
const pkg = JSON.parse(await readFile(path.join(rootDir, `package.json`), `utf8`))
const version = pkg.version
console.log(`> 构建 uTools 前端资源(version: ${version})`)
await run(`pnpm`, [`--filter`, `@md/web`, `run`, `build:utools`], { cwd: rootDir })
await ensureFileExists(distDir, `apps/utools/dist`)
await ensureFileExists(manifestPath, `apps/utools/plugin.json`)
await ensureFileExists(iconSource, `public/mpmd/icon-256.png`)
const manifest = JSON.parse(await readFile(manifestPath, `utf8`))
manifest.version = version
await writeFile(manifestPath, JSON.stringify(manifest, null, 2) + `\n`, `utf8`)
await cp(iconSource, iconTarget)
await rm(releaseDir, { recursive: true, force: true })
await mkdir(releaseDir, { recursive: true })
const packageName = `md-utools-v${version}`
const packageRoot = path.join(releaseDir, packageName)
await rm(packageRoot, { recursive: true, force: true })
await mkdir(packageRoot, { recursive: true })
await cp(distDir, path.join(packageRoot, `dist`), { recursive: true })
for (const file of [`plugin.json`, `preload.js`, `logo.png`, `README.md`, `package.json`]) {
const source = path.join(utoolsDir, file)
await ensureFileExists(source, `apps/utools/${file}`)
await cp(source, path.join(packageRoot, file), { recursive: true })
}
const zipPath = path.join(releaseDir, `${packageName}.zip`)
await new Promise((resolve, reject) => {
const output = fs.createWriteStream(zipPath)
const archive = archiver(`zip`, { zlib: { level: 9 } })
output.on(`close`, resolve)
archive.on(`error`, reject)
archive.pipe(output)
archive.directory(packageRoot, false)
archive.finalize()
})
console.log(`✔ uTools 插件打包完成 => ${path.relative(rootDir, zipPath)}`)
}
main().catch((error) => {
console.error(error)
process.exitCode = 1
})