Skip to content

Instantly share code, notes, and snippets.

@bfred-it bfred-it/README.md
Last active Nov 11, 2016

Embed
What would you like to do?

work-cli

Personal launcher to start working in whatever directory you're into. Customize it as needed

$ work
# tries launching GitHub.app
# tries launching Sublime Text
# tries launching the build tool (npm run watch, gulp, grump in this order)

Install

git clone https://gist.github.com/12080878a1aae21f7fb4169954d7c6de.git work-cli
cd work-cli
npm install
npm link # this installs a global executable called "work", but it's just a link to the current dir

Uninstall:

npm unlink
#!/usr/bin/env node
// github (closest repo, parent folders, then search down)
// sublime (closest project, or cwd)
// detect npm run dev, npm run watch, gulp, grunt
const path = require('path');
const findUp = require('find-up');
const findUpGlob = require('find-up-glob');
const globby = require('globby');
const readPkg = require('read-pkg');
const spawn = require('cross-spawn').spawn;
const spawnConfig = {
cwd: process.cwd(),
stdio: ['ignore', process.stdout, process.stderr]
};
const npmScripts = ['dev', 'watch'];
// const windowz = require('windowz')
// console.log(windowz.getAll())
// const exec = require('executive');
function findUpOrDown(findMe) {
return findUpGlob(findMe)
.then(file => file || globby([findMe, '*/' + findMe]))
.then(paths => {
if (paths[0]) {
return paths[0];
}
throw new Error('path not found');
});
}
const repo = findUpOrDown('.git').then(path.dirname);
const pkg = findUpOrDown('package.json');
/**
* OPEN GITHUB.APP
*/
repo.then(git => spawn('github', [git]));
/**
* OPEN SUBLIME TEXT
*/
findUpOrDown('*.sublime-project')
.catch(() => repo)
.catch(() => pkg)
.catch(() => process.cwd())
.then(proj => spawn('subl', [proj]));
/**
* START BUILD SYSTEM
*/
// const run = cmd => config => exec(`cd ${path.dirname(config)}; ${cmd}`);
pkg.then(pkg => Promise.all([
readPkg(pkg),
path.dirname(pkg)
])).then(([pkg, cwd]) => {
spawnConfig.cwd = cwd;
for (const name of npmScripts) {
if (pkg.scripts && pkg.scripts[name]) {
return spawn('npm', ['run-script', name], spawnConfig);
}
}
findUp('gulpfile.js').then(config => {
if (config) {
spawn('gulp', spawnConfig);
}
});
findUp('gruntfile.js').then(config => {
if (config) {
spawn('grunt', spawnConfig);
}
});
});
{
"name": "work-cli",
"version": "1.0.0",
"description": "",
"bin": {
"work": "index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Federico Brigante <npm@bfred.it> (https://twitter.com/bfred_it)",
"license": "ISC",
"dependencies": {
"cross-spawn": "^4.0.0",
"find-up": "^1.1.2",
"find-up-glob": "^1.0.0",
"globby": "^6.0.0",
"npm-run-script": "0.0.4",
"read-pkg": "^1.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.