@struggler/cli 1.0.12 → 1.0.14

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)"
@@ -55,9 +56,16 @@ link:
55
56
  $(PNPM) setup; \
56
57
  fi
57
58
  @$(PNPM) link --global
59
+ @hash -r
60
+ @which struggler-cli || true
61
+ @struggler-cli -v || true
62
+ @echo ""
63
+ @$(NODE) $(CLI_ENTRY) completion install || true
58
64
 
59
65
  unlink:
60
- @$(PNPM) unlink --global @struggler/cli
66
+ @$(PNPM) unlink --global @struggler/cli || true
67
+ @npm uninstall -g @struggler/cli || true
68
+ @hash -r
61
69
 
62
70
  init:
63
71
  $(NODE) $(CLI_ENTRY) --config $(CONFIG) --dir $(DIR) $(DRY_RUN) $(JSON) init
@@ -79,3 +87,19 @@ example-upload:
79
87
 
80
88
  test:
81
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
@@ -1,6 +1,8 @@
1
1
  # struggler-cli
2
2
 
3
- `struggler-cli` is a small deployment CLI for front-end build assets on Qiniu Cloud. It can generate versioned paths, upload build output, refresh CDN URLs, and now supports `dry-run`, concurrent uploads, ignore rules, manifest export, JSON output, and a one-shot `deploy` command.
3
+ `struggler-cli` is a small deployment CLI for front-end build assets on Qiniu Cloud. It supports versioned paths, upload, CDN refresh, upload cache, profiles for multiple Qiniu accounts, `dry-run`, concurrency, ignore rules, manifest export, JSON output, and a one-shot `deploy` command.
4
+
5
+ Works on **macOS, Linux, and Windows** (Node.js `path` / `fs` APIs are cross-platform).
4
6
 
5
7
  ## Install
6
8
 
@@ -14,26 +16,121 @@ For local command usage:
14
16
  pnpm link --global
15
17
  ```
16
18
 
17
- ## Config Files
19
+ ## First-time setup (recommended: profiles)
20
+
21
+ Use **profiles** to store Qiniu credentials under your user home: `~/.struggler-cli/` (Windows: `%USERPROFILE%\\.struggler-cli\\`). This folder is created automatically; it is **not** shipped with the npm package.
22
+
23
+ ```text
24
+ ~/.struggler-cli/ # created on first profile add/import (Windows/macOS/Linux)
25
+ profiles/
26
+ prod.json # Qiniu credentials for production
27
+ staging.json
28
+ current # one line: active profile name, e.g. prod
29
+
30
+ your-project/
31
+ command/
32
+ config.json # deploy prefix (publicPath / base), from init
33
+ upload-cache.json # upload cache (optional)
34
+ dist/ # build output to upload (-d)
35
+ ```
36
+
37
+ ### Step 1 — Create a profile
38
+
39
+ From your project root:
40
+
41
+ ```bash
42
+ struggler-cli profile add prod
43
+ ```
44
+
45
+ This creates `~/.struggler-cli/profiles/prod.json` from the built-in template. Open it and fill in:
46
+
47
+ | Field | Description |
48
+ |-------|-------------|
49
+ | `path` | CDN path prefix segment for this app |
50
+ | `accessKey` | Qiniu access key |
51
+ | `secretKey` | Qiniu secret key |
52
+ | `Bucket` | Bucket name |
53
+ | `zone` | e.g. `Zone_z0`, `Zone_z1`, `Zone_z2` |
54
+ | `domain` | CDN domain, e.g. `https://cdn.example.com/` |
55
+
56
+ ### Step 2 — Activate the profile
57
+
58
+ ```bash
59
+ struggler-cli profile use prod
60
+ ```
61
+
62
+ Or pass the profile name per command: `-c prod`.
63
+
64
+ ### Step 3 — Generate deploy metadata
65
+
66
+ ```bash
67
+ struggler-cli init -d ./dist
68
+ ```
69
+
70
+ Writes `command/config.json` with a timestamped `publicPath` and `base` URL (always under `./command/`, same as older versions).
71
+
72
+ ### Step 4 — Upload
73
+
74
+ ```bash
75
+ struggler-cli upload -d ./dist
76
+ ```
77
+
78
+ ### More environments
79
+
80
+ ```bash
81
+ struggler-cli profile add staging
82
+ # edit ~/.struggler-cli/profiles/staging.json
83
+
84
+ struggler-cli profile import dev ./path/to/existing-qiniu.json
85
+
86
+ struggler-cli profile list
87
+ struggler-cli profile current
88
+ struggler-cli profile use staging
89
+ struggler-cli init
90
+ struggler-cli upload -d ./dist
91
+ ```
92
+
93
+ Because profiles are user-level, they are outside your project repo by default.
18
94
 
19
- The CLI expects two files in the same directory:
95
+ ## Legacy setup (single `command/qiniu.json`)
20
96
 
