@savaryna/git-add-account 2.0.2 → 2.1.0

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.
Files changed (3) hide show
  1. package/README.md +4 -4
  2. package/bin/main.js +30 -36
  3. package/package.json +4 -2
package/README.md CHANGED
@@ -44,8 +44,8 @@ After going through all the steps, you will be presented with your public SSH ke
44
44
 
45
45
  A simple way to use multiple git accounts on one machine is to use different SSH configs based on the directory you are in. The way [@savaryna/add-git-account](https://www.npmjs.com/package/@savaryna/git-add-account) works is, it asks you for some basic information and then it creates some files under `.config` in the workspace directory you specified. Ex:
46
46
 
47
- 1. It creates a _(private/public)_ SSH keypair using `ssh-keygen -t ed25519 -C "john@github.com" -f /Users/john/code/work/.config/id_ed25519_git_github_com`. [See code](https://github.com/savaryna/git-add-account/blob/main/src/index.ts#L24-L25).
48
- 1. It creates a `sshconfig` file. [See code](https://github.com/savaryna/git-add-account/blob/main/src/index.ts#L35-L49).
47
+ 1. It creates a _(private/public)_ SSH keypair using `ssh-keygen -t ed25519 -C "john@github.com" -f /Users/john/code/work/.config/id_ed25519_git_github_com`. [See code](https://github.com/savaryna/git-add-account/blob/main/src/index.ts#L29-L30).
48
+ 1. It creates a `sshconfig` file. [See code](https://github.com/savaryna/git-add-account/blob/main/src/index.ts#L40-L48).
49
49
 
50
50
  ```ini
51
51
  # File at /Users/john/code/work/.config/sshconfig
@@ -59,7 +59,7 @@ A simple way to use multiple git accounts on one machine is to use different SSH
59
59
  IdentityFile /Users/john/code/work/.config/id_ed25519_git_github_com
60
60
  ```
61
61
 
62
- 1. It creates a `gitconfig` file. [See code](https://github.com/savaryna/git-add-account/blob/main/src/index.ts#L51-L79).
62
+ 1. It creates a `gitconfig` file. [See code](https://github.com/savaryna/git-add-account/blob/main/src/index.ts#L50-L58).
63
63
 
64
64
  ```ini
65
65
  # File at /Users/john/code/work/.config/gitconfig
@@ -81,7 +81,7 @@ A simple way to use multiple git accounts on one machine is to use different SSH
81
81
  signingkey = /Users/john/code/work/.config/id_ed25519_git_github_com
82
82
  ```
83
83
 
84
- 1. It runs `git config --global includeIf.gitdir:/Users/john/code/work/.path /Users/john/code/work/.config/gitconfig`, this makes sure that as long as you are in the workspace created earlier, **or any other subdirectory**, git will use the config from step **3** automatically[^3]. [See code](https://github.com/savaryna/git-add-account/blob/main/src/index.ts#L81-L82).
84
+ 1. It runs `git config --global includeIf.gitdir:/Users/john/code/work/.path /Users/john/code/work/.config/gitconfig`, this makes sure that as long as you are in the workspace created earlier, **or any other subdirectory**, git will use the config from step **3** automatically[^3]. [See code](https://github.com/savaryna/git-add-account/blob/main/src/index.ts#L60-L61).
85
85
  1. And finally, it presents you with your public SSH key so you can copy it and add it to your GIT provider of choice.
86
86
 
87
87
  ## License
package/bin/main.js CHANGED
@@ -1,40 +1,34 @@
1
1
  #!/usr/bin/env node
2
- "use strict";var T=Object.create;var y=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var I=Object.getOwnPropertyNames;var _=Object.getPrototypeOf,H=Object.prototype.hasOwnProperty;var A=(t,e,s,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of I(e))!H.call(t,r)&&r!==s&&y(t,r,{get:()=>e[r],enumerable:!(o=W(e,r))||o.enumerable});return t};var F=(t,e,s)=>(s=t!=null?T(_(t)):{},A(e||!t||!t.__esModule?y(s,"default",{value:t,enumerable:!0}):s,t));var h=require("util"),d=require("child_process"),c=(0,h.promisify)(d.exec);var i=require("fs/promises"),n=require("os"),a=require("path"),u=(t,e)=>(0,i.writeFile)(t,e,"utf8"),v=t=>(0,i.rm)(t,{recursive:!0,force:!0}),w=t=>(0,i.access)(t,i.constants.R_OK|i.constants.W_OK).then(()=>!0,()=>!1);var g=require("zod"),L=t=>e=>{let{success:s,error:o}=t.safeParse(e);return s?!0:o.format()._errors[0]},l=L;var k=F(require("prompts")),O="2.34.0",S=()=>c("git --version").then(({stdout:t})=>t.split(" ")[2]),m=(t=0,e=null)=>{console.log(e?`
2
+ "use strict";var $=Object.create;var y=Object.defineProperty;var H=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var F=Object.getPrototypeOf,L=Object.prototype.hasOwnProperty;var O=(t,e,s,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of A(e))!L.call(t,r)&&r!==s&&y(t,r,{get:()=>e[r],enumerable:!(o=H(e,r))||o.enumerable});return t};var d=(t,e,s)=>(s=t!=null?$(F(t)):{},O(e||!t||!t.__esModule?y(s,"default",{value:t,enumerable:!0}):s,t));var h=d(require("mustache"));var v=`# Config for GIT account {{{email}}}
3
+ Host {{{host.value}}}
4
+ HostName {{{host.value}}}
5
+ User git
6
+ AddKeysToAgent yes{{#useKeychain}}
7
+ UseKeychain yes{{/useKeychain}}
8
+ IdentitiesOnly yes
9
+ IdentityFile {{{workspace.privateKey}}}
10
+ `;var w=`# Config for GIT account {{{email}}}
11
+ [user]
12
+ name = {{{name.value}}}
13
+ email = {{{email}}}
14
+ [core]
15
+ sshCommand = ssh -F {{{workspace.sshConfig}}}
16
+ {{#signYourWork}}
17
+ [gpg]
18
+ format = ssh
19
+ [commit]
20
+ gpgsign = true
21
+ [push]
22
+ gpgsign = if-asked
23
+ [tag]
24
+ gpgsign = true
25
+ [user]
26
+ signingkey = {{{workspace.privateKey}}}
27
+ {{/signYourWork}}
28
+ `;var k=require("util"),x=require("child_process"),m=(0,k.promisify)(x.exec);var i=require("fs/promises"),n=require("os"),a=require("path"),u=(t,e)=>(0,i.writeFile)(t,e,"utf8"),P=t=>(0,i.rm)(t,{recursive:!0,force:!0}),C=t=>(0,i.access)(t,i.constants.R_OK|i.constants.W_OK).then(()=>!0,()=>!1);var c=require("zod"),G=t=>e=>{let{success:s,error:o}=t.safeParse(e);return s?!0:o.format()._errors[0]},l=G;var K=d(require("prompts")),R="2.34.0",j=()=>m("git --version").then(({stdout:t})=>t.split(" ")[2]),g=(t=0,e=null)=>{console.log(e?`
3
29
  ${e}
4
30
  `:""),console.log(`${t?"\u{1F635} Exited.":"\u2728 Done."} Thanks for using @savaryna/git-add-account!
5
- `),process.exit(t)},x=(t,e)=>(0,k.default)(t,{onCancel:()=>m(1),...e}),P=t=>x({type:"toggle",name:"overwrite",message:`Path ${t} already exists. Overwrite?`,initial:!1,active:"yes",inactive:"no"}),$=async()=>{let t=await S();return x([{type:"text",name:"name",message:"Your name:",validate:l(g.z.string()),format:e=>({value:e,camel:e.toLowerCase().replace(/[^\w]/g,"_")})},{type:"text",name:"email",message:"Email to use for this account:",validate:l(g.z.string().email())},{type:"text",name:"host",message:"Host to use for this account:",initial:e=>e.split("@")[1],validate:l(g.z.string().transform(e=>"https://"+e).refine(URL.canParse,{message:"Invalid host"})),format:e=>({value:e,camel:e.replace(/[^\w]/g,"_")})},{type:"text",name:"workspace",message:"Workspace to use for this account:",initial:e=>(0,a.resolve)((0,n.homedir)(),"code",e.camel),validate:l(g.z.string().refine(e=>!e.startsWith("~"),{message:'"~" is not supported'})),format:(e,{host:s})=>{let o=(0,a.resolve)((0,n.homedir)(),e),r=(0,a.resolve)(o,".config"),f=`id_ed25519_git_${s.camel}`;return{root:o,config:r,gitConfig:(0,a.resolve)(r,"gitconfig"),sshConfig:(0,a.resolve)(r,"sshconfig"),privateKey:(0,a.resolve)(r,f),publicKey:(0,a.resolve)(r,f+".pub")}}},{type:"toggle",name:"signYourWork",message:"Do you want to sign your work with SSH?",initial:!0,active:"yes",inactive:"no"},{type:e=>e&&t<O?"toggle":null,name:"signYourWork",initial:!0,message:`Your current git version (${t}) does not support SSH signing. Continue without?`,active:"yes",inactive:"no",format:e=>e?!e:m(1)}])};async function Y(){let{name:t,email:e,host:s,workspace:o,signYourWork:r}=await $();if(await w(o.config)){let{overwrite:p}=await P(o.config);p?await v(o.config):m(1)}await(0,i.mkdir)(o.config,{recursive:!0}),await c(`ssh-keygen -t ed25519 -C "${e}" -f ${o.privateKey}`);let f=await c(`ssh-keygen -y -P "" -f ${o.privateKey}`).then(()=>!1,()=>!0).then(p=>p&&(0,n.platform)()==="darwin"),C=`
6
- # Config for GIT account ${e}
7
- Host ${s.value}
8
- HostName ${s.value}
9
- User git
10
- AddKeysToAgent yes
11
- ${f?"UseKeychain yes":""}
12
- IdentitiesOnly yes
13
- IdentityFile ${o.privateKey}
14
- `.replace(/\n\s{4}/g,`
15
- `).concat(`
16
- `);await u(o.sshConfig,C);let K=`
17
- # Config for GIT account ${e}
18
- [user]
19
- name = ${t.value}
20
- email = ${e}
21
- [core]
22
- sshCommand = ssh -F ${o.sshConfig}
23
- ${r?`
24
- [gpg]
25
- format = ssh
26
- [commit]
27
- gpgsign = true
28
- [push]
29
- gpgsign = if-asked
30
- [tag]
31
- gpgsign = true
32
- [user]
33
- signingkey = ${o.privateKey}
34
- `:""}
35
- `.replace(/\n\s{4}/g,`
36
- `).concat(`
37
- `);await u(o.gitConfig,K),await c(`git config --global includeIf.gitdir:${o.root}/.path ${o.gitConfig}`);let b=await(0,i.readFile)(o.publicKey).then(p=>p.toString().trim());console.log(`
31
+ `),process.exit(t)},T=(t,e)=>(0,K.default)(t,{onCancel:()=>g(1),...e}),W=t=>T({type:"toggle",name:"overwrite",message:`Path ${t} already exists. Overwrite?`,initial:!1,active:"yes",inactive:"no"}),b=async()=>{let t=await j();return T([{type:"text",name:"name",message:"Your name:",validate:l(c.z.string()),format:e=>({value:e,camel:e.toLowerCase().replace(/[^\w]/g,"_")})},{type:"text",name:"email",message:"Email to use for this account:",validate:l(c.z.string().email())},{type:"text",name:"host",message:"Host to use for this account:",initial:e=>e.split("@")[1],validate:l(c.z.string().transform(e=>"https://"+e).refine(URL.canParse,{message:"Invalid host"})),format:e=>({value:e,camel:e.replace(/[^\w]/g,"_")})},{type:"text",name:"workspace",message:"Workspace to use for this account:",initial:e=>(0,a.resolve)((0,n.homedir)(),"code",e.camel),validate:l(c.z.string().refine(e=>!e.startsWith("~"),{message:'"~" is not supported'})),format:(e,{host:s})=>{let o=(0,a.resolve)((0,n.homedir)(),e),r=(0,a.resolve)(o,".config"),f=`id_ed25519_git_${s.camel}`;return{root:o,config:r,gitConfig:(0,a.resolve)(r,"gitconfig"),sshConfig:(0,a.resolve)(r,"sshconfig"),privateKey:(0,a.resolve)(r,f),publicKey:(0,a.resolve)(r,f+".pub")}}},{type:"toggle",name:"signYourWork",message:"Do you want to sign your work with SSH?",initial:!0,active:"yes",inactive:"no"},{type:e=>e&&t<R?"toggle":null,name:"signYourWork",initial:!0,message:`Your current git version (${t}) does not support SSH signing. Continue without?`,active:"yes",inactive:"no",format:e=>e?!e:g(1)}])};async function E(){let{name:t,email:e,host:s,workspace:o,signYourWork:r}=await b();if(await C(o.config)){let{overwrite:p}=await W(o.config);p?await P(o.config):g(1)}await(0,i.mkdir)(o.config,{recursive:!0}),await m(`ssh-keygen -t ed25519 -C "${e}" -f ${o.privateKey}`);let f=await m(`ssh-keygen -y -P "" -f ${o.privateKey}`).then(()=>!1,()=>!0).then(p=>p&&(0,n.platform)()==="darwin"),I=h.default.render(v,{email:e,host:s,useKeychain:f,workspace:o});await u(o.sshConfig,I);let Y=h.default.render(w,{name:t,email:e,workspace:o,signYourWork:r});await u(o.gitConfig,Y),await m(`git config --global includeIf.gitdir:${o.root}/.path ${o.gitConfig}`);let _=await(0,i.readFile)(o.publicKey).then(p=>p.toString().trim());console.log(`
38
32
  Your public SSH key is:
39
- `,b),console.log(`You can also find it here:
40
- `,o.publicKey),console.log("Add it to your favorite GIT provider and enjoy!")}Y().then(()=>m()).catch(t=>m(1,t.message));
33
+ `,_),console.log(`You can also find it here:
34
+ `,o.publicKey),console.log("Add it to your favorite GIT provider and enjoy!")}E().then(()=>g()).catch(t=>g(1,t.message));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@savaryna/git-add-account",
3
- "version": "2.0.2",
3
+ "version": "2.1.0",
4
4
  "description": "🔐 A small CLI app that allows you to easily add multiple GIT accounts on one machine. It switches between accounts automatically based on the workspace (directory/subdirectory) you are in.",
5
5
  "homepage": "https://github.com/savaryna/git-add-account#readme",
6
6
  "keywords": [
@@ -50,11 +50,13 @@
50
50
  "source": "src/index.ts",
51
51
  "main": "bin/main.js",
52
52
  "dependencies": {
53
+ "mustache": "^4.2.0",
53
54
  "prompts": "^2.4.2",
54
55
  "zod": "^3.20.6"
55
56
  },
56
57
  "devDependencies": {
57
- "@types/node": "^18.14.0",
58
+ "@types/mustache": "^4.2.5",
59
+ "@types/node": "^22.5.2",
58
60
  "@types/prompts": "^2.4.2",
59
61
  "tsup": "^8.2.4",
60
62
  "typescript": "^5.5.4"