11#!/usr/bin/env node
22
3- const { spawnSync } = require ( "child_process" )
4- const path = require ( "path" )
5-
63const PACK_CONFIG_DIFF_PACKAGE = "pack-config-diff"
74
8- function getNpxSpec ( ) {
9- try {
10- const pkg = require ( path . join ( __dirname , ".." , "package.json" ) )
11- const version =
12- ( pkg . optionalDependencies &&
13- pkg . optionalDependencies [ PACK_CONFIG_DIFF_PACKAGE ] ) ||
14- ( pkg . dependencies && pkg . dependencies [ PACK_CONFIG_DIFF_PACKAGE ] )
15- if ( version ) return `${ PACK_CONFIG_DIFF_PACKAGE } @${ version } `
16- } catch ( _ ) {
17- // Fall through to default
18- }
19- return `${ PACK_CONFIG_DIFF_PACKAGE } @^0.1.0`
20- }
21-
225function formatError ( error ) {
236 return error instanceof Error ? error . message : String ( error )
247}
258
26- function isMissingPackConfigDiff ( error ) {
27- return (
28- error &&
29- error . code === "MODULE_NOT_FOUND" &&
30- typeof error . message === "string" &&
31- error . message . includes ( `'${ PACK_CONFIG_DIFF_PACKAGE } '` )
32- )
33- }
34-
359function exitWithCode ( exitCode ) {
3610 if ( ! Number . isInteger ( exitCode ) ) {
3711 console . error (
@@ -41,57 +15,20 @@ function exitWithCode(exitCode) {
4115 }
4216
4317 // Forward only pack-config-diff's documented exit codes (0 = no diffs, 1 = diffs found).
44- // Any other code is a tool failure — normalize to 2, matching the npx fallback path .
18+ // Any other code is a tool failure — normalize to 2.
4519 process . exit ( exitCode <= 1 ? exitCode : 2 )
4620}
4721
48- function runViaNpx ( ) {
49- if ( process . env . CI ) {
50- console . error (
51- `[Shakapacker] ${ PACK_CONFIG_DIFF_PACKAGE } is not installed locally, and npx fallback requires an interactive prompt. ` +
52- `Install ${ PACK_CONFIG_DIFF_PACKAGE } locally before running in CI.`
53- )
54- process . exit ( 2 )
55- }
56-
57- console . error (
58- `[Shakapacker] ${ PACK_CONFIG_DIFF_PACKAGE } is not installed locally. ` +
59- "Falling back to npx and prompting before download."
60- )
61- const result = spawnSync ( "npx" , [ getNpxSpec ( ) , ...process . argv . slice ( 2 ) ] , {
62- stdio : "inherit"
63- } )
64-
65- if ( result . error ) {
66- console . error (
67- `[Shakapacker] Failed to run ${ PACK_CONFIG_DIFF_PACKAGE } via npx: ${ formatError ( result . error ) } `
68- )
69- process . exit ( 2 )
70- }
71-
72- if ( result . signal || result . status == null ) {
73- process . exit ( 2 )
74- }
75-
76- // Forward only pack-config-diff's documented exit codes (0 = no diffs, 1 = diffs found).
77- // Any other code (e.g. 127 = npx not found, 126 = permission denied) is a tool failure.
78- process . exit ( result . status <= 1 ? result . status : 2 )
79- }
80-
8122let run
8223
8324try {
8425 const loadedModule = require ( "pack-config-diff" )
8526 run = loadedModule . run
8627} catch ( error ) {
87- if ( isMissingPackConfigDiff ( error ) ) {
88- runViaNpx ( )
89- } else {
90- console . error (
91- `[Shakapacker] Failed to load ${ PACK_CONFIG_DIFF_PACKAGE } : ${ formatError ( error ) } `
92- )
93- process . exit ( 2 )
94- }
28+ console . error (
29+ `[Shakapacker] Failed to load ${ PACK_CONFIG_DIFF_PACKAGE } : ${ formatError ( error ) } `
30+ )
31+ process . exit ( 2 )
9532}
9633
9734if ( typeof run !== "function" ) {
0 commit comments