21
- - `qiniu.json`: Qiniu credentials and bucket metadata
22
- - `config.json`: generated deploy prefix metadata used by upload/refresh
97
+ Still supported for existing projects:
23
98
 
24
- Example `qiniu.json`:
99
+ ```text
100
+ command/
101
+ qiniu.json
102
+ config.json
103
+ ```
25
104
 
26
- ```json
27
- {
28
- "path": "your-project",
29
- "accessKey": "",
30
- "secretKey": "",
31
- "Bucket": "",
32
- "zone": "Zone_z1",
33
- "domain": "https://cdn.example.com/"
34
- }
105
+ ```bash
106
+ struggler-cli init -c ./command/qiniu.json -d ./dist
107
+ struggler-cli upload -c ./command/qiniu.json -d ./dist
35
108
  ```
36
109
 
110
+ If no profile is active and you omit `-c`, the CLI defaults to `./command/qiniu.json`.
111
+
112
+ ## How `-c` resolves config
113
+
114
+ | You pass | Resolved to |
115
+ |----------|-------------|
116
+ | *(omit)* | Active profile in `~/.struggler-cli/current`, else `./command/qiniu.json` |
117
+ | `-c prod` | `~/.struggler-cli/profiles/prod.json` |
118
+ | `-c ./command/qiniu.json` | That file path (legacy) |
119
+
120
+ `config.json` and upload cache stay in `./command/` unless you pass `--config-dir`.
121
+
122
+ ## Profile commands
123
+
124
+ ```bash
125
+ struggler-cli profile list
126
+ struggler-cli profile use <name>
127
+ struggler-cli profile current
128
+ struggler-cli profile add <name>
129
+ struggler-cli profile import <name> <file>
130
+ ```
131
+
132
+ ## Other config files
133
+
37
134
  Optional ignore file:
38
135
 
39
136
  ```text
