@initx-plugin/cp 0.1.2 → 0.1.3
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/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -2,7 +2,7 @@ import { InitxPlugin, InitxMatcherRules, InitxContext } from '@initx-plugin/core
|
|
|
2
2
|
|
|
3
3
|
declare class CpPlugin extends InitxPlugin {
|
|
4
4
|
rules: InitxMatcherRules;
|
|
5
|
-
handle(ctx: InitxContext, ...args: string[]): Promise<void>;
|
|
5
|
+
handle(ctx: InitxContext, ...args: (string | undefined)[]): Promise<void>;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
export { CpPlugin as default };
|
package/dist/index.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { InitxPlugin, InitxMatcherRules, InitxContext } from '@initx-plugin/core
|
|
|
2
2
|
|
|
3
3
|
declare class CpPlugin extends InitxPlugin {
|
|
4
4
|
rules: InitxMatcherRules;
|
|
5
|
-
handle(ctx: InitxContext, ...args: string[]): Promise<void>;
|
|
5
|
+
handle(ctx: InitxContext, ...args: (string | undefined)[]): Promise<void>;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
export { CpPlugin as default };
|
package/dist/index.mjs
CHANGED
|
@@ -3,4 +3,4 @@ import{InitxPlugin as X}from"@initx-plugin/core";import{existsSync as p,readFile
|
|
|
3
3
|
`,"utf8")}function me(){return T().order}function N(e){const t=T(),n=t.order.indexOf(e);n!==-1&&t.order.splice(n,1),t.order.unshift(e),j(t)}function ke(e){const t=T(),n=t.order.indexOf(e);n!==-1&&(t.order.splice(n,1),j(t))}function $e(e,t){const n=T(),r=n.order.indexOf(e);r!==-1&&(n.order[r]=t,j(n))}var g=(e=>(e.SSH="ssh",e.GPG="gpg",e.CWD="cwd",e))(g||{}),f=(e=>(e.SET="set",e.GET="get",e.LIST="list",e.LS="ls",e.RM="rm",e.RENAME="rename",e.STATUS="status",e.SETUP="setup",e.HELP="help",e))(f||{});const be=["repo","token","branch","path"],_=new Set(Object.values(g)),ve={[g.SSH]:Ee,[g.GPG]:Te,[g.CWD]:Ge};async function q(e){const[t]=e;if(!t){a.error(`Please enter the copy type. Available presets: ${Object.values(g).join(", ")}`);return}if(_.has(t)){await xe(t);return}await Ce(t)}async function Se(){if(!K.stdin.isTTY){a.error("cp (interactive picker) requires an interactive TTY.");return}const e=Object.values(g);let t=[];try{t=await d("Loading data keys",()=>B())}catch(s){a.warn(`Could not load data keys: ${s.message}`)}const n=new Set([...e,...t]),r=me().filter(s=>n.has(s)),o=new Set,i=[];for(const s of r)o.has(s)||(i.push(s),o.add(s));for(const s of[...t].sort())o.has(s)||(i.push(s),o.add(s));for(const s of e)o.has(s)||(i.push(s),o.add(s));if(i.length===0){a.warn("No copyable keys found.");return}const c=i.map(s=>{const z=_.has(s);return{name:s,value:s,description:z?"preset":"data"}}),u=await l.search("Select a key to copy",c);await q([u])}async function xe(e){const t=ve[e];if(!t){a.error(`Unknown preset: ${e}`);return}await t(),N(e)}async function Ce(e){try{const t=await d(`Fetching "${e}"`,()=>Y(e));if(t===null){a.error(`Data key "${e}" not found. Use \`ix cp-config set ${e} <value>\` to create it.`);return}await G(t),a.success(`Data key "${e}" copied to clipboard`),N(e)}catch(t){a.error(`Failed to fetch "${e}": ${t.message}`)}}async function G(e){await ae(e)}async function Ee(){const e=h(ee.homedir(),".ssh");if(!p(e)){a.error(`SSH directory not found, path: ${e}`);return}const t=Z(e).filter(i=>i.endsWith(".pub"));if(t.length===0){a.error("SSH key not found");return}let n;if(t.length===1)n=t[0];else{const i=await l.select("Select SSH key",t);n=t[i]}const r=h(e,n),o=C(r,"utf8");await G(o),a.success("Key copied to clipboard")}async function Te(){const e=await ne();if(e.length===0){a.error("No GPG keys found");return}let t=0;e.length>1&&(t=await l.select("Select GPG key",e.map(({key:o,name:i,email:c})=>`${o} - ${i} <${c}>`)));const{key:n}=e[t],r=await re("gpg",["--armor","--export",n]);await G(r.content),a.success("GPG public key copied to clipboard")}async function Ge(){await G(te()),a.success("Current working directory copied to clipboard")}const $=new Set(be);async function Fe(e){const[t,...n]=e;if(!t||f.HELP===t||t==="--help"||t==="-h"){W();return}switch(t){case f.SET:await Pe(n);break;case f.GET:await Ue(n[0]);break;case f.LIST:case f.LS:await Re();break;case f.RM:await He(n[0]);break;case f.RENAME:await Ae(n);break;case f.STATUS:M();break;case f.SETUP:await De();break;default:a.error(`Unknown command: ${t}`),W()}}async function Pe(e){const[t,...n]=e;if(!t){a.error("Usage: cp-config set <key> <value>");return}if($.has(t)){const o=n.join(" ").trim();if(!o&&t!=="token"&&t!=="branch"&&t!=="path"){a.error(`Usage: cp-config set ${t} <value>`);return}t==="token"&&a.warn("Token is saved in plaintext at ~/.initx/cp/config.json. Prefer a fine-grained GitHub PAT with minimal scopes."),b(t,o||(t==="branch"?"main":t==="path"?"data":"")),a.success(`Config "${t}" saved`);return}const r=n.join(" ");if(!r){a.error("Usage: cp-config set <key> <value>");return}try{await d(`Saving "${t}" to GitHub`,()=>ye(t,r)),a.success(`Data "${t}" saved`),N(t)}catch(o){a.error(`Failed to save data: ${o.message}`)}}async function Ue(e){if(!e){a.error("Usage: cp-config get <key>");return}if($.has(e)){const t=ie(e);if(!t){a.warn(`Config "${e}" is not set`);return}console.log(t);return}try{const t=await d(`Fetching "${e}" from GitHub`,()=>Y(e));if(t===null){a.error(`Data key "${e}" not found`);return}console.log(t)}catch(t){a.error(`Failed to get data: ${t.message}`)}}async function Re(){try{const e=await d("Listing data keys",()=>B());if(e.length===0){a.info("No data keys configured");return}console.log(e.join(`
|
|
4
4
|
`))}catch(e){a.error(`Failed to list data: ${e.message}`)}}async function He(e){if(!e){a.error("Usage: cp-config rm <key>");return}if($.has(e)){se(e),a.success(`Config "${e}" removed`);return}try{await d(`Removing "${e}" from GitHub`,()=>ge(e)),a.success(`Data "${e}" removed`),ke(e)}catch(t){a.error(`Failed to remove data: ${t.message}`)}}async function Ae(e){const[t,n]=e;if(!t||!n){a.error("Usage: cp-config rename <oldKey> <newKey>");return}if($.has(t)||$.has(n)){a.error(`Cannot rename reserved config keys (${Array.from($).join(", ")}).`);return}try{await d(`Renaming "${t}" to "${n}"`,()=>we(t,n)),$e(t,n),a.success(`Data "${t}" renamed to "${n}"`)}catch(r){a.error(`Failed to rename data: ${r.message}`)}}function M(){const e={...w()};typeof e.token=="string"&&e.token&&(e.token="***"),console.log(JSON.stringify(e,null,2))}async function De(){if(!K.stdin.isTTY){a.error("cp-config setup requires an interactive TTY. Use `cp-config set <key> <value>` instead.");return}const e=w();a.info("cp-config setup \u2014 press Enter to keep the current value, type to overwrite.");try{const t=e.repo?`GitHub repo (current: ${e.repo})`:"GitHub repo (owner/repo)",n=await l.input(t,{default:e.repo??void 0,required:!0,pattern:/^[^/\s]+\/[^/\s]+$/,patternError:'Repo must be in the form "owner/repo".'});b("repo",n),a.success('Config "repo" saved');const r=e.token?"GitHub PAT (Enter to keep current value, or paste a new token)":"GitHub PAT",o=await l.password(r,{mask:"*"});o&&o!==e.token?(a.warn("Token is saved in plaintext at ~/.initx/cp/config.json. Prefer a fine-grained GitHub PAT with minimal scopes."),b("token",o),a.success('Config "token" saved')):!o&&e.token?a.info("Token unchanged."):!o&&!e.token&&a.warn("Token not set. You can set it later via `cp-config set token <value>`.");const i=e.branch?`Branch (current: ${e.branch})`:"Branch",c=await l.input(i,{default:e.branch||"main"});b("branch",c||"main"),a.success('Config "branch" saved');const u=e.path?`Path inside repo (current: ${e.path})`:"Path inside repo",s=await l.input(u,{default:e.path||"data"});b("path",s||"data"),a.success('Config "path" saved'),console.log(`
|
|
5
5
|
Current config:`),M()}catch(t){a.warn(`Setup cancelled. ${t.message||""} Existing config left untouched.`)}}function W(){console.log(["Usage: cp-config <command> [args]","","Commands:"," setup Interactive setup for repo / token / branch / path"," set <key> <value> Set a config value (repo, token, branch, path) or a data key"," get <key> Get a config or data value"," list, ls List data keys"," rm <key> Remove a config or data key"," rename <old> <new> Rename a data key (file) without changing its value"," status Show config (token redacted)"," help Show this help","","Config keys: repo, token, branch, path. Anything else is treated as a data key.","","Examples:"," cp-config setup"," cp-config set repo owner/private-repo"," cp-config set token ghp_xxxxxxxxxxxx",' cp-config set my-secret "hello world"'," cp-config get my-secret"," cp-config list"," cp-config rm my-secret"," cp-config rename my-secret renamed-secret"].join(`
|
|
6
|
-
`))}class je extends X{rules=[{matching:"cp",description:"Copy SSH / GPG / CWD / data key to clipboard (interactive picker when no key given)"},{matching:"cp-config",description:"Configure cp plugin (repo, token, branch, path) and data keys"}];async handle(t,...n){if(t.key==="cp-config"){await Fe(
|
|
6
|
+
`))}class je extends X{rules=[{matching:"cp",description:"Copy SSH / GPG / CWD / data key to clipboard (interactive picker when no key given)"},{matching:"cp-config",description:"Configure cp plugin (repo, token, branch, path) and data keys"}];async handle(t,...n){const r=n.filter(o=>typeof o=="string");if(t.key==="cp-config"){await Fe(r);return}if(r.length===0){await Se();return}await q(r)}}export{je as default};
|