@initx-plugin/env 0.1.3 → 0.1.5
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/README.md +48 -2
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.mjs +20 -22
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -54,6 +54,14 @@ or the full namespace, including multiple subgroups. `apiBaseUrl` means the API
|
|
|
54
54
|
`stores`; `"path": ""` explicitly selects repository root. For a GitHub vault, use `"type": "github"`
|
|
55
55
|
and `"repo": "owner/vault"`. Examples are in [examples](examples/).
|
|
56
56
|
|
|
57
|
+
The vault project/repository must already exist, but it can be empty. The first `env push <store>`
|
|
58
|
+
or `env set <store> K=V` creates the configured branch (default `main`) and the store file in one
|
|
59
|
+
commit, on both GitHub and GitLab. No README or manual branch creation is required. `list` and
|
|
60
|
+
`env-config status` report zero stores for a confirmed empty vault without writing anything.
|
|
61
|
+
An initialized vault with a missing configured branch still fails. The token must have permission
|
|
62
|
+
to create the first commit; GitLab instance rules for the initial/default branch may require a
|
|
63
|
+
Maintainer even when a Developer can write to other branches.
|
|
64
|
+
|
|
57
65
|
All storage commands and `env-config status` accept `--config <file>`. Relative paths resolve
|
|
58
66
|
against the caller's cwd. Without the flag, only cwd's `.initx-env.json` is checked; parents are
|
|
59
67
|
never searched. An explicitly missing file, invalid JSON, unknown schema or unknown field fails
|
|
@@ -98,6 +106,14 @@ Legacy flat config remains GitHub config; its token is accepted only on the defa
|
|
|
98
106
|
| `initx env run <store> -- <command> [...args]` | Inject a store directly into one child process and return its exit status |
|
|
99
107
|
| `initx env apply <store> --repo o/r` | Write a store into a repo's GitHub Actions secrets |
|
|
100
108
|
|
|
109
|
+
Command failures print only the error message through `logger.error` and exit with code 1, without a stack
|
|
110
|
+
trace or minified source dump. A failed connectivity check in `env-config status` also exits
|
|
111
|
+
with code 1. `env run` continues to forward the child's own exit status, signals and output.
|
|
112
|
+
Terminal messages use the existing utils color builders: errors are red, successful changes
|
|
113
|
+
green, warnings/modified keys yellow, and configuration values/store names blue. Message-body
|
|
114
|
+
colors are disabled when redirected or when `NO_COLOR`/`FORCE_COLOR=0` is set; `FORCE_COLOR=1`
|
|
115
|
+
enables them explicitly. JSON, exported variable values and child output are not colorized.
|
|
116
|
+
|
|
101
117
|
```bash
|
|
102
118
|
# write / update a store
|
|
103
119
|
initx env set mail-peon-publish EXTENSION_ID=abc CHROME_REFRESH_TOKEN=1//0g...
|
|
@@ -160,7 +176,7 @@ tests exercise the real direct-child termination and listener cleanup by trigger
|
|
|
160
176
|
signal event over IPC; interactive console Ctrl+C still needs a manual console check.
|
|
161
177
|
|
|
162
178
|
`run` writes no `.env`, `$GITHUB_ENV`, dotenv artifact or secret cache, and emits no GitHub masking
|
|
163
|
-
protocol. Its own diagnostics
|
|
179
|
+
protocol. Its own diagnostics use the utils logger without values, tokens, complete response bodies or
|
|
164
180
|
command argument lists. **Child output is inherited without masking**: a script can still print
|
|
165
181
|
secrets. The parent environment, including inherited GitHub tokens, is retained. This is process
|
|
166
182
|
injection for trusted scripts and stores, not an isolation sandbox or encrypted secret manager.
|
|
@@ -273,7 +289,8 @@ hints; concurrent remote-only changes can be replaced. Pull/review before force
|
|
|
273
289
|
|
|
274
290
|
GitLab uses Files GET/POST/PUT, a conditional Commits delete action, and Tree offset pagination
|
|
275
291
|
(`per_page=100`). It supports full subgroups and numeric IDs, and does not require keyset pagination, newer Tree metadata or job-token
|
|
276
|
-
features. Missing paths are empty only after validating repository/ref access
|
|
292
|
+
features. Missing paths are empty only after validating repository/ref access or confirming an
|
|
293
|
+
empty repository (GitLab project `empty_repo`, GitHub's explicit empty-repository response). Tree accepts both
|
|
277
294
|
legacy 200 empty arrays and newer 404 responses. Directory listing is not recursive. Errors expose
|
|
278
295
|
only a category, safe hint and HTTP status, never upstream response text or request URLs.
|
|
279
296
|
|
|
@@ -346,6 +363,35 @@ pnpm lint
|
|
|
346
363
|
pnpm typecheck
|
|
347
364
|
```
|
|
348
365
|
|
|
366
|
+
To debug a GitLab request with an existing local initx installation, run `pnpm stub` and
|
|
367
|
+
`ix plugin add .` in this plugin directory. This links the plugin, without installing the CLI
|
|
368
|
+
in the project. Leave stub mode enabled while diagnosing; source changes then take effect directly.
|
|
369
|
+
The switch comes from initx's context (`ctx.cliOptions.debug` or `ctx.cliOptions.d`). In the
|
|
370
|
+
business project, repeat your original command with initx's existing global debug option:
|
|
371
|
+
|
|
372
|
+
```powershell
|
|
373
|
+
ix env push my-store --token <gitlab-token> --debug
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
Plugin debug messages use `logger.debug` and retain only empty-vault initialization, write results,
|
|
377
|
+
failed requests, and permission summaries. Routine read requests, expected missing-file/ref responses,
|
|
378
|
+
and successful project lookups are silent. No URLs, tokens, request/response
|
|
379
|
+
bodies, commit messages or variable values are logged. For a branch-write permission failure,
|
|
380
|
+
debug mode makes additional read-only project/branch/protection requests; failed diagnostics
|
|
381
|
+
preserve the original error and never retry the write. `accessLevel: 30` means Developer and
|
|
382
|
+
`40` means Maintainer. `allowedPushLevels` lists numeric levels from the exact-name protection
|
|
383
|
+
rule, when readable. A missing/unreadable rule does not rule out wildcard or initial default-branch
|
|
384
|
+
policies. For an empty project, creating the first branch is still subject to GitLab permissions.
|
|
385
|
+
In GitLab CE 15.9.4, default-branch protection can apply before the first branch exists:
|
|
386
|
+
`ProtectedBranch.protected?` treats an empty project with default-branch protection as protected.
|
|
387
|
+
If a Developer token is denied on the first commit, use a Maintainer token with `api`, or have
|
|
388
|
+
a project maintainer configure the intended initial-branch policy. A failed protection lookup
|
|
389
|
+
(`403`) means the token cannot inspect the rule; it does not mean there is no protection.
|
|
390
|
+
See the versioned [ProtectedBranch model](https://github.com/gitlabhq/gitlabhq/blob/v15.9.4/app/models/protected_branch.rb).
|
|
391
|
+
All human-readable messages use the existing utils logger (which currently writes to stdout).
|
|
392
|
+
Raw JSON, shell exports and GitHub Actions protocol lines remain data output without logger prefixes;
|
|
393
|
+
the child's own stdout/stderr and exit status remain unchanged.
|
|
394
|
+
|
|
349
395
|
`pnpm test` runs unit tests and isolated child-process fixtures with fake GitHub/GitLab responses and
|
|
350
396
|
temporary directories. Tests use the existing core matcher and direct command handlers, without
|
|
351
397
|
installing or importing the initx CLI. Child-process tests check execution markers and nonzero
|
package/dist/index.d.mts
CHANGED
|
@@ -27,6 +27,7 @@ declare class EnvPlugin extends InitxPlugin<EnvPluginStore> {
|
|
|
27
27
|
defaultStore: EnvPluginStore;
|
|
28
28
|
rules: InitxMatcherRules;
|
|
29
29
|
handle(ctx: InitxContext<EnvPluginStore>, ...args: (string | undefined)[]): Promise<void>;
|
|
30
|
+
private dispatch;
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
export { EnvPlugin as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -27,6 +27,7 @@ declare class EnvPlugin extends InitxPlugin<EnvPluginStore> {
|
|
|
27
27
|
defaultStore: EnvPluginStore;
|
|
28
28
|
rules: InitxMatcherRules;
|
|
29
29
|
handle(ctx: InitxContext<EnvPluginStore>, ...args: (string | undefined)[]): Promise<void>;
|
|
30
|
+
private dispatch;
|
|
30
31
|
}
|
|
31
32
|
|
|
32
33
|
export { EnvPlugin as default };
|
package/dist/index.mjs
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import{InitxPlugin as Nu}from"@initx-plugin/core";import{readFile as su,copyFile as Iu,writeFile as au,stat as Su,readdir as Uu}from"node:fs/promises";import f from"node:process";import{logger as F,inquirer as b}from"@initx-plugin/utils";import{readFileSync as Ou,appendFileSync as ju}from"node:fs";import{resolve as Gu}from"node:path";import{z as A}from"zod";import{Buffer as I}from"node:buffer";import{randomBytes as cu}from"node:crypto";import Fu from"libsodium-wrappers";import{constants as Pu}from"node:os";import Ru from"cross-spawn";function M(u){return u.replace(/-([a-z])/g,(t,e)=>e.toUpperCase())}function z(u,t){return u[t]??u[M(t)]}function Vu(u){if(typeof u=="string")return u===""?void 0:u;if(typeof u=="number")return String(u);if(Array.isArray(u))return u.length?u.map(String).join(","):void 0}function Lu(u={},t=[]){const e=new Set(t);return{raw:u,string:n=>Vu(z(u,n)),boolean:n=>{const r=z(u,n);return typeof r=="boolean"?r:e.has(`--${n}`)||e.has(`--${M(n)}`)},has:n=>z(u,n)!==void 0?!0:e.has(`--${n}`)||e.has(`--${M(n)}`)}}function P(u){if(!u.has("config"))return;const t=u.raw.config;if(typeof t!="string"||!t.trim())throw new Error("--config \u9700\u8981\u5355\u4E2A\u6587\u4EF6\u8DEF\u5F84");return t}const Hu={authentication:"\u8BA4\u8BC1\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5\u5F53\u524D\u540E\u7AEF\u7684 token",permission:"\u6CA1\u6709\u6743\u9650\uFF0C\u8BF7\u68C0\u67E5 token \u6388\u6743\u4E0E\u5206\u652F\u4FDD\u62A4","not-found":"\u4ED3\u5E93\u6216 ref \u4E0D\u5B58\u5728\uFF0C\u6216\u5F53\u524D token \u65E0\u6743\u8BBF\u95EE",conflict:"\u8FDC\u7AEF\u7248\u672C\u5DF2\u53D8\u5316\uFF0C\u8BF7\u91CD\u65B0\u8BFB\u53D6\uFF1Bpush \u53EF\u7528 --force \u91CD\u8BFB\u540E\u91CD\u8BD5\u4E00\u6B21",validation:"\u8BF7\u6C42\u6216\u54CD\u5E94\u4E0D\u5408\u6CD5\uFF0C\u8BF7\u68C0\u67E5\u8DEF\u5F84\u3001ref \u4E0E\u914D\u7F6E","rate-limit":"API \u8BF7\u6C42\u88AB\u9650\u6D41\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5",network:"\u7F51\u7EDC\u8BF7\u6C42\u5931\u8D25\u6216\u8D85\u65F6\uFF1B\u5199\u5165\u7ED3\u679C\u53EF\u80FD\u672A\u77E5\uFF0C\u8BF7\u5148\u91CD\u8BFB\u786E\u8BA4",unsupported:"\u5B58\u50A8\u540E\u7AEF\u4E0D\u652F\u6301\u6B64\u64CD\u4F5C"};class d extends Error{constructor(t,e){super(`${Hu[t]}${e?`\uFF08HTTP ${e}\uFF09`:""}`),this.code=t,this.status=e,this.name="StorageError"}}function Ku(u){return u instanceof d&&u.code==="conflict"}function q(u){const t=u.status,e=t===429||t===403&&(u.headers.get("x-ratelimit-remaining")==="0"||u.headers.has("retry-after"));return new d(e?"rate-limit":t===401?"authentication":t===403?"permission":t===404?"not-found":t>=500?"network":"validation",t)}const $={github:"https://api.github.com",gitlab:"https://gitlab.com/api/v4"};function D(u,t=!1){if(!(u===""&&t)){if(u.split("/").some(e=>e==="."||e===".."))throw Object.assign(new d("validation"),{message:"\u8DEF\u5F84\u4E0D\u80FD\u5305\u542B . \u6216 .."});if(!u||/[\\\x00-\x1F\x7F]/.test(u)||u.startsWith("/")||/^[A-Z]:/i.test(u)||u.split("/").some(e=>!e||e==="."||e===".."))throw new d("validation")}}function k(u){try{const t=new URL(u);if(!["https:","http:"].includes(t.protocol)||t.username||t.password||t.search||t.hash)throw new Error("Invalid API base URL");return t.href.replace(/\/+$/,"")}catch{throw new d("validation")}}function x(u,t){if(D(t),/\s/.test(t)||(u==="github"?t.split("/").length!==2:!/^\d+$/.test(t)&&t.split("/").length<2))throw new Error(u==="github"?"\u4ED3\u5E93\u8981\u5199\u6210 owner/repo \u5F62\u5F0F":"GitLab \u4ED3\u5E93\u5FC5\u987B\u662F\u6570\u5B57\u9879\u76EE ID \u6216 group/subgroup/project")}const Xu=A.object({schema:A.literal(1),storage:A.object({type:A.enum(["github","gitlab"]),apiBaseUrl:A.string().min(1).optional(),repo:A.string().min(1),branch:A.string().min(1).optional(),path:A.string().optional()}).strict()}).strict();class R extends Error{constructor(t){super(t),this.name="ProjectConfigError"}}function Ju(u,t=f.cwd()){const e=Gu(t,u||".initx-env.json");let n;try{n=Ou(e,"utf8")}catch(o){if(!u&&o.code==="ENOENT")return;throw new R("\u65E0\u6CD5\u8BFB\u53D6\u9879\u76EE\u914D\u7F6E\u6587\u4EF6\uFF0C\u8BF7\u68C0\u67E5 --config \u8DEF\u5F84\u4E0E\u6587\u4EF6\u6743\u9650")}let r;try{r=Xu.parse(JSON.parse(n))}catch{throw new R("\u9879\u76EE\u914D\u7F6E\u5FC5\u987B\u662F schema 1 \u7684\u5408\u6CD5 JSON\uFF0C\u4E14\u53EA\u80FD\u5305\u542B\u975E\u654F\u611F storage \u5B9A\u4F4D\u5B57\u6BB5\uFF08\u7981\u6B62 token / vars\uFF09")}try{const o={...r.storage,apiBaseUrl:k(r.storage.apiBaseUrl||$[r.storage.type])};return x(o.type,o.repo),o.path!==void 0&&D(o.path,!0),{file:e,storage:o}}catch{throw new R("\u9879\u76EE storage \u7684 API \u5730\u5740\u3001\u4ED3\u5E93\u6216\u8DEF\u5F84\u4E0D\u5408\u6CD5")}}const V="main",L="stores";function H(u,t){return`${u}:${k(t)}`}function S(u){const t=u.storage?.type||"github";if(t!=="github"&&t!=="gitlab")throw new Error("\u672C\u5730 storage.type \u5FC5\u987B\u662F github \u6216 gitlab");return{type:t,apiBaseUrl:k(u.storage?.apiBaseUrl||$[t])}}function Yu(u,t){const e=t.env??f.env,n=Ju(t.config,t.cwd),r=S(u),o=t.type||e.INITX_ENV_TYPE||n?.storage.type||r.type;if(o!=="github"&&o!=="gitlab")throw new Error("storage.type \u5FC5\u987B\u662F github \u6216 gitlab");const i=o,s=n?.storage.type===i,a=t.apiBaseUrl||e.INITX_ENV_API_BASE_URL||(s?n.storage.apiBaseUrl:void 0)||(r.type===i?r.apiBaseUrl:$[i]),E=k(a),c=r.type===i&&r.apiBaseUrl===E,l=s&&n.storage.apiBaseUrl===E,h=c?u.storage||u:{},p=l?n.storage:void 0,w={type:t.type?"override":e.INITX_ENV_TYPE?"INITX_ENV_TYPE":n?"project":"local/default",apiBaseUrl:t.apiBaseUrl?"override":e.INITX_ENV_API_BASE_URL?"INITX_ENV_API_BASE_URL":l?"project":c?"local/default":"default",repo:"",branch:"",path:""};function B(y,g){const _=`INITX_ENV_${y.toUpperCase()}`,m=[[t[y]||void 0,"override"],[e[_]||void 0,_],[p?.[y],"project"],[h[y]||void 0,"local"],[g,"default"]].find(([_u])=>_u!==void 0);return w[y]=m[1],m[0]}return{type:i,apiBaseUrl:E,repo:B("repo",""),branch:B("branch",V),path:B("path",L),sources:w,configSource:n?.file||"local/env/default"}}function Wu(u,t,e){const n=t.env??f.env;if(t.token)return{token:t.token,source:"flag"};if(n.INITX_ENV_TOKEN)return{token:n.INITX_ENV_TOKEN,source:"INITX_ENV_TOKEN"};const r=e.type==="github"&&e.apiBaseUrl===$.github;if(r){for(const i of["GH_TOKEN","GITHUB_TOKEN"])if(n[i])return{token:n[i],source:i}}const o=u.credentials?.[H(e.type,e.apiBaseUrl)];return o?{token:o,source:"config"}:r&&u.token?{token:u.token,source:"config"}:{token:"",source:"none"}}function K(u,t={}){const e=Yu(u,t);if(!e.repo)throw new Error("\u8FD8\u6CA1\u914D vault \u4ED3\u5E93\uFF0C\u8BF7\u914D\u7F6E .initx-env.json \u6216 env-config set repo <owner/repo>");x(e.type,e.repo),D(e.path,!0);const{token:n,source:r}=Wu(u,t,e);if(!n)throw new Error("\u6CA1\u6709\u53EF\u7528\u7684\u5F53\u524D\u540E\u7AEF token\uFF0C\u8BF7\u8BBE\u7F6E INITX_ENV_TOKEN\u3001--token \u6216\u7ED1\u5B9A\u5F53\u524D API \u5730\u5740\u7684\u672C\u5730\u51ED\u636E");return{...e,token:n,tokenSource:r}}function Mu(u){return u?"***":"(\u672A\u8BBE\u7F6E)"}function Z(u){return Mu(u)}async function C(u,t={}){try{return await fetch(u,{...t,redirect:"error",signal:AbortSignal.timeout(3e4)})}catch{throw new d("network")}}async function N(u){try{return await u.json()}catch(t){throw new d(t instanceof SyntaxError?"validation":"network")}}function v(u){if(!u.ok)throw q(u)}function lu(u){return{Authorization:`Bearer ${u}`,Accept:"application/vnd.github+json","X-GitHub-Api-Version":"2022-11-28","User-Agent":"initx-plugin-env"}}class zu{constructor(t,e){this.location=t,x("github",t.repo),this.base=`${k(t.apiBaseUrl)}/repos/${t.repo.split("/").map(encodeURIComponent).join("/")}`,this.headers=lu(e)}capabilities={read:!0,list:!0,write:!0,delete:!0,conditionalWrite:!0};base;headers;url(t){D(t,!0);const e=t?`/${t.split("/").map(encodeURIComponent).join("/")}`:"";return`${this.base}/contents${e}?${new URLSearchParams({ref:this.location.branch})}`}async checkTarget(){const t=await C(`${this.base}/commits/${encodeURIComponent(this.location.branch)}`,{headers:this.headers});v(t)}async read(t){D(t);const e=await C(this.url(t),{headers:this.headers});if(e.status===404)return await this.checkTarget(),null;v(e);const n=await N(e);if(!n||n.type!=="file"||typeof n.sha!="string"||!n.sha||typeof n.content!="string"||n.encoding!=="base64")throw new d("validation");return{content:I.from(n.content,"base64").toString("utf8"),version:n.sha}}async list(t){const e=await C(this.url(t),{headers:this.headers});if(e.status===404)return await this.checkTarget(),[];v(e);const n=await N(e);if(!Array.isArray(n)||n.some(r=>!r||typeof r.name!="string"||typeof r.path!="string"||typeof r.type!="string"))throw new d("validation");return n.map(r=>({name:r.name,path:r.path,kind:r.type==="file"?"file":r.type==="dir"?"directory":"other"}))}async write(t,e,n){D(t);const r=n.condition;if(r.kind!=="create"&&(r.kind!=="update"||!r.expectedVersion))throw new d("validation");const o=await C(this.url(t),{method:"PUT",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({branch:this.location.branch,message:n.message||"env: write store",content:I.from(e).toString("base64"),...r.kind==="update"?{sha:r.expectedVersion}:{}})});if([404,409,422].includes(o.status)){const i=await this.read(t);if(r.kind==="create"?i!==null:i?.version!==r.expectedVersion)throw new d("conflict",o.status)}if(!o.ok)throw q(o)}async remove(t,e){if(D(t),!e.expectedVersion)throw new d("validation");const n=await C(this.url(t),{method:"DELETE",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({branch:this.location.branch,message:e.message||"env: remove store",sha:e.expectedVersion})});if([404,409,422].includes(n.status)&&(await this.read(t))?.version!==e.expectedVersion)throw new d("conflict",n.status);v(n)}}async function Eu(u){if(u.status!==400)return;let t;try{t=await N(u)}catch(e){if(e instanceof d&&e.code==="network")throw e}if(t?.message==="You are not allowed to push into this branch")throw new d("permission",400)}class qu{constructor(t,e){this.location=t,x("gitlab",t.repo),this.base=`${k(t.apiBaseUrl)}/projects/${encodeURIComponent(t.repo)}/repository`,this.headers={"PRIVATE-TOKEN":e,"Content-Type":"application/json"}}capabilities={read:!0,list:!0,write:!0,delete:!0,conditionalWrite:!0};base;headers;fileUrl(t){return D(t),`${this.base}/files/${encodeURIComponent(t)}`}async checkTarget(){const t=await C(`${this.base}/commits/${encodeURIComponent(this.location.branch)}`,{headers:this.headers});v(t)}async read(t){const e=await C(`${this.fileUrl(t)}?${new URLSearchParams({ref:this.location.branch})}`,{headers:this.headers});if(e.status===404)return await this.checkTarget(),null;v(e);const n=await N(e);if(!n||typeof n.content!="string"||n.encoding!=="base64"||typeof n.last_commit_id!="string"||!n.last_commit_id)throw new d("validation");return{content:I.from(n.content,"base64").toString("utf8"),version:n.last_commit_id}}async list(t){D(t,!0);const e=[];await this.checkTarget();for(let n=1;;n++){const r=new URLSearchParams({ref:this.location.branch,per_page:"100",page:String(n),...t?{path:t}:{}}),o=await C(`${this.base}/tree?${r}`,{headers:this.headers});if(o.status===404)return await this.checkTarget(),e;v(o);const i=await N(o);if(!Array.isArray(i)||i.some(a=>!a||typeof a.name!="string"||typeof a.path!="string"||typeof a.type!="string"))throw new d("validation");e.push(...i.map(a=>({name:a.name,path:a.path,kind:a.type==="blob"?"file":a.type==="tree"?"directory":"other"})));const s=o.headers.get("x-next-page");if(!i.length||s===""||s===null&&i.length<100)return e;if(s!==null&&s!==String(n+1))throw new d("validation")}}async write(t,e,n){const r=n.condition;if(r.kind!=="create"&&(r.kind!=="update"||!r.expectedVersion))throw new d("validation");const o=await C(this.fileUrl(t),{method:r.kind==="create"?"POST":"PUT",headers:this.headers,body:JSON.stringify({branch:this.location.branch,commit_message:n.message||"env: write store",content:I.from(e).toString("base64"),encoding:"base64",...r.kind==="update"?{last_commit_id:r.expectedVersion}:{}})});if(await Eu(o),[400,404,409,422].includes(o.status)){const i=await this.read(t);if(r.kind==="create"?i!==null:i?.version!==r.expectedVersion)throw new d("conflict",o.status);if(r.kind==="update"&&o.status===400&&i?.content===e)return}if(!o.ok)throw q(o)}async remove(t,e){if(D(t),!e.expectedVersion)throw new d("validation");const n=await C(`${this.base}/commits`,{method:"POST",headers:this.headers,body:JSON.stringify({branch:this.location.branch,commit_message:e.message||"env: remove store",actions:[{action:"delete",file_path:t,last_commit_id:e.expectedVersion}]})});if(await Eu(n),[400,404,409,422].includes(n.status)&&(await this.read(t))?.version!==e.expectedVersion)throw new d("conflict",n.status);v(n)}}const Zu={github:zu,gitlab:qu};function X(u){const t=Zu[u.type];if(!t)throw new d("unsupported");return new t(u,u.token)}function J(u,t){if(!u.capabilities[t])throw new d("unsupported")}const Q=1,Qu=A.object({repo:A.string().regex(/^[^/\s]+\/[^/\s]+$/,"\u5FC5\u987B\u662F owner/repo \u5F62\u5F0F"),env:A.string().nullish()}),ut=A.object({schema:A.number().int().positive().default(Q),vars:A.record(A.string(),A.string()).default({}),targets:A.object({envFiles:A.array(A.string().min(1)).optional(),secrets:A.array(Qu).optional()}).optional()});function pu(){return{schema:Q,vars:{}}}function T(u){const t=u.trim().replace(/\.json$/i,"");if(!t)throw new Error("store \u540D\u4E0D\u80FD\u4E3A\u7A7A");if(t.includes("/")||t.includes("\\"))throw new Error(`store \u540D\u4E0D\u80FD\u5305\u542B\u8DEF\u5F84\u5206\u9694\u7B26\uFF1A"${u}"`);if(t==="."||t==="..")throw new Error(`store \u540D\u4E0D\u80FD\u662F "${t}"`);if(/[\u0000-\u001F\u007F]/.test(t))throw new Error("store \u540D\u4E0D\u80FD\u5305\u542B\u63A7\u5236\u5B57\u7B26");if(t.length>100)throw new Error("store \u540D\u592A\u957F\uFF08\u4E0A\u9650 100 \u5B57\u7B26\uFF09");return t}function tt(u){return/^[A-Z_]\w*$/i.test(u)?u.toUpperCase().startsWith("GITHUB_")?"GITHUB_ \u524D\u7F00\u662F GitHub \u4FDD\u7559\u7684":null:"\u53EA\u5141\u8BB8\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\uFF0C\u4E14\u4E0D\u80FD\u4EE5\u6570\u5B57\u5F00\u5934"}function et(u){return/^[A-Z_][\w.-]*$/i.test(u)}function fu(u,t){const e=T(t),n=(u||"").trim().replace(/^\/+|\/+$/g,""),r=n?`${n}/${e}.json`:`${e}.json`;return D(r),r}function nt(u,t){let e;try{e=JSON.parse(u)}catch{throw new Error(`store "${t}" \u4E0D\u662F\u5408\u6CD5 JSON`)}const n=ut.safeParse(e);if(!n.success)throw new Error(`store "${t}" \u7ED3\u6784\u4E0D\u5408\u6CD5\uFF1A${n.error.issues.map(r=>`${r.path.join(".")||"(root)"} ${r.message}`).join("; ")}`);return n.data}function rt(u){return`${JSON.stringify({...u,schema:u.schema??Q},null,2)}
|
|
2
|
-
`}async function
|
|
3
|
-
`:e==="r"?"\r":e==="t"?" ":e)}function
|
|
4
|
-
`)){const n=/[ \t]#/.exec(e);if(n!==null)return e.slice(0,n.index).replace(/[ \t]+$/,"")}return t.replace(/[ \t]+$/,"")}function
|
|
5
|
-
`)&&t.pop(),t}function
|
|
6
|
-
${u[s]}`,
|
|
1
|
+
import{InitxPlugin as ju}from"@initx-plugin/core";import{logger as c,useColors as Su,inquirer as _}from"@initx-plugin/utils";import{readFile as cu,copyFile as Uu,writeFile as lu,stat as Ou,readdir as Ru}from"node:fs/promises";import h from"node:process";import{readFileSync as Pu,appendFileSync as Gu}from"node:fs";import{resolve as Lu}from"node:path";import{z as C}from"zod";import{Buffer as S}from"node:buffer";import{randomBytes as Fu}from"node:crypto";import pu from"libsodium-wrappers";import{constants as Vu}from"node:os";import Hu from"cross-spawn";function z(u){return u.replace(/-([a-z])/g,(t,e)=>e.toUpperCase())}function Z(u,t){return u[t]??u[z(t)]}function Ku(u){if(typeof u=="string")return u===""?void 0:u;if(typeof u=="number")return String(u);if(Array.isArray(u))return u.length?u.map(String).join(","):void 0}function Xu(u={},t=[]){const e=new Set(t);return{raw:u,string:n=>Ku(Z(u,n)),boolean:n=>{const o=Z(u,n);return typeof o=="boolean"?o:e.has(`--${n}`)||e.has(`--${z(n)}`)},has:n=>Z(u,n)!==void 0?!0:e.has(`--${n}`)||e.has(`--${z(n)}`)}}function L(u){if(!u.has("config"))return;const t=u.raw.config;if(typeof t!="string"||!t.trim())throw new Error("--config \u9700\u8981\u5355\u4E2A\u6587\u4EF6\u8DEF\u5F84");return t}const Ju={authentication:"\u8BA4\u8BC1\u5931\u8D25\uFF0C\u8BF7\u68C0\u67E5\u5F53\u524D\u540E\u7AEF\u7684 token",permission:"\u6CA1\u6709\u6743\u9650\uFF0C\u8BF7\u68C0\u67E5 token \u6388\u6743\u4E0E\u5206\u652F\u4FDD\u62A4","not-found":"\u4ED3\u5E93\u6216 ref \u4E0D\u5B58\u5728\uFF0C\u6216\u5F53\u524D token \u65E0\u6743\u8BBF\u95EE",conflict:"\u8FDC\u7AEF\u7248\u672C\u5DF2\u53D8\u5316\uFF0C\u8BF7\u91CD\u65B0\u8BFB\u53D6\uFF1Bpush \u53EF\u7528 --force \u91CD\u8BFB\u540E\u91CD\u8BD5\u4E00\u6B21",validation:"\u8BF7\u6C42\u6216\u54CD\u5E94\u4E0D\u5408\u6CD5\uFF0C\u8BF7\u68C0\u67E5\u8DEF\u5F84\u3001ref \u4E0E\u914D\u7F6E","rate-limit":"API \u8BF7\u6C42\u88AB\u9650\u6D41\uFF0C\u8BF7\u7A0D\u540E\u91CD\u8BD5",network:"\u7F51\u7EDC\u8BF7\u6C42\u5931\u8D25\u6216\u8D85\u65F6\uFF1B\u5199\u5165\u7ED3\u679C\u53EF\u80FD\u672A\u77E5\uFF0C\u8BF7\u5148\u91CD\u8BFB\u786E\u8BA4",unsupported:"\u5B58\u50A8\u540E\u7AEF\u4E0D\u652F\u6301\u6B64\u64CD\u4F5C"};class g extends Error{constructor(t,e){super(`${Ju[t]}${e?`\uFF08HTTP ${e}\uFF09`:""}`),this.code=t,this.status=e,this.name="StorageError"}}function Yu(u){return u instanceof g&&u.code==="conflict"}function Q(u){const t=u.status,e=t===429||t===403&&(u.headers.get("x-ratelimit-remaining")==="0"||u.headers.has("retry-after"));return new g(e?"rate-limit":t===401?"authentication":t===403?"permission":t===404?"not-found":t>=500?"network":"validation",t)}const k={github:"https://api.github.com",gitlab:"https://gitlab.com/api/v4"};function B(u,t=!1){if(!(u===""&&t)){if(u.split("/").some(e=>e==="."||e===".."))throw Object.assign(new g("validation"),{message:"\u8DEF\u5F84\u4E0D\u80FD\u5305\u542B . \u6216 .."});if(!u||/[\\\x00-\x1F\x7F]/.test(u)||u.startsWith("/")||/^[A-Z]:/i.test(u)||u.split("/").some(e=>!e||e==="."||e===".."))throw new g("validation")}}function T(u){try{const t=new URL(u);if(!["https:","http:"].includes(t.protocol)||t.username||t.password||t.search||t.hash)throw new Error("Invalid API base URL");return t.href.replace(/\/+$/,"")}catch{throw new g("validation")}}function I(u,t){if(B(t),/\s/.test(t)||(u==="github"?t.split("/").length!==2:!/^\d+$/.test(t)&&t.split("/").length<2))throw new Error(u==="github"?"\u4ED3\u5E93\u8981\u5199\u6210 owner/repo \u5F62\u5F0F":"GitLab \u4ED3\u5E93\u5FC5\u987B\u662F\u6570\u5B57\u9879\u76EE ID \u6216 group/subgroup/project")}const qu=C.object({schema:C.literal(1),storage:C.object({type:C.enum(["github","gitlab"]),apiBaseUrl:C.string().min(1).optional(),repo:C.string().min(1),branch:C.string().min(1).optional(),path:C.string().optional()}).strict()}).strict();class V extends Error{constructor(t){super(t),this.name="ProjectConfigError"}}function Wu(u,t=h.cwd()){const e=Lu(t,u||".initx-env.json");let n;try{n=Pu(e,"utf8")}catch(r){if(!u&&r.code==="ENOENT")return;throw new V("\u65E0\u6CD5\u8BFB\u53D6\u9879\u76EE\u914D\u7F6E\u6587\u4EF6\uFF0C\u8BF7\u68C0\u67E5 --config \u8DEF\u5F84\u4E0E\u6587\u4EF6\u6743\u9650")}let o;try{o=qu.parse(JSON.parse(n))}catch{throw new V("\u9879\u76EE\u914D\u7F6E\u5FC5\u987B\u662F schema 1 \u7684\u5408\u6CD5 JSON\uFF0C\u4E14\u53EA\u80FD\u5305\u542B\u975E\u654F\u611F storage \u5B9A\u4F4D\u5B57\u6BB5\uFF08\u7981\u6B62 token / vars\uFF09")}try{const r={...o.storage,apiBaseUrl:T(o.storage.apiBaseUrl||k[o.storage.type])};return I(r.type,r.repo),r.path!==void 0&&B(r.path,!0),{file:e,storage:r}}catch{throw new V("\u9879\u76EE storage \u7684 API \u5730\u5740\u3001\u4ED3\u5E93\u6216\u8DEF\u5F84\u4E0D\u5408\u6CD5")}}const H="main",K="stores";function X(u,t){return`${u}:${T(t)}`}function U(u){const t=u.storage?.type||"github";if(t!=="github"&&t!=="gitlab")throw new Error("\u672C\u5730 storage.type \u5FC5\u987B\u662F github \u6216 gitlab");return{type:t,apiBaseUrl:T(u.storage?.apiBaseUrl||k[t])}}function Mu(u,t){const e=t.env??h.env,n=Wu(t.config,t.cwd),o=U(u),r=t.type||e.INITX_ENV_TYPE||n?.storage.type||o.type;if(r!=="github"&&r!=="gitlab")throw new Error("storage.type \u5FC5\u987B\u662F github \u6216 gitlab");const i=r,s=n?.storage.type===i,a=t.apiBaseUrl||e.INITX_ENV_API_BASE_URL||(s?n.storage.apiBaseUrl:void 0)||(o.type===i?o.apiBaseUrl:k[i]),E=T(a),F=o.type===i&&o.apiBaseUrl===E,p=s&&n.storage.apiBaseUrl===E,d=F?u.storage||u:{},f=p?n.storage:void 0,y={type:t.type?"override":e.INITX_ENV_TYPE?"INITX_ENV_TYPE":n?"project":"local/default",apiBaseUrl:t.apiBaseUrl?"override":e.INITX_ENV_API_BASE_URL?"INITX_ENV_API_BASE_URL":p?"project":F?"local/default":"default",repo:"",branch:"",path:""};function A(m,w){const j=`INITX_ENV_${m.toUpperCase()}`,v=[[t[m]||void 0,"override"],[e[j]||void 0,j],[f?.[m],"project"],[d[m]||void 0,"local"],[w,"default"]].find(([Nu])=>Nu!==void 0);return y[m]=v[1],v[0]}return{type:i,apiBaseUrl:E,repo:A("repo",""),branch:A("branch",H),path:A("path",K),sources:y,configSource:n?.file||"local/env/default"}}function zu(u,t,e){const n=t.env??h.env;if(t.token)return{token:t.token,source:"flag"};if(n.INITX_ENV_TOKEN)return{token:n.INITX_ENV_TOKEN,source:"INITX_ENV_TOKEN"};const o=e.type==="github"&&e.apiBaseUrl===k.github;if(o){for(const i of["GH_TOKEN","GITHUB_TOKEN"])if(n[i])return{token:n[i],source:i}}const r=u.credentials?.[X(e.type,e.apiBaseUrl)];return r?{token:r,source:"config"}:o&&u.token?{token:u.token,source:"config"}:{token:"",source:"none"}}function J(u,t={}){const e=Mu(u,t);if(!e.repo)throw new Error("\u8FD8\u6CA1\u914D vault \u4ED3\u5E93\uFF0C\u8BF7\u914D\u7F6E .initx-env.json \u6216 env-config set repo <owner/repo>");I(e.type,e.repo),B(e.path,!0);const{token:n,source:o}=zu(u,t,e);if(!n)throw new Error("\u6CA1\u6709\u53EF\u7528\u7684\u5F53\u524D\u540E\u7AEF token\uFF0C\u8BF7\u8BBE\u7F6E INITX_ENV_TOKEN\u3001--token \u6216\u7ED1\u5B9A\u5F53\u524D API \u5730\u5740\u7684\u672C\u5730\u51ED\u636E");return{...e,token:n,tokenSource:o}}function Zu(u){return u?"***":"(\u672A\u8BBE\u7F6E)"}function uu(u){return Zu(u)}async function x(u,t={}){try{return await fetch(u,{...t,redirect:"error",signal:AbortSignal.timeout(3e4)})}catch{throw new g("network")}}async function b(u){try{return await u.json()}catch(t){throw new g(t instanceof SyntaxError?"validation":"network")}}function D(u){if(!u.ok)throw Q(u)}function Eu(u){return{Authorization:`Bearer ${u}`,Accept:"application/vnd.github+json","X-GitHub-Api-Version":"2022-11-28","User-Agent":"initx-plugin-env"}}class Qu{constructor(t,e){this.location=t,I("github",t.repo),this.base=`${T(t.apiBaseUrl)}/repos/${t.repo.split("/").map(encodeURIComponent).join("/")}`,this.headers=Eu(e)}capabilities={read:!0,list:!0,write:!0,delete:!0,conditionalWrite:!0};base;headers;url(t){B(t,!0);const e=t?`/${t.split("/").map(encodeURIComponent).join("/")}`:"";return`${this.base}/contents${e}?${new URLSearchParams({ref:this.location.branch})}`}async checkTarget(){const t=await x(`${this.base}/commits/${encodeURIComponent(this.location.branch)}`,{headers:this.headers});if(!await this.isEmptyRepository(t)){if(t.status===404){const e=await x(`${this.base}/commits?per_page=1`,{headers:this.headers});if(await this.isEmptyRepository(e))return;D(e)}D(t)}}async isEmptyRepository(t){return t.status!==409?!1:(await b(t))?.message==="Git Repository is empty."}async read(t){B(t);const e=await x(this.url(t),{headers:this.headers});if(e.status===404||await this.isEmptyRepository(e))return await this.checkTarget(),null;D(e);const n=await b(e);if(!n||n.type!=="file"||typeof n.sha!="string"||!n.sha||typeof n.content!="string"||n.encoding!=="base64")throw new g("validation");return{content:S.from(n.content,"base64").toString("utf8"),version:n.sha}}async list(t){const e=await x(this.url(t),{headers:this.headers});if(e.status===404||await this.isEmptyRepository(e))return await this.checkTarget(),[];D(e);const n=await b(e);if(!Array.isArray(n)||n.some(o=>!o||typeof o.name!="string"||typeof o.path!="string"||typeof o.type!="string"))throw new g("validation");return n.map(o=>({name:o.name,path:o.path,kind:o.type==="file"?"file":o.type==="dir"?"directory":"other"}))}async write(t,e,n){B(t);const o=n.condition;if(o.kind!=="create"&&(o.kind!=="update"||!o.expectedVersion))throw new g("validation");const r=await x(this.url(t),{method:"PUT",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({branch:this.location.branch,message:n.message||"env: write store",content:S.from(e).toString("base64"),...o.kind==="update"?{sha:o.expectedVersion}:{}})});if([404,409,422].includes(r.status)){const i=await this.read(t);if(o.kind==="create"?i!==null:i?.version!==o.expectedVersion)throw new g("conflict",r.status)}if(!r.ok)throw Q(r)}async remove(t,e){if(B(t),!e.expectedVersion)throw new g("validation");const n=await x(this.url(t),{method:"DELETE",headers:{...this.headers,"Content-Type":"application/json"},body:JSON.stringify({branch:this.location.branch,message:e.message||"env: remove store",sha:e.expectedVersion})});if([404,409,422].includes(n.status)&&(await this.read(t))?.version!==e.expectedVersion)throw new g("conflict",n.status);D(n)}}function l(u,t,e="stdout"){return h.env.NO_COLOR!==void 0||h.env.FORCE_COLOR==="0"||!h[e].isTTY&&!h.env.FORCE_COLOR?u:Su(u)[t]().toString()}function ut(u){const t=u instanceof Error&&u.message?u.message:"\u547D\u4EE4\u6267\u884C\u5931\u8D25";c.error(l(t,"red")),h.exitCode=1}function $(u,t){u&&c.debug(l(`GitLab ${JSON.stringify(t)}`,"gray"))}function fu(u){return[0,5,10,20,30,40,50].includes(u)?u:void 0}async function tt(u){if(u.status!==400)return;let t;try{t=await b(u)}catch(e){if(e instanceof g&&e.code==="network")throw e}if(t?.message==="You are not allowed to push into this branch")throw new g("permission",400)}class et{constructor(t,e){this.location=t,I("gitlab",t.repo),this.projectUrl=`${T(t.apiBaseUrl)}/projects/${encodeURIComponent(t.repo)}`,this.base=`${this.projectUrl}/repository`,this.headers={"PRIVATE-TOKEN":e,"Content-Type":"application/json"},this.debug=t.debug===!0}capabilities={read:!0,list:!0,write:!0,delete:!0,conditionalWrite:!0};projectUrl;base;headers;debug;fileUrl(t){return B(t),`${this.base}/files/${encodeURIComponent(t)}`}async request(t,e,n={}){let o;try{o=await x(e,{...n,headers:this.headers})}catch(r){throw $(this.debug,{operation:t,event:"network-error"}),r}return n.method?$(this.debug,{operation:t,event:"write-result",status:o.status}):!o.ok&&o.status!==404&&$(this.debug,{operation:t,event:"request-failed",status:o.status}),o}async projectInfo(){const t=await this.request("project",this.projectUrl);D(t);const e=await b(t);if(!e||typeof e.empty_repo!="boolean")throw new g("validation");return e}async diagnosePermission(){if(this.debug)try{const t=await this.projectInfo(),e=[t.permissions?.project_access?.access_level,t.permissions?.group_access?.access_level].map(fu).filter(o=>o!==void 0);if($(this.debug,{operation:"project",event:"permissions",emptyRepo:t.empty_repo,archived:typeof t.archived=="boolean"?t.archived:void 0,accessLevel:e.length?Math.max(...e):void 0}),!t.empty_repo){const o=await this.request("branch",`${this.base}/branches/${encodeURIComponent(this.location.branch)}`);if(o.ok){const r=await b(o);$(this.debug,{operation:"branch",event:"permissions",protected:typeof r?.protected=="boolean"?r.protected:void 0,canPush:typeof r?.can_push=="boolean"?r.can_push:void 0})}}const n=await this.request("protection",`${this.projectUrl}/protected_branches/${encodeURIComponent(this.location.branch)}`);if(n.ok){const o=await b(n);if(Array.isArray(o?.push_access_levels)){const r=o.push_access_levels.map(i=>fu(i?.access_level)).filter(i=>i!==void 0);$(this.debug,{operation:"protection",event:"permissions",allowedPushLevels:r})}}}catch{$(this.debug,{operation:"project",event:"unavailable"})}}async rejectPermission(t){try{await tt(t)}catch(e){throw e instanceof g&&e.code==="permission"&&await this.diagnosePermission(),e}t.status===403&&await this.diagnosePermission()}async checkTarget(){const t=await this.request("commit",`${this.base}/commits/${encodeURIComponent(this.location.branch)}`);if(t.status===404){if((await this.projectInfo()).empty_repo)return $(this.debug,{operation:"project",event:"empty-repository"}),!1;$(this.debug,{operation:"commit",event:"request-failed",status:t.status})}return D(t),!0}async read(t){const e=await this.request("file-read",`${this.fileUrl(t)}?${new URLSearchParams({ref:this.location.branch})}`);if(e.status===404)return await this.checkTarget(),null;D(e);const n=await b(e);if(!n||typeof n.content!="string"||n.encoding!=="base64"||typeof n.last_commit_id!="string"||!n.last_commit_id)throw new g("validation");return{content:S.from(n.content,"base64").toString("utf8"),version:n.last_commit_id}}async list(t){B(t,!0);const e=[];if(!await this.checkTarget())return e;for(let n=1;;n++){const o=new URLSearchParams({ref:this.location.branch,per_page:"100",page:String(n),...t?{path:t}:{}}),r=await this.request("tree",`${this.base}/tree?${o}`);if(r.status===404)return await this.checkTarget(),e;D(r);const i=await b(r);if(!Array.isArray(i)||i.some(a=>!a||typeof a.name!="string"||typeof a.path!="string"||typeof a.type!="string"))throw new g("validation");e.push(...i.map(a=>({name:a.name,path:a.path,kind:a.type==="blob"?"file":a.type==="tree"?"directory":"other"})));const s=r.headers.get("x-next-page");if(!i.length||s===""||s===null&&i.length<100)return e;if(s!==null&&s!==String(n+1))throw new g("validation")}}async write(t,e,n){const o=n.condition;if(o.kind!=="create"&&(o.kind!=="update"||!o.expectedVersion))throw new g("validation");const r=await this.request(o.kind==="create"?"file-create":"file-update",this.fileUrl(t),{method:o.kind==="create"?"POST":"PUT",headers:this.headers,body:JSON.stringify({branch:this.location.branch,commit_message:n.message||"env: write store",content:S.from(e).toString("base64"),encoding:"base64",...o.kind==="update"?{last_commit_id:o.expectedVersion}:{}})});if(await this.rejectPermission(r),[400,404,409,422].includes(r.status)){const i=await this.read(t);if(o.kind==="create"?i!==null:i?.version!==o.expectedVersion)throw new g("conflict",r.status);if(o.kind==="update"&&r.status===400&&i?.content===e)return}if(!r.ok)throw Q(r)}async remove(t,e){if(B(t),!e.expectedVersion)throw new g("validation");const n=await this.request("file-delete",`${this.base}/commits`,{method:"POST",headers:this.headers,body:JSON.stringify({branch:this.location.branch,commit_message:e.message||"env: remove store",actions:[{action:"delete",file_path:t,last_commit_id:e.expectedVersion}]})});if(await this.rejectPermission(n),[400,404,409,422].includes(n.status)&&(await this.read(t))?.version!==e.expectedVersion)throw new g("conflict",n.status);D(n)}}const nt={github:Qu,gitlab:et};function Y(u){const t=nt[u.type];if(!t)throw new g("unsupported");return new t(u,u.token)}function q(u,t){if(!u.capabilities[t])throw new g("unsupported")}const tu=1,ot=C.object({repo:C.string().regex(/^[^/\s]+\/[^/\s]+$/,"\u5FC5\u987B\u662F owner/repo \u5F62\u5F0F"),env:C.string().nullish()}),rt=C.object({schema:C.number().int().positive().default(tu),vars:C.record(C.string(),C.string()).default({}),targets:C.object({envFiles:C.array(C.string().min(1)).optional(),secrets:C.array(ot).optional()}).optional()});function hu(){return{schema:tu,vars:{}}}function N(u){const t=u.trim().replace(/\.json$/i,"");if(!t)throw new Error("store \u540D\u4E0D\u80FD\u4E3A\u7A7A");if(t.includes("/")||t.includes("\\"))throw new Error(`store \u540D\u4E0D\u80FD\u5305\u542B\u8DEF\u5F84\u5206\u9694\u7B26\uFF1A"${u}"`);if(t==="."||t==="..")throw new Error(`store \u540D\u4E0D\u80FD\u662F "${t}"`);if(/[\u0000-\u001F\u007F]/.test(t))throw new Error("store \u540D\u4E0D\u80FD\u5305\u542B\u63A7\u5236\u5B57\u7B26");if(t.length>100)throw new Error("store \u540D\u592A\u957F\uFF08\u4E0A\u9650 100 \u5B57\u7B26\uFF09");return t}function it(u){return/^[A-Z_]\w*$/i.test(u)?u.toUpperCase().startsWith("GITHUB_")?"GITHUB_ \u524D\u7F00\u662F GitHub \u4FDD\u7559\u7684":null:"\u53EA\u5141\u8BB8\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\uFF0C\u4E14\u4E0D\u80FD\u4EE5\u6570\u5B57\u5F00\u5934"}function st(u){return/^[A-Z_][\w.-]*$/i.test(u)}function du(u,t){const e=N(t),n=(u||"").trim().replace(/^\/+|\/+$/g,""),o=n?`${n}/${e}.json`:`${e}.json`;return B(o),o}function at(u,t){let e;try{e=JSON.parse(u)}catch{throw new Error(`store "${t}" \u4E0D\u662F\u5408\u6CD5 JSON`)}const n=rt.safeParse(e);if(!n.success)throw new Error(`store "${t}" \u7ED3\u6784\u4E0D\u5408\u6CD5\uFF1A${n.error.issues.map(o=>`${o.path.join(".")||"(root)"} ${o.message}`).join("; ")}`);return n.data}function ct(u){return`${JSON.stringify({...u,schema:u.schema??tu},null,2)}
|
|
2
|
+
`}async function eu(u,t){q(u,"list");const e=(t||"").trim().replace(/^\/+|\/+$/g,"");return(await u.list(e)).filter(n=>n.kind==="file"&&n.name.toLowerCase().endsWith(".json")).map(n=>n.name.replace(/\.json$/i,"")).sort((n,o)=>n.localeCompare(o))}async function O(u,t,e){const n=du(t,e);q(u,"read");const o=await u.read(n);if(!o)return null;const r=N(e);return{name:r,path:n,version:o.version,store:at(o.content,r)}}async function nu(u,t,e,n,o,r){q(u,"write"),q(u,"conditionalWrite"),await u.write(du(t,e),ct(n),{message:o,condition:r===void 0?{kind:"create"}:{kind:"update",expectedVersion:r}})}function gu(u,t){const e={added:[],updated:[],unchanged:[]};for(const[n,o]of Object.entries(t)){const r=u.vars[n];r===void 0?(u.vars[n]=o,e.added.push(n)):r===o?e.unchanged.push(n):(u.vars[n]=o,e.updated.push(n))}return e}const wu=/^export[ \t]+/,lt=/^([^=]*)=(.*)$/;function Ft(u){const t=u.replace(/[ \t]+$/,"");return t===""||/^[ \t]/.test(t)?null:t}const pt=/^[ \t]*#/;function yu(u){return typeof u=="object"&&u!==null&&u.code==="ENOENT"}async function Et(u){try{return await Ou(u)}catch(t){if(yu(t))return null;throw t}}function ft(u){return u.replace(/\\([nrt"\\])/g,(t,e)=>e==="n"?`
|
|
3
|
+
`:e==="r"?"\r":e==="t"?" ":e)}function ht(u){const t=u.replace(/^[ \t]+/,"");for(const e of t.split(`
|
|
4
|
+
`)){const n=/[ \t]#/.exec(e);if(n!==null)return e.slice(0,n.index).replace(/[ \t]+$/,"")}return t.replace(/[ \t]+$/,"")}function dt(u){if(u==="")return[];const t=u.split(/\r?\n/);return u.endsWith(`
|
|
5
|
+
`)&&t.pop(),t}function Cu(u){for(let t=0;t<u.length;t+=1){if(u[t]==="\\"){t+=1;continue}if(u[t]==='"')return t}return-1}function gt(u,t){const e=u[t];if(pt.test(e))return null;const n=wu.test(e),o=lt.exec(n?e.replace(wu,""):e);if(o===null)return null;const r=Ft(o[1]);if(r===null)return null;const i=o[2];let s=t,a="none",E;if(i.startsWith('"')){let F=i.slice(1),p=Cu(F);for(;p===-1&&s+1<u.length;)s+=1,F+=`
|
|
6
|
+
${u[s]}`,p=Cu(F);a="double",E=ft(p===-1?F:F.slice(0,p))}else if(i.startsWith("'")){const F=i.indexOf("'",1);a="single",E=F===-1?i.slice(1):i.slice(1,F)}else E=ht(i);return{key:r,value:E,exportPrefix:n,quote:a,raw:e,startLine:t,endLine:s}}function W(u){const t=u.includes(`\r
|
|
7
7
|
`)?`\r
|
|
8
8
|
`:`
|
|
9
9
|
`,e=u.endsWith(`
|
|
10
|
-
`),n=
|
|
11
|
-
`){const e=Object.entries(u).map(([n,
|
|
12
|
-
`))return`${t}=${e}`;const n=
|
|
10
|
+
`),n=dt(u),o=new Map,r=[],i=[];for(let s=0;s<n.length;){const a=gt(n,s);if(a===null){s+=1;continue}o.has(a.key)&&!r.includes(a.key)&&r.push(a.key),o.set(a.key,a.value),i.push(a),s=a.endLine+1}return{lines:n,pairs:o,duplicates:r,eol:t,trailingNewline:e,entries:i}}const wt=/[\s"'\\#$`]/;function yt(u){return u!==""&&!wt.test(u)}function Ct(u){return yt(u)?u:`"${u.replace(/\\/g,"\\\\").replace(/"/g,'\\"').replace(/\n/g,"\\n").replace(/\r/g,"\\r").replace(/\t/g,"\\t")}"`}function ou(u,t,e){return`${e?"export ":""}${u}=${Ct(t)}`}function Au(u,t,e){return`${u.join(t)}${e?t:""}`}function Bu(u,t){const{pairs:e}=W(u),n=[],o=[],r=[],i=new Map;for(const[s,a]of Object.entries(t))i.set(s,a),e.has(s)?e.get(s)===a?r.push(s):o.push(s):n.push(s);return{added:n,updated:o,unchanged:r,target:i}}function At(u,t){if(u===""){const F=Bt(t);return{content:F,added:Object.keys(t),updated:[],unchanged:[],duplicates:[],changed:F!==u}}const e=W(u),{added:n,updated:o,unchanged:r,target:i}=Bu(u,t),s=e.lines,a=new Map;e.entries.forEach((F,p)=>a.set(F.key,p));for(let F=e.entries.length-1;F>=0;F-=1){const p=e.entries[F];if(a.get(p.key)!==F)continue;const d=i.get(p.key);if(d===void 0||d===p.value)continue;const f=ou(p.key,d,p.exportPrefix);s.splice(p.startLine,p.endLine-p.startLine+1,f)}if(n.length>0){s.length>0&&s[s.length-1].trim()!==""&&s.push("");for(const F of n)s.push(ou(F,t[F],!1))}const E=Au(s,e.eol,e.trailingNewline);return{content:E,added:n,updated:o,unchanged:r,duplicates:e.duplicates,changed:E!==u}}function Bt(u,t=`
|
|
11
|
+
`){const e=Object.entries(u).map(([n,o])=>ou(n,o,!1));return e.length===0?"":Au(e,t,!0)}function Dt(u,t){const{added:e,updated:n,unchanged:o}=Bu(u,t),r=[...W(u).pairs.keys()].filter(i=>!(i in t));return{added:e,updated:n,unchanged:o,localOnly:r}}async function ru(u){try{return await cu(u,"utf8")}catch(t){if(yu(t))return null;throw t}}async function mt(u,t){const e=await Et(u);if(e!==null&&e.isFile()){if(await cu(u,"utf8")!==t){const n=`${u}.bak`;return await Uu(u,n),await lu(u,t,"utf8"),{backupPath:n}}return{backupPath:null}}return await lu(u,t,"utf8"),{backupPath:null}}function Du(){return h.env.GITHUB_ACTIONS==="true"?!0:typeof h.env.GITHUB_ENV=="string"&&h.env.GITHUB_ENV.length>0}function bt(u){let t=`EOF_${Fu(4).toString("hex")}`;for(;u.includes(t);)t=`EOF_${Fu(4).toString("hex")}`;return t}function vt(u){return Object.entries(u).map(([t,e])=>{if(!e.includes(`
|
|
12
|
+
`))return`${t}=${e}`;const n=bt(e);return[`${t}<<${n}`,e,n].join(`
|
|
13
13
|
`)}).join(`
|
|
14
|
-
`)}const
|
|
15
|
-
`)||e.includes("\r")){const n=e.replace(
|
|
16
|
-
`)}function
|
|
17
|
-
`)||u.includes("\r"))for(const t of u.split(/\r?\n/))t!==""&&console.log(`::add-mask::${t}`);console.log(`::add-mask::${u}`)}function
|
|
18
|
-
${a}`,{encoding:"utf8"}),{target:"github_env",injected:
|
|
14
|
+
`)}const $t=/[\s'"$]/,kt=/[\\"$`]/g;function xt(u){return Object.entries(u).map(([t,e])=>{if(e.includes(`
|
|
15
|
+
`)||e.includes("\r")){const n=e.replace(kt,o=>`\\${o}`).replace(/\r\n|\r|\n/g,"\\n");return`${t}="${n}"`}return $t.test(e)?`${t}='${e.replace(/'/g,"'\\''")}'`:`${t}=${e}`}).join(`
|
|
16
|
+
`)}function _t(u){if(u.includes(`
|
|
17
|
+
`)||u.includes("\r"))for(const t of u.split(/\r?\n/))t!==""&&console.log(`::add-mask::${t}`);console.log(`::add-mask::${u}`)}function Tt(u,t={}){const{forceStdout:e=!1,mask:n=!0}=t,o=Object.keys(u),r=[];if(n&&o.length>0&&Du())for(const a of o)_t(u[a]),r.push(a);const i=e?void 0:h.env.GITHUB_ENV;if(i){const a=vt(u);return a!==""&&Gu(i,`
|
|
18
|
+
${a}`,{encoding:"utf8"}),{target:"github_env",injected:o,masked:r}}const s=xt(u);return s!==""&&console.log(s),{target:"stdout",injected:o,masked:r}}const It=/^\.env(?:\..+)?$/,Nt=[".bak",".example",".sample",".template",".keys",".enc"];async function jt(u,t){const[e="list",...n]=t;switch(e){case"list":case"ls":return St(u);case"pull":return Rt(u,n);case"set":return Gt(u,n);case"push":return Lt(u,n);case"setup":return Ht(u,n);default:throw new Error(`\u672A\u77E5\u5B50\u547D\u4EE4 "${e}"\uFF0C\u53EF\u7528\uFF1Alist | pull | set | push | setup | apply\uFF08npx initx env help\uFF09`)}}function R(u,t={}){const e=J(u.store,{token:u.options.string("token"),config:L(u.options),repo:t.repo});return{config:e,client:Y({...e,debug:u.options.boolean("debug")||u.options.boolean("d")})}}async function St(u){const{config:t,client:e}=R(u),n=await eu(e,t.path);if(!n.length){c.warn(l(`${t.repo} \u7684 ${t.path}/ \u4E0B\u8FD8\u6CA1\u6709 store`,"yellow")),c.info("\u5EFA\u4E00\u4E2A\uFF1Anpx initx env set <\u540D\u5B57> KEY=value");return}c.info(`${l(t.repo,"blue")} \xB7 ${l(`${t.path}/`,"blue")} \u5171 ${n.length} \u4E2A store\uFF1A`);for(const o of n)c.info(` - ${l(o,"blue")}`)}async function mu(u,t,e){if(e)return N(e);const n=await eu(u,t);if(!n.length)throw new Error("\u4ED3\u5E93\u91CC\u8FD8\u6CA1\u6709 store\u3002\u5148\u5EFA\u4E00\u4E2A\uFF1Anpx initx env set <\u540D\u5B57> KEY=value");if(n.length===1)return n[0];if(!h.stdin.isTTY)throw new Error(`\u6709\u591A\u4E2A store\uFF0C\u975E\u4EA4\u4E92\u73AF\u5883\u4E0B\u5FC5\u987B\u6307\u5B9A\u540D\u5B57\uFF1A
|
|
19
19
|
${n.join(`
|
|
20
|
-
`)}`);return
|
|
21
|
-
${
|
|
22
|
-
`)}`);const o=await b.select("\u5199\u5230\u54EA\u4E2A\u6587\u4EF6",r);return[r[o]]}async function Nt(){return(await Uu(f.cwd(),{withFileTypes:!0})).filter(u=>u.isFile()&&bt.test(u.name)).map(u=>u.name).filter(u=>!kt.some(t=>u.endsWith(t))).sort((u,t)=>{const e=n=>n===".env.local"?0:n===".env"?1:2;return e(u)-e(t)||u.localeCompare(t)})}async function It(u,t){const{config:e,client:n}=O(u),r=await Cu(n,e.path,t[0]),o=await ru(n,e.path,r),i=o.store.vars,s=Object.keys(i);if(!s.length){F.warn(`store "${r}" \u91CC\u6CA1\u6709\u4EFB\u4F55\u53D8\u91CF`);return}const a=await _t(u,o),E=u.options.boolean("dry-run");for(const c of a){const l=await nu(c)??"",h=dt(l,i);if(!h.changed){F.info(`${c}: \u5DF2\u7ECF\u662F\u6700\u65B0\u7684\uFF08${s.length} \u4E2A\u53D8\u91CF\uFF0C\u65E0\u9700\u6539\u52A8\uFF09`);continue}if(E){const w=wt(l,i);F.info(`${c}\uFF08--dry-run\uFF0C\u672A\u5199\u5165\uFF09\uFF1A`);for(const B of w.added)F.info(` + ${B}`);for(const B of w.updated)F.info(` ~ ${B}`);continue}const{backupPath:p}=await At(c,h.content);F.success(`${c}: \u65B0\u589E ${h.added.length} \xB7 \u66F4\u65B0 ${h.updated.length} \xB7 \u672A\u53D8 ${h.unchanged.length}`),p&&F.info(` \u65E7\u6587\u4EF6\u5907\u4EFD\u5728 ${p}`),h.duplicates.length&&F.warn(` \u6CE8\u610F\uFF1A${h.duplicates.join(", ")} \u5728\u6587\u4EF6\u91CC\u51FA\u73B0\u591A\u6B21\uFF0C\u53EA\u66F4\u65B0\u4E86\u6700\u540E\u4E00\u884C`)}}function St(u){const t={};for(const e of u){const n=e.indexOf("=");if(n<=0)throw new Error("\u53C2\u6570\u8981\u5199\u6210 KEY=value \u5F62\u5F0F");const r=e.slice(0,n).trim(),o=e.slice(n+1);if(!et(r))throw new Error(`\u53D8\u91CF\u540D\u4E0D\u5408\u6CD5\uFF1A"${r}"`);t[r]=o}if(!Object.keys(t).length)throw new Error("\u81F3\u5C11\u7ED9\u4E00\u4E2A KEY=value");return t}async function Ut(u,t){const[e,...n]=t;if(!e)throw new Error("\u7528\u6CD5\uFF1Anpx initx env set <store \u540D> KEY=value [KEY2=value2 ...]");const r=T(e),o=St(n),{config:i,client:s}=O(u),a=await U(s,i.path,r),E=a?.store??pu(),c=hu(E,o);if(!c.added.length&&!c.updated.length){F.info(`store "${r}" \u65E0\u9700\u6539\u52A8\uFF08${c.unchanged.length} \u4E2A\u53D8\u91CF\u503C\u76F8\u540C\uFF09`);return}await tu(s,i.path,r,E,`env: set ${Object.keys(o).join(", ")} in ${r}`,a?.version),F.success(`store "${r}" \u5DF2\u66F4\u65B0\uFF1A\u65B0\u589E ${c.added.length} \xB7 \u4FEE\u6539 ${c.updated.length}`);for(const l of c.added)F.info(` + ${l}`);for(const l of c.updated)F.info(` ~ ${l}`)}async function Ot(u,t){const[e]=t;if(!e)throw new Error("\u7528\u6CD5\uFF1Anpx initx env push <store \u540D> [--from .env.local] [--prune] [--force]");const n=T(e),r=u.options.string("from")??await jt(),o=await nu(r);if(o===null)throw new Error(`\u627E\u4E0D\u5230\u6587\u4EF6 "${r}"\uFF08\u7528 --from \u6307\u5B9A\uFF09`);const i=Object.fromEntries(Y(o).pairs);if(!Object.keys(i).length)throw new Error(`"${r}" \u91CC\u6CA1\u89E3\u6790\u51FA\u4EFB\u4F55 KEY=value`);const{config:s,client:a}=O(u),E=await U(a,s.path,n),c=E?.store??pu(),l=hu(c,i),h=[];if(u.options.boolean("prune"))for(const w of Object.keys(c.vars))w in i||(delete c.vars[w],h.push(w));if(!l.added.length&&!l.updated.length&&!h.length){F.info(`store "${n}" \u65E0\u9700\u6539\u52A8\uFF08${r} \u91CC\u7684\u503C\u90FD\u4E00\u6837\uFF09`);return}const p=`env: push ${n} from ${r}`;try{await tu(a,s.path,n,c,p,E?.version)}catch(w){if(!Ku(w)||!u.options.boolean("force"))throw w;const B=await ru(a,s.path,n);F.warn("\u8FDC\u7AEF\u5DF2\u66F4\u65B0\uFF0C--force\uFF1A\u4EE5\u672C\u5730\u6587\u4EF6\u4E3A\u51C6\u91CD\u65B0\u5199\u5165"),await tu(a,s.path,n,c,p,B.version)}F.success(`store "${n}" \u5DF2\u66F4\u65B0\uFF1A\u65B0\u589E ${l.added.length} \xB7 \u4FEE\u6539 ${l.updated.length}${h.length?` \xB7 \u5220\u9664 ${h.length}`:""}`)}async function jt(){for(const u of[".env.local",".env"])if(await nu(u)!==null)return u;throw new Error("\u5F53\u524D\u76EE\u5F55\u6CA1\u6709 .env.local / .env\uFF08\u7528 --from \u6307\u5B9A\u6587\u4EF6\uFF09")}async function Gt(u,t){const{config:e,client:n}=O(u),r=await Cu(n,e.path,t[0]),o=(await ru(n,e.path,r)).store.vars;if(!Object.keys(o).length)throw new Error(`store "${r}" \u91CC\u6CA1\u6709\u4EFB\u4F55\u53D8\u91CF`);if(u.options.boolean("json")){console.log(JSON.stringify(o)),F.info(`\u5DF2\u8F93\u51FA ${Object.keys(o).length} \u4E2A\u53D8\u91CF\uFF08JSON\uFF09`);return}const i=$t(o,{forceStdout:u.options.boolean("stdout"),mask:!u.options.boolean("no-mask")});if(i.target==="github_env"){F.success(`\u5DF2\u6CE8\u5165\u672C\u6B21 job \u7684\u540E\u7EED\u6B65\u9AA4\uFF08${i.injected.length} \u4E2A\u53D8\u91CF\uFF0C${i.masked.length} \u4E2A\u5DF2\u8131\u654F\uFF09`);return}Du()||F.warn("\u5F53\u524D\u4E0D\u5728 GitHub Actions \u91CC\uFF0C\u53D8\u91CF\u6253\u5230 stdout\uFF08\u53EF\u4EE5 eval \u6216\u91CD\u5B9A\u5411\uFF09")}const yu=32;let ou;function Pt(){return ou||(ou=Fu.ready.then(()=>Fu)),ou}function Rt(u,t){let e;try{e=u.from_base64(t.trim(),u.base64_variants.ORIGINAL)}catch{throw new Error("GitHub \u516C\u94A5\u89E3\u7801\u5931\u8D25\uFF1A\u5B83\u4E0D\u662F\u5408\u6CD5\u7684 base64\uFF08ORIGINAL \u53D8\u4F53\uFF09\u5B57\u7B26\u4E32\uFF1B\u8BF7\u786E\u8BA4\u4F20\u5165\u7684\u662F public-key \u63A5\u53E3\u8FD4\u56DE\u7684 key \u5B57\u6BB5\uFF0C\u800C\u4E0D\u662F key_id")}if(e.length!==yu)throw new Error(`GitHub \u516C\u94A5\u957F\u5EA6\u4E0D\u6B63\u786E\uFF1AX25519 \u516C\u94A5\u5FC5\u987B\u662F ${yu} \u5B57\u8282\uFF0C\u5B9E\u9645 ${e.length} \u5B57\u8282\u3002 \u8FD9\u79CD\u60C5\u51B5\u901A\u5E38\u662F key_id \u4E0E key \u4E24\u4E2A\u5B57\u6BB5\u5F04\u53CD\u4E86\uFF08\u6216\u628A key_id \u5F53\u6210\u4E86\u516C\u94A5\uFF09\uFF1B\u8BF7\u6539\u7528 public-key \u63A5\u53E3\u8FD4\u56DE\u7684 key \u5B57\u6BB5`);return e}async function Vt(u,t){const e=await Pt(),n=Rt(e,u),r=e.crypto_box_seal(e.from_string(t),n);return e.to_base64(r,e.base64_variants.ORIGINAL)}async function mu(u,t,e={}){const n=await C(u,{...e,headers:{...lu(t),...e.headers??{}}});return v(n),n}function vu(u,t){const[e,n]=u.split("/",2),r=`https://api.github.com/repos/${encodeURIComponent(e)}/${encodeURIComponent(n)}`;return t?`${r}/environments/${encodeURIComponent(t)}/secrets`:`${r}/actions/secrets`}async function Lt(u,t,e){const n=await mu(`${vu(u,e)}/public-key`,t),r=await N(n);if(!r.key_id||!r.key)throw new Error(`\u62FF\u4E0D\u5230 ${u} \u7684\u516C\u94A5\uFF08\u8FD4\u56DE\u4F53\u91CC\u6CA1\u6709 key_id/key\uFF09`);return{keyId:r.key_id,key:r.key}}async function Ht(u,t,e,n,r={}){const{keyId:o,key:i}=await Lt(u,t,r.env),s=await Vt(i,n);return(await mu(`${vu(u,r.env)}/${encodeURIComponent(e)}`,t,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({encrypted_value:s,key_id:o})})).status===201?"created":"updated"}function Kt(u,t,e=f.env){const n=t||e.INITX_ENV_TARGET_TOKEN||(u.type==="github"&&u.apiBaseUrl===$.github?u.token:"");if(!n)throw new Error("GitHub Actions \u76EE\u6807\u9700\u8981\u72EC\u7ACB\u51ED\u636E\uFF1A--target-token \u6216 INITX_ENV_TARGET_TOKEN");return{write(r,o,i,s){return x("github",r),Ht(r,n,o,i,{env:s})}}}const Xt=48*1024;async function Jt(u,t){const[e]=t;if(!e)throw new Error("\u7528\u6CD5\uFF1Anpx initx env apply <store \u540D> --repo <owner/repo> [--env <\u73AF\u5883\u540D>] [--only A,B] [--dry-run]");const n=T(e),r=K(u.store,{token:u.options.string("token"),config:P(u.options)}),o=X(r),i=Kt(r,u.options.string("target-token")),s=await U(o,r.path,n);if(!s)throw new Error(`\u4ED3\u5E93\u91CC\u6CA1\u6709 store "${n}"\u3002\u7528 npx initx env list \u770B\u770B\u6709\u54EA\u4E9B`);const a=s.store.targets?.secrets?.[0],E=u.options.string("repo")??a?.repo;if(!E)throw new Error("\u8981\u6307\u5B9A\u5199\u5165\u76EE\u6807\uFF1A--repo <owner/repo>\uFF0C\u6216\u5728 store \u91CC\u5199 targets.secrets");x("github",E);const c=u.options.string("env")??a?.env??null,l=u.options.string("only")?.split(",").map(g=>g.trim()).filter(Boolean),h=Object.entries(s.store.vars).filter(([g])=>!l||l.includes(g));if(!h.length)throw new Error(l?.length?`--only \u91CC\u6CA1\u6709\u4EFB\u4F55\u53D8\u91CF\u547D\u4E2D store "${n}"`:`store "${n}" \u91CC\u6CA1\u6709\u53D8\u91CF`);const p=[];for(const[g,_]of h){const m=tt(g);if(m){p.push(`${g} \u2014 \u540D\u5B57\u4E0D\u5408\u6CD5\uFF1A${m}`);continue}I.byteLength(_,"utf8")>Xt&&p.push(`${g} \u2014 \u503C\u8D85\u8FC7 48 KB \u4E0A\u9650`)}if(p.length){F.error("\u8FD9\u4E9B\u53D8\u91CF\u4E0D\u80FD\u5199\u8FDB Actions secrets\uFF1A");for(const g of p)F.error(` ${g}`);if(!u.options.boolean("skip-invalid"))throw new Error(`\u6709 ${p.length} \u4E2A\u53D8\u91CF\u4E0D\u5408\u6CD5\uFF0C\u5DF2\u4E2D\u6B62\uFF08\u6539\u540D\u540E\u7528 --only \u6392\u9664\uFF0C\u6216\u52A0 --skip-invalid \u8DF3\u8FC7\uFF09`);F.warn("--skip-invalid\uFF1A\u8DF3\u8FC7\u8FD9\u4E9B\u53D8\u91CF")}const w=new Set(p.map(g=>g.split(" \u2014 ")[0])),B=h.filter(([g])=>!w.has(g));if(u.options.boolean("dry-run")){F.info(`--dry-run\uFF1A\u5C06\u5199\u5165 ${B.length} \u4E2A secret \u2192 ${E}${c?`\uFF08\u73AF\u5883 ${c}\uFF09`:"\uFF08\u4ED3\u5E93\u7EA7\uFF09"}`);for(const[g]of B)F.info(` ${g}`);return}F.info(`\u5199\u5165 ${E}${c?`\uFF08\u73AF\u5883 ${c}\uFF09`:"\uFF08\u4ED3\u5E93\u7EA7\uFF09"} \u2026`);const y=[];for(const[g,_]of B)try{const m=await i.write(E,g,_,c);F.success(`${g}: ${m==="created"?"\u65B0\u5EFA":"\u66F4\u65B0"}`)}catch(m){y.push(g),F.error(`${g}: \u5931\u8D25 \u2014 ${m.message}`)}if(y.length)throw new Error(`${y.length} \u4E2A secret \u5199\u5165\u5931\u8D25\uFF1A${y.join(", ")}`);F.success(`\u5B8C\u6210\uFF1A${B.length} \u4E2A secret \u2192 ${E}`),c&&F.info("\u73AF\u5883\u7EA7 secret \u9700\u8981\u8BE5 environment \u5DF2\u5B58\u5728\uFF0C\u4E14 job \u5F15\u7528\u5B83\u65F6\u624D\u53EF\u7528")}const j=["type","apiBaseUrl","repo","token","branch","path"],$u="token \u4F1A\u4EE5**\u660E\u6587**\u5B58\u5728 ~/.initx/stores/@initx-plugin/env/store.json\uFF1B\u4ECB\u610F\u7684\u8BDD\u6539\u7528\u73AF\u5883\u53D8\u91CF INITX_ENV_TOKEN\uFF0C\u6216\u6BCF\u6B21\u7528 --token \u4F20\u3002";function iu(u){return j.includes(u)}async function Yt(u,t,e){const[n="status",...r]=t;switch(n){case"setup":return Wt(u);case"set":return G(u,r);case"get":return Mt(u,r);case"list":case"ls":return zt(u);case"rm":case"remove":return qt(u,r);case"status":return Zt(u,e);default:ku()}}async function Wt(u){if(!f.stdin.isTTY)throw new Error(`setup \u9700\u8981\u4EA4\u4E92\u5F0F\u7EC8\u7AEF\u3002\u975E\u4EA4\u4E92\u73AF\u5883\u8BF7\u7528\uFF1A
|
|
20
|
+
`)}`);return _.search("\u9009\u4E00\u4E2A store",n)}async function iu(u,t,e){const n=await O(u,t,e);if(!n)throw new Error(`\u4ED3\u5E93\u91CC\u6CA1\u6709 store "${e}"\u3002\u7528 npx initx env list \u770B\u770B\u6709\u54EA\u4E9B`);return n}async function Ut(u,t){const e=u.options.string("to");if(e)return e.split(",").map(i=>i.trim()).filter(Boolean);const n=t.store.targets?.envFiles;if(n?.length)return n;const o=await Ot();if(o.length===1)return o;if(o.length===0){if(!h.stdin.isTTY)return[".env.local"];if(!await _.confirm("\u5F53\u524D\u76EE\u5F55\u6CA1\u6709 .env \u6587\u4EF6\uFF0C\u65B0\u5EFA .env.local\uFF1F"))throw new Error("\u5DF2\u53D6\u6D88\uFF08\u4E5F\u53EF\u4EE5\u7528 --to <\u6587\u4EF6> \u6307\u5B9A\u76EE\u6807\uFF09");return[".env.local"]}if(!h.stdin.isTTY)throw new Error(`\u5F53\u524D\u76EE\u5F55\u6709\u591A\u4E2A .env \u6587\u4EF6\uFF0C\u975E\u4EA4\u4E92\u73AF\u5883\u4E0B\u8BF7\u7528 --to \u6307\u5B9A\uFF1A
|
|
21
|
+
${o.join(`
|
|
22
|
+
`)}`);const r=await _.select("\u5199\u5230\u54EA\u4E2A\u6587\u4EF6",o);return[o[r]]}async function Ot(){return(await Ru(h.cwd(),{withFileTypes:!0})).filter(u=>u.isFile()&&It.test(u.name)).map(u=>u.name).filter(u=>!Nt.some(t=>u.endsWith(t))).sort((u,t)=>{const e=n=>n===".env.local"?0:n===".env"?1:2;return e(u)-e(t)||u.localeCompare(t)})}async function Rt(u,t){const{config:e,client:n}=R(u),o=await mu(n,e.path,t[0]),r=await iu(n,e.path,o),i=r.store.vars,s=Object.keys(i);if(!s.length){c.warn(l(`store "${o}" \u91CC\u6CA1\u6709\u4EFB\u4F55\u53D8\u91CF`,"yellow"));return}const a=await Ut(u,r),E=u.options.boolean("dry-run");for(const F of a){const p=await ru(F)??"",d=At(p,i);if(!d.changed){c.info(`${l(F,"blue")}: \u5DF2\u7ECF\u662F\u6700\u65B0\u7684\uFF08${s.length} \u4E2A\u53D8\u91CF\uFF0C\u65E0\u9700\u6539\u52A8\uFF09`);continue}if(E){const y=Dt(p,i);c.info(`${l(F,"blue")}\uFF08${l("--dry-run\uFF0C\u672A\u5199\u5165","yellow")}\uFF09\uFF1A`);for(const A of y.added)c.info(l(` + ${A}`,"green"));for(const A of y.updated)c.info(l(` ~ ${A}`,"yellow"));continue}const{backupPath:f}=await mt(F,d.content);c.success(l(`${F}: \u65B0\u589E ${d.added.length} \xB7 \u66F4\u65B0 ${d.updated.length} \xB7 \u672A\u53D8 ${d.unchanged.length}`,"green")),f&&c.info(` \u65E7\u6587\u4EF6\u5907\u4EFD\u5728 ${l(f,"blue")}`),d.duplicates.length&&c.warn(l(` \u6CE8\u610F\uFF1A${d.duplicates.join(", ")} \u5728\u6587\u4EF6\u91CC\u51FA\u73B0\u591A\u6B21\uFF0C\u53EA\u66F4\u65B0\u4E86\u6700\u540E\u4E00\u884C`,"yellow"))}}function Pt(u){const t={};for(const e of u){const n=e.indexOf("=");if(n<=0)throw new Error("\u53C2\u6570\u8981\u5199\u6210 KEY=value \u5F62\u5F0F");const o=e.slice(0,n).trim(),r=e.slice(n+1);if(!st(o))throw new Error(`\u53D8\u91CF\u540D\u4E0D\u5408\u6CD5\uFF1A"${o}"`);t[o]=r}if(!Object.keys(t).length)throw new Error("\u81F3\u5C11\u7ED9\u4E00\u4E2A KEY=value");return t}async function Gt(u,t){const[e,...n]=t;if(!e)throw new Error("\u7528\u6CD5\uFF1Anpx initx env set <store \u540D> KEY=value [KEY2=value2 ...]");const o=N(e),r=Pt(n),{config:i,client:s}=R(u),a=await O(s,i.path,o),E=a?.store??hu(),F=gu(E,r);if(!F.added.length&&!F.updated.length){c.info(`store "${l(o,"blue")}" \u65E0\u9700\u6539\u52A8\uFF08${F.unchanged.length} \u4E2A\u53D8\u91CF\u503C\u76F8\u540C\uFF09`);return}await nu(s,i.path,o,E,`env: set ${Object.keys(r).join(", ")} in ${o}`,a?.version),c.success(l(`store "${o}" \u5DF2\u66F4\u65B0\uFF1A\u65B0\u589E ${F.added.length} \xB7 \u4FEE\u6539 ${F.updated.length}`,"green"));for(const p of F.added)c.info(l(` + ${p}`,"green"));for(const p of F.updated)c.info(l(` ~ ${p}`,"yellow"))}async function Lt(u,t){const[e]=t;if(!e)throw new Error("\u7528\u6CD5\uFF1Anpx initx env push <store \u540D> [--from .env.local] [--prune] [--force]");const n=N(e),o=u.options.string("from")??await Vt(),r=await ru(o);if(r===null)throw new Error(`\u627E\u4E0D\u5230\u6587\u4EF6 "${o}"\uFF08\u7528 --from \u6307\u5B9A\uFF09`);const i=Object.fromEntries(W(r).pairs);if(!Object.keys(i).length)throw new Error(`"${o}" \u91CC\u6CA1\u89E3\u6790\u51FA\u4EFB\u4F55 KEY=value`);const{config:s,client:a}=R(u),E=await O(a,s.path,n),F=E?.store??hu(),p=gu(F,i),d=[];if(u.options.boolean("prune"))for(const y of Object.keys(F.vars))y in i||(delete F.vars[y],d.push(y));if(!p.added.length&&!p.updated.length&&!d.length){c.info(`store "${l(n,"blue")}" \u65E0\u9700\u6539\u52A8\uFF08${l(o,"blue")} \u91CC\u7684\u503C\u90FD\u4E00\u6837\uFF09`);return}const f=`env: push ${n} from ${o}`;try{await nu(a,s.path,n,F,f,E?.version)}catch(y){if(!Yu(y)||!u.options.boolean("force"))throw y;const A=await iu(a,s.path,n);c.warn(l("\u8FDC\u7AEF\u5DF2\u66F4\u65B0\uFF0C--force\uFF1A\u4EE5\u672C\u5730\u6587\u4EF6\u4E3A\u51C6\u91CD\u65B0\u5199\u5165","yellow")),await nu(a,s.path,n,F,f,A.version)}c.success(l(`store "${n}" \u5DF2\u66F4\u65B0\uFF1A\u65B0\u589E ${p.added.length} \xB7 \u4FEE\u6539 ${p.updated.length}${d.length?` \xB7 \u5220\u9664 ${d.length}`:""}`,"green"))}async function Vt(){for(const u of[".env.local",".env"])if(await ru(u)!==null)return u;throw new Error("\u5F53\u524D\u76EE\u5F55\u6CA1\u6709 .env.local / .env\uFF08\u7528 --from \u6307\u5B9A\u6587\u4EF6\uFF09")}async function Ht(u,t){const{config:e,client:n}=R(u),o=await mu(n,e.path,t[0]),r=(await iu(n,e.path,o)).store.vars;if(!Object.keys(r).length)throw new Error(`store "${o}" \u91CC\u6CA1\u6709\u4EFB\u4F55\u53D8\u91CF`);if(u.options.boolean("json")){console.log(JSON.stringify(r)),c.info(`\u5DF2\u8F93\u51FA ${Object.keys(r).length} \u4E2A\u53D8\u91CF\uFF08JSON\uFF09`);return}const i=Tt(r,{forceStdout:u.options.boolean("stdout"),mask:!u.options.boolean("no-mask")});if(i.target==="github_env"){c.success(l(`\u5DF2\u6CE8\u5165\u672C\u6B21 job \u7684\u540E\u7EED\u6B65\u9AA4\uFF08${i.injected.length} \u4E2A\u53D8\u91CF\uFF0C${i.masked.length} \u4E2A\u5DF2\u8131\u654F\uFF09`,"green"));return}Du()||c.warn(l("\u5F53\u524D\u4E0D\u5728 GitHub Actions \u91CC\uFF0C\u53D8\u91CF\u6253\u5230 stdout\uFF08\u53EF\u4EE5 eval \u6216\u91CD\u5B9A\u5411\uFF09","yellow"))}const bu=32;let su;function Kt(){return su||(su=pu.ready.then(()=>pu)),su}function Xt(u,t){let e;try{e=u.from_base64(t.trim(),u.base64_variants.ORIGINAL)}catch{throw new Error("GitHub \u516C\u94A5\u89E3\u7801\u5931\u8D25\uFF1A\u5B83\u4E0D\u662F\u5408\u6CD5\u7684 base64\uFF08ORIGINAL \u53D8\u4F53\uFF09\u5B57\u7B26\u4E32\uFF1B\u8BF7\u786E\u8BA4\u4F20\u5165\u7684\u662F public-key \u63A5\u53E3\u8FD4\u56DE\u7684 key \u5B57\u6BB5\uFF0C\u800C\u4E0D\u662F key_id")}if(e.length!==bu)throw new Error(`GitHub \u516C\u94A5\u957F\u5EA6\u4E0D\u6B63\u786E\uFF1AX25519 \u516C\u94A5\u5FC5\u987B\u662F ${bu} \u5B57\u8282\uFF0C\u5B9E\u9645 ${e.length} \u5B57\u8282\u3002 \u8FD9\u79CD\u60C5\u51B5\u901A\u5E38\u662F key_id \u4E0E key \u4E24\u4E2A\u5B57\u6BB5\u5F04\u53CD\u4E86\uFF08\u6216\u628A key_id \u5F53\u6210\u4E86\u516C\u94A5\uFF09\uFF1B\u8BF7\u6539\u7528 public-key \u63A5\u53E3\u8FD4\u56DE\u7684 key \u5B57\u6BB5`);return e}async function Jt(u,t){const e=await Kt(),n=Xt(e,u),o=e.crypto_box_seal(e.from_string(t),n);return e.to_base64(o,e.base64_variants.ORIGINAL)}async function vu(u,t,e={}){const n=await x(u,{...e,headers:{...Eu(t),...e.headers??{}}});return D(n),n}function $u(u,t){const[e,n]=u.split("/",2),o=`https://api.github.com/repos/${encodeURIComponent(e)}/${encodeURIComponent(n)}`;return t?`${o}/environments/${encodeURIComponent(t)}/secrets`:`${o}/actions/secrets`}async function Yt(u,t,e){const n=await vu(`${$u(u,e)}/public-key`,t),o=await b(n);if(!o.key_id||!o.key)throw new Error(`\u62FF\u4E0D\u5230 ${u} \u7684\u516C\u94A5\uFF08\u8FD4\u56DE\u4F53\u91CC\u6CA1\u6709 key_id/key\uFF09`);return{keyId:o.key_id,key:o.key}}async function qt(u,t,e,n,o={}){const{keyId:r,key:i}=await Yt(u,t,o.env),s=await Jt(i,n);return(await vu(`${$u(u,o.env)}/${encodeURIComponent(e)}`,t,{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify({encrypted_value:s,key_id:r})})).status===201?"created":"updated"}function Wt(u,t,e=h.env){const n=t||e.INITX_ENV_TARGET_TOKEN||(u.type==="github"&&u.apiBaseUrl===k.github?u.token:"");if(!n)throw new Error("GitHub Actions \u76EE\u6807\u9700\u8981\u72EC\u7ACB\u51ED\u636E\uFF1A--target-token \u6216 INITX_ENV_TARGET_TOKEN");return{write(o,r,i,s){return I("github",o),qt(o,n,r,i,{env:s})}}}const Mt=48*1024;async function zt(u,t){const[e]=t;if(!e)throw new Error("\u7528\u6CD5\uFF1Anpx initx env apply <store \u540D> --repo <owner/repo> [--env <\u73AF\u5883\u540D>] [--only A,B] [--dry-run]");const n=N(e),o=J(u.store,{token:u.options.string("token"),config:L(u.options)}),r=Y({...o,debug:u.options.boolean("debug")||u.options.boolean("d")}),i=Wt(o,u.options.string("target-token")),s=await O(r,o.path,n);if(!s)throw new Error(`\u4ED3\u5E93\u91CC\u6CA1\u6709 store "${n}"\u3002\u7528 npx initx env list \u770B\u770B\u6709\u54EA\u4E9B`);const a=s.store.targets?.secrets?.[0],E=u.options.string("repo")??a?.repo;if(!E)throw new Error("\u8981\u6307\u5B9A\u5199\u5165\u76EE\u6807\uFF1A--repo <owner/repo>\uFF0C\u6216\u5728 store \u91CC\u5199 targets.secrets");I("github",E);const F=u.options.string("env")??a?.env??null,p=u.options.string("only")?.split(",").map(w=>w.trim()).filter(Boolean),d=Object.entries(s.store.vars).filter(([w])=>!p||p.includes(w));if(!d.length)throw new Error(p?.length?`--only \u91CC\u6CA1\u6709\u4EFB\u4F55\u53D8\u91CF\u547D\u4E2D store "${n}"`:`store "${n}" \u91CC\u6CA1\u6709\u53D8\u91CF`);const f=[];for(const[w,j]of d){const v=it(w);if(v){f.push(`${w} \u2014 \u540D\u5B57\u4E0D\u5408\u6CD5\uFF1A${v}`);continue}S.byteLength(j,"utf8")>Mt&&f.push(`${w} \u2014 \u503C\u8D85\u8FC7 48 KB \u4E0A\u9650`)}if(f.length){c.error(l("\u8FD9\u4E9B\u53D8\u91CF\u4E0D\u80FD\u5199\u8FDB Actions secrets\uFF1A","red"));for(const w of f)c.error(l(` ${w}`,"red"));if(!u.options.boolean("skip-invalid"))throw new Error(`\u6709 ${f.length} \u4E2A\u53D8\u91CF\u4E0D\u5408\u6CD5\uFF0C\u5DF2\u4E2D\u6B62\uFF08\u6539\u540D\u540E\u7528 --only \u6392\u9664\uFF0C\u6216\u52A0 --skip-invalid \u8DF3\u8FC7\uFF09`);c.warn(l("--skip-invalid\uFF1A\u8DF3\u8FC7\u8FD9\u4E9B\u53D8\u91CF","yellow"))}const y=new Set(f.map(w=>w.split(" \u2014 ")[0])),A=d.filter(([w])=>!y.has(w));if(u.options.boolean("dry-run")){c.info(`${l("--dry-run","yellow")}\uFF1A\u5C06\u5199\u5165 ${A.length} \u4E2A secret \u2192 ${l(E,"blue")}${F?`\uFF08\u73AF\u5883 ${l(F,"blue")}\uFF09`:"\uFF08\u4ED3\u5E93\u7EA7\uFF09"}`);for(const[w]of A)c.info(` ${l(w,"blue")}`);return}c.info(`\u5199\u5165 ${l(E,"blue")}${F?`\uFF08\u73AF\u5883 ${l(F,"blue")}\uFF09`:"\uFF08\u4ED3\u5E93\u7EA7\uFF09"} \u2026`);const m=[];for(const[w,j]of A)try{const v=await i.write(E,w,j,F);c.success(l(`${w}: ${v==="created"?"\u65B0\u5EFA":"\u66F4\u65B0"}`,"green"))}catch(v){m.push(w),c.error(l(`${w}: \u5931\u8D25 \u2014 ${v.message}`,"red"))}if(m.length)throw new Error(`${m.length} \u4E2A secret \u5199\u5165\u5931\u8D25\uFF1A${m.join(", ")}`);c.success(l(`\u5B8C\u6210\uFF1A${A.length} \u4E2A secret \u2192 ${E}`,"green")),F&&c.info("\u73AF\u5883\u7EA7 secret \u9700\u8981\u8BE5 environment \u5DF2\u5B58\u5728\uFF0C\u4E14 job \u5F15\u7528\u5B83\u65F6\u624D\u53EF\u7528")}const P=["type","apiBaseUrl","repo","token","branch","path"],ku="token \u4F1A\u4EE5**\u660E\u6587**\u5B58\u5728 ~/.initx/stores/@initx-plugin/env/store.json\uFF1B\u4ECB\u610F\u7684\u8BDD\u6539\u7528\u73AF\u5883\u53D8\u91CF INITX_ENV_TOKEN\uFF0C\u6216\u6BCF\u6B21\u7528 --token \u4F20\u3002";function au(u){return P.includes(u)}async function Zt(u,t,e){const[n="status",...o]=t;switch(n){case"setup":return Qt(u);case"set":return G(u,o);case"get":return ue(u,o);case"list":case"ls":return te(u);case"rm":case"remove":return ee(u,o);case"status":return ne(u,e);default:_u()}}async function Qt(u){if(!h.stdin.isTTY)throw new Error(`setup \u9700\u8981\u4EA4\u4E92\u5F0F\u7EC8\u7AEF\u3002\u975E\u4EA4\u4E92\u73AF\u5883\u8BF7\u7528\uFF1A
|
|
23
23
|
npx initx env-config set repo <owner/repo>
|
|
24
|
-
npx initx env-config set token <pat>`);const t=
|
|
24
|
+
npx initx env-config set token <pat>`);const t=U(u),e=await _.input(`vault \u4ED3\u5E93\uFF08${t.type}\uFF09`,{default:M(u,"repo"),required:!0});G(u,["repo",e]);const n=await _.password(`${t.type} token\uFF08\u56DE\u8F66\u4FDD\u7559\u5F53\u524D\u503C\uFF09`,{mask:"*"});n&&G(u,["token",n]);const o=await _.input("\u5206\u652F",{default:M(u,"branch")||H});G(u,["branch",o||H]);const r=await _.input("store \u76EE\u5F55",{default:M(u,"path")||K});G(u,["path",r||K]),c.success(l("\u672C\u5730 vault \u914D\u7F6E\u5DF2\u4FDD\u5B58","green")),c.warn(l(ku,"yellow"))}function G(u,t){const[e,...n]=t;if(!e||!au(e))throw new Error(`\u7528\u6CD5\uFF1Anpx initx env-config set <${P.join("|")}> <\u503C>`);const o=n.join(" ").trim();if(!o)throw new Error(`${e} \u7684\u503C\u4E0D\u80FD\u4E3A\u7A7A`);const r=U(u);if(e==="type"||e==="apiBaseUrl"){if(e==="type"&&o!=="github"&&o!=="gitlab")throw new Error("type \u5FC5\u987B\u662F github \u6216 gitlab");const i=e==="type"?o:r.type,s=e==="apiBaseUrl"?T(o):i===r.type?r.apiBaseUrl:k[i],a=r.type===i&&r.apiBaseUrl===s;u.storage={...a?u.storage||{repo:u.repo,branch:u.branch,path:u.path}:{},type:i,apiBaseUrl:s}}else e==="token"?u.storage?(u.credentials??={})[X(r.type,r.apiBaseUrl)]=o:u.token=o:(e==="repo"&&I(r.type,o),e==="path"&&B(o),u.storage?u.storage[e]=o:u[e]=o);if(e==="token"){c.success(l(`token \u5DF2\u4FDD\u5B58\uFF1A${uu(o)}`,"green")),c.warn(l(ku,"yellow"));return}c.success(`${e} = ${l(o,"green")}`)}function ue(u,t){const[e]=t;if(!e||!au(e))throw new Error(`\u7528\u6CD5\uFF1Anpx initx env-config get <${P.join("|")}>`);c.info(`${e} = ${l(xu(u,e),"blue")}`)}function te(u){for(const t of P)c.info(`${t}: ${l(xu(u,t),"blue")}`)}function ee(u,t){const[e]=t;if(!e||!au(e))throw new Error(`\u7528\u6CD5\uFF1Anpx initx env-config rm <${P.join("|")}>`);if(e==="token"){const n=U(u);delete u.credentials?.[X(n.type,n.apiBaseUrl)],n.type==="github"&&n.apiBaseUrl===k.github&&delete u.token}else e==="type"||e==="apiBaseUrl"?delete u.storage:u.storage?delete u.storage[e]:delete u[e];c.success(l(`\u5DF2\u6E05\u9664 ${e}`,"green"))}async function ne(u,t){const e=L(t);let n;try{n=J(u,{token:t.string("token"),config:e})}catch(i){if(i instanceof V)throw i;c.warn(l(i.message,"yellow"));return}if(c.info(`\u5B58\u50A8\u540E\u7AEF: ${l(n.type,"blue")}`),c.info(`API \u5730\u5740: ${l(n.apiBaseUrl,"blue")}`),c.info(`\u914D\u7F6E\u6765\u6E90: ${l(n.configSource,"blue")}`),c.info(`\u5B57\u6BB5\u6765\u6E90: ${l(JSON.stringify(n.sources),"gray")}`),c.info(`vault \u4ED3\u5E93: ${l(n.repo,"blue")}`),c.info(`\u5206\u652F: ${l(n.branch,"blue")}`),c.info(`store \u76EE\u5F55: ${l(n.path,"blue")}`),c.info(`token: ${l(uu(n.token),"yellow")}\uFF08\u6765\u81EA ${n.tokenSource}\uFF09`),t.boolean("offline")){c.info(l("\uFF08--offline\uFF1A\u8DF3\u8FC7\u8FDE\u901A\u6027\u68C0\u67E5\uFF09","gray"));return}const o=Y({...n,debug:t.boolean("debug")||t.boolean("d")}),r=await eu(o,n.path);c.success(l(`\u8FDE\u901A\u6B63\u5E38\uFF0C${n.path}/ \u4E0B\u6709 ${r.length} \u4E2A store`,"green"))}function M(u,t){const e=U(u);return t==="type"||t==="apiBaseUrl"?e[t]:t==="token"?u.credentials?.[X(e.type,e.apiBaseUrl)]||(e.type==="github"&&e.apiBaseUrl===k.github?u.token:void 0):u.storage?u.storage[t]:u[t]}function xu(u,t){const e=M(u,t);return t==="token"?uu(e||""):e??"(\u672A\u8BBE\u7F6E)"}function oe(){_u()}function _u(){c.info(`
|
|
25
25
|
\u7528\u6CD5\uFF1Anpx initx env-config <\u547D\u4EE4>
|
|
26
26
|
|
|
27
27
|
setup \u4EA4\u4E92\u5F0F\u914D\u7F6E\u5F53\u524D\u672C\u5730\u540E\u7AEF\uFF08repo / token / branch / path\uFF09
|
|
@@ -44,9 +44,7 @@ get/list/set/rm \u4EC5\u64CD\u4F5C\u672C\u5730\u914D\u7F6E\uFF1B\u5207\u6362 typ
|
|
|
44
44
|
npx initx env-config set type gitlab
|
|
45
45
|
npx initx env-config set apiBaseUrl https://gitlab.example.com/api/v4
|
|
46
46
|
npx initx env-config set repo group/subgroup/vault
|
|
47
|
-
`)}function
|
|
48
|
-
`),te(l)}catch(n){const r=e==="\u53D8\u91CF\u6821\u9A8C"||e==="\u53C2\u6570"?`\uFF1A${n.message}`:"";f.stderr.write(`env run: ${e}\u5931\u8D25${r}
|
|
49
|
-
`),f.exitCode=1}}const oe=["list","ls","pull","set","push","setup","apply","run","help"],ie=["setup","set","get","list","ls","rm","remove","status","help"];class se extends Nu{defaultStore={branch:V,path:L};rules=[{matching:"env",description:"Env vars from a private repo: env list | pull | set | push | setup | apply | run",optional:[void 0,...oe]},{matching:"env-config",description:"Configure GitHub/GitLab vault storage and check connectivity",optional:[void 0,...ie]}];async handle(t,...e){const n=e.filter(a=>typeof a=="string"),r=Lu(t.cliOptions,t.optionsList),o=t.store??{};if(t.key==="env-config"){if(n[0]==="help"||r.boolean("help")){Qt();return}await Yt(o,n,r);return}if(n[0]==="run"&&!r.boolean("help")&&!r.boolean("h")){await re({store:o,options:r},n.slice(1));return}if(!n.length||n[0]==="help"||r.boolean("help")||r.boolean("h")){ae();return}const[i,...s]=n;if(i==="apply"){await Jt({store:o,options:r},s);return}await xt({store:o,options:r},[i,...s])}}function ae(){console.log(`
|
|
47
|
+
`)}function Tu(u){return 128+(Vu.signals[u]??1)}function re(u,t,e){return new Promise(n=>{let o,r,i,s=!1;const a=f=>{const y=f.code;return{code:y==="ENOENT"?127:y==="EACCES"?126:1,errorCode:y??"UNKNOWN"}},E=f=>{s||(s=!0,clearTimeout(r),h.removeListener("SIGINT",p),h.removeListener("SIGTERM",d),n(f))},F=f=>{if(!(s||i||o.exitCode!==null||o.signalCode!==null)){i=f;try{o.kill(f)}catch{}r=setTimeout(()=>{try{o.kill("SIGKILL")}catch{}finally{o.unref(),E({code:Tu(f),signal:f})}},5e3)}};function p(){F("SIGINT")}function d(){F("SIGTERM")}try{o=Hu(u,t,{env:e,stdio:"inherit",shell:!1})}catch(f){E(a(f));return}h.on("SIGINT",p),h.on("SIGTERM",d),o.once("error",f=>E(a(f))),o.once("close",(f,y)=>{const A=i??y??void 0;E(A?{code:Tu(A),signal:A}:{code:f??1})})})}function ie(u){if(h.exitCode=u.code,u.signal&&h.platform!=="win32"){const t=u.signal;h.once("beforeExit",()=>{try{h.kill(h.pid,t)}catch{h.exitCode=u.code}})}}const Iu="\u7528\u6CD5\uFF1Ainitx env run <store> [--config <file>] [--token <token>] -- <command> [...args]";function se(u,t){if(u.length!==1||!u[0])throw new Error(Iu);const e=N(u[0]);if(!Array.isArray(t)||!t.every(n=>typeof n=="string")||!t[0]?.trim())throw new Error(Iu);if(t.some(n=>n.includes("\0")))throw new Error("\u547D\u4EE4\u6216\u53C2\u6570\u4E0D\u80FD\u5305\u542B NUL");return{name:e,command:t[0],args:t.slice(1)}}function ae(u,t=h.env,e=h.platform){const n={...t},o=new Set;for(const[r,i]of Object.entries(u)){if(!/^[A-Z_]\w*$/i.test(r))throw new Error(`\u53D8\u91CF\u540D ${JSON.stringify(r)} \u4E0D\u5408\u6CD5\uFF1A\u53EA\u5141\u8BB8\u5B57\u6BCD\u3001\u6570\u5B57\u3001\u4E0B\u5212\u7EBF\uFF0C\u4E0D\u80FD\u4EE5\u6570\u5B57\u5F00\u5934`);if(typeof i!="string"||i.includes("\0"))throw new Error(`\u53D8\u91CF ${JSON.stringify(r)} \u5FC5\u987B\u662F\u5B57\u7B26\u4E32\u4E14\u4E0D\u80FD\u5305\u542B NUL`);const s=e==="win32"?r.toUpperCase():r;if(o.has(s))throw new Error(`\u53D8\u91CF ${JSON.stringify(r)} \u5728 Windows \u4E0A\u5B58\u5728\u5927\u5C0F\u5199\u51B2\u7A81`);o.add(s)}if(e==="win32"){const r=new Set;for(const i of Object.keys(n)){const s=i.toUpperCase();o.has(s)||r.has(s)?delete n[i]:r.add(s)}}return{...n,...u}}async function ce(u,t){let e="\u53C2\u6570";try{const n=se(t,u.options.raw["--"]),o=new Set(["--","token","config","debug","d"]);if(Object.keys(u.options.raw).some(d=>!o.has(d)))throw new Error("run \u53EA\u652F\u6301 --token / --config \u548C\u5168\u5C40 --debug / -d\uFF1B\u5B50\u8FDB\u7A0B\u53C2\u6570\u5FC5\u987B\u653E\u5728 -- \u540E");const r=u.options.raw.token;if(r!==void 0&&typeof r!="string"&&typeof r!="number")throw new Error("--token \u9700\u8981\u5355\u4E2A\u503C");const i=u.options.raw.config;if(i!==void 0&&(typeof i!="string"||!i))throw new Error("--config \u9700\u8981\u5355\u4E2A\u6587\u4EF6\u8DEF\u5F84");e="\u914D\u7F6E";const s=J(u.store,{token:u.options.string("token"),config:L(u.options)});e="\u8BFB\u53D6 store";const a=Y({...s,debug:u.options.boolean("debug")||u.options.boolean("d")}),E=await O(a,s.path,n.name);if(!E)throw new Error("store \u4E0D\u5B58\u5728");e="\u53D8\u91CF\u6821\u9A8C";const F=ae(E.store.vars);e="\u542F\u52A8\u5B50\u8FDB\u7A0B";const p=await re(n.command,n.args,F);p.errorCode&&c.error(l(`env run: \u542F\u52A8\u5931\u8D25\uFF08${p.errorCode}\uFF09`,"red")),ie(p)}catch(n){const o=e==="\u53D8\u91CF\u6821\u9A8C"||e==="\u53C2\u6570"?`\uFF1A${n.message}`:"";c.error(l(`env run: ${e}\u5931\u8D25${o}`,"red")),h.exitCode=1}}const le=["list","ls","pull","set","push","setup","apply","run","help"],Fe=["setup","set","get","list","ls","rm","remove","status","help"];class pe extends ju{defaultStore={branch:H,path:K};rules=[{matching:"env",description:"Env vars from a private repo: env list | pull | set | push | setup | apply | run",optional:[void 0,...le]},{matching:"env-config",description:"Configure GitHub/GitLab vault storage and check connectivity",optional:[void 0,...Fe]}];async handle(t,...e){try{await this.dispatch(t,...e)}catch(n){ut(n)}}async dispatch(t,...e){const n=e.filter(a=>typeof a=="string"),o=Xu(t.cliOptions,t.optionsList);c.setLevel(o.boolean("debug")||o.boolean("d")?"debug":"info");const r=t.store??{};if(t.key==="env-config"){if(n[0]==="help"||o.boolean("help")){oe();return}await Zt(r,n,o);return}if(n[0]==="run"&&!o.boolean("help")&&!o.boolean("h")){await ce({store:r,options:o},n.slice(1));return}if(!n.length||n[0]==="help"||o.boolean("help")||o.boolean("h")){Ee();return}const[i,...s]=n;if(i==="apply"){await zt({store:r,options:o},s);return}await jt({store:r,options:o},[i,...s])}}function Ee(){c.info(`
|
|
50
48
|
\u7528\u6CD5\uFF1Anpx initx env <\u547D\u4EE4>
|
|
51
49
|
|
|
52
50
|
list \u5217\u51FA vault \u4ED3\u5E93\u91CC\u6240\u6709 store
|
|
@@ -83,4 +81,4 @@ get/list/set/rm \u4EC5\u64CD\u4F5C\u672C\u5730\u914D\u7F6E\uFF1B\u5207\u6362 typ
|
|
|
83
81
|
npx initx env pull mail-peon-publish
|
|
84
82
|
npx initx env setup mail-peon-publish --token "$GITHUB_TOKEN"
|
|
85
83
|
npx initx env apply mail-peon-publish --repo mail-peon/mail-peon
|
|
86
|
-
`)}export{
|
|
84
|
+
`)}export{pe as default};
|
package/package.json
CHANGED