Skip to content

Commit c5c3e30

Browse files
committed
feat: Added handler for * expansion in zsh and updated the help for the import command
1 parent 8171646 commit c5c3e30

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/commands/import.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
1-
import { Flags } from '@oclif/core';
1+
import fs from 'node:fs/promises';
22
import path from 'node:path';
33

44
import { BaseCommand } from '../common/base-command.js';
55
import { ImportOrchestrator } from '../orchestrators/import.js';
6+
import { ShellUtils } from '../utils/shell.js';
67

78
export default class Import extends BaseCommand {
89
static strict = false;
9-
static override description = 'Generate codify configs from existing installations'
10+
static override description =
11+
`Generate codify configs from already installed packages. Use a list of space separated arguments to specify the resource types to import. Leave blank to import all resource in an existing *.codify.json file.
12+
13+
Modes:
14+
1. No args: if no args are specified and an *.codify.json already exists. Then codify will update the existing file with any new changes to the resources specified in the file/files.
15+
16+
Command: codify import
17+
18+
2. With args: specify specific resources to import using arguments. Wild card matching is supported using '*' and ? (Note: in zsh * expands to the current dir and needs to be escaped using \\* or '*'). A prompt will be shown if more information is required to complete the import.
19+
20+
Example: codify import nvm asdf\\*, codify import \\* (for importing all supported resources)
21+
22+
The results can then be saved:
23+
a. To an existing *.codify.json file
24+
b. To a new file
25+
c. Or only printed to console
26+
27+
Codify will try to smartly insert new configs by following existing spacing and formatting.
28+
`
29+
static override args = ''
30+
1031
static override examples = [
11-
'<%= config.bin %> <%= command.id %> homebrew nvm',
32+
'<%= config.bin %> <%= command.id %> homebrew nvm asdf\\*',
33+
'<%= config.bin %> <%= command.id %>',
34+
'<%= config.bin %> <%= command.id %> git-clone --path ../my/other/folder',
35+
'<%= config.bin %> <%= command.id %> \\*'
1236
]
1337

1438
public async run(): Promise<void> {
@@ -24,12 +48,25 @@ export default class Import extends BaseCommand {
2448
.filter((r) => r.type === 'arg')
2549
.map((r) => r.input);
2650

51+
const cleanedArgs = await this.cleanupZshStarExpansion(args);
52+
2753
await ImportOrchestrator.run({
28-
typeIds: args,
54+
typeIds: cleanedArgs,
2955
path: resolvedPath,
3056
secureMode: flags.secure,
3157
}, this.reporter)
3258

3359
process.exit(0)
3460
}
61+
62+
private async cleanupZshStarExpansion(args: string[]): Promise<string[]> {
63+
const combinedArgs = args.join(' ');
64+
const zshStarExpansion = (await ShellUtils.isZshShell())
65+
? (await fs.readdir(process.cwd())).filter((name) => !name.startsWith('.')).join(' ')
66+
: ''
67+
68+
return combinedArgs
69+
.replaceAll(zshStarExpansion, '*')
70+
.split(' ')
71+
}
3572
}

src/utils/shell.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import util from 'node:util';
2+
import cp from 'node:child_process';
3+
4+
const exec = util.promisify(cp.exec);
5+
6+
export class ShellUtils {
7+
static async isZshShell(): Promise<boolean> {
8+
try {
9+
await exec('echo $ZSH_VERSION');
10+
return true;
11+
} catch {
12+
return false;
13+
}
14+
}
15+
16+
}

0 commit comments

Comments
 (0)