gitsu-cli 1.0.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.
- package/.github/ninja.svg +1 -0
- package/.github/workflows/build.yml +46 -0
- package/.husky/commit-msg +1 -0
- package/.husky/pre-commit +1 -0
- package/.husky/pre-push +1 -0
- package/.nvmrc +1 -0
- package/LICENSE +21 -0
- package/README.md +63 -0
- package/biome.json +31 -0
- package/lib/package.json +45 -0
- package/lib/src/app.js +26 -0
- package/lib/src/commands/edit.js +22 -0
- package/lib/src/commands/init.js +61 -0
- package/lib/src/commands/list.js +37 -0
- package/lib/src/functions/bootstrap-config.js +47 -0
- package/lib/src/functions/get-current-choice.js +31 -0
- package/lib/src/functions/save-git-config.js +26 -0
- package/lib/src/utils/choice.js +17 -0
- package/lib/src/utils/get-config.js +19 -0
- package/package.json +76 -0
- package/src/app.ts +35 -0
- package/src/commands/edit.ts +26 -0
- package/src/commands/init.ts +59 -0
- package/src/commands/list.ts +30 -0
- package/src/functions/bootstrap-config.ts +49 -0
- package/src/functions/get-current-choice.ts +23 -0
- package/src/functions/save-git-config.ts +17 -0
- package/src/types.d.ts +4 -0
- package/src/utils/choice.ts +17 -0
- package/src/utils/get-config.ts +9 -0
- package/tsconfig.json +101 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<svg viewBox="0 0 36 36" xmlns="http://www.w3.org/2000/svg"><path d="m1.072 17.668 2.048-1.434 6.31 9.011-2.048 1.434z" fill="#42484c"/><path d="m3.897 16.909-2.457 1.721c-.225.158-.539.102-.696-.123-.158-.225-.102-.539.123-.696l2.457-1.721c.225-.158.539-.102.696.123.158.225.103.538-.123.696zm6.555 7.618-4.096 2.868c-.225.158-.539.102-.696-.123-.158-.225-.102-.539.123-.696l4.096-2.868c.225-.158.539-.102.696.123.158.225.102.538-.123.696z" fill="#66757f"/><path d="m6.971 26.966 2.868-2.008 5.161 7.373-2.867 2.008zm-4.752-7.66 2.048-1.434.573.82-2.048 1.433zm1.147 1.638 2.048-1.434.573.82-2.048 1.434zm1.147 1.639 2.048-1.434.574.82-2.048 1.434zm1.147 1.639 2.048-1.434.574.819-2.048 1.434z"/><path d="m23.35 20.14s3.921 3.102 2.223 6.86c.001 0-4.541-2.318-2.223-6.86z" fill="#292f33"/><path d="m25.967 25.593c.191-1.638-.552-3.065-1.296-4.05-.765-.365-1.418-.543-1.667-.602-.04.111-.061.215-.093.324.139 2.639 1.694 3.814 3.056 4.328z"/><g fill="#292f33"><path d="m23 20s4.905.968 5.112 5.086c0 0-5.099.013-5.112-5.086zm9 16v-2c0-3.314-2.685-6-6-6h-16c-3.313 0-6 2.686-6 6v2z"/><path d="m12.799 26.751h10.403v4.249h-10.403z"/><path d="m14 27h8s-1.018 7-4 7-4-7-4-7"/><path d="m13.64 30.038c1.744 1.268 2.848 1.963 4.36 1.963s2.615-.696 4.359-1.963v-5.749h-8.72v5.749z"/></g><path d="m13.632 25.973c1.216 1.374 2.724 1.746 4.364 1.746 1.639 0 3.146-.373 4.363-1.746v-3.491h-8.728v3.491z"/><path d="m11.444 15.936c0 1.448-.734 2.622-1.639 2.622s-1.639-1.174-1.639-2.622.734-2.623 1.639-2.623c.905-.001 1.639 1.174 1.639 2.623m16.389 0c0 1.448-.733 2.622-1.639 2.622-.905 0-1.639-1.174-1.639-2.622s.733-2.623 1.639-2.623c.906-.001 1.639 1.174 1.639 2.623" fill="#292f33"/><path d="m9.477 16.54c0-7.54 2.523-11.54 8.523-11.54s8.522 4 8.522 11.54c0 5.821-3.815 10.54-8.522 10.54s-8.523-4.719-8.523-10.54" fill="#292f33"/><path d="m11 28c0 1 1 3 3 5 1.581 1.581 2.719 3 2.719 3 .797-.005 1.9-.004 2.73-.001-.891-2.02-2.477-3.471-3.279-4.273-1-1-3.17-2.726-3.17-4.726 0-1-2 1-2 1" fill="#66757f"/><path d="m16.719 36 2.724-.001c-.512-1.161-1.251-2.127-1.94-2.897-.505.613-.992 1.346-1.405 2.16.388.449.621.738.621.738z" fill="#292f33"/><path d="m25 28c0 1-1 3-3 5-1.581 1.581-2.5 3-2.5 3-.902-.004-1.418-.002-3 0 .891-2.02 2.528-3.472 3.33-4.274 1-1 3.17-2.726 3.17-4.726 0-1 2 1 2 1" fill="#66757f"/><path d="m18 13.711c-2.995 0-8.116-2.044-7.064 1.98.614 2.348 1.417 2.881 3.475 2.606 2.124-.283 1.991-1.016 3.495-1.016s1.56.733 3.684 1.016c2.058.274 2.861-.258 3.475-2.606 1.051-4.024-4.07-1.98-7.065-1.98z" fill="#ffdc5d"/><path d="m14 17c-.55 0-1-.45-1-1v-1c0-.55.45-1 1-1s1 .45 1 1v1c0 .55-.45 1-1 1m8 0c-.55 0-1-.45-1-1v-1c0-.55.45-1 1-1s1 .45 1 1v1c0 .55-.45 1-1 1" fill="#662113"/></svg>
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
name: build
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
pull_request:
|
|
7
|
+
branches: [main]
|
|
8
|
+
|
|
9
|
+
permissions:
|
|
10
|
+
contents: write
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- name: Checkout
|
|
17
|
+
uses: actions/checkout@v4
|
|
18
|
+
|
|
19
|
+
- name: Use Node.js 22.x
|
|
20
|
+
uses: actions/setup-node@v4
|
|
21
|
+
with:
|
|
22
|
+
node-version: 22
|
|
23
|
+
|
|
24
|
+
- name: Install Dependencies
|
|
25
|
+
run: npm install
|
|
26
|
+
|
|
27
|
+
- name: Build
|
|
28
|
+
run: npm run build
|
|
29
|
+
|
|
30
|
+
- name: Lint
|
|
31
|
+
id: lint
|
|
32
|
+
run: npm run lint
|
|
33
|
+
|
|
34
|
+
release:
|
|
35
|
+
needs: [build]
|
|
36
|
+
runs-on: ubuntu-latest
|
|
37
|
+
steps:
|
|
38
|
+
- name: Checkout
|
|
39
|
+
uses: actions/checkout@v4
|
|
40
|
+
|
|
41
|
+
- name: Semantic Release
|
|
42
|
+
id: release
|
|
43
|
+
uses: cycjimmy/semantic-release-action@v4
|
|
44
|
+
env:
|
|
45
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
46
|
+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npx --no -- commitlint --edit "$1"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npx --no -- lint-staged
|
package/.husky/pre-push
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
npm run lint && npm run build && npm run test
|
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v22
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022-present Jamie Weavis
|
|
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,63 @@
|
|
|
1
|
+
# gitsu <img alt="Ninja Emoji" align="right" width=40 height=40 alt="Screenshot" src="./.github/ninja.svg">
|
|
2
|
+
|
|
3
|
+
> 🥷 Util for quickly & easily switching git users from a config stored at ~/.gitsurc
|
|
4
|
+
|
|
5
|
+
[](https://github.com/jamieweavis/streaker/actions)
|
|
6
|
+
[](https://github.com/jamieweavis/streaker/releases)
|
|
7
|
+
[](https://github.com/jamieweavis/streaker/releases)
|
|
8
|
+
[](https://github.com/jamieweavis/streaker/blob/main/LICENSE)
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```sh
|
|
13
|
+
npm install gitsu-cli -g
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
Open switcher to select a user from your `~/.gitsurc` config
|
|
19
|
+
```sh
|
|
20
|
+
gitsu
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
List users stored in your `~/.gitsurc` config
|
|
24
|
+
```sh
|
|
25
|
+
gitsu --list (or -l)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
Open your `~/.gitsurc` config for editing in your default editor
|
|
29
|
+
```sh
|
|
30
|
+
gitsu --edit (or -e)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Building
|
|
34
|
+
|
|
35
|
+
Compiling a production build
|
|
36
|
+
```sh
|
|
37
|
+
npm run build
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Running a production build
|
|
41
|
+
```sh
|
|
42
|
+
npm start
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Running in development mode
|
|
46
|
+
```sh
|
|
47
|
+
npm run dev
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Linking the package to your global npm modules
|
|
51
|
+
```sh
|
|
52
|
+
npm link
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
Unlinking the package from your global npm modules
|
|
56
|
+
```sh
|
|
57
|
+
npm unlink gitsu-cli -g
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Built With
|
|
61
|
+
|
|
62
|
+
- [Node.js](https://nodejs.org/)
|
|
63
|
+
- [TypeScript](https://www.typescriptlang.org/)
|
package/biome.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://biomejs.dev/schemas/1.9.4/schema.json",
|
|
3
|
+
"vcs": {
|
|
4
|
+
"enabled": false,
|
|
5
|
+
"clientKind": "git",
|
|
6
|
+
"useIgnoreFile": false
|
|
7
|
+
},
|
|
8
|
+
"files": {
|
|
9
|
+
"ignoreUnknown": false,
|
|
10
|
+
"ignore": []
|
|
11
|
+
},
|
|
12
|
+
"formatter": {
|
|
13
|
+
"enabled": true,
|
|
14
|
+
"indentStyle": "space",
|
|
15
|
+
"indentWidth": 2
|
|
16
|
+
},
|
|
17
|
+
"organizeImports": {
|
|
18
|
+
"enabled": true
|
|
19
|
+
},
|
|
20
|
+
"linter": {
|
|
21
|
+
"enabled": true,
|
|
22
|
+
"rules": {
|
|
23
|
+
"recommended": true
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"javascript": {
|
|
27
|
+
"formatter": {
|
|
28
|
+
"quoteStyle": "single"
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
package/lib/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "gitsu-cli",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "Util for quickly & easily switching git users from a config stored at ~/.gitsu",
|
|
5
|
+
"author": "Jamie Weavis",
|
|
6
|
+
"homepage": "https://github.com/jamieweavis/gitsu#readme",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/jamieweavis/gitsu.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/jamieweavis/gitsu/issues"
|
|
14
|
+
},
|
|
15
|
+
"main": "./lib/src/app.js",
|
|
16
|
+
"bin": {
|
|
17
|
+
"gitsu": "./lib/src/app.js"
|
|
18
|
+
},
|
|
19
|
+
"keywords": ["git", "user", "switch", "gitsu", "su", "switch-user"],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"start": "node ./lib/src/app.js",
|
|
22
|
+
"dev": "ts-node-dev --respawn ./src/app.ts",
|
|
23
|
+
"build": "tsc",
|
|
24
|
+
"lint": "biome check src",
|
|
25
|
+
"lint:write": "npm run lint -- --write",
|
|
26
|
+
"format": "biome format src",
|
|
27
|
+
"format:write": "npm run format -- --write"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@types/configstore": "^6.0.2",
|
|
31
|
+
"@types/inquirer": "^9.0.7",
|
|
32
|
+
"chalk": "^5.4.1",
|
|
33
|
+
"commander": "^13.1.0",
|
|
34
|
+
"configstore": "^7.0.0",
|
|
35
|
+
"cosmiconfig": "^8.3.6",
|
|
36
|
+
"inquirer": "^12.5.0",
|
|
37
|
+
"log-symbols": "^7.0.0",
|
|
38
|
+
"typescript": "^5.8.2"
|
|
39
|
+
},
|
|
40
|
+
"devDependencies": {
|
|
41
|
+
"@biomejs/biome": "^1.9.4",
|
|
42
|
+
"ts-node": "^10.9.2",
|
|
43
|
+
"ts-node-dev": "^2.0.0"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/lib/src/app.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
8
|
+
const commander_1 = require("commander");
|
|
9
|
+
const package_json_1 = require("../package.json");
|
|
10
|
+
const edit_1 = __importDefault(require("./commands/edit"));
|
|
11
|
+
const init_1 = __importDefault(require("./commands/init"));
|
|
12
|
+
const list_1 = __importDefault(require("./commands/list"));
|
|
13
|
+
const bootstrap = () => {
|
|
14
|
+
commander_1.program.description(`Util for quickly & easily switching git users from a config stored at ${chalk_1.default.cyan('~/.gitsu')}`);
|
|
15
|
+
commander_1.program.version(package_json_1.version);
|
|
16
|
+
commander_1.program.option('-e, --edit', 'edit the configured list of git users with your preferred EDITOR');
|
|
17
|
+
commander_1.program.option('-l, --list', 'list the current user and configured gitsu users');
|
|
18
|
+
commander_1.program.parse();
|
|
19
|
+
const options = commander_1.program.opts();
|
|
20
|
+
if (options.list)
|
|
21
|
+
return (0, list_1.default)();
|
|
22
|
+
if (options.edit)
|
|
23
|
+
return (0, edit_1.default)();
|
|
24
|
+
return (0, init_1.default)();
|
|
25
|
+
};
|
|
26
|
+
bootstrap();
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const node_child_process_1 = __importDefault(require("node:child_process"));
|
|
7
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
8
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
9
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
10
|
+
const log_symbols_1 = __importDefault(require("log-symbols"));
|
|
11
|
+
exports.default = () => {
|
|
12
|
+
try {
|
|
13
|
+
const editor = process.env.EDITOR || 'vi';
|
|
14
|
+
console.log(`${log_symbols_1.default.info} Opening ${chalk_1.default.cyan('~/.gitsurc')} with ${chalk_1.default.cyan(editor)}`);
|
|
15
|
+
node_child_process_1.default.spawn(editor, [node_path_1.default.join(node_os_1.default.homedir(), '.gitsurc')], {
|
|
16
|
+
stdio: 'inherit',
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
catch (error) {
|
|
20
|
+
console.log(`${log_symbols_1.default.error} ${chalk_1.default.red('Failed to open ~/.gitsurc')}`, error);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
16
|
+
const inquirer_1 = __importDefault(require("inquirer"));
|
|
17
|
+
const log_symbols_1 = __importDefault(require("log-symbols"));
|
|
18
|
+
const bootstrap_config_1 = __importDefault(require("../functions/bootstrap-config"));
|
|
19
|
+
const get_current_choice_1 = __importDefault(require("../functions/get-current-choice"));
|
|
20
|
+
const save_git_config_1 = __importDefault(require("../functions/save-git-config"));
|
|
21
|
+
const choice_1 = require("../utils/choice");
|
|
22
|
+
const get_config_1 = __importDefault(require("../utils/get-config"));
|
|
23
|
+
exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
+
// Ensure ~/.gitsurc config exists
|
|
25
|
+
yield (0, bootstrap_config_1.default)();
|
|
26
|
+
// Fetch configured gitsu users from config
|
|
27
|
+
const configResult = yield (0, get_config_1.default)();
|
|
28
|
+
const users = configResult.config;
|
|
29
|
+
// Prompt for new user defaulting to the next choice in the list (maybe in the future we can sort by most recently used)
|
|
30
|
+
let answers;
|
|
31
|
+
try {
|
|
32
|
+
const currentChoice = yield (0, get_current_choice_1.default)();
|
|
33
|
+
answers = yield inquirer_1.default.prompt([
|
|
34
|
+
{
|
|
35
|
+
type: 'list',
|
|
36
|
+
name: 'user',
|
|
37
|
+
message: 'Which user would you like to use?',
|
|
38
|
+
choices: () => users.map((user) => (0, choice_1.formatChoice)(user)),
|
|
39
|
+
default: (0, choice_1.defaultChoice)(users, currentChoice || users[0]),
|
|
40
|
+
},
|
|
41
|
+
]);
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
console.log(`\n${log_symbols_1.default.error} ${chalk_1.default.red('Switch aborted by user')}`);
|
|
45
|
+
}
|
|
46
|
+
// Save new user to git config
|
|
47
|
+
try {
|
|
48
|
+
if (!answers) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const user = (0, choice_1.parseChoice)(answers.user);
|
|
52
|
+
const saved = yield (0, save_git_config_1.default)(user);
|
|
53
|
+
if (!saved) {
|
|
54
|
+
throw new Error('Failed to save git config');
|
|
55
|
+
}
|
|
56
|
+
console.log(`${log_symbols_1.default.success} Switched to user ${chalk_1.default.cyan((0, choice_1.formatChoice)(user))}`);
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
console.log(`${log_symbols_1.default.error} ${chalk_1.default.red('Failed to switch user')}`, error);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
16
|
+
const log_symbols_1 = __importDefault(require("log-symbols"));
|
|
17
|
+
const get_current_choice_1 = __importDefault(require("../functions/get-current-choice"));
|
|
18
|
+
const choice_1 = require("../utils/choice");
|
|
19
|
+
const get_config_1 = __importDefault(require("../utils/get-config"));
|
|
20
|
+
exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
21
|
+
try {
|
|
22
|
+
const configResult = yield (0, get_config_1.default)();
|
|
23
|
+
const users = configResult.config;
|
|
24
|
+
const currentChoice = yield (0, get_current_choice_1.default)();
|
|
25
|
+
users.map((user) => {
|
|
26
|
+
if (currentChoice === (0, choice_1.formatChoice)(user)) {
|
|
27
|
+
console.log(`${log_symbols_1.default.success} ${(0, choice_1.formatChoice)(user)}`);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
console.log(` ${chalk_1.default.grey((0, choice_1.formatChoice)(user))}`);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
console.log(`${log_symbols_1.default.error} ${chalk_1.default.red('Failed to list configured gitsu users')}`, error);
|
|
36
|
+
}
|
|
37
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
16
|
+
const log_symbols_1 = __importDefault(require("log-symbols"));
|
|
17
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
18
|
+
const node_os_1 = __importDefault(require("node:os"));
|
|
19
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
20
|
+
const choice_1 = require("../utils/choice");
|
|
21
|
+
const get_config_1 = __importDefault(require("../utils/get-config"));
|
|
22
|
+
const get_current_choice_1 = __importDefault(require("./get-current-choice"));
|
|
23
|
+
exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
24
|
+
try {
|
|
25
|
+
yield (0, get_config_1.default)();
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
28
|
+
catch (e) {
|
|
29
|
+
try {
|
|
30
|
+
const currentChoice = yield (0, get_current_choice_1.default)();
|
|
31
|
+
const user = (0, choice_1.parseChoice)(currentChoice);
|
|
32
|
+
const configPath = node_path_1.default.join(node_os_1.default.homedir(), '/', '.gitsurc');
|
|
33
|
+
node_fs_1.default.writeFileSync(configPath, JSON.stringify([
|
|
34
|
+
{
|
|
35
|
+
name: user.name,
|
|
36
|
+
email: user.email,
|
|
37
|
+
},
|
|
38
|
+
]));
|
|
39
|
+
console.info(`${log_symbols_1.default.info} Created initial ${chalk_1.default.cyan('~/.gitsu')} with user ${chalk_1.default.cyan(currentChoice)}`);
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
catch (error) {
|
|
43
|
+
console.error(`${log_symbols_1.default.error} ${chalk_1.default.red('Failed to create ~/.gitsu config file')}`, error);
|
|
44
|
+
return false;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
});
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const node_child_process_1 = __importDefault(require("node:child_process"));
|
|
16
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
17
|
+
const log_symbols_1 = __importDefault(require("log-symbols"));
|
|
18
|
+
const choice_1 = require("../utils/choice");
|
|
19
|
+
exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
try {
|
|
21
|
+
const gitUserEmail = yield node_child_process_1.default.execSync('git config user.email');
|
|
22
|
+
const email = gitUserEmail.toString().trim();
|
|
23
|
+
const gitUserName = yield node_child_process_1.default.execSync('git config user.name');
|
|
24
|
+
const name = gitUserName.toString().trim();
|
|
25
|
+
return (0, choice_1.formatChoice)({ name, email });
|
|
26
|
+
}
|
|
27
|
+
catch (error) {
|
|
28
|
+
console.error(`${log_symbols_1.default.error} ${chalk_1.default.red('Failed to fetch current git user')}`, error);
|
|
29
|
+
return '';
|
|
30
|
+
}
|
|
31
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const node_child_process_1 = __importDefault(require("node:child_process"));
|
|
16
|
+
const log_symbols_1 = __importDefault(require("log-symbols"));
|
|
17
|
+
exports.default = (user) => __awaiter(void 0, void 0, void 0, function* () {
|
|
18
|
+
try {
|
|
19
|
+
yield node_child_process_1.default.exec(`git config user.name "${user.name}" && git config user.email "${user.email}"`);
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
console.error(`${log_symbols_1.default.error} Failed to save git config`, error);
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.defaultChoice = exports.parseChoice = exports.formatChoice = void 0;
|
|
4
|
+
const formatChoice = (user) => `${user.name} <${user.email}>`;
|
|
5
|
+
exports.formatChoice = formatChoice;
|
|
6
|
+
const parseChoice = (choice) => {
|
|
7
|
+
const [name, email] = choice.split('<');
|
|
8
|
+
return { name: name.trim(), email: email.slice(0, -1).trim() };
|
|
9
|
+
};
|
|
10
|
+
exports.parseChoice = parseChoice;
|
|
11
|
+
const defaultChoice = (users, currentChoice) => {
|
|
12
|
+
const firstNotCurrentUser = users.find((user) => (0, exports.formatChoice)(user) !== currentChoice);
|
|
13
|
+
if (!firstNotCurrentUser)
|
|
14
|
+
return currentChoice;
|
|
15
|
+
return `${firstNotCurrentUser.name} <${firstNotCurrentUser.email}>`;
|
|
16
|
+
};
|
|
17
|
+
exports.defaultChoice = defaultChoice;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const cosmiconfig_1 = require("cosmiconfig");
|
|
13
|
+
exports.default = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
14
|
+
const explorer = (0, cosmiconfig_1.cosmiconfig)('gitsu');
|
|
15
|
+
const result = yield explorer.search();
|
|
16
|
+
if (!result)
|
|
17
|
+
throw new Error('Failed to find ~/.gitsu configuration file');
|
|
18
|
+
return result;
|
|
19
|
+
});
|
package/package.json
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "gitsu-cli",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Util for quickly & easily switching git users from a config stored at ~/.gitsurc",
|
|
5
|
+
"author": "Jamie Weavis",
|
|
6
|
+
"homepage": "https://github.com/jamieweavis/gitsu#readme",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/jamieweavis/gitsu.git"
|
|
11
|
+
},
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/jamieweavis/gitsu/issues"
|
|
14
|
+
},
|
|
15
|
+
"main": "./lib/src/app.js",
|
|
16
|
+
"bin": {
|
|
17
|
+
"gitsu": "./lib/src/app.js"
|
|
18
|
+
},
|
|
19
|
+
"keywords": ["git", "user", "switch", "gitsu", "su", "switch-user"],
|
|
20
|
+
"scripts": {
|
|
21
|
+
"start": "node ./lib/src/app.js",
|
|
22
|
+
"dev": "ts-node-dev --respawn ./src/app.ts",
|
|
23
|
+
"build": "tsc",
|
|
24
|
+
"lint": "biome check src",
|
|
25
|
+
"lint:write": "npm run lint -- --write",
|
|
26
|
+
"format": "biome format src",
|
|
27
|
+
"format:write": "npm run format -- --write",
|
|
28
|
+
"prepare": "husky"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"@types/configstore": "^6.0.2",
|
|
32
|
+
"@types/inquirer": "^9.0.7",
|
|
33
|
+
"chalk": "^5.4.1",
|
|
34
|
+
"commander": "^13.1.0",
|
|
35
|
+
"configstore": "^7.0.0",
|
|
36
|
+
"cosmiconfig": "^8.3.6",
|
|
37
|
+
"inquirer": "^12.5.0",
|
|
38
|
+
"log-symbols": "^7.0.0",
|
|
39
|
+
"typescript": "^5.8.2"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@biomejs/biome": "^1.9.4",
|
|
43
|
+
"@commitlint/cli": "^19.8.0",
|
|
44
|
+
"@commitlint/config-conventional": "^19.8.0",
|
|
45
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
46
|
+
"@semantic-release/commit-analyzer": "^13.0.1",
|
|
47
|
+
"@semantic-release/git": "^10.0.1",
|
|
48
|
+
"@semantic-release/github": "^11.0.1",
|
|
49
|
+
"@semantic-release/npm": "^12.0.1",
|
|
50
|
+
"@semantic-release/release-notes-generator": "^14.0.3",
|
|
51
|
+
"commitizen": "^4.3.1",
|
|
52
|
+
"cz-conventional-changelog": "^3.3.0",
|
|
53
|
+
"husky": "^9.1.7",
|
|
54
|
+
"lint-staged": "^15.5.0",
|
|
55
|
+
"semantic-release": "^24.2.3",
|
|
56
|
+
"ts-node": "^10.9.2",
|
|
57
|
+
"ts-node-dev": "^2.0.0"
|
|
58
|
+
},
|
|
59
|
+
"release": {
|
|
60
|
+
"branches": ["main"],
|
|
61
|
+
"plugins": [
|
|
62
|
+
"@semantic-release/changelog",
|
|
63
|
+
"@semantic-release/commit-analyzer",
|
|
64
|
+
"@semantic-release/npm",
|
|
65
|
+
"@semantic-release/git",
|
|
66
|
+
"@semantic-release/github",
|
|
67
|
+
"@semantic-release/release-notes-generator"
|
|
68
|
+
]
|
|
69
|
+
},
|
|
70
|
+
"lint-staged": {
|
|
71
|
+
"*.ts": ["biome format --write", "biome lint --write"]
|
|
72
|
+
},
|
|
73
|
+
"commitlint": {
|
|
74
|
+
"extends": ["@commitlint/config-conventional"]
|
|
75
|
+
}
|
|
76
|
+
}
|
package/src/app.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import chalk from 'chalk';
|
|
4
|
+
import { program } from 'commander';
|
|
5
|
+
|
|
6
|
+
import { version } from '../package.json';
|
|
7
|
+
import edit from './commands/edit';
|
|
8
|
+
import init from './commands/init';
|
|
9
|
+
import list from './commands/list';
|
|
10
|
+
|
|
11
|
+
const bootstrap = () => {
|
|
12
|
+
program.description(
|
|
13
|
+
`Util for quickly & easily switching git users from a config stored at ${chalk.cyan(
|
|
14
|
+
'~/.gitsurc',
|
|
15
|
+
)}`,
|
|
16
|
+
);
|
|
17
|
+
program.version(version);
|
|
18
|
+
program.option(
|
|
19
|
+
'-e, --edit',
|
|
20
|
+
'edit the configured list of git users with your preferred EDITOR',
|
|
21
|
+
);
|
|
22
|
+
program.option(
|
|
23
|
+
'-l, --list',
|
|
24
|
+
'list the current user and configured gitsu users',
|
|
25
|
+
);
|
|
26
|
+
program.parse();
|
|
27
|
+
|
|
28
|
+
const options = program.opts();
|
|
29
|
+
if (options.list) return list();
|
|
30
|
+
if (options.edit) return edit();
|
|
31
|
+
|
|
32
|
+
return init();
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
bootstrap();
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import child_process from 'node:child_process';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import logSymbols from 'log-symbols';
|
|
6
|
+
|
|
7
|
+
export default () => {
|
|
8
|
+
try {
|
|
9
|
+
const editor = process.env.EDITOR || 'vi';
|
|
10
|
+
|
|
11
|
+
console.log(
|
|
12
|
+
`${logSymbols.info} Opening ${chalk.cyan('~/.gitsurc')} with ${chalk.cyan(
|
|
13
|
+
editor,
|
|
14
|
+
)}`,
|
|
15
|
+
);
|
|
16
|
+
|
|
17
|
+
child_process.spawn(editor, [path.join(os.homedir(), '.gitsurc')], {
|
|
18
|
+
stdio: 'inherit',
|
|
19
|
+
});
|
|
20
|
+
} catch (error) {
|
|
21
|
+
console.log(
|
|
22
|
+
`${logSymbols.error} ${chalk.red('Failed to open ~/.gitsurc')}`,
|
|
23
|
+
error,
|
|
24
|
+
);
|
|
25
|
+
}
|
|
26
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import inquirer from 'inquirer';
|
|
3
|
+
import logSymbols from 'log-symbols';
|
|
4
|
+
|
|
5
|
+
import bootstrapConfig from '../functions/bootstrap-config';
|
|
6
|
+
import getCurrentChoice from '../functions/get-current-choice';
|
|
7
|
+
import saveGitConfig from '../functions/save-git-config';
|
|
8
|
+
import type { IUser } from '../types';
|
|
9
|
+
import { defaultChoice, formatChoice, parseChoice } from '../utils/choice';
|
|
10
|
+
import getConfig from '../utils/get-config';
|
|
11
|
+
|
|
12
|
+
export default async () => {
|
|
13
|
+
// Ensure ~/.gitsurc config exists
|
|
14
|
+
await bootstrapConfig();
|
|
15
|
+
|
|
16
|
+
// Fetch configured gitsu users from config
|
|
17
|
+
const configResult = await getConfig();
|
|
18
|
+
const users = configResult.config;
|
|
19
|
+
|
|
20
|
+
// Prompt for new user defaulting to the next choice in the list (maybe in the future we can sort by most recently used)
|
|
21
|
+
let answers: { user: string } | undefined;
|
|
22
|
+
try {
|
|
23
|
+
const currentChoice = await getCurrentChoice();
|
|
24
|
+
answers = await inquirer.prompt([
|
|
25
|
+
{
|
|
26
|
+
type: 'list',
|
|
27
|
+
name: 'user',
|
|
28
|
+
message: 'Which user would you like to use?',
|
|
29
|
+
choices: () => users.map((user: IUser) => formatChoice(user)),
|
|
30
|
+
default: defaultChoice(users, currentChoice || users[0]),
|
|
31
|
+
},
|
|
32
|
+
]);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
console.log(`\n${logSymbols.error} ${chalk.red('Switch aborted by user')}`);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Save new user to git config
|
|
38
|
+
try {
|
|
39
|
+
if (!answers) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const user = parseChoice(answers.user);
|
|
43
|
+
const saved = await saveGitConfig(user);
|
|
44
|
+
if (!saved) {
|
|
45
|
+
throw new Error('Failed to save git config');
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
console.log(
|
|
49
|
+
`${logSymbols.success} Switched to user ${chalk.cyan(
|
|
50
|
+
formatChoice(user),
|
|
51
|
+
)}`,
|
|
52
|
+
);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
console.log(
|
|
55
|
+
`${logSymbols.error} ${chalk.red('Failed to switch user')}`,
|
|
56
|
+
error,
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import logSymbols from 'log-symbols';
|
|
3
|
+
|
|
4
|
+
import getCurrentChoice from '../functions/get-current-choice';
|
|
5
|
+
import type { IUser } from '../types';
|
|
6
|
+
import { formatChoice } from '../utils/choice';
|
|
7
|
+
import getConfig from '../utils/get-config';
|
|
8
|
+
|
|
9
|
+
export default async () => {
|
|
10
|
+
try {
|
|
11
|
+
const configResult = await getConfig();
|
|
12
|
+
const users = configResult.config;
|
|
13
|
+
const currentChoice = await getCurrentChoice();
|
|
14
|
+
|
|
15
|
+
users.map((user: IUser) => {
|
|
16
|
+
if (currentChoice === formatChoice(user)) {
|
|
17
|
+
console.log(`${logSymbols.success} ${formatChoice(user)}`);
|
|
18
|
+
} else {
|
|
19
|
+
console.log(` ${chalk.grey(formatChoice(user))}`);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
} catch (error) {
|
|
23
|
+
console.log(
|
|
24
|
+
`${logSymbols.error} ${chalk.red(
|
|
25
|
+
'Failed to list configured gitsu users',
|
|
26
|
+
)}`,
|
|
27
|
+
error,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import os from 'node:os';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import logSymbols from 'log-symbols';
|
|
6
|
+
|
|
7
|
+
import { parseChoice } from '../utils/choice';
|
|
8
|
+
import getConfig from '../utils/get-config';
|
|
9
|
+
import getCurrentChoice from './get-current-choice';
|
|
10
|
+
|
|
11
|
+
export default async (): Promise<boolean> => {
|
|
12
|
+
try {
|
|
13
|
+
await getConfig();
|
|
14
|
+
|
|
15
|
+
return true;
|
|
16
|
+
} catch (e) {
|
|
17
|
+
try {
|
|
18
|
+
const currentChoice = await getCurrentChoice();
|
|
19
|
+
const user = parseChoice(currentChoice);
|
|
20
|
+
const configPath = path.join(os.homedir(), '/', '.gitsurc');
|
|
21
|
+
|
|
22
|
+
fs.writeFileSync(
|
|
23
|
+
configPath,
|
|
24
|
+
JSON.stringify([
|
|
25
|
+
{
|
|
26
|
+
name: user.name,
|
|
27
|
+
email: user.email,
|
|
28
|
+
},
|
|
29
|
+
]),
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
console.info(
|
|
33
|
+
`${logSymbols.info} Created initial ${chalk.cyan(
|
|
34
|
+
'~/.gitsurc',
|
|
35
|
+
)} with user ${chalk.cyan(currentChoice)}`,
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
return true;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.error(
|
|
41
|
+
`${logSymbols.error} ${chalk.red(
|
|
42
|
+
'Failed to create ~/.gitsurc config file',
|
|
43
|
+
)}`,
|
|
44
|
+
error,
|
|
45
|
+
);
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import child_process from 'node:child_process';
|
|
2
|
+
import chalk from 'chalk';
|
|
3
|
+
import logSymbols from 'log-symbols';
|
|
4
|
+
|
|
5
|
+
import { formatChoice } from '../utils/choice';
|
|
6
|
+
|
|
7
|
+
export default async (): Promise<string> => {
|
|
8
|
+
try {
|
|
9
|
+
const gitUserEmail = await child_process.execSync('git config user.email');
|
|
10
|
+
const email = gitUserEmail.toString().trim();
|
|
11
|
+
|
|
12
|
+
const gitUserName = await child_process.execSync('git config user.name');
|
|
13
|
+
const name = gitUserName.toString().trim();
|
|
14
|
+
|
|
15
|
+
return formatChoice({ name, email });
|
|
16
|
+
} catch (error) {
|
|
17
|
+
console.error(
|
|
18
|
+
`${logSymbols.error} ${chalk.red('Failed to fetch current git user')}`,
|
|
19
|
+
error,
|
|
20
|
+
);
|
|
21
|
+
return '';
|
|
22
|
+
}
|
|
23
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import child_process from 'node:child_process';
|
|
2
|
+
import logSymbols from 'log-symbols';
|
|
3
|
+
|
|
4
|
+
import type { IUser } from '../types';
|
|
5
|
+
|
|
6
|
+
export default async (user: IUser): Promise<boolean> => {
|
|
7
|
+
try {
|
|
8
|
+
await child_process.exec(
|
|
9
|
+
`git config user.name "${user.name}" && git config user.email "${user.email}"`,
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
return true;
|
|
13
|
+
} catch (error) {
|
|
14
|
+
console.error(`${logSymbols.error} Failed to save git config`, error);
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
};
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { IUser } from '../types';
|
|
2
|
+
|
|
3
|
+
export const formatChoice = (user: IUser): string =>
|
|
4
|
+
`${user.name} <${user.email}>`;
|
|
5
|
+
|
|
6
|
+
export const parseChoice = (choice: string): IUser => {
|
|
7
|
+
const [name, email] = choice.split('<');
|
|
8
|
+
return { name: name.trim(), email: email.slice(0, -1).trim() };
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export const defaultChoice = (users: IUser[], currentChoice: string) => {
|
|
12
|
+
const firstNotCurrentUser = users.find(
|
|
13
|
+
(user: IUser) => formatChoice(user) !== currentChoice,
|
|
14
|
+
);
|
|
15
|
+
if (!firstNotCurrentUser) return currentChoice;
|
|
16
|
+
return `${firstNotCurrentUser.name} <${firstNotCurrentUser.email}>`;
|
|
17
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { cosmiconfig } from 'cosmiconfig';
|
|
2
|
+
import { name } from '../../package.json';
|
|
3
|
+
|
|
4
|
+
export default async () => {
|
|
5
|
+
const explorer = cosmiconfig('gitsu');
|
|
6
|
+
const result = await explorer.search();
|
|
7
|
+
if (!result) throw new Error('Failed to find ~/.gitsurc configuration file');
|
|
8
|
+
return result;
|
|
9
|
+
};
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig.json to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Enable incremental compilation */
|
|
7
|
+
// "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
|
|
8
|
+
// "tsBuildInfoFile": "./", /* Specify the folder for .tsbuildinfo incremental compilation files. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for TC39 stage 2 draft decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h' */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using `jsx: react-jsx*`.` */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for `createElement`. This only applies when targeting `react` JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
|
|
26
|
+
/* Modules */
|
|
27
|
+
"module": "commonjs" /* Specify what module code is generated. */,
|
|
28
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
29
|
+
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
|
|
30
|
+
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
|
|
31
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
32
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
33
|
+
// "typeRoots": [], /* Specify multiple folders that act like `./node_modules/@types`. */
|
|
34
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
35
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
36
|
+
"resolveJsonModule": true /* Enable importing .json files */,
|
|
37
|
+
// "noResolve": true, /* Disallow `import`s, `require`s or `<reference>`s from expanding the number of files TypeScript should add to a project. */
|
|
38
|
+
|
|
39
|
+
/* JavaScript Support */
|
|
40
|
+
// "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the `checkJS` option to get errors from these files. */
|
|
41
|
+
// "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
|
|
42
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from `node_modules`. Only applicable with `allowJs`. */
|
|
43
|
+
|
|
44
|
+
/* Emit */
|
|
45
|
+
// "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
|
|
46
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
47
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
48
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
49
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If `declaration` is true, also designates a file that bundles all .d.ts output. */
|
|
50
|
+
"outDir": "./lib" /* Specify an output folder for all emitted files. */,
|
|
51
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
52
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
53
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
54
|
+
// "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types */
|
|
55
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
56
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
57
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
58
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
59
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
60
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
61
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
62
|
+
// "stripInternal": true, /* Disable emitting declarations that have `@internal` in their JSDoc comments. */
|
|
63
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like `__extends` in compiled output. */
|
|
64
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
65
|
+
// "preserveConstEnums": true, /* Disable erasing `const enum` declarations in generated code. */
|
|
66
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
67
|
+
// "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
|
|
68
|
+
|
|
69
|
+
/* Interop Constraints */
|
|
70
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
71
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
72
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables `allowSyntheticDefaultImports` for type compatibility. */,
|
|
73
|
+
// "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
|
|
74
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
75
|
+
|
|
76
|
+
/* Type Checking */
|
|
77
|
+
"strict": true /* Enable all strict type-checking options. */,
|
|
78
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied `any` type.. */
|
|
79
|
+
// "strictNullChecks": true, /* When type checking, take into account `null` and `undefined`. */
|
|
80
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
81
|
+
// "strictBindCallApply": true, /* Check that the arguments for `bind`, `call`, and `apply` methods match the original function. */
|
|
82
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
83
|
+
// "noImplicitThis": true, /* Enable error reporting when `this` is given the type `any`. */
|
|
84
|
+
// "useUnknownInCatchVariables": true, /* Type catch clause variables as 'unknown' instead of 'any'. */
|
|
85
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
86
|
+
// "noUnusedLocals": true, /* Enable error reporting when a local variables aren't read. */
|
|
87
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read */
|
|
88
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
89
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
90
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
91
|
+
// "noUncheckedIndexedAccess": true, /* Include 'undefined' in index signature results */
|
|
92
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
93
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type */
|
|
94
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
95
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
96
|
+
|
|
97
|
+
/* Completeness */
|
|
98
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
99
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
100
|
+
}
|
|
101
|
+
}
|