-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup.mjs
More file actions
executable file
·268 lines (226 loc) · 8.1 KB
/
setup.mjs
File metadata and controls
executable file
·268 lines (226 loc) · 8.1 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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env node
import { spawnSync } from 'node:child_process'
import fsSync from 'node:fs'
import fs from 'node:fs/promises'
import path from 'node:path'
import process from 'node:process'
import { fileURLToPath } from 'node:url'
const templateRoot = path.dirname(fileURLToPath(import.meta.url))
const scaffoldRoot = path.join(templateRoot, 'scaffold', 'downstream')
function usage() {
process.stdout.write(`Usage:
node setup.mjs <command> [dir] [options]
Commands:
link [product_dir] Create/refresh vendor symlinks in product_dir (default: cwd).
bootstrap [product_dir] link + build pluxel dist + pnpm install (default: cwd).
init <product_dir> Scaffold a downstream repo, then (optionally) bootstrap it.
bootstrap-template Build pluxel dist + pnpm install (for this template repo).
Environment:
PLUXEL_DIR Path to a local pluxel repo (default: ../pluxel).
Init options:
--name <name> package.json name (default: basename of product_dir)
--no-bootstrap only scaffold, skip pnpm install/build
`)
}
function absDir(input) {
return path.resolve(input)
}
async function ensureDir(dir) {
await fs.mkdir(dir, { recursive: true })
}
async function isSymlink(p) {
try {
return (await fs.lstat(p)).isSymbolicLink()
} catch {
return false
}
}
async function ensureRealDir(p) {
if (await isSymlink(p)) {
await fs.unlink(p)
}
await ensureDir(p)
}
function detectPluxelDir() {
const env = process.env.PLUXEL_DIR
if (env) return absDir(env)
return absDir(path.join(templateRoot, '..', 'pluxel'))
}
async function linkFileOrDir(linkPath, targetPath) {
const linkDir = path.dirname(linkPath)
await ensureDir(linkDir)
const relTarget = path.relative(linkDir, targetPath) || '.'
try {
const st = await fs.lstat(linkPath)
if (st.isDirectory() && !st.isSymbolicLink()) {
throw new Error(`refusing to replace real directory: ${linkPath}`)
}
} catch {}
await fs.rm(linkPath, { force: true, recursive: false })
await fs.symlink(relTarget, linkPath)
}
async function linkVendorTemplate(productDir) {
const vendorRoot = path.join(productDir, 'vendor', 'pluxel-template')
await ensureRealDir(vendorRoot)
await linkFileOrDir(path.join(vendorRoot, 'packages'), path.join(templateRoot, 'packages'))
await linkFileOrDir(path.join(vendorRoot, 'plugins'), path.join(templateRoot, 'plugins'))
await linkFileOrDir(path.join(vendorRoot, 'agents'), path.join(templateRoot, 'agents'))
await linkFileOrDir(path.join(vendorRoot, 'AGENTS.md'), path.join(templateRoot, 'AGENTS.md'))
// Ensure vendored sources can resolve their `extends: "../../../tsconfig*.json"` against the downstream root.
await linkFileOrDir(path.join(vendorRoot, 'tsconfig.base.json'), path.join(productDir, 'tsconfig.base.json'))
await linkFileOrDir(path.join(vendorRoot, 'tsconfig.json'), path.join(productDir, 'tsconfig.json'))
}
async function linkVendorPluxel(productDir) {
const pluxelDir = detectPluxelDir()
const hmrPath = path.join(pluxelDir, 'packages', 'hmr')
try {
const stat = await fs.stat(hmrPath)
if (!stat.isDirectory()) throw new Error('not a dir')
} catch {
throw new Error(`missing pluxel repo at ${pluxelDir} (set PLUXEL_DIR=/path/to/pluxel)`)
}
const vendorRoot = path.join(productDir, 'vendor', 'pluxel')
await ensureRealDir(vendorRoot)
await linkFileOrDir(path.join(vendorRoot, 'packages'), path.join(pluxelDir, 'packages'))
}
async function cmdLink(productDir) {
const productAbs = absDir(productDir)
const pkgJson = path.join(productAbs, 'package.json')
try {
await fs.stat(pkgJson)
} catch {
throw new Error(`not a repo root (missing package.json): ${productAbs}`)
}
await linkVendorTemplate(productAbs)
await linkVendorPluxel(productAbs)
process.stdout.write(`[setup] linked vendor in ${path.basename(productAbs)}\n`)
}
function runPnpm(cwd, args) {
const res = spawnSync('pnpm', args, { cwd, stdio: 'inherit' })
if (res.error) throw res.error
if (typeof res.status === 'number' && res.status !== 0) process.exit(res.status)
}
function buildPluxelDist() {
const pluxelDir = detectPluxelDir()
const hmrPath = path.join(pluxelDir, 'packages', 'hmr')
try {
fsSync.statSync(hmrPath)
} catch {
throw new Error(`missing pluxel repo at ${pluxelDir} (set PLUXEL_DIR=/path/to/pluxel)`)
}
process.stdout.write('[setup] build upstream dist outputs (pluxel)\n')
runPnpm(pluxelDir, ['install'])
runPnpm(pluxelDir, ['--filter', '@pluxel/core', '--filter', '@pluxel/hmr', '--filter', '@pluxel/test', 'build'])
}
async function cmdBootstrap(productDir) {
const productAbs = absDir(productDir)
await cmdLink(productAbs)
buildPluxelDist()
process.stdout.write(`[setup] pnpm install (${productAbs})\n`)
runPnpm(productAbs, ['install'])
}
async function cmdBootstrapTemplate() {
buildPluxelDist()
process.stdout.write(`[setup] pnpm install (${templateRoot})\n`)
runPnpm(templateRoot, ['install'])
}
async function writeFileOnce(filePath, content) {
await ensureDir(path.dirname(filePath))
await fs.writeFile(filePath, content, { flag: 'wx' })
}
async function copyFileOnce(srcPath, destPath) {
await ensureDir(path.dirname(destPath))
await fs.copyFile(srcPath, destPath, fsSync.constants.COPYFILE_EXCL)
}
async function copyDirContentsOnce(srcDir, destDir) {
const entries = await fs.readdir(srcDir, { withFileTypes: true })
for (const ent of entries) {
const src = path.join(srcDir, ent.name)
const dest = path.join(destDir, ent.name)
if (ent.isDirectory()) {
await ensureDir(dest)
await copyDirContentsOnce(src, dest)
} else if (ent.isFile()) {
await copyFileOnce(src, dest)
} else if (ent.isSymbolicLink()) {
const target = await fs.readlink(src)
await fs.symlink(target, dest)
}
}
}
async function ensureEmptyDir(dir) {
await ensureDir(dir)
const entries = await fs.readdir(dir)
if (entries.length > 0) {
throw new Error(`target directory is not empty: ${dir}`)
}
}
function parseInitOptions(args) {
const opts = { name: null, bootstrap: true }
for (let i = 0; i < args.length; i++) {
const a = args[i]
if (a === '--no-bootstrap') {
opts.bootstrap = false
continue
}
if (a === '--name') {
const v = args[i + 1]
if (!v) throw new Error('missing value for --name')
opts.name = v
i++
continue
}
throw new Error(`unknown option: ${a}`)
}
return opts
}
async function copyTemplateConfig(productAbs) {
// "Major essentials" copied from template. Avoid copying vendored sources or template runtime data.
const files = ['biome.jsonc', 'tsconfig.base.json', 'tsconfig.json']
for (const f of files) {
await copyFileOnce(path.join(templateRoot, f), path.join(productAbs, f))
}
}
async function writeDownstreamPackageJson(productAbs, name) {
const src = path.join(scaffoldRoot, 'package.json')
const raw = await fs.readFile(src, 'utf8')
const rendered = raw.replaceAll('__NAME__', name)
await writeFileOnce(path.join(productAbs, 'package.json'), `${rendered.trim()}\n`)
}
async function cmdInit(productDir, args) {
const productAbs = absDir(productDir)
const opts = parseInitOptions(args)
const name = opts.name ?? path.basename(productAbs)
await ensureEmptyDir(productAbs)
await copyTemplateConfig(productAbs)
await copyDirContentsOnce(path.join(scaffoldRoot, 'base'), productAbs)
await fs.chmod(path.join(productAbs, 'setup.mjs'), 0o755)
await ensureDir(path.join(productAbs, 'plugins'))
await writeDownstreamPackageJson(productAbs, name)
process.stdout.write(`[setup] created downstream at ${productAbs}\n`)
if (opts.bootstrap) {
await cmdBootstrap(productAbs)
}
}
const [cmd, arg1, ...rest] = process.argv.slice(2)
try {
if (!cmd || cmd === '-h' || cmd === '--help') {
usage()
process.exit(0)
}
if (cmd === 'link') {
await cmdLink(arg1 ?? process.cwd())
} else if (cmd === 'bootstrap') {
await cmdBootstrap(arg1 ?? process.cwd())
} else if (cmd === 'init' || cmd === 'create') {
if (!arg1) throw new Error('missing <product_dir>')
await cmdInit(arg1, rest)
} else if (cmd === 'bootstrap-template' || cmd === 'template-bootstrap') {
await cmdBootstrapTemplate()
} else {
throw new Error(`unknown command: ${cmd}`)
}
} catch (err) {
process.stderr.write(`[setup] ${err instanceof Error ? err.message : String(err)}\n`)
process.exit(2)
}