Skip to content

Commit bb00362

Browse files
committed
fix: Added static help files for top level commands as well: codify apply, codify plan, codify ...
1 parent f32286b commit bb00362

2 files changed

Lines changed: 31 additions & 11 deletions

File tree

scripts/patch-oclif.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
// give instant feedback before Node launches.
77
//
88
// What the injected bash does (inside the else block, before the "$NODE ... $DIR/run" line):
9-
// - codify --help / -h → cats dist/static/help.txt and exits (no Node startup)
10-
// - codify --version / -v → cats dist/static/version.txt and exits
11-
// - codify apply/destroy/plan → prints "Running Codify <cmd>..." immediately
12-
// (suppressed when --output json or -o json is passed)
13-
// - everything else → falls through to normal Node.js launch
9+
// - codify --help / -h → cats dist/static/help.txt and exits (no Node startup)
10+
// - codify <cmd> --help / -h → cats dist/static/<cmd>-help.txt and exits
11+
// - codify --version / -v → cats dist/static/version.txt and exits
12+
// - codify apply/destroy/plan → prints "Running Codify <cmd>..." immediately
13+
// (suppressed when --output json or -o json is passed)
14+
// - everything else → falls through to normal Node.js launch
1415
//
15-
// Static files (dist/static/help.txt, dist/static/version.txt) are generated in scripts/pkg.ts
16-
// after the esbuild step by running ./bin/dev.js --help and ./bin/dev.js --version.
16+
// Static files (dist/static/*.txt) are generated in scripts/pkg.ts after the esbuild step.
1717
// Missing static files are guarded by [ -f ] so all cases fall back to Node gracefully.
1818
//
1919
// Note: console.log('Running Codify apply/destroy...') was removed from src/commands/apply.ts
@@ -34,11 +34,11 @@ if (!existsSync(BIN_JS)) {
3434
process.exit(0);
3535
}
3636

37-
const content = await fs.readFile(BIN_JS, 'utf8');
37+
let content = await fs.readFile(BIN_JS, 'utf8');
3838

3939
if (content.includes('CODIFY_PATCH_START')) {
40-
console.log('oclif bin.js already patched. Skipping.');
41-
process.exit(0);
40+
console.log('Removing existing patch to reapply...');
41+
content = content.replace(/ # CODIFY_PATCH_START[\s\S]*?# CODIFY_PATCH_END[^\n]*\n/, '');
4242
}
4343

4444
const SEARCH = ' if [ "\\$DEBUG" == "*" ]; then\n echoerr';
@@ -49,14 +49,21 @@ if (idx === -1) {
4949
}
5050

5151
// Patch uses \\$ so that it survives the JS string — in the generated shell script each \\$ becomes \$
52-
// which Bash then interprets as a literal $ (not a template substitution in the JS template literal).
52+
// which bash then interprets as a literal $ (not a template substitution in the JS template literal).
53+
// Bash default-value syntax ${1:-} is avoided since ${...} would be evaluated as a JS template expression.
5354
const PATCH = ` # CODIFY_PATCH_START — do not remove this marker
5455
_first_arg=""
5556
if [ "\\$#" -gt 0 ]; then _first_arg="\\$1"; fi
57+
_second_arg=""
58+
if [ "\\$#" -gt 1 ]; then _second_arg="\\$2"; fi
5659
if [ "\\$_first_arg" = "--help" ] || [ "\\$_first_arg" = "-h" ]; then
5760
_help_file="\\$DIR/../dist/static/help.txt"
5861
if [ -f "\\$_help_file" ]; then cat "\\$_help_file"; exit 0; fi
5962
fi
63+
if [ "\\$_second_arg" = "--help" ] || [ "\\$_second_arg" = "-h" ]; then
64+
_cmd_help_file="\\$DIR/../dist/static/\\$_first_arg-help.txt"
65+
if [ -f "\\$_cmd_help_file" ]; then cat "\\$_cmd_help_file"; exit 0; fi
66+
fi
6067
if [ "\\$_first_arg" = "--version" ] || [ "\\$_first_arg" = "-v" ] || [ "\\$_first_arg" = "version" ]; then
6168
_version_file="\\$DIR/../dist/static/version.txt"
6269
if [ -f "\\$_version_file" ]; then cat "\\$_version_file"; exit 0; fi

scripts/pkg.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ const versionOutput = execSync('./bin/dev.js --version', { shell: 'zsh' }).toStr
4040
await fs.writeFile('./.build/dist/static/help.txt', helpOutput, 'utf8');
4141
await fs.writeFile('./.build/dist/static/version.txt', versionOutput + '\n', 'utf8');
4242

43+
const commandFiles = await fs.readdir('./src/commands');
44+
const commands = commandFiles
45+
.filter(f => f.endsWith('.ts') && !f.startsWith('index'))
46+
.map(f => f.replace(/\.ts$/, ''));
47+
for (const cmd of commands) {
48+
const cmdHelp = execSync(`./bin/dev.js ${cmd} --help`, {
49+
shell: 'zsh',
50+
env: { ...process.env, FORCE_COLOR: '1' },
51+
}).toString();
52+
await fs.writeFile(`./.build/dist/static/${cmd}-help.txt`, cmdHelp, 'utf8');
53+
}
54+
console.log(chalk.magenta(`Generated help files for: ${commands.join(', ')}`))
55+
4356
console.log(chalk.magenta('Install production dependencies'))
4457
execSync('npm install --production', { cwd: './.build', shell: 'zsh' })
4558

0 commit comments

Comments
 (0)