acryl 0.2.5 → 0.2.7

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.
package/bin.js CHANGED
@@ -1,24 +1,32 @@
1
1
  #!/usr/bin/env node
2
2
  import { spawn } from 'node:child_process'
3
3
  import { createRequire } from 'node:module'
4
- import { dirname, join } from 'node:path'
4
+ import { join } from 'node:path'
5
5
  import { createInterface } from 'node:readline/promises'
6
6
  import { stdin, stderr } from 'node:process'
7
- import { runtimeLauncher, targetFor } from './runtime.js'
7
+ import { existsSync } from 'node:fs'
8
+ import { fileURLToPath, pathToFileURL } from 'node:url'
9
+ import { forwardCommand, launcherCommand } from './forward.js'
10
+ import { forwardStopSignals, runtimeCommand, runtimeLauncher, targetFor } from './runtime.js'
8
11
  import { acquireDesktopInstaller } from './desktop-runtime.js'
9
- import { acquireWebRuntime, fetchReleaseManifest, hasVerifiedWebRuntime, manifestUrl, managedWebRoot, selectWebArtifact } from './web-runtime.js'
12
+ import { acquireWebRuntime, fetchReleaseManifest, hasVerifiedWebRuntime, manifestUrl, managedWebRoot, selectWebArtifact, webRuntimeCommand } from './web-runtime.js'
10
13
 
11
14
  const require = createRequire(import.meta.url)
12
15
  const selectorVersion = require('./package.json').version
13
16
  const args = process.argv.slice(2)
14
17
 
