Skip to content

Commit 6472dcb

Browse files
authored
chore: webpack 🤗 typescript (#1)
1 parent ca51406 commit 6472dcb

13 files changed

+117
-110
lines changed

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules
22
package-lock.json
33
yarn.lock
4+
bin

‎bin/index.js

-5
This file was deleted.

‎jsconfig.json

-7
This file was deleted.

‎package.json

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
{
22
"name": "git-wiz",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"author": {
55
"email": "[email protected]",
66
"name": "Mosh Feu",
77
"url": "https://github.com/moshfeu"
88
},
9+
"scripts": {
10+
"build": "webpack"
11+
},
912
"bin": {
1013
"git-wiz": "bin/index.js"
1114
},
12-
"dependencies": {
13-
"commander": "^6.1.0",
14-
"inquirer": "^7.3.3"
15-
},
1615
"files": [
17-
"bin/index.js",
18-
"utils"
16+
"bin/index.js"
1917
],
2018
"repository": {
2119
"url": "https://github.com/spread-the-code/git-wiz"
2220
},
2321
"homepage": "https://github.com/spread-the-code/git-wiz#readme",
2422
"license": "MIT",
25-
"description": "Make git commands interactive"
23+
"description": "Make git commands interactive",
24+
"dependencies": {
25+
"commander": "^6.1.0",
26+
"inquirer": "^7.3.3",
27+
"ts-loader": "^8.0.3",
28+
"typescript": "^4.0.2"
29+
},
30+
"devDependencies": {
31+
"@types/inquirer": "^7.3.1",
32+
"json-loader": "^0.5.7",
33+
"webpack": "^4.44.1",
34+
"webpack-cli": "^3.3.12"
35+
}
2636
}

‎src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { init } from './utils/program';
2+
3+
init();

‎src/utils/cli.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import inquirer from 'inquirer';
2+
3+
export async function showFilesChooser(choices: Array<string>) {
4+
const answer = await inquirer.prompt<{
5+
files: Array<string>
6+
}>([
7+
{
8+
type: 'checkbox',
9+
name: 'files',
10+
message: 'What to add?',
11+
choices,
12+
validate: (answer: string) => {
13+
if (answer.length < 1) {
14+
return 'Are you tricking me 🤨? Please choose files to add';
15+
}
16+
return true;
17+
},
18+
},
19+
]);
20+
21+
return answer.files;
22+
}
+4-10
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
//@ts-check
2-
const cp = require('child_process');
3-
/**
4-
* @param {string} command
5-
* @returns {Promise<string>}
6-
*/
7-
function execCommand(command) {
1+
import {exec} from 'child_process';
2+
3+
export function execCommand(command: string): Promise<string> {
84
return new Promise((resolve, reject) =>
9-
cp.exec(
5+
exec(
106
command,
117
{
128
cwd: process.cwd(),
@@ -22,5 +18,3 @@ function execCommand(command) {
2218
)
2319
);
2420
}
25-
26-
exports.execCommand = execCommand;
+19-40
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
//@ts-check
2-
const { execCommand } = require('../utils/exec');
1+
import { execCommand } from './exec';
32

4-
/**
5-
* @typedef {{
6-
* status: 'tracked' | 'staged' | 'untracked'
7-
* path: string
8-
* }} File
9-
* @returns {Promise<Array<File>>}
10-
*/
11-
async function gitStatus() {
3+
type File = {
4+
status: 'tracked' | 'staged' | 'untracked';
5+
path: string;
6+
};
7+
8+
async function runCommand(command: string, files: Array<string>) {
9+
const gitCommand = `git ${command} ${files.join(' ')}`;
10+
await execCommand(gitCommand);
11+
console.log('\x1b[0m', `"${gitCommand}"`, '\x1b[32m', 'did great 🤟');
12+
}
13+
14+
export async function gitStatus(): Promise<Array<File>> {
1215
const status = await execCommand('git status --porcelain=v2 -uall');
13-
/**
14-
* @type Array<File>
15-
*/
16-
const initialFiles = [];
1716
const files = status
1817
.split(/\n/g)
1918
.filter((line) => line)
@@ -26,51 +25,31 @@ async function gitStatus() {
2625
if (stagedIndication !== '.') {
2726
prev.push({
2827
status: 'staged',
29-
path
28+
path,
3029
});
3130
}
3231
if (changedIndication !== '.') {
3332
prev.push({
3433
status: 'tracked',
35-
path
34+
path,
3635
});
3736
}
3837
} else if (index === '?') {
3938
prev.push({
4039
status: 'untracked',
41-
path
40+
path,
4241
});
4342
}
4443
return prev;
45-
}, initialFiles);
44+
}, []);
4645

4746
return files;
4847
}
4948

50-
/**
51-
* @param {string} command
52-
* @param {Array<string>} files
53-
*/
54-
async function runCommand(command, files) {
55-
const gitCommand = `git ${command} ${files.join(' ')}`;
56-
await execCommand(gitCommand);
57-
console.log('\x1b[0m', `"${gitCommand}"`, '\x1b[32m', 'did great 🤟');
58-
}
59-
60-
/**
61-
* @param {Array<string>} files
62-
*/
63-
async function gitAdd(files) {
49+
export async function gitAdd(files: Array<string>) {
6450
await runCommand('add', files);
6551
}
6652

67-
/**
68-
* @param {Array<string>} files
69-
*/
70-
async function gitReset(files) {
53+
export async function gitReset(files: Array<string>) {
7154
await runCommand('reset HEAD -- ', files);
7255
}
73-
74-
exports.gitAdd = gitAdd;
75-
exports.gitReset = gitReset;
76-
exports.gitStatus = gitStatus;

‎utils/program.js ‎src/utils/program.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const { program } = require('commander');
2-
const { add, reset } = require('./wiz');
3-
const { version } = require('../package.json');
1+
import { program } from 'commander';
2+
import { add, reset } from './wiz';
3+
import { version } from '../../package.json';
44

5-
function init() {
5+
export function init() {
66
try {
77
program.version(version);
88

@@ -21,5 +21,3 @@ function init() {
2121
console.log(error);
2222
}
2323
}
24-
25-
exports.init = init;

‎utils/wiz.js ‎src/utils/wiz.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
const { showFilesChooser } = require('./cli');
2-
const { gitStatus, gitAdd, gitReset } = require('./git');
1+
import { showFilesChooser } from './cli';
2+
import { gitStatus, gitAdd, gitReset } from './git';
33

4-
async function add() {
4+
export async function add() {
55
try {
66
const status = (await gitStatus()).filter(
77
(file) => file.status !== 'staged'
@@ -18,7 +18,7 @@ async function add() {
1818
}
1919
}
2020

21-
async function reset() {
21+
export async function reset() {
2222
try {
2323
const status = (await gitStatus()).filter(
2424
(file) => file.status === 'staged'
@@ -37,6 +37,3 @@ async function reset() {
3737
console.log('\x1b[31m', 'Oops, something went wrong', err);
3838
}
3939
}
40-
41-
exports.add = add;
42-
exports.reset = reset;

‎tsconfig.json

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"compilerOptions": {
3+
"outDir": "./dist/",
4+
"noImplicitAny": true,
5+
"module": "es6",
6+
"target": "es5",
7+
"jsx": "react",
8+
"allowJs": true,
9+
"allowSyntheticDefaultImports": true,
10+
"moduleResolution": "node",
11+
"resolveJsonModule": true,
12+
"types": [
13+
"commander"
14+
]
15+
}
16+
}

‎utils/cli.js

-27
This file was deleted.

‎webpack.config.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
const path = require('path');
2+
const webpack = require('webpack');
3+
4+
module.exports = {
5+
entry: './src/index.ts',
6+
target: 'node',
7+
module: {
8+
rules: [
9+
{
10+
test: /\.ts$/,
11+
use: 'ts-loader',
12+
exclude: /node_modules/,
13+
},
14+
],
15+
},
16+
resolve: {
17+
extensions: [ '.ts', '.js' ],
18+
},
19+
output: {
20+
filename: 'index.js',
21+
path: path.resolve(__dirname, 'bin'),
22+
},
23+
plugins: [
24+
new webpack.BannerPlugin({ banner: "#!/usr/bin/env node", raw: true }),
25+
]
26+
};

0 commit comments

Comments
 (0)