|
#!/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); |
|
} |
|
}); |
|
}); |