@savaryna/git-add-account 0.0.1-beta

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.
@@ -0,0 +1,21 @@
1
+ name: Publish package
2
+
3
+ on:
4
+ release:
5
+ types: [created]
6
+ workflow_dispatch:
7
+
8
+ jobs:
9
+ publish-npm:
10
+ runs-on: macos-latest
11
+ steps:
12
+ - uses: actions/checkout@v3
13
+ - uses: actions/setup-node@v3
14
+ with:
15
+ node-version: 16
16
+ registry-url: https://registry.npmjs.org/
17
+ scope: '@savaryna'
18
+ - run: npm ci
19
+ - run: npm publish --access=public
20
+ env:
21
+ NODE_AUTH_TOKEN: ${{secrets.npm_token}}
@@ -0,0 +1,20 @@
1
+ {
2
+ "arrowParens": "always",
3
+ "bracketSameLine": false,
4
+ "bracketSpacing": true,
5
+ "embeddedLanguageFormatting": "auto",
6
+ "htmlWhitespaceSensitivity": "css",
7
+ "insertPragma": false,
8
+ "jsxSingleQuote": false,
9
+ "printWidth": 120,
10
+ "proseWrap": "preserve",
11
+ "quoteProps": "as-needed",
12
+ "requirePragma": false,
13
+ "semi": true,
14
+ "singleAttributePerLine": false,
15
+ "singleQuote": true,
16
+ "tabWidth": 2,
17
+ "trailingComma": "es5",
18
+ "useTabs": false,
19
+ "vueIndentScriptAndStyle": false
20
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Alex Tofan
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # @savaryna/add-git-account
2
+
3
+ > 🔐 A small CLI app that allows you to easily add multiple GIT accounts on one machine.
4
+
5
+ ## Usage
6
+
7
+ Run the command
8
+ ```shell
9
+ npx @savaryna/git-add-account
10
+ ```
11
+
12
+ After going through all the steps
13
+
14
+ ```shell
15
+ Need to install the following packages:
16
+ @savaryna/git-add-account
17
+ Ok to proceed? (y)
18
+ ✔ Name to use for this account: … Example Name
19
+ ✔ Email to use for this account: … example@email.com
20
+ ✔ Workspace to use for this account: … ~/code/email
21
+ ✔ Name to use for SSH keys: … email_example_name
22
+ Enter passphrase (empty for no passphrase):
23
+ Enter same passphrase again:
24
+ ✔ Enable signed commits for this account? … no / yes
25
+
26
+ Your public SSH key was added to the clipboard.
27
+ You can also find it here: ~/.ssh//git_email_example_name.pub
28
+ Add it to your favorite GIT provider and enjoy!
29
+
30
+ ✨ Done. Thanks for using git-add-account!
31
+ ```
32
+
33
+ Your public SSH key will automatically be added to your clipboard so you can add it to your GIT provider. For example GitHub:
34
+
35
+ 1. Go to your account [settings/keys](https://github.com/settings/keys)
36
+ 2. Click on `New SSH key`
37
+ 3. Give it a title
38
+ 4. Paste in the public SSH key
39
+ 5. Repeat steps 2 - 4 to add a `Signing Key` if you chose to have signed commits
40
+ 6. Done! Now you can go to the workspace you chose for the account `~/code/email` in this example, and all the GIT
41
+ commands issued from this and children directories will use your newly created account.
42
+
43
+ ## License
44
+
45
+ MIT – See [LICENSE](LICENSE) file.
@@ -0,0 +1,4 @@
1
+ import { exec } from 'child_process';
2
+ import { promisify } from 'util';
3
+
4
+ export default promisify(exec);
@@ -0,0 +1,7 @@
1
+ import exec from './exec.js';
2
+
3
+ export const fileExists = (file) =>
4
+ exec(`ls ${file}`).then(
5
+ () => true,
6
+ () => false
7
+ );
@@ -0,0 +1,8 @@
1
+ import p from 'prompts';
2
+
3
+ export const exit = (code = 0) => {
4
+ console.log(`\n${code ? '😵 Exiting.' : '✨ Done.'} Thanks for using git-add-account!\n`);
5
+ process.exit(code);
6
+ };
7
+
8
+ export default (prompts, options) => p(prompts, { onCancel: () => exit(1), ...options });
@@ -0,0 +1,7 @@
1
+ export { z } from 'zod';
2
+
3
+ export default (schema) => (value) => {
4
+ const { success, error } = schema.safeParse(value);
5
+ if (!success) return error.format()._errors[0];
6
+ return true;
7
+ };
package/index.js ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env node
2
+
3
+ // Set options as a parameter, environment variable, or rc file.
4
+ // eslint-disable-next-line no-global-assign
5
+ require = require("esm")(module/* , options */)
6
+ module.exports = require("./main.js")
package/main.js ADDED
@@ -0,0 +1,144 @@
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));
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@savaryna/git-add-account",
3
+ "version": "0.0.1-beta",
4
+ "description": "Easily add multiple GIT accounts.",
5
+ "homepage": "https://github.com/savaryna/git-add-account#readme",
6
+ "keywords": [
7
+ "add",
8
+ "multiple",
9
+ "git",
10
+ "account",
11
+ "accounts",
12
+ "on",
13
+ "one",
14
+ "machine",
15
+ "ssh",
16
+ "verified",
17
+ "commit"
18
+ ],
19
+ "author": "Alex Tofan",
20
+ "license": "MIT",
21
+ "bugs": {
22
+ "url": "https://github.com/savaryna/git-add-account/issues"
23
+ },
24
+ "repository": {
25
+ "type": "git",
26
+ "url": "git+https://github.com/savaryna/git-add-account.git"
27
+ },
28
+ "bin": {
29
+ "gac": "index.js",
30
+ "git-add-account": "index.js"
31
+ },
32
+ "main": "index.js",
33
+ "module": "main.js",
34
+ "os": [
35
+ "darwin"
36
+ ],
37
+ "dependencies": {
38
+ "esm": "^3.2.25",
39
+ "prompts": "^2.4.2",
40
+ "zod": "^3.20.6"
41
+ },
42
+ "devDependencies": {
43
+ "@types/node": "^18.14.0",
44
+ "@types/prompts": "^2.4.2"
45
+ }
46
+ }