Skip to content

Commit

Permalink
1.0.1
Browse files Browse the repository at this point in the history
- cleans up code
- adds key to help menu
  • Loading branch information
gabrielcsapo committed Sep 3, 2018
1 parent a74dd59 commit f736750
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 154 deletions.
2 changes: 0 additions & 2 deletions .eslintignore

This file was deleted.

15 changes: 0 additions & 15 deletions .eslintrc

This file was deleted.

5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 1.0.1 (09/02/2018)

- cleans up code
- adds key to help menu

# 1.0.0 (12/18/2017)

- removes commander
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ npm install git-unstaged -g
```
Usage: git-unstaged [options]
Key:
M -> modified
A -> added
D -> deleted
R -> renamed
C -> copied
?? -> untracked
Commands:
-h, --help, help Output usage information
-v, --version, version Output the version number
Expand Down
1 change: 0 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
- [ ] remove sync operations
99 changes: 43 additions & 56 deletions bin/git-unstaged.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,38 @@
#!/usr/bin/env node

const path = require('path');
const chalk = require('chalk');
const { search } = require('../lib/search');
const path = require('path')
const chalk = require('chalk')
const { search } = require('../lib/search')
const { colorize } = require('../lib/utils')

const log = console.log; // eslint-disable-line

const args = process.argv.slice(2);
let program = {};
const args = process.argv.slice(2)
let program = {}

args.forEach((arg, i) => {
switch (arg) {
case '-v':
case '--version':
case 'version':
console.log(`v${require('../package.json').version}`); // eslint-disable-line
process.exit(0);
break;
process.exit(0)
case '-h':
case '--help':
case 'help':
console.log(`` + // eslint-disable-line
`
Usage: git-unstaged [options]
Key:
${colorize('M -> modified')}
${colorize('A -> added')}
${colorize('D -> deleted')}
${colorize('R -> renamed')}
${colorize('C -> copied')}
${colorize('?? -> untracked')}
Commands:
-h, --help, help Output usage information
-v, --version, version Output the version number
Expand All @@ -31,62 +41,39 @@ Options:
-b, --baseDirectory [path] The base directory where operations should start from
-d, --depth [value] The specified depth you want to recursively search for git repos
-a, --all Show all git repos and their status
`);
process.exit(0);
break;
`)
process.exit(0)
case '-d':
case '--depth':
program['depth'] = args[i + 1];
break;
program['depth'] = args[i + 1]
break
case '-di':
case '--directory':
program['directory'] = path.resolve(process.cwd(), args[i + 1]);
break;
program['directory'] = path.resolve(process.cwd(), args[i + 1])
break
}
});
})

const { depth=0, all=true } = program;
const { depth = 0, all = true } = program;

(async function() {
const output = await search(process.cwd(), depth);
(async function () {
const output = await search(process.cwd(), depth)

Object.keys(output).forEach((repo) => {
const lines = output[repo].split('\n');

if(all && lines.length <= 2) {
log(chalk.white.underline(repo));
log(output[repo]);
} else if(lines.length > 2) {
log(chalk.white.underline(repo));
lines.forEach((value, index) => {
if(index == 0) {
return log(chalk.white(value));
} else {
// 'M' -> 'modified'
// 'A ' -> 'added'
// 'D' -> 'deleted'
// 'R' -> 'renamed'
// 'C' -> 'copied'
// '??' -> 'untracked'
const lines = output[repo].split('\n')

switch(value.substring(0, 2).trim()) {
case 'M':
return log(chalk.red(value));
case 'A':
return log(chalk.green(value));
case 'C':
return log(chalk.yellow(value));
case 'R':
return log(chalk.magenta(value));
case 'D':
return log(chalk.bgRed.white(value));
case '??':
return log(chalk.gray(value));
default:
return log(chalk.white(value));
}
}
});
}
});
}());
if (all && lines.length <= 2) {
log(chalk.white.underline(repo))
log(output[repo])
} else if (lines.length > 2) {
log(chalk.white.underline(repo))
lines.forEach((value, index) => {
if (index === 0) {
return log(chalk.white(value))
} else {
return log(colorize(value))
}
})
}
})
}())
63 changes: 14 additions & 49 deletions lib/search.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,33 @@
const fs = require('fs');
const path = require('path');
const child_process = require('child_process');
const { promisify } = require('util');
const path = require('path')
const { getContents, getDirectories } = require('./utils')

