Skip to content

Commit 5200a30

Browse files
committed
Update preinstall script to enable use as a Git dependency
1 parent 64dc33a commit 5200a30

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

react_on_rails_pro/package.json

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

0 commit comments

Comments
 (0)