Skip to content

Commit ecb26c8

Browse files
committed
Update preinstall script to enable use as a Git dependency
1 parent 0d87ea7 commit ecb26c8

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

react_on_rails_pro/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@
100100
}
101101
},
102102
"files": [
103-
"packages/node-renderer/dist"
103+
"packages/node-renderer/dist",
104+
"script/preinstall.js"
104105
],
105106
"scripts": {
106-
"preinstall": "yarn run link-source && yalc add --link react-on-rails",
107+
"preinstall": "node ./script/preinstall.js",
107108
"postinstall": "test -f post-yarn-install.local && ./post-yarn-install.local || true",
108109
"link-source": "cd ../packages/react-on-rails && yarn && yalc publish",
109110
"test": "nps test",
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env node
2+
3+
const path = require('path');
4+
const cp = require('child_process');
5+
6+
const inNodeModules = __dirname.split(path.sep).includes('node_modules');
7+
8+
if (inNodeModules) {
9+
console.log('preinstall: running inside node_modules — skipping link steps.');
10+
process.exit(0);
11+
}
12+
13+
function runCommand(cmd, args) {
14+
const res = cp.spawnSync(cmd, args, { stdio: 'inherit' });
15+
if (res.error) throw res.error;
16+
if (res.status !== 0) throw new Error(`${cmd} ${args.join(' ')} exited with status ${res.status}`);
17+
}
18+
19+
try {
20+
// Run the original optional link steps sequentially in a cross-platform way.
21+
// First run the package script `link-source` via yarn, which will run any shell ops inside the script
22+
// (yarn itself will handle invoking a shell for the script body), then call yalc.
23+
runCommand('yarn', ['run', 'link-source']);
24+
runCommand('yalc', ['add', '--link', 'react-on-rails']);
25+
} catch (err) {
26+
// Don't fail the overall install if these optional commands aren't available or fail,
27+
// just log the error.
28+
console.error('preinstall: optional link steps failed or are unavailable — continuing.', err);
29+
// Keep the exit code 0 so the install doesn't fail.
30+
process.exit(0);
31+
}

0 commit comments

Comments
 (0)