15
- function run(command, commandArgs) {
18
+ function run({ command, args: commandArgs }) {
16
19
  const child = spawn(command, commandArgs, { stdio: 'inherit' })
20
+ const stopForwarding = forwardStopSignals(child)
17
21
  child.once('error', cause => {
22
+ stopForwarding()
18
23
  console.error(`acryl: ${cause.message}`)
19
24
  process.exitCode = 1
20
25
  })
21
- child.once('exit', code => { process.exitCode = code ?? 1 })
26
+ child.once('exit', code => {
27
+ stopForwarding()
28
+ process.exitCode = code ?? 1
29
+ })
22
30
  }
23
31
 
24
32
  async function confirmDownload() {
@@ -65,10 +73,38 @@ async function runWeb(webArgs) {
65
73
  throw new Error('Web runtime acquisition requires confirmation. Re-run with: acryl web --yes')
66
74
  }
67
75
  const result = await acquireWebRuntime({ root: managedWebRoot(), version: selectorVersion, target, manifest })
68
- run(join(result.root, 'runtime', 'bin', process.platform === 'win32' ? 'acryl-web.cmd' : 'acryl-web'), forwarded)
76
+ run(webRuntimeCommand(result.root, forwarded))
77
+ }
78
+
79
+ /** The launcher (spec 050), bundled into this package as one file at pack time (apps/acryl-launcher-tui/scripts/bundle.mjs). */
80
+ const LAUNCHER = fileURLToPath(new URL('./launcher.js', import.meta.url))
81
+
82
+ /** A bare `acryl` in a terminal: every step offered; what the person chose then runs exactly as if they had typed it. */
83
+ async function openLauncher() {
84
+ const { runLauncher } = await import(pathToFileURL(LAUNCHER).href)
85
+ const chosen = await runLauncher()
86
+ if (!chosen) return
87
+ const target = launcherCommand(chosen.command, {
88
+ env: process.env, platform: process.platform, cwd: process.cwd(), execPath: process.execPath, bin: fileURLToPath(import.meta.url), exists: existsSync,
89
+ })
90
+ if (target.error) throw new Error(target.error)
91
+ console.log(`\n$ ${chosen.line}\n`)
92
+ const child = spawn(target.command, target.args, { stdio: 'inherit', cwd: target.cwd, shell: target.shell === true })
93
+ const stopForwarding = forwardStopSignals(child)
94
+ child.once('error', cause => { stopForwarding(); console.error(`acryl: ${cause.message}`); process.exitCode = 1 })
95
+ child.once('exit', code => { stopForwarding(); process.exitCode = code ?? 1 })
69
96
  }
70
97
 
71
- if (args[0] === 'web') {
98
+ // The short commands (acryl new, plugin, blends, update) run the part that owns them (forward.js).
99
+ const forwarded = forwardCommand(args, { env: process.env, platform: process.platform, cwd: process.cwd(), execPath: process.execPath, exists: existsSync })
100
+ if (forwarded?.error) {
101
+ console.error(`acryl: ${forwarded.error}`)
102
+ process.exitCode = 2
103
+ } else if (forwarded) {
104
+ const child = spawn(forwarded.command, forwarded.args, { stdio: 'inherit', cwd: forwarded.cwd, shell: forwarded.shell === true })
105
+ child.once('error', cause => { console.error(`acryl: ${cause.message}`); process.exitCode = 1 })
106
+ child.once('exit', code => { process.exitCode = code ?? 1 })
107
+ } else if (args[0] === 'web') {
72
108
  runWeb(args.slice(1)).catch(cause => {
73
109
  console.error(`acryl: ${cause instanceof Error ? cause.message : String(cause)}`)
74
110
  process.exitCode = 1
@@ -78,8 +114,15 @@ if (args[0] === 'web') {
78
114
  console.error(`acryl: ${cause instanceof Error ? cause.message : String(cause)}`)
79
115
  process.exitCode = 1
80
116
  })
117
+ } else if (args.length === 0 && stdin.isTTY && process.stdout.isTTY && existsSync(LAUNCHER)) {
118
+ openLauncher().catch(cause => {
119
+ console.error(`acryl: ${cause instanceof Error ? cause.message : String(cause)}`)
120
+ process.exitCode = 1
121
+ })
81
122
  } else {
82
- try { run(runtimeLauncher(), args) } catch (cause) {
123
+ // `acryl code` is ACRYL Code, the IDE (spec 050 Q1); with no terminal, or no launcher, a bare `acryl` keeps opening it as before.
124
+ const ideArgs = args[0] === 'code' ? args.slice(1) : args
125
+ try { run(runtimeCommand(runtimeLauncher(), ideArgs)) } catch (cause) {
83
126
  console.error(`acryl: ${cause instanceof Error ? cause.message : String(cause)}`)
84
127
  process.exitCode = 1
85
128
  }
package/forward.js ADDED
@@ -0,0 +1,86 @@
1
+ // The short commands of `acryl`: each runs a part that already exists, with the same flags, and no `--` in front of them.
2
+ //
3
+ // acryl new <name> [starter] makes an app: create-acrylblends (the app, in the frame, installed and started)
4
+ // acryl plugin <verb> ... inside an app: the app's own door (publish, pull, ...); `plugins` means the same
5
+ // acryl blends <command> ... the acrylblends command (add, upgrade, open, preflight, ...)
6
+ // acryl update acrylblends upgrade, in the app
7
+ //
8
+ // The functions here only decide what to run; bin.js runs it. Nothing here touches the network or the disk except `existsSync` of the door file.
9
+ import { existsSync } from 'node:fs'
10
+ import { basename, dirname, join, resolve, win32 } from 'node:path'
11
+
12
+ /** The commands this module answers. `acryl` with anything else keeps its old meaning. */
13
+ export const FORWARDED = ['new', 'create', 'plugin', 'plugins', 'blends', 'update']
14
+
15
+ /** The package `acryl new` runs. `ACRYL_CREATE` pins another (a version, a tarball) without a new flag. */
16
+ export const CREATE_SPEC = 'create-acrylblends@latest'
17
+ export const BLENDS_SPEC = 'acrylblends'
18
+
19
+ /** The door of an app: the same file the app's `npm run acryl` runs. */
20
+ export const DOOR = join('plugins', 'acryl-door', 'src', 'cli', 'acryl.ts')
21
+
22
+ /**
23
+ * The npm that runs a package from the registry (or a local one), without a shell wherever that is possible: the npm that started this process (npx sets
24
+ * npm_execpath), else on Windows the npm CLI file that installs next to node.exe, run by node itself; only when neither exists `npm`, and `npm.cmd`
25
+ * through a shell on Windows, as a .cmd file must be.
26
+ */
27
+ export function npmExec(context, spec, rest) {
28
+ return npmCommand(context, ['exec', '--yes', spec, '--', ...rest])
29
+ }
30
+
31
+ /** npm with these arguments, found as `npmExec` describes. */
32
+ export function npmCommand({ env, platform, execPath, exists = existsSync }, args) {
33
+ const cli = env.npm_execpath
34
+ if (cli && /npm-cli\.c?js$/u.test(basename(cli))) return { command: execPath, args: [cli, ...args] }
35
+ if (platform === 'win32') {
36
+ const beside = win32.join(win32.dirname(execPath), 'node_modules', 'npm', 'bin', 'npm-cli.js')
37
+ if (exists(beside)) return { command: execPath, args: [beside, ...args] }
38
+ return { command: 'npm.cmd', args, shell: true }
39
+ }
40
+ return { command: 'npm', args }
41
+ }
42
+
43
+ /**
44
+ * What the launcher's chosen command runs (spec 050). `acryl ...` runs this same `acryl` again (`bin`, with the Node running it), so the launcher
45
+ * and the typed command take one path; `npm ...` (an app's own scripts) runs npm. `cwd` is the command's folder, relative to where acryl started.
46
+ */
47
+ export function launcherCommand({ program, args, cwd: folder }, context) {
48
+ const cwd = resolve(context.cwd, folder)
49
+ if (program === 'acryl') return { command: context.execPath, args: [context.bin, ...args], cwd }
50
+ if (program === 'npm') return { ...npmCommand(context, args), cwd }
51
+ return { error: `the launcher asked to run ${program}, which acryl does not run` }
52
+ }
53
+
54
+ /** The app folder that holds this directory: the nearest parent with the door file. */
55
+ export function appRoot(from, exists = existsSync) {
56
+ let dir = resolve(from)
57
+ for (;;) {
58
+ if (exists(join(dir, DOOR))) return dir
59
+ const parent = dirname(dir)
60
+ if (parent === dir) return undefined
61
+ dir = parent
62
+ }
63
+ }
64
+
65
+ /**
66
+ * What a forwarded command runs: `{ command, args, cwd?, shell? }`, or `{ error }` with a line a person can act on. Returns undefined for a command this
67
+ * module does not forward.
68
+ */
69
+ export function forwardCommand(argv, context) {
70
+ const [command, ...rest] = argv
71
+ if (!FORWARDED.includes(command)) return undefined
72
+ const { env, cwd } = context
73
+ if (command === 'new' || command === 'create')
74
+ return npmExec(context, env.ACRYL_CREATE?.trim() || CREATE_SPEC, rest)
75
+ if (command === 'blends') return npmExec(context, BLENDS_SPEC, rest)
76
+ if (command === 'update') return npmExec(context, BLENDS_SPEC, ['upgrade', ...rest])
77
+ // plugin | plugins: the door of the app this directory is in.
78
+ const root = appRoot(cwd, context.exists)
79
+ if (root === undefined)
80
+ return {
81
+ error: `acryl ${command} works inside an app folder (the one acryl new made): cd into it first`,
82
+ }
83
+ if (rest.length === 0)
84
+ return { error: `acryl ${command} needs what to do, for example: acryl plugin publish my-plugin` }
85
+ return { command: context.execPath, args: [join(root, DOOR), 'plugin', ...rest], cwd: root }
86
+ }