@@ -53,66 +150,81 @@ legacy/**
53
150
  Initialize versioned config:
54
151
 
55
152
  ```bash
56
- node ./index.js --config ./command/qiniu.json --dir ./dist init
153
+ struggler-cli init -d ./dist
57
154
  ```
58
155
 
59
- Preview upload work without changing files or calling Qiniu:
156
+ Preview upload:
60
157
 
61
158
  ```bash
62
- node ./index.js --config ./command/qiniu.json --dir ./dist --dry-run upload
159
+ struggler-cli --dry-run upload -d ./dist
63
160
  ```
64
161
 
65
162
  Upload with concurrency:
66
163
 
67
164
  ```bash
68
- node ./index.js --config ./command/qiniu.json --dir ./dist --concurrency 8 upload
165
+ struggler-cli upload -d ./dist --concurrency 8
69
166
  ```
70
167
 
71
- Upload with ignore patterns and a manifest:
168
+ Full deploy:
72
169
 
73
170
  ```bash
74
- node ./index.js --config ./command/qiniu.json --dir ./dist --exclude ".DS_Store,*.map" --manifest ./artifacts/upload-manifest.json upload
171
+ struggler-cli deploy -d ./dist --concurrency 8
75
172
  ```
76
173
 
77
- Refresh CDN for generated URLs:
174
+ Machine-readable output:
78
175
 
79
176
  ```bash
80
- node ./index.js --config ./command/qiniu.json --dir ./dist refresh
177
+ struggler-cli deploy -d ./dist --json --dry-run
81
178
  ```
82
179
 
83
- Run the full flow:
180
+ ## Windows notes
84
181
 
85
- ```bash
86
- node ./index.js --config ./command/qiniu.json --dir ./dist --concurrency 8 deploy
87
- ```
182
+ - The folder name `.struggler-cli` is valid on Windows; Node resolves paths with `\` automatically.
183
+ - User-level profile root on Windows is `%USERPROFILE%\\.struggler-cli\\`.
184
+ - In **Cmd** or **PowerShell**, run the same commands as on Unix.
185
+ - Avoid spaces in profile names; use `prod`, `staging`, `dev`.
186
+ - When scripting, quote paths: `--config-dir "./command"`.
187
+
188
+ ## Shell completion
189
+
190
+ Enable tab completion for subcommands, flags, and profile names.
88
191
 
89
- Skip parts of deploy:
192
+ ### zsh (recommended)
193
+
194
+ One-liner install:
90
195
 
91
196
  ```bash
92
- node ./index.js --config ./command/qiniu.json --dir ./dist --skip-refresh deploy
197
+ make completion-install
198
+ source ~/.zshrc
93
199
  ```
94
200
 
95
- Machine-readable output:
201
+ Or manually:
96
202
 
97
203
  ```bash
98
- node ./index.js --config ./command/qiniu.json --dir ./dist --json --dry-run deploy
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
99
210
  ```
100
211
 
101
- Version bump script:
212
+ ### bash
102
213
 
103
214
  ```bash
104
- pnpm add-version
215
+ struggler-cli completion bash >> ~/.bash_profile
216
+ source ~/.bash_profile
105
217
  ```
106
218
 
107
- ## Makefile Shortcuts
219
+ After installation, `struggler-cli <TAB>` completes subcommands, flags, and `profile use <TAB>` completes your profile names.
220
+
221
+ ## Makefile shortcuts
108
222
 
109
223
  ```bash
110
224
  make init
111
225
  make upload
112
226
  make refresh
113
227
  make deploy
114
- make upload DIR=./test/dist3 CONFIG=./test/command/qiniu.json DRY_RUN=--dry-run
115
- make deploy MANIFEST=./artifacts/deploy.json JSON=--json SKIP_REFRESH=--skip-refresh
116
228
  ```
117
229
 
118
230
  ## Test
@@ -121,4 +233,4 @@ make deploy MANIFEST=./artifacts/deploy.json JSON=--json SKIP_REFRESH=--skip-ref
121
233
  pnpm test
122
234
  ```
123
235
 
124
- The automated tests cover config path resolution, init dry-run behavior, upload ignore handling, manifest generation, and deploy JSON summaries.
236
+ Tests cover profile resolution, config paths, init, upload, and deploy.
@@ -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
@@ -2,8 +2,12 @@ var init = require('./init');
2
2
  var upload = require('./upload');
3
3
  var refresh = require('./refresh');
4
4
  var deploy = require('./deploy');
5
+ var profile = require('./profile');
6
+ var completion = require('./completion');
5
7
 
6
8
  exports.init = init;
7
9
  exports.upload = upload;
8
10
  exports.refresh = refresh;
9
11
  exports.deploy = deploy;
12
+ exports.profile = profile;
13
+ exports.completion = completion;
@@ -0,0 +1,88 @@
1
+ const path = require('path');
2
+ const chalk = require('chalk');
3
+ const {
4
+ listProfiles,
5
+ readCurrentProfile,
6
+ writeCurrentProfile,
7
+ importProfile,
8
+ addProfile,
9
+ getProfilePath,
10
+ getProfilesDir,
11
+ } = require('../lib/profile');
12
+ const { printMessage, printJson, shouldUseJson } = require('../lib/output');
13
+ const { getLocale } = require('../lib/i18n');
14
+
15
+ const TEMPLATE_PATH = path.resolve(__dirname, '../def/qiniu.json');
16
+
17
+ function listAction(options) {
18
+ const { messages } = getLocale(options.lang);
19
+ const profiles = listProfiles();
20
+ const current = readCurrentProfile();
21
+
22
+ if (profiles.length === 0) {
23
+ printMessage(options, chalk.yellow(` ${messages.profileListEmpty}`));
24
+ printMessage(options, chalk.dim(` ${getProfilesDir()}`));
25
+ return { profiles: [], current: null };
26
+ }
27
+
28
+ printMessage(options, messages.profileListTitle);
29
+ profiles.forEach((name) => {
30
+ const marker = name === current ? chalk.green(' *') : ' ';
31
+ const file = getProfilePath(name);
32
+ printMessage(options, `${marker} ${name}${chalk.dim(` → ${file}`)}`);
33
+ });
34
+ if (current) {
35
+ printMessage(options, chalk.dim(messages.profileListCurrentHint));
36
+ }
37
+ const result = { profiles, current };
38
+ if (shouldUseJson(options)) {
39
+ printJson(result);
40
+ }
41
+ return result;
42
+ }
43
+
44
+ function useAction(name, options) {
45
+ const { messages } = getLocale(options.lang);
46
+ const profilePath = writeCurrentProfile(name);
47
+ printMessage(options, chalk.green(` ✓ ${messages.profileUseDone(name)}`));
48
+ printMessage(options, chalk.dim(` ${profilePath}`));
49
+ return { name, path: profilePath };
50
+ }
51
+
52
+ function currentAction(options) {
53
+ const { messages } = getLocale(options.lang);
54
+ const current = readCurrentProfile();
55
+ if (!current) {
56
+ printMessage(options, chalk.yellow(` ${messages.profileNoCurrent}`));
57
+ return { current: null };
58
+ }
59
+ const profilePath = getProfilePath(current);
60
+ printMessage(options, `${messages.profileCurrentLabel} ${chalk.cyan(current)}`);
61
+ printMessage(options, chalk.dim(` ${profilePath}`));
62
+ return { current, path: profilePath };
63
+ }
64
+
65
+ function addAction(name, options) {
66
+ const { messages } = getLocale(options.lang);
67
+ const profilePath = addProfile(name, TEMPLATE_PATH);
68
+ printMessage(options, chalk.green(` ✓ ${messages.profileAddDone(name)}`));
69
+ printMessage(options, chalk.dim(` ${profilePath}`));
70
+ printMessage(options, chalk.dim(messages.profileEditHint));
71
+ return { name, path: profilePath };
72
+ }
73
+
74
+ function importAction(name, fromPath, options) {
75
+ const { messages } = getLocale(options.lang);
76
+ const profilePath = importProfile(name, fromPath);
77
+ printMessage(options, chalk.green(` ✓ ${messages.profileImportDone(name)}`));
78
+ printMessage(options, chalk.dim(` ${profilePath}`));
79
+ return { name, path: profilePath, from: path.resolve(fromPath) };
80
+ }
81
+
82
+ module.exports = {
83
+ list: listAction,
84
+ use: useAction,
85
+ current: currentAction,
86
+ add: addAction,
87
+ import: importAction,
88
+ };
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
 
@@ -58,7 +59,7 @@ program.configureHelp({
58
59
  },
59
60
  })
60
61
 
61
- program.name("struggler-cli").description(locale.appDescription).version(packageJson.version, "-v, --version", locale.options.version).helpOption("-h, --help", locale.options.help).option("-c, --config <path>", locale.options.config, "./command/qiniu.json").option("--config-dir [path]", locale.options.configDir).option("-d, --dir <path>", locale.options.dir, "./dist").option("--dry-run", locale.options.dryRun).option("--concurrency <number>", locale.options.concurrency, "5").option("--exclude <pattern>", locale.options.exclude).option("--ignore-file <path>", locale.options.ignoreFile, ".strugglerignore").option("--manifest <path>", locale.options.manifest).option("--json", locale.options.json).option("--skip-init", locale.options.skipInit).option("--skip-refresh", locale.options.skipRefresh).option("--no-cache", locale.options.noCache).option("--lang <lang>", locale.options.lang, lang)
62
+ program.name("struggler-cli").description(locale.appDescription).version(packageJson.version, "-v, --version", locale.options.version).helpOption("-h, --help", locale.options.help).option("-c, --config <path>", locale.options.config).option("--config-dir [path]", locale.options.configDir).option("-d, --dir <path>", locale.options.dir, "./dist").option("--dry-run", locale.options.dryRun).option("--concurrency <number>", locale.options.concurrency, "5").option("--exclude <pattern>", locale.options.exclude).option("--ignore-file <path>", locale.options.ignoreFile, ".strugglerignore").option("--manifest <path>", locale.options.manifest).option("--json", locale.options.json).option("--skip-init", locale.options.skipInit).option("--skip-refresh", locale.options.skipRefresh).option("--no-cache", locale.options.noCache).option("--lang <lang>", locale.options.lang, lang)
62
63
 
63
64
  program
64
65
  .command("init")
@@ -88,7 +89,53 @@ program
88
89
  await command.deploy(program.opts())
89
90
  })
90
91
 
91
- program.addHelpCommand(true, locale.help.helpCommandDescription)
92
+ const profileCmd = program.command("profile").description(locale.commands.profile)
93
+
94
+ profileCmd
95
+ .command("list")
96
+ .description(locale.commands.profileList)
97
+ .action(() => {
98
+ command.profile.list(program.opts())
99
+ })
100
+
101
+ profileCmd
102
+ .command("use <name>")
103
+ .description(locale.commands.profileUse)
104
+ .action((name) => {
105
+ command.profile.use(name, program.opts())
106
+ })
107
+
108
+ profileCmd
109
+ .command("current")
110
+ .description(locale.commands.profileCurrent)
111
+ .action(() => {
112
+ command.profile.current(program.opts())
113
+ })
114
+
115
+ profileCmd
116
+ .command("add <name>")
117
+ .description(locale.commands.profileAdd)
118
+ .action((name) => {
119
+ command.profile.add(name, program.opts())
120
+ })
121
+
122
+ profileCmd
123
+ .command("import <name> <file>")
124
+ .description(locale.commands.profileImport)
125
+ .action((name, file) => {
126
+ command.profile.import(name, file, program.opts())
127
+ })
128
+
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)
92
139
 
93
140
  program.parseAsync().catch(error => {
94
141
  if (isJsonMode) {
package/lib/config.js CHANGED
@@ -1,5 +1,6 @@
1
1
  let path = require('path')
2
2
  const { getJsonData } = require('./files');
3
+ const { resolveProfileConfigPath } = require('./profile');
3
4
 
4
5
  const DEFAULT_QINIU_CONFIG = './command/qiniu.json';
5
6
  const DEFAULT_META_DIR = './command';
@@ -11,6 +12,10 @@ function resolveFromCwd(targetPath) {
11
12
  }
12
13
 
13
14
  function getQiniuConfigPath(options = {}) {
15
+ const profilePath = resolveProfileConfigPath(options);
16
+ if (profilePath) {
17
+ return profilePath;
18
+ }
14
19
  return resolveFromCwd(options.config || DEFAULT_QINIU_CONFIG);
15
20
  }
16
21
 
@@ -25,24 +30,19 @@ function resolveConfigDirOption(options = {}) {
25
30
  return null;
26
31
  }
27
32
 
28
- /** config.json / upload-cache.json 所在目录;优先级:CLI --config-dir > qiniu.json configDir > qiniu 同目录 > ./command */
33
+ /** config.json / upload-cache.json 所在目录;优先级:CLI --config-dir > qiniu.json configDir > 默认 ./command(与旧版一致,不随 -c 目录变化) */
29
34
  function getMetaDir(options = {}) {
30
35
  const cliConfigDir = resolveConfigDirOption(options);
31
36
  if (cliConfigDir) {
32
37
  return resolveFromCwd(cliConfigDir);
33
38
  }
34
39
 
35
- const qiniuConfigPath = getQiniuConfigPath(options);
36
- const qiniuConfig = getJsonData(qiniuConfigPath);
40
+ const qiniuConfig = getJsonData(getQiniuConfigPath(options));
37
41
  if (qiniuConfig.configDir) {
38
42
  return resolveFromCwd(qiniuConfig.configDir);
39
43
  }
40
44
 
41
- if (!options.config) {
42
- return resolveFromCwd(DEFAULT_META_DIR);
43
- }
44
-
45
- return path.dirname(qiniuConfigPath);
45
+ return resolveFromCwd(DEFAULT_META_DIR);
46
46
  }
