@struggler/cli 1.0.13 → 1.0.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/Makefile CHANGED
@@ -13,7 +13,7 @@ JSON ?=
13
13
  SKIP_INIT ?=
14
14
  SKIP_REFRESH ?=
15
15
 
16
- .PHONY: help install reinstall link unlink init upload refresh deploy add-version example-upload test
16
+ .PHONY: help install reinstall link unlink init upload refresh deploy add-version example-upload test completion-install
17
17
 
18
18
  help:
19
19
  @echo "Available targets:"
@@ -28,6 +28,7 @@ help:
28
28
  @echo " make add-version Run the package version bump script"
29
29
  @echo " make example-upload Upload the sample test/dist3 directory"
30
30
  @echo " make test Run the automated test suite"
31
+ @echo " make completion-install Install zsh completion (adds to ~/.zshrc)"
31
32
  @echo ""
32
33
  @echo "Variables:"
33
34
  @echo " CONFIG=$(CONFIG)"
@@ -58,6 +59,8 @@ link:
58
59
  @hash -r
59
60
  @which struggler-cli || true
60
61
  @struggler-cli -v || true
62
+ @echo ""
63
+ @$(NODE) $(CLI_ENTRY) completion install || true
61
64
 
62
65
  unlink:
63
66
  @$(PNPM) unlink --global @struggler/cli || true
@@ -84,3 +87,19 @@ example-upload:
84
87
 
85
88
  test:
86
89
  $(PNPM) test
90
+
91
+ completion-install:
92
+ @FPATH_DIR="$${HOME}/.zsh/completions"; \
93
+ mkdir -p "$$FPATH_DIR"; \
94
+ $(NODE) $(CLI_ENTRY) completion zsh > "$$FPATH_DIR/_struggler-cli"; \
95
+ echo " ✓ zsh completion installed: $$FPATH_DIR/_struggler-cli"; \
96
+ if ! grep -q "$$FPATH_DIR" "$${HOME}/.zshrc" 2>/dev/null; then \
97
+ echo "" >> "$${HOME}/.zshrc"; \
98
+ echo "# struggler-cli zsh completion" >> "$${HOME}/.zshrc"; \
99
+ echo "fpath=($$FPATH_DIR \$$fpath)" >> "$${HOME}/.zshrc"; \
100
+ echo "autoload -Uz compinit && compinit" >> "$${HOME}/.zshrc"; \
101
+ echo " ✓ added fpath to ~/.zshrc"; \
102
+ else \
103
+ echo " · fpath already in ~/.zshrc, skipped"; \
104
+ fi; \
105
+ echo " → run: source ~/.zshrc"
package/README.md CHANGED
@@ -185,6 +185,39 @@ struggler-cli deploy -d ./dist --json --dry-run
185
185
  - Avoid spaces in profile names; use `prod`, `staging`, `dev`.
186
186
  - When scripting, quote paths: `--config-dir "./command"`.
187
187
 
188
+ ## Shell completion
189
+
190
+ Enable tab completion for subcommands, flags, and profile names.
191
+
192
+ ### zsh (recommended)
193
+
194
+ One-liner install:
195
+
196
+ ```bash
197
+ make completion-install
198
+ source ~/.zshrc
199
+ ```
200
+
201
+ Or manually:
202
+
203
+ ```bash
204
+ mkdir -p ~/.zsh/completions
205
+ struggler-cli completion zsh > ~/.zsh/completions/_struggler-cli
206
+ # add to ~/.zshrc if not already present:
207
+ echo 'fpath=(~/.zsh/completions $fpath)' >> ~/.zshrc
208
+ echo 'autoload -Uz compinit && compinit' >> ~/.zshrc
209
+ source ~/.zshrc
210
+ ```
211
+
212
+ ### bash
213
+
214
+ ```bash
215
+ struggler-cli completion bash >> ~/.bash_profile
216
+ source ~/.bash_profile
217
+ ```
218
+
219
+ After installation, `struggler-cli <TAB>` completes subcommands, flags, and `profile use <TAB>` completes your profile names.
220
+
188
221
  ## Makefile shortcuts
189
222
 
