Skip to content

Commit 6cbf366

Browse files
author
Dainer Mesa
committed
fix: enhance ng wrapper script to reliably resolve Angular CLI entrypoint
1 parent cf9d69c commit 6cbf366

1 file changed

Lines changed: 38 additions & 8 deletions

File tree

bin/install

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -116,23 +116,53 @@ if [[ -n "${ng_path}" ]]; then
116116
# Create the bin directory if it doesn't exist
117117
mkdir -p "${install_path}/bin"
118118

119-
# Create a wrapper script
119+
# Create a wrapper script that reliably resolves the JS entrypoint
120120
cat > "${install_path}/bin/ng" << EOF
121121
#!/usr/bin/env node
122-
123-
// Find the real ng.js module
124-
const ngPath = '${ng_path}';
122+
// This wrapper attempts to resolve the real JS entrypoint for @angular/cli
123+
// using Node's module resolution anchored at the install path. This avoids
124+
// accidentally requiring a shell shim (which causes a syntax error in Node).
125125
const fs = require('fs');
126126
127-
if (!fs.existsSync(ngPath)) {
128-
console.error(\`Could not find Angular CLI at: \${ngPath}\`);
127+
function tryResolve() {
128+
const candidates = [
129+
'${install_path}/lib/node_modules',
130+
'${install_path}/node_modules',
131+
'${install_path}/.pnpm/node_modules',
132+
'${install_path}/.pnpm/@angular+cli@${version}/node_modules'
133+
];
134+
135+
for (const base of candidates) {
136+
try {
137+
const resolved = require.resolve('@angular/cli/bin/ng', { paths: [base] });
138+
if (fs.existsSync(resolved)) return resolved;
139+
} catch (e) {
140+
// ignore and continue
141+
}
142+
}
143+
144+
// Fallback to several common JS file locations
145+
const fallback = [
146+
'${install_path}/lib/node_modules/@angular/cli/bin/ng.js',
147+
'${install_path}/node_modules/@angular/cli/bin/ng.js',
148+
'${install_path}/.pnpm/node_modules/@angular/cli/bin/ng.js',
149+
'${install_path}/.pnpm/@angular+cli@${version}/node_modules/@angular/cli/bin/ng.js'
150+
];
151+
for (const f of fallback) {
152+
if (fs.existsSync(f)) return f;
153+
}
154+
return null;
155+
}
156+
157+
const ngPath = tryResolve();
158+
if (!ngPath) {
159+
console.error('Could not resolve @angular/cli binary (ng) from install path.');
129160
process.exit(1);
130161
}
131162
132-
// Load and run the CLI
133163
require(ngPath);
134164
EOF
135-
165+
136166
chmod +x "${install_path}/bin/ng"
137167
else
138168
echo "Could not locate ng binary after install." >&2

0 commit comments

Comments
 (0)