@savaryna/git-add-account 2.0.0 → 2.0.2
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 +64 -56
- package/bin/main.js +40 -0
- package/package.json +17 -7
- package/helpers/exec.js +0 -4
- package/helpers/file.js +0 -24
- package/helpers/prompts.js +0 -110
- package/helpers/validate.js +0 -9
- package/index.js +0 -88
package/README.md
CHANGED
|
@@ -1,81 +1,88 @@
|
|
|
1
1
|
# @savaryna/add-git-account
|
|
2
2
|
|
|
3
|
-
🔐 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) you are in.
|
|
3
|
+
🔐 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.
|
|
4
4
|
|
|
5
5
|
## Usage
|
|
6
6
|
|
|
7
|
-
Run the command direcly
|
|
7
|
+
Run the command direcly with
|
|
8
|
+
|
|
8
9
|
```shell
|
|
9
10
|
npx @savaryna/git-add-account
|
|
10
11
|
```
|
|
11
12
|
|
|
12
|
-
or if you want
|
|
13
|
+
or if you want, first install it globally
|
|
13
14
|
|
|
14
15
|
```shell
|
|
15
16
|
npm i -g @savaryna/git-add-account
|
|
17
|
+
```
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
git-add-account
|
|
19
|
+
then you can run it using
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
```shell
|
|
22
|
+
git-add-account
|
|
22
23
|
```
|
|
23
24
|
|
|
24
|
-
|
|
25
|
+
or
|
|
25
26
|
|
|
26
27
|
```shell
|
|
27
|
-
|
|
28
|
-
✔ Email to use for this account: … example@email.com
|
|
29
|
-
✔ Workspace to use for this account: … /Users/savaryna/code/email
|
|
30
|
-
✔ Name to use for SSH keys: … email_example_name
|
|
31
|
-
Enter passphrase (empty for no passphrase):
|
|
32
|
-
Enter same passphrase again:
|
|
33
|
-
✔ Do you want to sign your work? … no / yes
|
|
34
|
-
|
|
35
|
-
Your public SSH key is: ssh-ed25519 AAAAC3NlZ...DITJheGo example@email.com
|
|
36
|
-
You can also find it here: /Users/savaryna/.ssh/git_email_example_name.pub
|
|
37
|
-
Add it to your favorite GIT provider and enjoy!
|
|
38
|
-
|
|
39
|
-
✨ Done. Thanks for using @savaryna/git-add-account!
|
|
28
|
+
gaa
|
|
40
29
|
```
|
|
41
30
|
|
|
42
|
-
|
|
31
|
+
After going through all the steps, you will be presented with your public SSH key so you can copy, and add it to your GIT provider. For example GitHub[^1]:
|
|
43
32
|
|
|
44
33
|
1. Go to your account [settings / keys](https://github.com/settings/keys)
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
34
|
+
1. Click on `New SSH key`
|
|
35
|
+
1. Give it a title
|
|
36
|
+
1. Choose `Authentication Key` for key type
|
|
37
|
+
1. Paste in the public SSH key copied earlier in the key field
|
|
38
|
+
1. Click on `Add SSH key`
|
|
39
|
+
1. Repeat steps **2 through 6** to add a `Signing Key` key type, if you chose to sign your work (Commits, Tags, Pushes)[^2]
|
|
40
|
+
1. Done! Now, you can go to the workspace you chose for the account, ex: `cd /Users/john/code/work`, and all the `git`
|
|
41
|
+
commands issued from this, **or any other subdirectory**, will automatically use the correct account/ssh keys.
|
|
42
|
+
|
|
43
|
+
## Example of how it works
|
|
44
|
+
|
|
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
|
+
|
|
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).
|
|
49
|
+
|
|
50
|
+
```ini
|
|
51
|
+
# File at /Users/john/code/work/.config/sshconfig
|
|
52
|
+
# Config for GIT account john@github.com
|
|
53
|
+
Host github.com
|
|
54
|
+
HostName github.com
|
|
55
|
+
User git
|
|
56
|
+
AddKeysToAgent yes
|
|
57
|
+
UseKeychain yes
|
|
58
|
+
IdentitiesOnly yes
|
|
59
|
+
IdentityFile /Users/john/code/work/.config/id_ed25519_git_github_com
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
1. It creates a `gitconfig` file. [See code](https://github.com/savaryna/git-add-account/blob/main/src/index.ts#L51-L79).
|
|
63
|
+
|
|
64
|
+
```ini
|
|
65
|
+
# File at /Users/john/code/work/.config/gitconfig
|
|
66
|
+
# Config for GIT account john@github.com
|
|
67
|
+
[user]
|
|
68
|
+
name = John Doe
|
|
69
|
+
email = john@github.com
|
|
70
|
+
[core]
|
|
71
|
+
sshCommand = ssh -F /Users/john/code/work/.config/sshconfig
|
|
72
|
+
[gpg]
|
|
73
|
+
format = ssh
|
|
74
|
+
[commit]
|
|
75
|
+
gpgsign = true
|
|
76
|
+
[push]
|
|
77
|
+
gpgsign = if-asked
|
|
78
|
+
[tag]
|
|
79
|
+
gpgsign = true
|
|
80
|
+
[user]
|
|
81
|
+
signingkey = /Users/john/code/work/.config/id_ed25519_git_github_com
|
|
82
|
+
```
|
|
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).
|
|
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.
|
|
79
86
|
|
|
80
87
|
## License
|
|
81
88
|
|
|
@@ -83,3 +90,4 @@ A simple way to use multiple git accounts on one machine is to use multiple SSH
|
|
|
83
90
|
|
|
84
91
|
[^1]: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account?tool=webui
|
|
85
92
|
[^2]: https://docs.github.com/en/authentication/managing-commit-signature-verification
|
|
93
|
+
[^3]: https://git-scm.com/docs/git-config#_conditional_includes
|
package/bin/main.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
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?`
|
|
3
|
+
${e}
|
|
4
|
+
`:""),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(`
|
|
38
|
+
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));
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@savaryna/git-add-account",
|
|
3
|
-
"version": "2.0.
|
|
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) you are in.",
|
|
3
|
+
"version": "2.0.2",
|
|
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": [
|
|
7
7
|
"add",
|
|
@@ -34,19 +34,29 @@
|
|
|
34
34
|
},
|
|
35
35
|
"type": "commonjs",
|
|
36
36
|
"files": [
|
|
37
|
-
"
|
|
37
|
+
"bin"
|
|
38
38
|
],
|
|
39
39
|
"bin": {
|
|
40
|
-
"gaa": "
|
|
41
|
-
"git-add-account": "
|
|
40
|
+
"gaa": "bin/main.js",
|
|
41
|
+
"git-add-account": "bin/main.js"
|
|
42
42
|
},
|
|
43
|
-
"
|
|
43
|
+
"scripts": {
|
|
44
|
+
"link": "npm unlink -g && npm link",
|
|
45
|
+
"dev": "tsup --watch",
|
|
46
|
+
"build": "tsup",
|
|
47
|
+
"pretest": "npm run build",
|
|
48
|
+
"test": "npm publish --dry-run"
|
|
49
|
+
},
|
|
50
|
+
"source": "src/index.ts",
|
|
51
|
+
"main": "bin/main.js",
|
|
44
52
|
"dependencies": {
|
|
45
53
|
"prompts": "^2.4.2",
|
|
46
54
|
"zod": "^3.20.6"
|
|
47
55
|
},
|
|
48
56
|
"devDependencies": {
|
|
49
57
|
"@types/node": "^18.14.0",
|
|
50
|
-
"@types/prompts": "^2.4.2"
|
|
58
|
+
"@types/prompts": "^2.4.2",
|
|
59
|
+
"tsup": "^8.2.4",
|
|
60
|
+
"typescript": "^5.5.4"
|
|
51
61
|
}
|
|
52
62
|
}
|
package/helpers/exec.js
DELETED
package/helpers/file.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
const { access, constants, mkdir, readFile, rm, writeFile } = require('fs');
|
|
2
|
-
const { homedir, platform } = require('os');
|
|
3
|
-
const { promisify } = require('util');
|
|
4
|
-
const { resolve } = require('path');
|
|
5
|
-
|
|
6
|
-
module.exports.createFile = (path, data) => promisify(writeFile)(path, data, 'utf8');
|
|
7
|
-
|
|
8
|
-
module.exports.readFile = promisify(readFile);
|
|
9
|
-
|
|
10
|
-
module.exports.home = homedir();
|
|
11
|
-
|
|
12
|
-
module.exports.platform = platform();
|
|
13
|
-
|
|
14
|
-
module.exports.mkdir = promisify(mkdir);
|
|
15
|
-
|
|
16
|
-
module.exports.remove = (path) => promisify(rm)(path, { recursive: true, force: true });
|
|
17
|
-
|
|
18
|
-
module.exports.resolve = resolve;
|
|
19
|
-
|
|
20
|
-
module.exports.hasReadWriteAccess = (path) =>
|
|
21
|
-
promisify(access)(path, constants.R_OK | constants.W_OK).then(
|
|
22
|
-
() => true,
|
|
23
|
-
() => false
|
|
24
|
-
);
|
package/helpers/prompts.js
DELETED
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
const { default: exec } = require('./exec');
|
|
2
|
-
const { home, resolve } = require('./file');
|
|
3
|
-
const { default: validate, z } = require('./validate');
|
|
4
|
-
const defaultPrompts = require('prompts');
|
|
5
|
-
|
|
6
|
-
const MIN_GIT_VERSION = '2.34.0'; // Lower versions don't support SSH for GPG signing
|
|
7
|
-
|
|
8
|
-
const getGitVersion = () => exec('git --version').then(({ stdout }) => stdout.split(' ')[2]);
|
|
9
|
-
|
|
10
|
-
const exit = (code = 0, reason = null) => {
|
|
11
|
-
console.log(reason ? `\n${reason}\n` : '');
|
|
12
|
-
console.log(`${code ? '😵 Exited.' : '✨ Done.'} Thanks for using @savaryna/git-add-account!\n`);
|
|
13
|
-
process.exit(code);
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
// Add onCancel handler for all prompts
|
|
17
|
-
const prompts = (questions, options) => defaultPrompts(questions, { onCancel: () => exit(1), ...options });
|
|
18
|
-
|
|
19
|
-
const overwritePathPrompt = (path) =>
|
|
20
|
-
prompts({
|
|
21
|
-
type: 'toggle',
|
|
22
|
-
name: 'overwrite',
|
|
23
|
-
message: `Path ${path} already exists. Overwrite?`,
|
|
24
|
-
initial: false,
|
|
25
|
-
active: 'yes',
|
|
26
|
-
inactive: 'no',
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
const cliPrompts = () =>
|
|
30
|
-
prompts([
|
|
31
|
-
{
|
|
32
|
-
type: 'text',
|
|
33
|
-
name: 'name',
|
|
34
|
-
message: 'Your name:',
|
|
35
|
-
validate: validate(z.string()),
|
|
36
|
-
format: (value) => ({
|
|
37
|
-
value,
|
|
38
|
-
camel: value.toLowerCase().replace(/[^\w]/g, '_'),
|
|
39
|
-
}),
|
|
40
|
-
},
|
|
41
|
-
{
|
|
42
|
-
type: 'text',
|
|
43
|
-
name: 'email',
|
|
44
|
-
message: 'Email to use for this account:',
|
|
45
|
-
validate: validate(z.string().email()),
|
|
46
|
-
},
|
|
47
|
-
{
|
|
48
|
-
type: 'text',
|
|
49
|
-
name: 'host',
|
|
50
|
-
message: 'Host to use for this account:',
|
|
51
|
-
initial: (email) => email.split('@')[1],
|
|
52
|
-
validate: validate(
|
|
53
|
-
z
|
|
54
|
-
.string()
|
|
55
|
-
.transform((h) => 'https://' + h)
|
|
56
|
-
.refine(URL.canParse, { message: 'Invalid host' })
|
|
57
|
-
),
|
|
58
|
-
format: (value) => ({
|
|
59
|
-
value,
|
|
60
|
-
camel: value.replace(/[^\w]/g, '_'),
|
|
61
|
-
}),
|
|
62
|
-
},
|
|
63
|
-
{
|
|
64
|
-
type: 'text',
|
|
65
|
-
name: 'workspace',
|
|
66
|
-
message: 'Workspace to use for this account:',
|
|
67
|
-
initial: (host) => resolve(home, 'code', host.camel),
|
|
68
|
-
validate: validate(z.string().refine((value) => !value.startsWith('~'), { message: '"~" is not supported' })),
|
|
69
|
-
format: (value, { host }) => {
|
|
70
|
-
const root = resolve(home, value);
|
|
71
|
-
const config = resolve(root, '.config');
|
|
72
|
-
const sshKeyFileName = `id_ed25519_git_${host.camel}`;
|
|
73
|
-
|
|
74
|
-
return {
|
|
75
|
-
root,
|
|
76
|
-
config,
|
|
77
|
-
gitConfig: resolve(config, 'gitconfig'),
|
|
78
|
-
sshConfig: resolve(config, 'sshconfig'),
|
|
79
|
-
privateKey: resolve(config, sshKeyFileName),
|
|
80
|
-
publicKey: resolve(config, sshKeyFileName + '.pub'),
|
|
81
|
-
};
|
|
82
|
-
},
|
|
83
|
-
},
|
|
84
|
-
{
|
|
85
|
-
type: 'toggle',
|
|
86
|
-
name: 'signYourWork',
|
|
87
|
-
message: 'Do you want to sign your work with SSH?',
|
|
88
|
-
initial: true,
|
|
89
|
-
active: 'yes',
|
|
90
|
-
inactive: 'no',
|
|
91
|
-
},
|
|
92
|
-
{
|
|
93
|
-
type: async (prev) => {
|
|
94
|
-
this.gitVersion = await getGitVersion();
|
|
95
|
-
return prev && this.gitVersion < MIN_GIT_VERSION ? 'toggle' : null;
|
|
96
|
-
},
|
|
97
|
-
name: 'signYourWork',
|
|
98
|
-
message: () => `Your current git version (${this.gitVersion}) does not support SSH signing. Continue without?`,
|
|
99
|
-
initial: true,
|
|
100
|
-
active: 'yes',
|
|
101
|
-
inactive: 'no',
|
|
102
|
-
format: (value) => (value ? !value : exit(1)),
|
|
103
|
-
},
|
|
104
|
-
]);
|
|
105
|
-
|
|
106
|
-
module.exports.exit = exit;
|
|
107
|
-
|
|
108
|
-
module.exports.overwritePathPrompt = overwritePathPrompt;
|
|
109
|
-
|
|
110
|
-
module.exports.default = cliPrompts;
|
package/helpers/validate.js
DELETED
package/index.js
DELETED
|
@@ -1,88 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
const { default: exec } = require('./helpers/exec');
|
|
4
|
-
const { createFile, hasReadWriteAccess, platform, mkdir, readFile, remove } = require('./helpers/file');
|
|
5
|
-
const { default: prompts, overwritePathPrompt, exit } = require('./helpers/prompts');
|
|
6
|
-
|
|
7
|
-
async function main() {
|
|
8
|
-
const { name, email, host, workspace, signYourWork } = await prompts();
|
|
9
|
-
|
|
10
|
-
// Check already existing workspace config
|
|
11
|
-
if (await hasReadWriteAccess(workspace.config)) {
|
|
12
|
-
const { overwrite } = await overwritePathPrompt(workspace.config);
|
|
13
|
-
|
|
14
|
-
if (overwrite) {
|
|
15
|
-
await remove(workspace.config);
|
|
16
|
-
} else {
|
|
17
|
-
exit(1);
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// Create workspace/config dir
|
|
22
|
-
await mkdir(workspace.config, { recursive: true });
|
|
23
|
-
|
|
24
|
-
// Generate ssh key
|
|
25
|
-
await exec(`ssh-keygen -t ed25519 -C "${email}" -f ${workspace.privateKey}`);
|
|
26
|
-
|
|
27
|
-
// Use keychain if the system is MacOS and a passphrase was used
|
|
28
|
-
const useKeychain = await exec(`ssh-keygen -y -P "" -f ${workspace.privateKey}`)
|
|
29
|
-
.then(
|
|
30
|
-
() => false,
|
|
31
|
-
() => true
|
|
32
|
-
)
|
|
33
|
-
.then((hasPassphrase) => hasPassphrase && platform === 'darwin');
|
|
34
|
-
|
|
35
|
-
// Create sshconfig for the workspace
|
|
36
|
-
const sshConfig = `
|
|
37
|
-
# Config for GIT account ${email}
|
|
38
|
-
Host ${host.value}
|
|
39
|
-
HostName ${host.value}
|
|
40
|
-
User git
|
|
41
|
-
AddKeysToAgent yes
|
|
42
|
-
${useKeychain ? 'UseKeychain yes' : ''}
|
|
43
|
-
IdentitiesOnly yes
|
|
44
|
-
IdentityFile ${workspace.privateKey}
|
|
45
|
-
`.replace(/\n\s{4}/g, '\n');
|
|
46
|
-
|
|
47
|
-
await createFile(workspace.sshConfig, sshConfig);
|
|
48
|
-
|
|
49
|
-
// Create gitconfig for the workspace
|
|
50
|
-
const gitConfig = `
|
|
51
|
-
[user]
|
|
52
|
-
name = ${name.value}
|
|
53
|
-
email = ${email}
|
|
54
|
-
[core]
|
|
55
|
-
sshCommand = ssh -F ${workspace.sshConfig}
|
|
56
|
-
${
|
|
57
|
-
!signYourWork
|
|
58
|
-
? ''
|
|
59
|
-
: `
|
|
60
|
-
[gpg]
|
|
61
|
-
format = ssh
|
|
62
|
-
[commit]
|
|
63
|
-
gpgsign = true
|
|
64
|
-
[push]
|
|
65
|
-
gpgsign = if-asked
|
|
66
|
-
[tag]
|
|
67
|
-
gpgsign = true
|
|
68
|
-
[user]
|
|
69
|
-
signingkey = ${workspace.privateKey}
|
|
70
|
-
`
|
|
71
|
-
}
|
|
72
|
-
`.replace(/\n\s{4}/g, '\n');
|
|
73
|
-
|
|
74
|
-
await createFile(workspace.gitConfig, gitConfig);
|
|
75
|
-
|
|
76
|
-
// Include workspace config in the global config
|
|
77
|
-
await exec(`git config --global includeIf.gitdir:${workspace.root}/.path ${workspace.gitConfig}`);
|
|
78
|
-
|
|
79
|
-
const publicKey = await readFile(workspace.publicKey).then((buffer) => buffer.toString().trim());
|
|
80
|
-
|
|
81
|
-
console.log('\nYour public SSH key is: ', publicKey);
|
|
82
|
-
console.log('You can also find it here: ', workspace.publicKey);
|
|
83
|
-
console.log('Add it to your favorite GIT provider and enjoy!');
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
main()
|
|
87
|
-
.then(() => exit())
|
|
88
|
-
.catch((error) => exit(1, error.message));
|