47
47
 
48
48
  function getConfigPath(options = {}) {
package/lib/i18n.js CHANGED
@@ -30,15 +30,24 @@ const LANGUAGES = {
30
30
  deployUnit: '个',
31
31
  errorTitle: '出错了',
32
32
  errorMissingConfig: '配置不完整,以下字段缺失或为空:',
33
- errorMissingConfigHint: '请检查 command/qiniu.json command/config.json,或运行 struggler-cli init 初始化配置。',
33
+ errorMissingConfigHint: '请检查七牛配置(~/.struggler-cli/profiles,Windows 为 %USERPROFILE%\\.struggler-cli\\profiles)或 command/qiniu.json,以及 command/config.json;也可先运行 struggler-cli init',
34
+ profileListEmpty: '尚无 profile,请先执行:struggler-cli profile add <name>',
35
+ profileListTitle: '可用 profile:',
36
+ profileListCurrentHint: '(* 为当前激活)',
37
+ profileUseDone: (name) => `已切换 profile:${name}`,
38
+ profileNoCurrent: '未设置当前 profile。可执行:struggler-cli profile use <name>',
39
+ profileCurrentLabel: '当前 profile:',
40
+ profileAddDone: (name) => `已创建 profile:${name}`,
41
+ profileImportDone: (name) => `已导入 profile:${name}`,
42
+ profileEditHint: '请编辑该文件,填写 accessKey、secretKey、Bucket、zone、domain 等字段。',
34
43
  dryRunLabel: '预览',
35
44
  dryRunPlan: (action, n) => `[预览] ${action} 计划 (${n} 个文件)`,
36
45
  manifestWritten: '清单已写入:',
37
46
  },
38
47
  options: {
39
48
  version: '显示版本号。',
40
- config: '指定上传配置文件路径。',
41
- configDir: '指定 config.json、upload-cache.json 所在目录;可写 --config-dir ./command,单独写 --config-dir 时默认为 ./command。',
49
+ config: '七牛配置:profile 名(如 prod)、文件路径(如 ./command/qiniu.json);不传则优先用 ~/.struggler-cli/current(Windows 为 %USERPROFILE%\\.struggler-cli\\current),否则回退 ./command/qiniu.json。',
50
+ configDir: '指定 config.json、upload-cache.json 所在目录;不传时固定为 ./command(与旧版一致);可写 --config-dir ./command。',
42
51
  dir: '指定要上传的目录。',
43
52
  dryRun: '仅预览执行计划,不写文件也不调用七牛接口。',
44
53
  concurrency: '设置上传并发数。',
@@ -57,6 +66,13 @@ const LANGUAGES = {
57
66
  upload: '将指定目录下文件上传到七牛云。',
58
67
  refresh: '刷新七牛云 CDN 文件。',
59
68
  deploy: '一次执行 init、upload 和 refresh。',
69
+ profile: '管理多套七牛配置(profile)。',
70
+ profileList: '列出所有 profile',
71
+ profileUse: '切换当前 profile',
72
+ profileCurrent: '显示当前 profile',
73
+ profileAdd: '从模版新建 profile',
74
+ profileImport: '从已有文件导入 profile',
75
+ completion: '输出 shell 补全脚本(zsh / bash),或 install 自动安装到当前 shell',
60
76
  },
61
77
  help: {
62
78
  helpCommandDescription: '显示指定命令的帮助信息',
@@ -104,15 +120,24 @@ const LANGUAGES = {
104
120
  deployUnit: '',
105
121
  errorTitle: 'Error',
106
122
  errorMissingConfig: 'Incomplete config, the following fields are missing or empty:',
107
- errorMissingConfigHint: 'Check command/qiniu.json and command/config.json, or run struggler-cli init.',
123
+ errorMissingConfigHint: 'Check Qiniu config (~/.struggler-cli/profiles, Windows: %USERPROFILE%\\.struggler-cli\\profiles) or command/qiniu.json, plus command/config.json; you can also run struggler-cli init first.',
124
+ profileListEmpty: 'No profiles yet. Run: struggler-cli profile add <name>',
125
+ profileListTitle: 'Profiles:',
126
+ profileListCurrentHint: '(* = active)',
127
+ profileUseDone: (name) => `Active profile: ${name}`,
128
+ profileNoCurrent: 'No active profile. Run: struggler-cli profile use <name>',
129
+ profileCurrentLabel: 'Active profile:',
130
+ profileAddDone: (name) => `Created profile: ${name}`,
131
+ profileImportDone: (name) => `Imported profile: ${name}`,
132
+ profileEditHint: 'Edit the file and fill accessKey, secretKey, Bucket, zone, domain.',
108
133
  dryRunLabel: 'dry-run',
109
134
  dryRunPlan: (action, n) => `[dry-run] ${action} plan (${n} files)`,
110
135
  manifestWritten: 'Manifest written:',
111
136
  },
112
137
  options: {
113
138
  version: 'Display the version number.',
114
- config: 'Specify the path to the upload configuration file.',
115
- configDir: 'Directory for config.json and upload-cache.json; use --config-dir ./command, or --config-dir alone for ./command.',
139
+ config: 'Qiniu config: profile name (e.g. prod) or file path (e.g. ./command/qiniu.json); when omitted, use ~/.struggler-cli/current first (Windows: %USERPROFILE%\\.struggler-cli\\current), then fallback to ./command/qiniu.json.',
140
+ configDir: 'Directory for config.json and upload-cache.json; defaults to ./command when omitted (legacy behavior).',
116
141
  dir: 'Specify the directory to upload.',
117
142
  dryRun: 'Preview actions without writing files or calling Qiniu APIs.',
118
143
  concurrency: 'Set upload concurrency.',
@@ -131,6 +156,13 @@ const LANGUAGES = {
131
156
  upload: 'Upload files under the specified directory to Qiniu Cloud.',
132
157
  refresh: 'Refresh Qiniu Cloud CDN files.',
133
158
  deploy: 'Run init, upload, and refresh in one command.',
159
+ profile: 'Manage multiple Qiniu configs (profiles).',
160
+ profileList: 'List profiles',
161
+ profileUse: 'Set active profile',
162
+ profileCurrent: 'Show active profile',
163
+ profileAdd: 'Create profile from template',
164
+ profileImport: 'Import profile from file',
165
+ completion: 'Output shell completion script (zsh / bash), or "install" to auto-install for current shell',
134
166
  },
135
167
  help: {
136
168
  helpCommandDescription: 'display help for command',
package/lib/profile.js ADDED
@@ -0,0 +1,148 @@
1
+ const fs = require('fs');
2
+ const os = require('os');
3
+ const path = require('path');
4
+ const { setSyncJsonData, getJsonData } = require('./files');
5
+
6
+ const PROFILE_DIR_NAME = '.struggler-cli';
7
+ const PROFILES_SUBDIR = 'profiles';
8
+ const CURRENT_FILE = 'current';
9
+ const PROFILE_NAME_RE = /^[a-zA-Z][a-zA-Z0-9_-]*$/;
10
+
11
+ function getStoreRoot() {
12
+ const customHome = process.env.STRUGGLER_CLI_HOME;
13
+ if (customHome && customHome.trim()) {
14
+ return path.join(path.resolve(customHome), PROFILE_DIR_NAME);
15
+ }
16
+ return path.join(os.homedir(), PROFILE_DIR_NAME);
17
+ }
18
+
19
+ function getProfilesDir() {
20
+ return path.join(getStoreRoot(), PROFILES_SUBDIR);
21
+ }
22
+
23
+ function getCurrentFile() {
24
+ return path.join(getStoreRoot(), CURRENT_FILE);
25
+ }
26
+
27
+ function assertProfileName(name) {
28
+ if (!PROFILE_NAME_RE.test(name)) {
29
+ throw new Error(
30
+ `Invalid profile name "${name}". Use letters, numbers, _ or -, and start with a letter.`
31
+ );
32
+ }
33
+ }
34
+
35
+ function getProfilePath(name) {
36
+ assertProfileName(name);
37
+ return path.join(getProfilesDir(), `${name}.json`);
38
+ }
39
+
40
+ function ensureProfilesDir() {
41
+ fs.mkdirSync(getProfilesDir(), { recursive: true });
42
+ }
43
+
44
+ function readCurrentProfile() {
45
+ const currentFile = getCurrentFile();
46
+ if (!fs.existsSync(currentFile)) {
47
+ return null;
48
+ }
49
+ const name = fs.readFileSync(currentFile, 'utf8').trim();
50
+ return name || null;
51
+ }
52
+
53
+ function writeCurrentProfile(name) {
54
+ assertProfileName(name);
55
+ const profilePath = getProfilePath(name);
56
+ if (!fs.existsSync(profilePath)) {
57
+ throw new Error(`Profile "${name}" does not exist. Run: struggler-cli profile list`);
58
+ }
59
+ fs.mkdirSync(getStoreRoot(), { recursive: true });
60
+ fs.writeFileSync(getCurrentFile(), `${name}\n`, 'utf8');
61
+ return profilePath;
62
+ }
63
+
64
+ function listProfiles() {
65
+ const dir = getProfilesDir();
66
+ if (!fs.existsSync(dir)) {
67
+ return [];
68
+ }
69
+ return fs.readdirSync(dir)
70
+ .filter((file) => file.endsWith('.json'))
71
+ .map((file) => path.basename(file, '.json'))
72
+ .sort();
73
+ }
74
+
75
+ function isFileConfigPath(configValue, cwd = process.cwd()) {
76
+ if (!configValue) {
77
+ return false;
78
+ }
79
+ if (configValue.includes('/') || configValue.includes('\\')) {
80
+ return true;
81
+ }
82
+ const resolved = path.resolve(cwd, configValue);
83
+ return fs.existsSync(resolved) && fs.statSync(resolved).isFile();
84
+ }
85
+
86
+ function resolveProfileConfigPath(options = {}, cwd = process.cwd()) {
87
+ const configValue = options.config;
88
+
89
+ if (!configValue) {
90
+ const current = readCurrentProfile();
91
+ if (current) {
92
+ return getProfilePath(current);
93
+ }
94
+ return null;
95
+ }
96
+
97
+ if (isFileConfigPath(configValue, cwd)) {
98
+ return path.resolve(cwd, configValue);
99
+ }
100
+
101
+ assertProfileName(configValue);
102
+ const profilePath = getProfilePath(configValue);
103
+ if (!fs.existsSync(profilePath)) {
104
+ throw new Error(
105
+ `Profile "${configValue}" not found at ${profilePath}. Run: struggler-cli profile list`
106
+ );
107
+ }
108
+ return profilePath;
109
+ }
110
+
111
+ function importProfile(name, fromPath, cwd = process.cwd()) {
112
+ assertProfileName(name);
113
+ const source = path.resolve(cwd, fromPath);
114
+ if (!fs.existsSync(source)) {
115
+ throw new Error(`Config file not found: ${source}`);
116
+ }
117
+ ensureProfilesDir();
118
+ const target = getProfilePath(name);
119
+ fs.copyFileSync(source, target);
120
+ return target;
121
+ }
122
+
123
+ function addProfile(name, templatePath) {
124
+ assertProfileName(name);
125
+ ensureProfilesDir();
126
+ const target = getProfilePath(name);
127
+ if (fs.existsSync(target)) {
128
+ throw new Error(`Profile "${name}" already exists.`);
129
+ }
130
+ setSyncJsonData(target, getJsonData(templatePath));
131
+ return target;
132
+ }
133
+
134
+ module.exports = {
135
+ PROFILE_DIR_NAME,
136
+ getStoreRoot,
137
+ getProfilesDir,
138
+ getProfilePath,
139
+ getCurrentFile,
140
+ assertProfileName,
141
+ readCurrentProfile,
142
+ writeCurrentProfile,
143
+ listProfiles,
144
+ isFileConfigPath,
145
+ resolveProfileConfigPath,
146
+ importProfile,
147
+ addProfile,
148
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@struggler/cli",
3
- "version": "1.0.12",
3
+ "version": "1.0.14",
4
4
  "description": "CLI to Upload vite packaged files to Qiniu Cloud OSS.",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -16,7 +16,7 @@ test('config helpers resolve qiniu and derived config paths together', () => {
16
16
  );
17
17
  assert.equal(
18
18
  getConfig(options),
19
- `${process.cwd()}/test/command/config.json`
19
+ `${process.cwd()}/command/config.json`
20
20
  );
21
21
  assert.equal(
22
22
  getDir(options),
@@ -48,6 +48,17 @@ test('config-dir flag without path defaults to ./command', () => {
48
48
  );
49
49
  });
50
50
 
51
+ test('custom -c without config-dir keeps meta files in ./command', () => {
52
+ const options = {
53
+ config: './def/qiniu.json',
54
+ };
55
+
56
+ assert.equal(
57
+ getConfig(options),
58
+ `${process.cwd()}/command/config.json`
59
+ );
60
+ });
61
+
51
62
  test('deploy helpers normalize concurrency and build remote keys', () => {
52
63
  assert.equal(normalizeConcurrency('0'), 5);
53
64
  assert.equal(normalizeConcurrency('3'), 3);
@@ -0,0 +1,129 @@
1
+ const test = require('node:test');
2
+ const assert = require('node:assert/strict');
3
+ const fs = require('fs');
4
+ const os = require('os');
5
+ const path = require('path');
6
+ const {
7
+ addProfile,
8
+ importProfile,
9
+ writeCurrentProfile,
10
+ readCurrentProfile,
11
+ listProfiles,
12
+ resolveProfileConfigPath,
13
+ isFileConfigPath,
14
+ } = require('../lib/profile');
15
+ const { getQiniuConfig } = require('../lib/config');
16
+
17
+ function makeWorkspace() {
18
+ const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'struggler-cli-profile-'));
19
+ return fs.realpathSync(dir);
20
+ }
21
+
22
+ function withTempProfileHome(workspace, run) {
23
+ const prevHome = process.env.STRUGGLER_CLI_HOME;
24
+ process.env.STRUGGLER_CLI_HOME = path.join(workspace, '.home');
25
+ try {
26
+ return run();
27
+ } finally {
28
+ if (prevHome === undefined) {
29
+ delete process.env.STRUGGLER_CLI_HOME;
30
+ } else {
31
+ process.env.STRUGGLER_CLI_HOME = prevHome;
32
+ }
33
+ }
34
+ }
35
+
36
+ function samePath(a, b) {
37
+ const norm = (p) => {
38
+ try {
39
+ return fs.realpathSync(p);
40
+ } catch {
41
+ const dir = path.dirname(p);
42
+ const base = path.basename(p);
43
+ try {
44
+ return path.join(fs.realpathSync(dir), base);
45
+ } catch {
46
+ return path.resolve(p);
47
+ }
48
+ }
49
+ };
50
+ return norm(a) === norm(b);
51
+ }
52
+
53
+ test('profile paths work on any platform', () => {
54
+ const workspace = makeWorkspace();
55
+ const prev = process.cwd();
56
+ process.chdir(workspace);
57
+ try {
58
+ withTempProfileHome(workspace, () => {
59
+ addProfile('prod', path.resolve(__dirname, '../def/qiniu.json'));
60
+ const profileFile = path.join(workspace, '.home', '.struggler-cli', 'profiles', 'prod.json');
61
+ assert.equal(fs.existsSync(profileFile), true);
62
+ assert.match(profileFile, /profiles[\\/]prod\.json$/);
63
+ });
64
+ } finally {
65
+ process.chdir(prev);
66
+ }
67
+ });
68
+
69
+ test('resolve current profile when -c is omitted', () => {
70
+ const workspace = makeWorkspace();
71
+ const prev = process.cwd();
72
+ process.chdir(workspace);
73
+ try {
74
+ withTempProfileHome(workspace, () => {
75
+ addProfile('staging', path.resolve(__dirname, '../def/qiniu.json'));
76
+ writeCurrentProfile('staging');
77
+ const resolved = resolveProfileConfigPath({});
78
+ const expected = path.join(workspace, '.home', '.struggler-cli', 'profiles', 'staging.json');
79
+ assert.equal(samePath(resolved, expected), true);
80
+ });
81
+ } finally {
82
+ process.chdir(prev);
83
+ }
84
+ });
85
+
86
+ test('-c prod resolves profile name; -c with path resolves file', () => {
87
+ const workspace = makeWorkspace();
88
+ const prev = process.cwd();
89
+ process.chdir(workspace);
90
+ try {
91
+ withTempProfileHome(workspace, () => {
92
+ importProfile('prod', path.resolve(__dirname, '../def/qiniu.json'));
93
+ const byName = resolveProfileConfigPath({ config: 'prod' });
94
+ assert.equal(
95
+ samePath(byName, path.join(workspace, '.home', '.struggler-cli', 'profiles', 'prod.json')),
96
+ true
97
+ );
98
+
99
+ const legacy = path.join(workspace, 'command', 'qiniu.json');
100
+ fs.mkdirSync(path.dirname(legacy), { recursive: true });
101
+ fs.copyFileSync(path.resolve(__dirname, '../def/qiniu.json'), legacy);
102
+ assert.equal(isFileConfigPath('./command/qiniu.json'), true);
103
+ assert.equal(
104
+ samePath(resolveProfileConfigPath({ config: './command/qiniu.json' }), legacy),
105
+ true
106
+ );
107
+ });
108
+ } finally {
109
+ process.chdir(prev);
110
+ }
111
+ });
112
+
113
+ test('getQiniuConfig falls back to command/qiniu.json without profile', () => {
114
+ const workspace = makeWorkspace();
115
+ const prev = process.cwd();
116
+ process.chdir(workspace);
117
+ try {
118
+ withTempProfileHome(workspace, () => {
119
+ assert.equal(listProfiles().length, 0);
120
+ assert.equal(readCurrentProfile(), null);
121
+ assert.equal(
122
+ samePath(getQiniuConfig({}), path.join(workspace, 'command', 'qiniu.json')),
123
+ true
124
+ );
125
+ });
126
+ } finally {
127
+ process.chdir(prev);
128
+ }
129
+ });