@mono-labs/cli 0.1.288 → 0.1.290
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/dist/lib/commands/build-process/buildBaseEnv.d.ts +17 -0
- package/dist/lib/commands/build-process/buildBaseEnv.d.ts.map +1 -0
- package/dist/lib/commands/build-process/buildBaseEnv.js +52 -0
- package/dist/lib/commands/build-process/cliFactory.d.ts +2 -0
- package/dist/lib/commands/build-process/cliFactory.d.ts.map +1 -1
- package/dist/lib/commands/build-process/cliFactory.js +18 -20
- package/dist/lib/commands/build-process/runMonoCommand.d.ts.map +1 -1
- package/dist/lib/commands/build-process/runMonoCommand.js +32 -13
- package/dist/lib/commands/build-process/runners/errors.d.ts +16 -0
- package/dist/lib/commands/build-process/runners/errors.d.ts.map +1 -0
- package/dist/lib/commands/build-process/runners/errors.js +42 -0
- package/dist/lib/commands/build-process/runners/processManager.d.ts +5 -0
- package/dist/lib/commands/build-process/runners/processManager.d.ts.map +1 -1
- package/dist/lib/commands/build-process/runners/processManager.js +29 -19
- package/dist/lib/commands/build-process/runners/runBackground.d.ts +2 -1
- package/dist/lib/commands/build-process/runners/runBackground.d.ts.map +1 -1
- package/dist/lib/commands/build-process/runners/runBackground.js +6 -6
- package/dist/lib/commands/build-process/runners/runForeground.d.ts +1 -1
- package/dist/lib/commands/build-process/runners/runForeground.d.ts.map +1 -1
- package/dist/lib/commands/build-process/runners/runForeground.js +5 -6
- package/dist/lib/commands/prune/prune.d.ts.map +1 -1
- package/dist/lib/commands/prune/prune.js +3 -1
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/index.js +23 -6
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/lib/commands/build-process/buildBaseEnv.ts +75 -0
- package/lib/commands/build-process/cliFactory.ts +20 -19
- package/lib/commands/build-process/runMonoCommand.ts +33 -11
- package/lib/commands/build-process/runners/errors.ts +42 -0
- package/lib/commands/build-process/runners/processManager.ts +25 -16
- package/lib/commands/build-process/runners/runBackground.ts +9 -6
- package/lib/commands/build-process/runners/runForeground.ts +6 -8
- package/lib/commands/prune/prune.ts +3 -1
- package/lib/index.ts +32 -8
- package/package.json +4 -4
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import os from 'node:os'
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A failure that knows which exit code mono should end with.
|
|
5
|
+
*
|
|
6
|
+
* Every runner rejects with this, and the top-level handler in `lib/index.ts` turns it into
|
|
7
|
+
* `process.exitCode`. Nothing between the runner and that handler may swallow it.
|
|
8
|
+
*/
|
|
9
|
+
export class MonoCommandError extends Error {
|
|
10
|
+
readonly exitCode: number
|
|
11
|
+
|
|
12
|
+
constructor(message: string, exitCode = 1) {
|
|
13
|
+
super(message)
|
|
14
|
+
this.name = 'MonoCommandError'
|
|
15
|
+
// 0 would report success for a failure, so never allow it here.
|
|
16
|
+
this.exitCode = Number.isInteger(exitCode) && exitCode > 0 ? exitCode : 1
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Shell convention: a child killed by a signal exits with 128 + the signal number. */
|
|
21
|
+
export function exitCodeFromChild(code: number | null, signal: NodeJS.Signals | null): number {
|
|
22
|
+
if (signal) {
|
|
23
|
+
const num = (os.constants.signals as Record<string, number>)[signal]
|
|
24
|
+
return num ? 128 + num : 1
|
|
25
|
+
}
|
|
26
|
+
return code ?? 1
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function childFailure(
|
|
30
|
+
cmd: string,
|
|
31
|
+
code: number | null,
|
|
32
|
+
signal: NodeJS.Signals | null,
|
|
33
|
+
detail = ''
|
|
34
|
+
): MonoCommandError {
|
|
35
|
+
const how = signal ? `was killed by signal ${signal}` : `exited with code ${code}`
|
|
36
|
+
return new MonoCommandError(`${cmd} ${how}${detail}`, exitCodeFromChild(code, signal))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** Exit code to use for any error reaching the top level. */
|
|
40
|
+
export function exitCodeOf(err: unknown): number {
|
|
41
|
+
return err instanceof MonoCommandError ? err.exitCode : 1
|
|
42
|
+
}
|
|
@@ -21,25 +21,34 @@ function isRunning(child: ChildProcess): boolean {
|
|
|
21
21
|
return Boolean(child.pid) && child.exitCode === null && child.signalCode === null
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/**
|
|
25
|
+
* Kill a child and everything it started, if it is still running.
|
|
26
|
+
* `detached` children lead their own process group on POSIX, so the whole group is signalled.
|
|
27
|
+
*/
|
|
28
|
+
export function killTree(child: ChildProcess, detached = false): void {
|
|
29
|
+
if (!isRunning(child)) return
|
|
30
|
+
try {
|
|
31
|
+
if (process.platform === 'win32') {
|
|
32
|
+
spawn('taskkill', ['/PID', String(child.pid), '/T', '/F'], {
|
|
33
|
+
shell: true,
|
|
34
|
+
stdio: 'ignore',
|
|
35
|
+
windowsHide: true,
|
|
36
|
+
})
|
|
37
|
+
} else if (detached) {
|
|
38
|
+
process.kill(-(child.pid as number), 'SIGTERM')
|
|
39
|
+
} else {
|
|
40
|
+
child.kill('SIGTERM')
|
|
41
|
+
}
|
|
42
|
+
} catch {
|
|
43
|
+
// ignore
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
24
47
|
export function killAllBackground(): void {
|
|
25
48
|
for (const child of Array.from(bgChildren)) {
|
|
26
49
|
bgChildren.delete(child)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
if (process.platform === 'win32') {
|
|
30
|
-
spawn('taskkill', ['/PID', String(child.pid), '/T', '/F'], {
|
|
31
|
-
shell: true,
|
|
32
|
-
stdio: 'ignore',
|
|
33
|
-
windowsHide: true,
|
|
34
|
-
})
|
|
35
|
-
} else {
|
|
36
|
-
// Registered children are always spawned `detached`, so they lead their own process
|
|
37
|
-
// group and the negative-pid form is ours to signal.
|
|
38
|
-
process.kill(-(child.pid as number), 'SIGTERM')
|
|
39
|
-
}
|
|
40
|
-
} catch {
|
|
41
|
-
// ignore
|
|
42
|
-
}
|
|
50
|
+
// Registered children are always spawned `detached`.
|
|
51
|
+
killTree(child, true)
|
|
43
52
|
}
|
|
44
53
|
bgChildren.clear()
|
|
45
54
|
}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { spawn } from 'child_process'
|
|
1
|
+
import { spawn, type ChildProcess } from 'child_process'
|
|
2
2
|
import { replaceTokens } from '../dataLayer'
|
|
3
3
|
import { registerBackground } from './processManager'
|
|
4
4
|
import { writeLog } from '@mono-labs/shared'
|
|
5
|
+
import { childFailure } from './errors'
|
|
5
6
|
|
|
6
7
|
function createdExpandedEnv(envObj: Record<string, unknown>): Record<string, unknown> {
|
|
7
8
|
const expandedEnv: Record<string, unknown> = {}
|
|
@@ -16,7 +17,9 @@ export function runBackground(
|
|
|
16
17
|
cmd: string,
|
|
17
18
|
envObj: Record<string, unknown> = {},
|
|
18
19
|
logName = 'background',
|
|
19
|
-
attached = false
|
|
20
|
+
attached = false,
|
|
21
|
+
baseEnv: NodeJS.ProcessEnv = process.env,
|
|
22
|
+
onSpawn?: (child: ChildProcess) => void
|
|
20
23
|
): Promise<void> {
|
|
21
24
|
const isWin = process.platform === 'win32'
|
|
22
25
|
|
|
@@ -33,7 +36,7 @@ export function runBackground(
|
|
|
33
36
|
const child = spawn(outCmd, {
|
|
34
37
|
shell: true,
|
|
35
38
|
stdio: attached ? 'inherit' : 'ignore',
|
|
36
|
-
env: { ...
|
|
39
|
+
env: { ...baseEnv, ...(expandedEnv as any) },
|
|
37
40
|
detached: !attached && !isWin,
|
|
38
41
|
windowsHide: !attached && isWin,
|
|
39
42
|
})
|
|
@@ -44,15 +47,15 @@ export function runBackground(
|
|
|
44
47
|
// completion right below, so there is never anything left of it to clean up — and by the
|
|
45
48
|
// time `runMonoCommand`'s `finally` fires, its pid is the OS's to reuse.
|
|
46
49
|
if (!attached) registerBackground(child)
|
|
50
|
+
onSpawn?.(child)
|
|
47
51
|
|
|
48
52
|
child.once('error', (err) => {
|
|
49
53
|
reject(err)
|
|
50
54
|
})
|
|
51
55
|
|
|
52
56
|
child.once('exit', (code, signal) => {
|
|
53
|
-
if (signal) return
|
|
54
|
-
|
|
55
|
-
reject(new Error(`${cmd} exited with code ${code}`))
|
|
57
|
+
if (code === 0 && !signal) return resolve()
|
|
58
|
+
reject(childFailure(outCmd, code, signal))
|
|
56
59
|
})
|
|
57
60
|
})
|
|
58
61
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { spawn } from 'child_process';
|
|
2
2
|
import { replaceTokens, setData } from '../dataLayer';
|
|
3
3
|
import { filterUnwantedEnvVars } from '../../../filterUnwantedEnvVars';
|
|
4
|
+
import { childFailure } from './errors';
|
|
4
5
|
|
|
5
6
|
// Regex to capture tokens like: {out:field value}
|
|
6
7
|
const TOKEN_RX = /\{out:(?<field>[^\s}]+)\s+(?<value>[^\s}]+)\}/g;
|
|
@@ -12,9 +13,10 @@ const TOKEN_RX = /\{out:(?<field>[^\s}]+)\s+(?<value>[^\s}]+)\}/g;
|
|
|
12
13
|
export function runForeground(
|
|
13
14
|
cmd: string,
|
|
14
15
|
envObj: NodeJS.ProcessEnv = {},
|
|
15
|
-
_options: Record<string, unknown> = {}
|
|
16
|
+
_options: Record<string, unknown> = {},
|
|
17
|
+
baseEnv: NodeJS.ProcessEnv = process.env
|
|
16
18
|
): Promise<string> {
|
|
17
|
-
const filteredEnv = filterUnwantedEnvVars(
|
|
19
|
+
const filteredEnv = filterUnwantedEnvVars(baseEnv);
|
|
18
20
|
const newEnv: NodeJS.ProcessEnv = { ...filteredEnv, ...envObj };
|
|
19
21
|
const newCmd = replaceTokens(cmd, newEnv) as string;
|
|
20
22
|
|
|
@@ -51,12 +53,8 @@ export function runForeground(
|
|
|
51
53
|
}
|
|
52
54
|
}
|
|
53
55
|
|
|
54
|
-
if (signal)
|
|
55
|
-
|
|
56
|
-
if (code === 0) return resolve(lastLine);
|
|
57
|
-
reject(
|
|
58
|
-
new Error(`${cmd} exited with code ${code}. Last line: ${lastLine}`)
|
|
59
|
-
);
|
|
56
|
+
if (code === 0 && !signal) return resolve(lastLine);
|
|
57
|
+
reject(childFailure(newCmd, code, signal, `. Last line: ${lastLine}`));
|
|
60
58
|
});
|
|
61
59
|
});
|
|
62
60
|
}
|
|
@@ -26,7 +26,7 @@ export function pruneRepo() {
|
|
|
26
26
|
const branchesToDelete = localBranches.filter((branch) => !remoteBranches.includes(branch))
|
|
27
27
|
if (branchesToDelete.length === 0) {
|
|
28
28
|
log('No local branches to delete.')
|
|
29
|
-
|
|
29
|
+
return
|
|
30
30
|
}
|
|
31
31
|
|
|
32
32
|
// Delete untracked local branches
|
|
@@ -39,6 +39,7 @@ export function pruneRepo() {
|
|
|
39
39
|
} catch (caught: unknown) {
|
|
40
40
|
const error = caught as { message?: string }
|
|
41
41
|
err(`Failed to delete branch ${branch}:`, error.message ?? String(caught))
|
|
42
|
+
process.exitCode = 1
|
|
42
43
|
}
|
|
43
44
|
})
|
|
44
45
|
|
|
@@ -46,5 +47,6 @@ export function pruneRepo() {
|
|
|
46
47
|
} catch (caught: unknown) {
|
|
47
48
|
const error = caught as { message?: string }
|
|
48
49
|
err('An error occurred:', error.message ?? String(caught))
|
|
50
|
+
process.exitCode = 1
|
|
49
51
|
}
|
|
50
52
|
}
|
package/lib/index.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
loadMergedEnv,
|
|
3
|
+
getInjectedEnvKeys,
|
|
4
|
+
getLoadedEnvFiles,
|
|
5
|
+
ENV_FILES_VAR,
|
|
6
|
+
INJECTED_KEYS_VAR,
|
|
7
|
+
} from '@mono-labs/project'
|
|
2
8
|
import { writeLog } from '@mono-labs/shared'
|
|
3
9
|
|
|
4
10
|
import { spawn } from 'node:child_process'
|
|
@@ -9,15 +15,17 @@ import { program } from './app'
|
|
|
9
15
|
import './commands/prune/index'
|
|
10
16
|
import './commands/build-process/index'
|
|
11
17
|
import { getMonoConfig } from './commands/loadFromRoot'
|
|
18
|
+
import { exitCodeFromChild, exitCodeOf } from './commands/build-process/runners/errors'
|
|
12
19
|
import { executeCommandsIfWorkspaceAction } from './commands/build-process/test'
|
|
13
20
|
|
|
14
|
-
|
|
21
|
+
const { config } = getMonoConfig() as any
|
|
22
|
+
|
|
23
|
+
// Global default from .mono/config.json `envFiles`; falls back to MONOLABS_ENV_FILES, then .env + .env.local
|
|
24
|
+
loadMergedEnv(config.envFiles)
|
|
15
25
|
|
|
16
26
|
const homeBin = path.join(os.homedir(), 'bin')
|
|
17
27
|
const PATH = [homeBin, process.env.PATH].filter(Boolean).join(path.delimiter)
|
|
18
28
|
|
|
19
|
-
const { config } = getMonoConfig() as any
|
|
20
|
-
|
|
21
29
|
const workspacemap = config.workspace?.packageMaps || {}
|
|
22
30
|
const preactions = config.workspace?.preactions || []
|
|
23
31
|
const envMapList: string[] = config.envMap ?? ['FAILURE']
|
|
@@ -48,13 +56,29 @@ program.on('command:*', (operands: string[]) => {
|
|
|
48
56
|
const child = spawn('yarn', args, {
|
|
49
57
|
stdio: 'inherit',
|
|
50
58
|
shell: process.platform === 'win32',
|
|
51
|
-
env: {
|
|
59
|
+
env: {
|
|
60
|
+
...process.env,
|
|
61
|
+
...envObj,
|
|
62
|
+
PATH,
|
|
63
|
+
[ENV_FILES_VAR]: getLoadedEnvFiles().join(path.delimiter),
|
|
64
|
+
[INJECTED_KEYS_VAR]: getInjectedEnvKeys().join(','),
|
|
65
|
+
},
|
|
52
66
|
})
|
|
53
67
|
|
|
54
|
-
child.on('
|
|
68
|
+
child.on('error', (err) => {
|
|
69
|
+
console.error(`✖ mono: failed to start yarn: ${err.message}`)
|
|
70
|
+
process.exitCode = 1
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
child.on('exit', (code, signal) => {
|
|
55
74
|
writeLog('Child process exited with code:', code)
|
|
56
|
-
process.exitCode = code
|
|
75
|
+
process.exitCode = exitCodeFromChild(code, signal)
|
|
57
76
|
})
|
|
58
77
|
})
|
|
59
78
|
|
|
60
|
-
|
|
79
|
+
// The single place where a mono failure becomes an exit code. Command actions are async, so
|
|
80
|
+
// parseAsync is required: with parse() a rejected action is dropped and mono exits 0.
|
|
81
|
+
program.parseAsync().catch((err: unknown) => {
|
|
82
|
+
console.error(`✖ mono: ${(err as Error)?.message ?? err}`)
|
|
83
|
+
process.exitCode = exitCodeOf(err)
|
|
84
|
+
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mono-labs/cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.290",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"description": "A CLI tool for building and deploying projects",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"build": "tsc",
|
|
28
28
|
"start": "node bin/mono.js",
|
|
29
29
|
"dev": "node bin/mono.js",
|
|
30
|
-
"test": "
|
|
30
|
+
"test": "tsc && node --test test/exit-codes.test.js",
|
|
31
31
|
"deploy": "tsc && npm publish --access public --registry https://registry.npmjs.org/",
|
|
32
32
|
"release:patch": "npm version patch -m \"chore: release %s\" && npm publish --access public",
|
|
33
33
|
"release:minor": "npm version minor -m \"chore: release %s\" && npm publish --access public",
|
|
@@ -45,8 +45,8 @@
|
|
|
45
45
|
"node": ">=20.0.0"
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@mono-labs/shared": "0.1.
|
|
49
|
-
"@mono-labs/project": "0.1.
|
|
48
|
+
"@mono-labs/shared": "0.1.290",
|
|
49
|
+
"@mono-labs/project": "0.1.290",
|
|
50
50
|
"chalk": "^4.1.2",
|
|
51
51
|
"commander": "^11.1.0",
|
|
52
52
|
"cross-spawn": "^7.0.6",
|