@savaryna/git-add-account 0.0.1-beta.2 → 0.0.1-beta.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/README.md +6 -9
- package/helpers/exec.js +3 -3
- package/helpers/file.js +20 -3
- package/helpers/prompts.js +7 -4
- package/helpers/validate.js +4 -2
- package/index.js +148 -4
- package/package.json +12 -9
- package/main.js +0 -144
package/README.md
CHANGED
|
@@ -1,9 +1,6 @@
|
|
|
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.
|
|
4
|
-
|
|
5
|
-
> **Warning**
|
|
6
|
-
> , this version was tested to work on MacOS only!
|
|
3
|
+
🔐 A small CLI app that allows you to easily add multiple GIT accounts on one machine. It switches between accounts automatically.
|
|
7
4
|
|
|
8
5
|
## Usage
|
|
9
6
|
|
|
@@ -20,14 +17,14 @@ Need to install the following packages:
|
|
|
20
17
|
Ok to proceed? (y)
|
|
21
18
|
✔ Name to use for this account: … Example Name
|
|
22
19
|
✔ Email to use for this account: … example@email.com
|
|
23
|
-
✔ Workspace to use for this account: …
|
|
20
|
+
✔ Workspace to use for this account: … /Users/savaryna/code/email
|
|
24
21
|
✔ Name to use for SSH keys: … email_example_name
|
|
25
22
|
Enter passphrase (empty for no passphrase):
|
|
26
23
|
Enter same passphrase again:
|
|
27
24
|
✔ Enable signed commits for this account? … no / yes
|
|
28
25
|
|
|
29
|
-
Your public SSH key
|
|
30
|
-
You can also find it here:
|
|
26
|
+
Your public SSH key is: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIGme0weZc05wJiXUBmg4Tk0Ox1rhZP6utY48GQTJheGo example@email.com
|
|
27
|
+
You can also find it here: /Users/savaryna/.ssh/git_email_example_name.pub
|
|
31
28
|
Add it to your favorite GIT provider and enjoy!
|
|
32
29
|
|
|
33
30
|
✨ Done. Thanks for using git-add-account!
|
|
@@ -40,8 +37,8 @@ Your public SSH key will automatically be added to your clipboard so you can add
|
|
|
40
37
|
3. Give it a title
|
|
41
38
|
4. Paste in the public SSH key
|
|
42
39
|
5. Repeat steps 2 - 4 to add a `Signing Key` if you chose to have signed commits
|
|
43
|
-
6. Done! Now you can go to the workspace you chose for the account
|
|
44
|
-
commands issued from this and children directories will use
|
|
40
|
+
6. Done! Now you can go to the workspace you chose for the account `/Users/savaryna/code/email` in this example, and all the GIT
|
|
41
|
+
commands issued from this and children directories will automatically use the correct account.
|
|
45
42
|
|
|
46
43
|
## License
|
|
47
44
|
|
package/helpers/exec.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
const { exec } = require('child_process');
|
|
2
|
+
const { promisify } = require('util');
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
module.exports.default = promisify(exec);
|
package/helpers/file.js
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
const { access, appendFile, constants, mkdir, readFile, unlink, writeFile } = require('fs');
|
|
2
|
+
const { homedir } = require('os');
|
|
3
|
+
const { promisify } = require('util');
|
|
4
|
+
const { resolve } = require('path');
|
|
2
5
|
|
|
3
|
-
|
|
4
|
-
|
|
6
|
+
module.exports.append = promisify(appendFile);
|
|
7
|
+
|
|
8
|
+
module.exports.createEmptyFile = (path) => promisify(writeFile)(path, '');
|
|
9
|
+
|
|
10
|
+
module.exports.home = homedir();
|
|
11
|
+
|
|
12
|
+
module.exports.mkdir = promisify(mkdir);
|
|
13
|
+
|
|
14
|
+
module.exports.readFile = promisify(readFile);
|
|
15
|
+
|
|
16
|
+
module.exports.remove = promisify(unlink);
|
|
17
|
+
|
|
18
|
+
module.exports.resolve = resolve;
|
|
19
|
+
|
|
20
|
+
module.exports.hasReadWriteAccess = (path) =>
|
|
21
|
+
promisify(access)(path, constants.R_OK | constants.W_OK).then(
|
|
5
22
|
() => true,
|
|
6
23
|
() => false
|
|
7
24
|
);
|
package/helpers/prompts.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
const prompts = require('prompts');
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
console.log(`\n${
|
|
3
|
+
const exit = (code = 0, reason = null) => {
|
|
4
|
+
console.log(reason ? `\n${reason}\n` : '');
|
|
5
|
+
console.log(`${code ? '😵 Exited.' : '✨ Done.'} Thanks for using git-add-account!\n`);
|
|
5
6
|
process.exit(code);
|
|
6
7
|
};
|
|
7
8
|
|
|
8
|
-
|
|
9
|
+
module.exports.exit = exit;
|
|
10
|
+
|
|
11
|
+
module.exports.default = (questions, options) => prompts(questions, { onCancel: () => exit(1), ...options });
|
package/helpers/validate.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
const { z } = require('zod');
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
module.exports.z = z;
|
|
4
|
+
|
|
5
|
+
module.exports.default = (schema) => (value) => {
|
|
4
6
|
const { success, error } = schema.safeParse(value);
|
|
5
7
|
if (!success) return error.format()._errors[0];
|
|
6
8
|
return true;
|
package/index.js
CHANGED
|
@@ -1,6 +1,150 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
const { default: exec } = require('./helpers/exec');
|
|
4
|
+
const { append, createEmptyFile, hasReadWriteAccess, home, mkdir, readFile, remove, resolve } = require('./helpers/file');
|
|
5
|
+
const { default: prompts, exit } = require('./helpers/prompts');
|
|
6
|
+
const { default: validate, z } = require('./helpers/validate');
|
|
7
|
+
|
|
8
|
+
const sshDirPath = resolve(home, '.ssh');
|
|
9
|
+
const sshConfigPath = resolve(sshDirPath, 'config');
|
|
10
|
+
|
|
11
|
+
const overwriteFilePrompt = (path) =>
|
|
12
|
+
prompts({
|
|
13
|
+
type: 'toggle',
|
|
14
|
+
name: 'overwrite',
|
|
15
|
+
message: `File ${path} already exists. Overwrite?`,
|
|
16
|
+
initial: false,
|
|
17
|
+
active: 'yes',
|
|
18
|
+
inactive: 'no',
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
async function main() {
|
|
22
|
+
const { name, email, workspace } = await prompts([
|
|
23
|
+
{
|
|
24
|
+
type: 'text',
|
|
25
|
+
name: 'name',
|
|
26
|
+
message: 'Name to use for this account:',
|
|
27
|
+
validate: validate(z.string()),
|
|
28
|
+
format: (value) => ({
|
|
29
|
+
original: value,
|
|
30
|
+
camel: value.toLowerCase().replace(/[^\w]/g, '_'),
|
|
31
|
+
}),
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
type: 'text',
|
|
35
|
+
name: 'email',
|
|
36
|
+
message: 'Email to use for this account:',
|
|
37
|
+
validate: validate(z.string().email()),
|
|
38
|
+
format: (value) => ({
|
|
39
|
+
address: value,
|
|
40
|
+
domain: value.match(/(?<=@).+(?=\.)/)[0].replace(/[^\w]/g, '_'),
|
|
41
|
+
}),
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
type: 'text',
|
|
45
|
+
name: 'workspace',
|
|
46
|
+
message: 'Workspace to use for this account:',
|
|
47
|
+
initial: (email) => resolve(home, `code/${email.domain}`),
|
|
48
|
+
validate: validate(z.string()),
|
|
49
|
+
},
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
const workspaceGitConfigPath = resolve(workspace, '.gitconfig');
|
|
53
|
+
|
|
54
|
+
if (await hasReadWriteAccess(workspaceGitConfigPath)) {
|
|
55
|
+
const { overwrite } = await overwriteFilePrompt(workspaceGitConfigPath);
|
|
56
|
+
|
|
57
|
+
if (overwrite) {
|
|
58
|
+
await remove(workspaceGitConfigPath);
|
|
59
|
+
} else {
|
|
60
|
+
exit(1);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const { sshKeyFileName } = await prompts([
|
|
65
|
+
{
|
|
66
|
+
type: 'text',
|
|
67
|
+
name: 'sshKeyFileName',
|
|
68
|
+
message: 'Name to use for SSH keys:',
|
|
69
|
+
initial: `${email.domain}_${name.camel}`,
|
|
70
|
+
validate: validate(z.string()),
|
|
71
|
+
format: (value) => `git_${value}`,
|
|
72
|
+
},
|
|
73
|
+
]);
|
|
74
|
+
|
|
75
|
+
const sshKeyPath = resolve(sshDirPath, sshKeyFileName);
|
|
76
|
+
|
|
77
|
+
if (await hasReadWriteAccess(sshKeyPath)) {
|
|
78
|
+
const { overwrite } = await overwriteFilePrompt(sshKeyPath);
|
|
79
|
+
|
|
80
|
+
if (overwrite) {
|
|
81
|
+
await remove(sshKeyPath);
|
|
82
|
+
} else {
|
|
83
|
+
exit(1);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Generate ssh key
|
|
88
|
+
await exec(`ssh-keygen -t ed25519 -C "${email.address}" -f ${sshKeyPath}`);
|
|
89
|
+
|
|
90
|
+
// Check to see if the user entered a passphrase
|
|
91
|
+
const hasPassphrase = await exec(`ssh-keygen -y -P "" -f ${sshKeyPath}`).then(
|
|
92
|
+
() => false,
|
|
93
|
+
() => true
|
|
94
|
+
);
|
|
95
|
+
|
|
96
|
+
const sshConfig = `
|
|
97
|
+
# Config for GIT account ${email.address}
|
|
98
|
+
Host *
|
|
99
|
+
AddKeysToAgent yes
|
|
100
|
+
${hasPassphrase ? 'UseKeychain yes' : ''}
|
|
101
|
+
IdentityFile ${sshKeyPath}
|
|
102
|
+
`.replace(/\n\s{4}/g, '\n');
|
|
103
|
+
|
|
104
|
+
// Add account to the ssh config
|
|
105
|
+
await append(sshConfigPath, sshConfig);
|
|
106
|
+
|
|
107
|
+
// Create workspace dir if it does not exist
|
|
108
|
+
await mkdir(workspace, { recursive: true });
|
|
109
|
+
|
|
110
|
+
// Create .gitconfig for the workspace
|
|
111
|
+
await createEmptyFile(workspaceGitConfigPath);
|
|
112
|
+
|
|
113
|
+
// Set user details
|
|
114
|
+
await exec(`git config --file ${workspaceGitConfigPath} user.name "${name.original}"`);
|
|
115
|
+
await exec(`git config --file ${workspaceGitConfigPath} user.email "${email.address}"`);
|
|
116
|
+
|
|
117
|
+
// Set default ssh command
|
|
118
|
+
await exec(`git config --file ${workspaceGitConfigPath} core.sshCommand "ssh -i ${sshKeyPath}"`);
|
|
119
|
+
|
|
120
|
+
const { enableSignedCommits } = await prompts({
|
|
121
|
+
type: 'toggle',
|
|
122
|
+
name: 'enableSignedCommits',
|
|
123
|
+
message: 'Enable signed commits for this account?',
|
|
124
|
+
initial: true,
|
|
125
|
+
active: 'yes',
|
|
126
|
+
inactive: 'no',
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
// Enable signed commits
|
|
130
|
+
if (enableSignedCommits) {
|
|
131
|
+
await exec(`git config --file ${workspaceGitConfigPath} gpg.format ssh`);
|
|
132
|
+
await exec(`git config --file ${workspaceGitConfigPath} commit.gpgsign true`);
|
|
133
|
+
await exec(`git config --file ${workspaceGitConfigPath} user.signingkey ${sshKeyPath}`);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Include workspace config in the global config
|
|
137
|
+
await exec(`git config --global includeIf.gitdir:${workspace}/.path ${workspaceGitConfigPath}`);
|
|
138
|
+
|
|
139
|
+
const publicSshKeyPath = resolve(sshDirPath, `${sshKeyFileName}.pub`);
|
|
140
|
+
// await exec(`pbcopy < ${publicSshKeyPath}`);
|
|
141
|
+
const publicSshKey = await readFile(publicSshKeyPath).then((buffer) => buffer.toString().trim());
|
|
142
|
+
|
|
143
|
+
console.log('\nYour public SSH key is: ', publicSshKey);
|
|
144
|
+
console.log('You can also find it here: ', publicSshKeyPath);
|
|
145
|
+
console.log('Add it to your favorite GIT provider and enjoy!');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
main()
|
|
149
|
+
.then(() => exit())
|
|
150
|
+
.catch((error) => exit(1, error.message));
|
package/package.json
CHANGED
|
@@ -1,20 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@savaryna/git-add-account",
|
|
3
|
-
"version": "0.0.1-beta.
|
|
4
|
-
"description": "🔐 A small CLI app that allows you to easily add multiple GIT accounts on one machine.",
|
|
3
|
+
"version": "0.0.1-beta.3",
|
|
4
|
+
"description": "🔐 A small CLI app that allows you to easily add multiple GIT accounts on one machine. It switches between accounts automatically.",
|
|
5
5
|
"homepage": "https://github.com/savaryna/git-add-account#readme",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"add",
|
|
8
8
|
"multiple",
|
|
9
9
|
"git",
|
|
10
|
+
"github",
|
|
10
11
|
"account",
|
|
11
12
|
"accounts",
|
|
13
|
+
"users",
|
|
12
14
|
"on",
|
|
13
15
|
"one",
|
|
14
16
|
"machine",
|
|
17
|
+
"mac",
|
|
18
|
+
"linux",
|
|
19
|
+
"windows",
|
|
15
20
|
"ssh",
|
|
16
21
|
"verified",
|
|
17
|
-
"commit"
|
|
22
|
+
"commit",
|
|
23
|
+
"auto",
|
|
24
|
+
"switch"
|
|
18
25
|
],
|
|
19
26
|
"author": "Alex Tofan",
|
|
20
27
|
"license": "MIT",
|
|
@@ -25,17 +32,13 @@
|
|
|
25
32
|
"type": "git",
|
|
26
33
|
"url": "git+https://github.com/savaryna/git-add-account.git"
|
|
27
34
|
},
|
|
35
|
+
"type": "commonjs",
|
|
28
36
|
"bin": {
|
|
29
|
-
"
|
|
37
|
+
"gaa": "index.js",
|
|
30
38
|
"git-add-account": "index.js"
|
|
31
39
|
},
|
|
32
40
|
"main": "index.js",
|
|
33
|
-
"module": "main.js",
|
|
34
|
-
"os": [
|
|
35
|
-
"darwin"
|
|
36
|
-
],
|
|
37
41
|
"dependencies": {
|
|
38
|
-
"esm": "^3.2.25",
|
|
39
42
|
"prompts": "^2.4.2",
|
|
40
43
|
"zod": "^3.20.6"
|
|
41
44
|
},
|
package/main.js
DELETED
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
import exec from './helpers/exec.js';
|
|
4
|
-
import { fileExists } from './helpers/file.js';
|
|
5
|
-
import prompts, { exit } from './helpers/prompts.js';
|
|
6
|
-
import validate, { z } from './helpers/validate.js';
|
|
7
|
-
|
|
8
|
-
const sshDir = '~/.ssh/';
|
|
9
|
-
|
|
10
|
-
const overwriteFilePrompt = (path) =>
|
|
11
|
-
prompts({
|
|
12
|
-
type: 'toggle',
|
|
13
|
-
name: 'overwrite',
|
|
14
|
-
message: `File ${path} already exists. Overwrite?`,
|
|
15
|
-
initial: false,
|
|
16
|
-
active: 'yes',
|
|
17
|
-
inactive: 'no',
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
async function main() {
|
|
21
|
-
const { name, email, workspace } = await prompts([
|
|
22
|
-
{
|
|
23
|
-
type: 'text',
|
|
24
|
-
name: 'name',
|
|
25
|
-
message: 'Name to use for this account:',
|
|
26
|
-
validate: validate(z.string()),
|
|
27
|
-
format: (value) => ({
|
|
28
|
-
original: value,
|
|
29
|
-
camel: value.toLowerCase().replace(/[^\w]/g, '_'),
|
|
30
|
-
}),
|
|
31
|
-
},
|
|
32
|
-
{
|
|
33
|
-
type: 'text',
|
|
34
|
-
name: 'email',
|
|
35
|
-
message: 'Email to use for this account:',
|
|
36
|
-
validate: validate(z.string().email()),
|
|
37
|
-
format: (value) => ({
|
|
38
|
-
address: value,
|
|
39
|
-
domain: value.match(/(?<=@).+(?=\.)/)[0].replace(/[^\w]/g, '_'),
|
|
40
|
-
}),
|
|
41
|
-
},
|
|
42
|
-
{
|
|
43
|
-
type: 'text',
|
|
44
|
-
name: 'workspace',
|
|
45
|
-
message: 'Workspace to use for this account:',
|
|
46
|
-
initial: (email) => `~/code/${email.domain}`,
|
|
47
|
-
validate: validate(z.string()),
|
|
48
|
-
},
|
|
49
|
-
]);
|
|
50
|
-
|
|
51
|
-
if (await fileExists(`${workspace}/.gitconfig`)) {
|
|
52
|
-
const { overwrite } = await overwriteFilePrompt(`${workspace}/.gitconfig`);
|
|
53
|
-
|
|
54
|
-
if (overwrite) {
|
|
55
|
-
await exec(`rm ${workspace}/.gitconfig`);
|
|
56
|
-
} else {
|
|
57
|
-
exit(1);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const { sshKeyFileName } = await prompts([
|
|
62
|
-
{
|
|
63
|
-
type: 'text',
|
|
64
|
-
name: 'sshKeyFileName',
|
|
65
|
-
message: 'Name to use for SSH keys:',
|
|
66
|
-
initial: `${email.domain}_${name.camel}`,
|
|
67
|
-
validate: validate(z.string()),
|
|
68
|
-
format: (value) => `git_${value}`,
|
|
69
|
-
},
|
|
70
|
-
]);
|
|
71
|
-
|
|
72
|
-
const sshKey = `${sshDir}${sshKeyFileName}`;
|
|
73
|
-
|
|
74
|
-
if (await fileExists(sshKey)) {
|
|
75
|
-
const { overwrite } = await overwriteFilePrompt(sshKey);
|
|
76
|
-
|
|
77
|
-
if (overwrite) {
|
|
78
|
-
await exec(`rm ${sshKey}`);
|
|
79
|
-
} else {
|
|
80
|
-
exit(1);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Generate ssh key
|
|
85
|
-
await exec(`ssh-keygen -t ed25519 -C "${email.address}" -f ${sshKey}`);
|
|
86
|
-
|
|
87
|
-
// Check to see if the user entered a passphrase
|
|
88
|
-
const hasPassphrase = await exec(`ssh-keygen -y -P "" -f ${sshKey}`).then(
|
|
89
|
-
() => false,
|
|
90
|
-
() => true
|
|
91
|
-
);
|
|
92
|
-
|
|
93
|
-
const sshConfig = `
|
|
94
|
-
# Config for GIT account ${email.address}
|
|
95
|
-
Host *
|
|
96
|
-
AddKeysToAgent yes
|
|
97
|
-
${hasPassphrase ? 'UseKeychain yes' : ''}
|
|
98
|
-
IdentityFile ${sshKey}
|
|
99
|
-
`.replace(/\n\s{4}/g, '\n');
|
|
100
|
-
|
|
101
|
-
// Add account to the ssh config
|
|
102
|
-
await exec(`echo "${sshConfig}" >> ${sshDir}/config`);
|
|
103
|
-
|
|
104
|
-
// Create workspace dir if it does not exist
|
|
105
|
-
await exec(`mkdir -p ${workspace}`);
|
|
106
|
-
|
|
107
|
-
// Create .gitconfig for the workspace
|
|
108
|
-
await exec(`touch ${workspace}/.gitconfig`);
|
|
109
|
-
|
|
110
|
-
// Set user details
|
|
111
|
-
await exec(`git config --file ${workspace}/.gitconfig user.name "${name.original}"`);
|
|
112
|
-
await exec(`git config --file ${workspace}/.gitconfig user.email "${email.address}"`);
|
|
113
|
-
|
|
114
|
-
// Set default ssh command
|
|
115
|
-
await exec(`git config --file ${workspace}/.gitconfig core.sshCommand "ssh -i ${sshKey}"`);
|
|
116
|
-
|
|
117
|
-
const { enableSignedCommits } = await prompts({
|
|
118
|
-
type: 'toggle',
|
|
119
|
-
name: 'enableSignedCommits',
|
|
120
|
-
message: 'Enable signed commits for this account?',
|
|
121
|
-
initial: true,
|
|
122
|
-
active: 'yes',
|
|
123
|
-
inactive: 'no',
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
// Enable signed commits
|
|
127
|
-
if (enableSignedCommits) {
|
|
128
|
-
await exec(`git config --file ${workspace}/.gitconfig gpg.format ssh`);
|
|
129
|
-
await exec(`git config --file ${workspace}/.gitconfig commit.gpgsign true`);
|
|
130
|
-
await exec(`git config --file ${workspace}/.gitconfig user.signingkey ${sshKey}`);
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
// Include workspace config in the global config
|
|
134
|
-
await exec(`git config --global includeIf.gitdir:${workspace}/.path ${workspace}/.gitconfig`);
|
|
135
|
-
|
|
136
|
-
const publicSshKeyFile = `${sshDir}/${sshKeyFileName}.pub`
|
|
137
|
-
await exec(`pbcopy < ${publicSshKeyFile}`);
|
|
138
|
-
|
|
139
|
-
console.log('\nYour public SSH key was added to the clipboard.')
|
|
140
|
-
console.log('You can also find it here: ', publicSshKeyFile)
|
|
141
|
-
console.log('Add it to your favorite GIT provider and enjoy!');
|
|
142
|
-
}
|
|
143
|
-
|
|
144
|
-
main().then(() => exit()).catch(() => exit(1));
|