@antigenic-oss/paint 0.2.4 → 0.2.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/bin/paint.js +78 -4
  2. package/package.json +1 -1
package/bin/paint.js CHANGED
@@ -10,6 +10,21 @@ const { spawn, spawnSync } = require('node:child_process')
10
10
  const ENTRY_REAL_PATH = fs.realpathSync(__filename)
11
11
  const APP_ROOT = path.resolve(path.dirname(ENTRY_REAL_PATH), '..')
12
12
  const STATE_DIR = path.join(os.homedir(), '.paint')
13
+ const APP_PACKAGE_JSON = path.join(APP_ROOT, 'package.json')
14
+ const APP_VERSION = (() => {
15
+ try {
16
+ const pkg = JSON.parse(fs.readFileSync(APP_PACKAGE_JSON, 'utf8'))
17
+ return typeof pkg.version === 'string' ? pkg.version : 'dev'
18
+ } catch {
19
+ return 'dev'
20
+ }
21
+ })()
22
+ const RUNNING_FROM_NODE_MODULES = APP_ROOT.includes(
23
+ `${path.sep}node_modules${path.sep}`,
24
+ )
25
+ const RUNTIME_ROOT = RUNNING_FROM_NODE_MODULES
26
+ ? path.join(STATE_DIR, 'runtime', APP_VERSION)
27
+ : APP_ROOT
13
28
 
14
29
  const APP_STATE_FILE = path.join(STATE_DIR, 'server.json')
15
30
  const TERMINAL_STATE_FILE = path.join(STATE_DIR, 'terminal.json')
@@ -52,6 +67,62 @@ function ensureStateDir() {
52
67
  fs.mkdirSync(STATE_DIR, { recursive: true })
53
68
  }
54
69
 
70
+ function ensureRuntimeRoot() {
71
+ if (!RUNNING_FROM_NODE_MODULES) return APP_ROOT
72
+
73
+ const stampFile = path.join(RUNTIME_ROOT, '.paint-runtime-stamp.json')
74
+ if (fs.existsSync(stampFile)) {
75
+ return RUNTIME_ROOT
76
+ }
77
+
78
+ ensureStateDir()
79
+ fs.mkdirSync(path.dirname(RUNTIME_ROOT), { recursive: true })
80
+
81
+ // Build outside node_modules so Next.js webpack always transpiles TS sources.
82
+ fs.rmSync(RUNTIME_ROOT, { recursive: true, force: true })
83
+ fs.mkdirSync(RUNTIME_ROOT, { recursive: true })
84
+
85
+ const runtimeEntries = [
86
+ 'bin',
87
+ 'public',
88
+ 'src',
89
+ 'next-env.d.ts',
90
+ 'next.config.ts',
91
+ 'postcss.config.mjs',
92
+ 'tsconfig.json',
93
+ 'package.json',
94
+ 'README.md',
95
+ 'LICENSE',
96
+ 'NOTICE',
97
+ ]
98
+
99
+ for (const entry of runtimeEntries) {
100
+ const src = path.join(APP_ROOT, entry)
101
+ const dst = path.join(RUNTIME_ROOT, entry)
102
+ if (!fs.existsSync(src)) continue
103
+
104
+ fs.cpSync(src, dst, { recursive: true, force: true })
105
+ }
106
+
107
+ const runtimeNodeModules = path.join(RUNTIME_ROOT, 'node_modules')
108
+ fs.symlinkSync(path.join(APP_ROOT, 'node_modules'), runtimeNodeModules, 'dir')
109
+
110
+ fs.writeFileSync(
111
+ stampFile,
112
+ JSON.stringify(
113
+ {
114
+ version: APP_VERSION,
115
+ sourceRoot: APP_ROOT,
116
+ preparedAt: now(),
117
+ },
118
+ null,
119
+ 2,
120
+ ),
121
+ )
122
+
123
+ return RUNTIME_ROOT
124
+ }
125
+
55
126
  function now() {
56
127
  return new Date().toISOString()
57
128
  }
@@ -181,12 +252,12 @@ function validatePort(port, flagName) {
181
252
  }
182
253
 
183
254
  function spawnDetached(command, args, opts) {
184
- const { env, logFile } = opts
255
+ const { env, logFile, cwd = APP_ROOT } = opts
185
256
  const fd = fs.openSync(logFile, 'a')
186
257
  fs.writeSync(fd, `\n[${now()}] spawn: ${command} ${args.join(' ')}\n`)
187
258
 
188
259
  const child = spawn(command, args, {
189
- cwd: APP_ROOT,
260
+ cwd,
190
261
  env,
191
262
  detached: true,
192
263
  stdio: ['ignore', fd, fd],
@@ -262,8 +333,9 @@ function ensureNextInstalled() {
262
333
  function ensureBuilt(forceRebuild) {
263
334
  ensureStateDir()
264
335
  ensureNextInstalled()
336
+ const runtimeRoot = ensureRuntimeRoot()
265
337
 
266
- const buildIdPath = path.join(APP_ROOT, '.next', 'BUILD_ID')
338
+ const buildIdPath = path.join(runtimeRoot, '.next', 'BUILD_ID')
267
339
  if (!forceRebuild && fs.existsSync(buildIdPath)) {
268
340
  return
269
341
  }
@@ -274,7 +346,7 @@ function ensureBuilt(forceRebuild) {
274
346
  // Use webpack for packaged/global runtime builds.
275
347
  // Turbopack can fail when the app lives under node_modules.
276
348
  const result = spawnSync(process.execPath, [NEXT_BIN, 'build', '--webpack'], {
277
- cwd: APP_ROOT,
349
+ cwd: runtimeRoot,
278
350
  env: process.env,
279
351
  stdio: 'inherit',
280
352
  })
@@ -302,6 +374,7 @@ async function startApp(options) {
302
374
 
303
375
  ensureBuilt(options.rebuild)
304
376
  ensureStateDir()
377
+ const runtimeRoot = ensureRuntimeRoot()
305
378
 
306
379
  const webChild = spawnDetached(
307
380
  process.execPath,
@@ -314,6 +387,7 @@ async function startApp(options) {
314
387
  options.host,
315
388
  ],
316
389
  {
390
+ cwd: runtimeRoot,
317
391
  env: process.env,
318
392
  logFile: WEB_LOG_FILE,
319
393
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antigenic-oss/paint",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "Visual editor for localhost web projects with a global paint CLI",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://github.com/Antigenic-OSS/pAInt",