diff --git a/.gitignore b/.gitignore index f846c68..a25a339 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules package-lock.json yarn.lock +bin diff --git a/bin/index.js b/bin/index.js deleted file mode 100755 index 5a0e190..0000000 --- a/bin/index.js +++ /dev/null @@ -1,5 +0,0 @@ -#! /usr/bin/env node - -const {init} = require('../utils/program'); - -init(); diff --git a/jsconfig.json b/jsconfig.json deleted file mode 100644 index 7a7755e..0000000 --- a/jsconfig.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "compilerOptions": { - "checkJs": true, - "resolveJsonModule": true - }, - "exclude": ["node_modules"] -} \ No newline at end of file diff --git a/package.json b/package.json index 80e4e32..9817af8 100644 --- a/package.json +++ b/package.json @@ -1,26 +1,36 @@ { "name": "git-wiz", - "version": "1.1.1", + "version": "1.1.2", "author": { "email": "moshfeu.dev@gmail.com", "name": "Mosh Feu", "url": "https://github.com/moshfeu" }, + "scripts": { + "build": "webpack" + }, "bin": { "git-wiz": "bin/index.js" }, - "dependencies": { - "commander": "^6.1.0", - "inquirer": "^7.3.3" - }, "files": [ - "bin/index.js", - "utils" + "bin/index.js" ], "repository": { "url": "https://github.com/spread-the-code/git-wiz" }, "homepage": "https://github.com/spread-the-code/git-wiz#readme", "license": "MIT", - "description": "Make git commands interactive" + "description": "Make git commands interactive", + "dependencies": { + "commander": "^6.1.0", + "inquirer": "^7.3.3", + "ts-loader": "^8.0.3", + "typescript": "^4.0.2" + }, + "devDependencies": { + "@types/inquirer": "^7.3.1", + "json-loader": "^0.5.7", + "webpack": "^4.44.1", + "webpack-cli": "^3.3.12" + } } diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..31313cb --- /dev/null +++ b/src/index.ts @@ -0,0 +1,3 @@ +import { init } from './utils/program'; + +init(); \ No newline at end of file diff --git a/src/utils/cli.ts b/src/utils/cli.ts new file mode 100644 index 0000000..2dda441 --- /dev/null +++ b/src/utils/cli.ts @@ -0,0 +1,22 @@ +import inquirer from 'inquirer'; + +export async function showFilesChooser(choices: Array) { + const answer = await inquirer.prompt<{ + files: Array + }>([ + { + type: 'checkbox', + name: 'files', + message: 'What to add?', + choices, + validate: (answer: string) => { + if (answer.length < 1) { + return 'Are you tricking me 🤨? Please choose files to add'; + } + return true; + }, + }, + ]); + + return answer.files; +} diff --git a/utils/exec.js b/src/utils/exec.ts similarity index 59% rename from utils/exec.js rename to src/utils/exec.ts index e3672c9..fb5fe05 100644 --- a/utils/exec.js +++ b/src/utils/exec.ts @@ -1,12 +1,8 @@ -//@ts-check -const cp = require('child_process'); -/** - * @param {string} command - * @returns {Promise} - */ -function execCommand(command) { +import {exec} from 'child_process'; + +export function execCommand(command: string): Promise { return new Promise((resolve, reject) => - cp.exec( + exec( command, { cwd: process.cwd(), @@ -22,5 +18,3 @@ function execCommand(command) { ) ); } - -exports.execCommand = execCommand; diff --git a/utils/git.js b/src/utils/git.ts similarity index 59% rename from utils/git.js rename to src/utils/git.ts index 77a326d..53544cd 100644 --- a/utils/git.js +++ b/src/utils/git.ts @@ -1,19 +1,18 @@ -//@ts-check -const { execCommand } = require('../utils/exec'); +import { execCommand } from './exec'; -/** - * @typedef {{ - * status: 'tracked' | 'staged' | 'untracked' - * path: string - * }} File - * @returns {Promise>} - */ -async function gitStatus() { +type File = { + status: 'tracked' | 'staged' | 'untracked'; + path: string; +}; + +async function runCommand(command: string, files: Array) { + const gitCommand = `git ${command} ${files.join(' ')}`; + await execCommand(gitCommand); + console.log('\x1b[0m', `"${gitCommand}"`, '\x1b[32m', 'did great 🤟'); +} + +export async function gitStatus(): Promise> { const status = await execCommand('git status --porcelain=v2 -uall'); - /** - * @type Array - */ - const initialFiles = []; const files = status .split(/\n/g) .filter((line) => line) @@ -26,51 +25,31 @@ async function gitStatus() { if (stagedIndication !== '.') { prev.push({ status: 'staged', - path + path, }); } if (changedIndication !== '.') { prev.push({ status: 'tracked', - path + path, }); } } else if (index === '?') { prev.push({ status: 'untracked', - path + path, }); } return prev; - }, initialFiles); + }, []); return files; } -/** - * @param {string} command - * @param {Array} files - */ -async function runCommand(command, files) { - const gitCommand = `git ${command} ${files.join(' ')}`; - await execCommand(gitCommand); - console.log('\x1b[0m', `"${gitCommand}"`, '\x1b[32m', 'did great 🤟'); -} - -/** - * @param {Array} files - */ -async function gitAdd(files) { +export async function gitAdd(files: Array) { await runCommand('add', files); } -/** - * @param {Array} files - */ -async function gitReset(files) { +export async function gitReset(files: Array) { await runCommand('reset HEAD -- ', files); } - -exports.gitAdd = gitAdd; -exports.gitReset = gitReset; -exports.gitStatus = gitStatus; diff --git a/utils/program.js b/src/utils/program.ts similarity index 66% rename from utils/program.js rename to src/utils/program.ts index 87d5411..52c0354 100644 --- a/utils/program.js +++ b/src/utils/program.ts @@ -1,8 +1,8 @@ -const { program } = require('commander'); -const { add, reset } = require('./wiz'); -const { version } = require('../package.json'); +import { program } from 'commander'; +import { add, reset } from './wiz'; +import { version } from '../../package.json'; -function init() { +export function init() { try { program.version(version); @@ -21,5 +21,3 @@ function init() { console.log(error); } } - -exports.init = init; diff --git a/utils/wiz.js b/src/utils/wiz.ts similarity index 82% rename from utils/wiz.js rename to src/utils/wiz.ts index 61ee9eb..1c95840 100644 --- a/utils/wiz.js +++ b/src/utils/wiz.ts @@ -1,7 +1,7 @@ -const { showFilesChooser } = require('./cli'); -const { gitStatus, gitAdd, gitReset } = require('./git'); +import { showFilesChooser } from './cli'; +import { gitStatus, gitAdd, gitReset } from './git'; -async function add() { +export async function add() { try { const status = (await gitStatus()).filter( (file) => file.status !== 'staged' @@ -18,7 +18,7 @@ async function add() { } } -async function reset() { +export async function reset() { try { const status = (await gitStatus()).filter( (file) => file.status === 'staged' @@ -37,6 +37,3 @@ async function reset() { console.log('\x1b[31m', 'Oops, something went wrong', err); } } - -exports.add = add; -exports.reset = reset; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..02b20b9 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,16 @@ +{ + "compilerOptions": { + "outDir": "./dist/", + "noImplicitAny": true, + "module": "es6", + "target": "es5", + "jsx": "react", + "allowJs": true, + "allowSyntheticDefaultImports": true, + "moduleResolution": "node", + "resolveJsonModule": true, + "types": [ + "commander" + ] + } +} \ No newline at end of file diff --git a/utils/cli.js b/utils/cli.js deleted file mode 100644 index 5a614c7..0000000 --- a/utils/cli.js +++ /dev/null @@ -1,27 +0,0 @@ -const inquirer = require('inquirer'); -/** - * @param {Array} choices - */ -async function showFilesChooser(choices) { - const answer = await inquirer.prompt([ - { - type: 'checkbox', - name: 'files', - message: 'What to add?', - choices, - /** - * @param {string} answer - */ - validate: (answer) => { - if (answer.length < 1) { - return 'Are you tricking me 🤨? Please choose files to add'; - } - return true; - }, - }, - ]); - - return answer.files; -} - -exports.showFilesChooser = showFilesChooser; \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js new file mode 100644 index 0000000..5aced31 --- /dev/null +++ b/webpack.config.js @@ -0,0 +1,26 @@ +const path = require('path'); +const webpack = require('webpack'); + +module.exports = { + entry: './src/index.ts', + target: 'node', + module: { + rules: [ + { + test: /\.ts$/, + use: 'ts-loader', + exclude: /node_modules/, + }, + ], + }, + resolve: { + extensions: [ '.ts', '.js' ], + }, + output: { + filename: 'index.js', + path: path.resolve(__dirname, 'bin'), + }, + plugins: [ + new webpack.BannerPlugin({ banner: "#!/usr/bin/env node", raw: true }), + ] +};