const exec = promisify(child_process.exec);
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);

async function getContents(directory) {
try {
const git = await stat(path.resolve(directory, '.git'));
if (git.isDirectory()) {
const { stdout } = await exec('git status --porcelain --branch', {
cwd: path.resolve(directory)
});
return stdout;
}
} catch (ex) {
return;
}
}

async function getDirectories(directory) {
try {
let found = [];
const files = await readdir(directory);
for(var i = 0; i < files.length; i++) {
const dir = await stat(path.resolve(directory, files[i]));
if(dir.isDirectory()) {
found.push(path.resolve(directory, files[i]));
}
}
return found;
} catch(ex) {
return [];
}
}

async function search(directory, depth) {
let found = [[directory]];
let output = {};
async function search (directory, depth) {
const found = [[directory]]
const output = {}

for (var i = 0; i <= depth; i++) {
for (var s = 0; s <= found[i].length - 1; s++) {
if (i < depth) {
found.push(await getDirectories(found[i][s]));
found.push(await getDirectories(found[i][s]))
}
}
}

let all = [].concat.apply([], found);
for(var p = 0; p < all.length; p++) {
const dir = all[p];
const contents = await getContents(path.resolve(directory, dir));
const all = [].concat.apply([], found)

for (const dir of all) {
const contents = await getContents(path.resolve(directory, dir))

if (contents) {
output[path.resolve(directory, dir)] = contents;
output[path.resolve(directory, dir)] = contents
}
}

return output;
return output
}

module.exports = {
search,
getDirectories,
getContents
};
}
76 changes: 76 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
const fs = require('fs')
const path = require('path')
const chalk = require('chalk')
const childProcess = require('child_process')
const { promisify } = require('util')
const debug = require('debug')

const exec = promisify(childProcess.exec)
const readdir = promisify(fs.readdir)
const stat = promisify(fs.stat)

async function getContents (directory) {
try {
const git = await stat(path.resolve(directory, '.git'))

if (git.isDirectory()) {
const { stdout } = await exec('git status --porcelain --branch', {
cwd: path.resolve(directory)
})
return stdout
}
} catch (ex) {
debug('failed to get git output', ex)
}
}

async function getDirectories (directory) {
const found = []

try {
const files = await readdir(directory)

for (const file of files) {
const dir = await stat(path.resolve(directory, file))
if (dir.isDirectory()) {
found.push(path.resolve(directory, file))
}
}
} catch (ex) {
debug('failed to parse directory', ex)
}

return found
}

function colorize (line) {
// 'M' -> 'modified'
// 'A ' -> 'added'
// 'D' -> 'deleted'
// 'R' -> 'renamed'
// 'C' -> 'copied'
// '??' -> 'untracked'

switch (line.substring(0, 2).trim()) {
case 'M':
return chalk.red(line)
case 'A':
return chalk.green(line)
case 'C':
return chalk.yellow(line)
case 'R':
return chalk.magenta(line)
case 'D':
return chalk.bgRed.white(line)
case '??':
return chalk.gray(line)
default:
return chalk.white(line)
}
}

module.exports = {
colorize,
getContents,
getDirectories
}
13 changes: 10 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "git-unstaged",
"version": "1.0.0",
"version": "1.0.1",
"description": "🎭 Get all unstaged git repos in a folder",
"author": "Gabriel J. Csapo <gabecsapo@gmail.com>",
"license": "MIT",
Expand All @@ -23,16 +23,23 @@
"node": ">= 8"
},
"scripts": {
"lint": "eslint .",
"lint": "standard --verbose",
"test": "tape test/*.js",
"coverage": "tap test/*.js --coverage --coverage-report=lcov",
"generate-docs": "tryitout"
},
"standard": {
"ignore": [
"docs/**",
"example/**",
"coverage/**"
]
},
"dependencies": {
"chalk": "^2.3.0"
},
"devDependencies": {
"eslint": "^4.15.0",
"standard": "^11.0.1",
"tap": "^11.0.1",
"tape": "^4.8.0",
"tryitout": "^2.0.1"
Expand Down
Loading

0 comments on commit f736750

Please sign in to comment.