190
223
  ```bash
@@ -0,0 +1,260 @@
1
+ const fs = require('fs');
2
+ const os = require('os');
3
+ const path = require('path');
4
+ const { listProfiles } = require('../lib/profile');
5
+
6
+ // Completion scripts are built from string arrays to avoid
7
+ // conflicts between JS template literals and shell variable syntax like ${(f)...}.
8
+
9
+ function zshScript() {
10
+ const lines = [
11
+ '#compdef struggler-cli',
12
+ '',
13
+ '_struggler_cli() {',
14
+ ' local context state line',
15
+ ' typeset -A opt_args',
16
+ '',
17
+ ' _arguments -C \\',
18
+ " '(-v --version)'{-v,--version}'[show version]' \\",
19
+ " '(-h --help)'{-h,--help}'[show help]' \\",
20
+ " '(-c --config)'{-c,--config}'[qiniu config: profile name or file path]:config:' \\",
21
+ " '--config-dir[meta config directory]:dir:_files -/' \\",
22
+ " '(-d --dir)'{-d,--dir}'[upload source directory]:dir:_files -/' \\",
23
+ " '--dry-run[preview without writing or calling qiniu]' \\",
24
+ " '--concurrency[upload concurrency]:number:(1 2 3 4 5 8 10)' \\",
25
+ " '--exclude[exclude glob pattern]:pattern:' \\",
26
+ " '--ignore-file[custom ignore file]:file:_files' \\",
27
+ " '--manifest[write result to manifest json]:file:_files' \\",
28
+ " '--json[machine-readable json output]' \\",
29
+ " '--skip-init[skip init step in deploy]' \\",
30
+ " '--skip-refresh[skip refresh step in deploy]' \\",
31
+ " '--no-cache[force re-upload all files]' \\",
32
+ " '--lang[ui language]:lang:(zh en)' \\",
33
+ " '1: :_struggler_cli_cmds' \\",
34
+ " '*::arg:->args'",
35
+ '',
36
+ ' case $state in',
37
+ ' args)',
38
+ ' case $line[1] in',
39
+ ' profile)',
40
+ ' _struggler_cli_profile',
41
+ ' ;;',
42
+ ' completion)',
43
+ " _arguments '1:shell:(zsh bash)'",
44
+ ' ;;',
45
+ ' init|upload|refresh|deploy)',
46
+ " _message 'no more subcommands'",
47
+ ' ;;',
48
+ ' esac',
49
+ ' ;;',
50
+ ' esac',
51
+ '}',
52
+ '',
53
+ '_struggler_cli_cmds() {',
54
+ ' local cmds',
55
+ ' cmds=(',
56
+ " 'init:generate versioned upload config'",
57
+ " 'upload:upload build files to qiniu'",
58
+ " 'refresh:refresh qiniu cdn urls'",
59
+ " 'deploy:run init + upload + refresh'",
60
+ " 'profile:manage qiniu credential profiles'",
61
+ " 'completion:output shell completion script'",
62
+ ' )',
63
+ " _describe 'command' cmds",
64
+ '}',
65
+ '',
66
+ '_struggler_cli_profile() {',
67
+ ' local subcmds',
68
+ ' subcmds=(',
69
+ " 'list:list all profiles'",
70
+ " 'use:set active profile'",
71
+ " 'current:show active profile'",
72
+ " 'add:create profile from template'",
73
+ " 'import:import profile from file'",
74
+ ' )',
75
+ '',
76
+ ' if (( CURRENT == 2 )); then',
77
+ " _describe 'profile subcommand' subcmds",
78
+ ' return',
79
+ ' fi',
80
+ '',
81
+ ' case $words[2] in',
82
+ ' use)',
83
+ // Use a dedicated CLI helper to list profiles, avoiding nested quote hell
84
+ ' local profiles',
85
+ ' profiles=($(struggler-cli completion --list-profiles 2>/dev/null))',
86
+ ' if [[ ${#profiles[@]} -gt 0 ]]; then',
87
+ " _describe 'profile' profiles",
88
+ ' else',
89
+ " _message 'profile name'",
90
+ ' fi',
91
+ ' ;;',
92
+ ' import)',
93
+ " (( CURRENT == 3 )) && _message 'profile name'",
94
+ ' (( CURRENT == 4 )) && _files',
95
+ ' ;;',
96
+ ' esac',
97
+ '}',
98
+ '',
99
+ '_struggler_cli',
100
+ '',
101
+ ];
102
+ return lines.join('\n');
103
+ }
104
+
105
+ function bashScript() {
106
+ const lines = [
107
+ '# bash completion for struggler-cli',
108
+ '# source this file or put in ~/.bash_completion.d/struggler-cli',
109
+ '',
110
+ '_struggler_cli_completion() {',
111
+ ' local cur prev words cword',
112
+ ' _init_completion 2>/dev/null || {',
113
+ ' COMPREPLY=()',
114
+ ' cur="${COMP_WORDS[COMP_CWORD]}"',
115
+ ' prev="${COMP_WORDS[COMP_CWORD-1]}"',
116
+ ' }',
117
+ '',
118
+ ' local cmds="init upload refresh deploy profile completion"',
119
+ ' local profile_cmds="list use current add import"',
120
+ ' local global_opts="-v --version -h --help -c --config --config-dir -d --dir --dry-run --concurrency --exclude --ignore-file --manifest --json --skip-init --skip-refresh --no-cache --lang"',
121
+ '',
122
+ ' if [[ ${COMP_CWORD} -eq 1 ]]; then',
123
+ ' COMPREPLY=($(compgen -W "${cmds}" -- "${cur}"))',
124
+ ' return',
125
+ ' fi',
126
+ '',
127
+ ' case "${COMP_WORDS[1]}" in',
128
+ ' profile)',
129
+ ' if [[ ${COMP_CWORD} -eq 2 ]]; then',
130
+ ' COMPREPLY=($(compgen -W "${profile_cmds}" -- "${cur}"))',
131
+ ' return',
132
+ ' fi',
133
+ ' if [[ "${COMP_WORDS[2]}" == "use" && ${COMP_CWORD} -eq 3 ]]; then',
134
+ ' local profiles',
135
+ ' profiles=$(struggler-cli completion --list-profiles 2>/dev/null)',
136
+ ' COMPREPLY=($(compgen -W "${profiles}" -- "${cur}"))',
137
+ ' return',
138
+ ' fi',
139
+ ' ;;',
140
+ ' completion)',
141
+ ' if [[ ${COMP_CWORD} -eq 2 ]]; then',
142
+ ' COMPREPLY=($(compgen -W "zsh bash" -- "${cur}"))',
143
+ ' return',
144
+ ' fi',
145
+ ' ;;',
146
+ ' init|upload|refresh|deploy)',
147
+ ' COMPREPLY=($(compgen -W "${global_opts}" -- "${cur}"))',
148
+ ' return',
149
+ ' ;;',
150
+ ' esac',
151
+ '}',
152
+ '',
153
+ 'complete -F _struggler_cli_completion struggler-cli',
154
+ '',
155
+ ];
156
+ return lines.join('\n');
157
+ }
158
+
159
+ function detectShell() {
160
+ const shellBin = process.env.SHELL || '';
161
+ if (shellBin.endsWith('zsh')) return 'zsh';
162
+ if (shellBin.endsWith('bash')) return 'bash';
163
+ return null;
164
+ }
165
+
166
+ function installZsh(script) {
167
+ const dir = path.join(os.homedir(), '.zsh', 'completions');
168
+ const file = path.join(dir, '_struggler-cli');
169
+ const rcFile = path.join(os.homedir(), '.zshrc');
170
+
171
+ fs.mkdirSync(dir, { recursive: true });
172
+ fs.writeFileSync(file, script, 'utf8');
173
+ console.log(' \u2713 zsh completion installed: ' + file);
174
+
175
+ let rc = '';
176
+ try { rc = fs.readFileSync(rcFile, 'utf8'); } catch { /* new file */ }
177
+
178
+ const fpathLine = 'fpath=(' + dir + ' $fpath)';
179
+ const compLine = 'autoload -Uz compinit && compinit';
180
+ const marker = '# struggler-cli completion';
181
+
182
+ if (!rc.includes(marker)) {
183
+ const addition = '\n' + marker + '\n' + fpathLine + '\n' + compLine + '\n';
184
+ fs.appendFileSync(rcFile, addition, 'utf8');
185
+ console.log(' \u2713 added fpath + compinit to ~/.zshrc');
186
+ } else {
187
+ console.log(' \u00b7 ~/.zshrc already configured, skipped');
188
+ }
189
+ console.log(' \u2192 run: source ~/.zshrc');
190
+ }
191
+
192
+ function installBash(script) {
193
+ const dir = path.join(os.homedir(), '.bash_completion.d');
194
+ const file = path.join(dir, 'struggler-cli');
195
+ const rcFile = path.join(os.homedir(), '.bash_profile');
196
+
197
+ fs.mkdirSync(dir, { recursive: true });
198
+ fs.writeFileSync(file, script, 'utf8');
199
+ console.log(' \u2713 bash completion installed: ' + file);
200
+
201
+ let rc = '';
202
+ try { rc = fs.readFileSync(rcFile, 'utf8'); } catch { /* new file */ }
203
+
204
+ const marker = '# struggler-cli completion';
205
+ if (!rc.includes(marker)) {
206
+ const addition = '\n' + marker + '\n[ -f ' + file + ' ] && source ' + file + '\n';
207
+ fs.appendFileSync(rcFile, addition, 'utf8');
208
+ console.log(' \u2713 added source line to ~/.bash_profile');
209
+ } else {
210
+ console.log(' \u00b7 ~/.bash_profile already configured, skipped');
211
+ }
212
+ console.log(' \u2192 run: source ~/.bash_profile');
213
+ }
214
+
215
+ function installAction(_options) {
216
+ const shell = detectShell();
217
+ if (!shell) {
218
+ process.stderr.write('Cannot detect shell from $SHELL. Run manually:\n');
219
+ process.stderr.write(' struggler-cli completion zsh > ~/.zsh/completions/_struggler-cli\n');
220
+ process.stderr.write(' struggler-cli completion bash > ~/.bash_completion.d/struggler-cli\n');
221
+ process.exitCode = 1;
222
+ return;
223
+ }
224
+ console.log(' detected shell: ' + shell);
225
+ if (shell === 'zsh') {
226
+ installZsh(zshScript());
227
+ } else {
228
+ installBash(bashScript());
229
+ }
230
+ }
231
+
232
+ function completionAction(shell, options) {
233
+ // Internal helper: list profile names for shell completion scripts
234
+ if (options && options.listProfiles) {
235
+ try {
236
+ listProfiles().forEach((name) => process.stdout.write(name + '\n'));
237
+ } catch {
238
+ // silently fail; completion scripts handle empty output
239
+ }
240
+ return;
241
+ }
242
+
243
+ // Auto-install to current shell
244
+ if (shell === 'install' || (options && options.install)) {
245
+ installAction(options);
246
+ return;
247
+ }
248
+
249
+ const s = (shell || '').toLowerCase();
250
+ if (s === 'zsh') {
251
+ process.stdout.write(zshScript());
252
+ } else if (s === 'bash') {
253
+ process.stdout.write(bashScript());
254
+ } else {
255
+ process.stderr.write('Usage: struggler-cli completion <zsh|bash|install>\n');
256
+ process.exitCode = 1;
257
+ }
258
+ }
259
+
260
+ module.exports = completionAction;
package/command/index.js CHANGED
@@ -3,9 +3,11 @@ var upload = require('./upload');
3
3
  var refresh = require('./refresh');
4
4
  var deploy = require('./deploy');
5
5
  var profile = require('./profile');
6
+ var completion = require('./completion');
6
7
 
7
8
  exports.init = init;
8
9
  exports.upload = upload;
9
10
  exports.refresh = refresh;
10
11
  exports.deploy = deploy;
11
12
  exports.profile = profile;
13
+ exports.completion = completion;
@@ -9,7 +9,7 @@ const {
9
9
  getProfilePath,
10
10
  getProfilesDir,
11
11
  } = require('../lib/profile');
12
- const { printMessage } = require('../lib/output');
12
+ const { printMessage, printJson, shouldUseJson } = require('../lib/output');
13
13
  const { getLocale } = require('../lib/i18n');
14
14
 
15
15
  const TEMPLATE_PATH = path.resolve(__dirname, '../def/qiniu.json');
@@ -34,7 +34,11 @@ function listAction(options) {
34
34
  if (current) {
35
35
  printMessage(options, chalk.dim(messages.profileListCurrentHint));
36
36
  }
37
- return { profiles, current };
37
+ const result = { profiles, current };
38
+ if (shouldUseJson(options)) {
39
+ printJson(result);
40
+ }
41
+ return result;
38
42
  }
39
43
 
40
44
  function useAction(name, options) {
package/index.js CHANGED
@@ -9,14 +9,15 @@ const { resolveLang, getLocale } = require("./lib/i18n")
9
9
  const lang = resolveLang(process.argv)
10
10
  const locale = getLocale(lang)
11
11
  const isJsonMode = process.argv.includes("--json")
12
+ const isVersionMode = process.argv.includes("-v") || process.argv.includes("--version")
12
13
 
13
- // 清除命令行
14
- if (!shouldUseJson({ json: isJsonMode })) {
15
- clear()
16
- }
14
+ // 只有在没有执行具体子命令时(即展示根 help)才显示 logo
15
+ const KNOWN_CMDS = new Set(['init', 'upload', 'refresh', 'deploy', 'profile', 'completion'])
16
+ const hasSubCmd = process.argv.slice(2).some((a) => KNOWN_CMDS.has(a))
17
+ const showLogo = !isJsonMode && !hasSubCmd && !isVersionMode
17
18
 
18
- // 输出Logo
19
- if (!isJsonMode) {
19
+ if (showLogo) {
20
+ clear()
20
21
  console.log(require('./lib/logo') + '\n')
21
22
  }
22
23
 
@@ -125,7 +126,16 @@ profileCmd
125
126
  command.profile.import(name, file, program.opts())
126
127
  })
127
128
 
128
- program.addHelpCommand(true, locale.help.helpCommandDescription)
129
+ program
130
+ .command("completion [shell]")
131
+ .description(locale.commands.completion)
132
+ .option("--list-profiles", "list profile names (used by shell completion)")
133
+ .addHelpText('after', '\nshell: zsh | bash | install (auto-detect and install)')
134
+ .action((shell, cmdOpts) => {
135
+ command.completion(shell, { ...program.opts(), ...cmdOpts })
136
+ })
137
+
138
+ program.addHelpCommand(false)
129
139
 
130
140
  program.parseAsync().catch(error => {
131
141
  if (isJsonMode) {
package/lib/i18n.js CHANGED
@@ -72,6 +72,7 @@ const LANGUAGES = {
72
72
  profileCurrent: '显示当前 profile',
73
73
  profileAdd: '从模版新建 profile',
74
74
  profileImport: '从已有文件导入 profile',
75
+ completion: '输出 shell 补全脚本(zsh / bash),或 install 自动安装到当前 shell',
75
76
  },
76
77
  help: {
77
78
  helpCommandDescription: '显示指定命令的帮助信息',
@@ -161,6 +162,7 @@ const LANGUAGES = {
161
162
  profileCurrent: 'Show active profile',
162
163
  profileAdd: 'Create profile from template',
163
164
  profileImport: 'Import profile from file',
165
+ completion: 'Output shell completion script (zsh / bash), or "install" to auto-install for current shell',
164
166
  },
165
167
  help: {
166
168
  helpCommandDescription: 'display help for command',
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@struggler/cli",
3
- "version": "1.0.13",
3
+ "version": "1.0.15",
4
4
  "description": "CLI to Upload vite packaged files to Qiniu Cloud OSS.",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
7
  "add-version": "node ./command/addVersion.js",
8
- "test": "node --test"
8
+ "test": "node --test",
9
+ "postinstall": "node ./scripts/postinstall.js"
9
10
  },
10
11
  "bin": {
11
12
  "struggler-cli": "./index.js"
@@ -0,0 +1,19 @@
1
+ #!/usr/bin/env node
2
+ // Only print the hint during global installs; skip in CI / local dev installs.
3
+ const isGlobal = process.env.npm_config_global === 'true';
4
+ const isCI = process.env.CI || process.env.CONTINUOUS_INTEGRATION;
5
+
6
+ if (!isGlobal || isCI) process.exit(0);
7
+
8
+ const shell = (process.env.SHELL || '').split('/').pop();
9
+ const supported = shell === 'zsh' || shell === 'bash';
10
+
11
+ console.log('');
12
+ console.log(' \u2728 struggler-cli installed!');
13
+ if (supported) {
14
+ console.log(' \u2192 Enable tab completion (one-time setup):');
15
+ console.log(' struggler-cli completion install');
16
+ } else {
17
+ console.log(' \u2192 Enable tab completion: struggler-cli completion <zsh|bash|install>');
18
+ }
19
+ console.log('');