-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a CLI command that generates the package.json "scripty"'s for you #1
Labels
Comments
Populating the "scripts" section of the package.json def has some niceties. Mainly:
I had thought about making a tool that populated the "scripts" section, but never got to a point where I really needed it – I guess I've only used the "scripts" field with simple configs. I'll try using |
Thanks @dasilvacontin! |
Open
🎉 #!/usr/bin/env node
'use strict'
const fs = require('fs')
const path = require('path')
const glob = require('minimatch')
function traverse(cwd, parts = [], targets = []) {
const dirs = fs.readdirSync(cwd)
dirs.forEach(node => {
const stat = fs.lstatSync(path.join(cwd, node))
if (stat.isDirectory()) {
traverse(path.join(cwd, node), parts.concat([node]), targets)
} else if (stat.isFile() && node.endsWith('.sh')) {
targets.push(parts.concat([node.split('.')[0]]).join(':'))
}
})
return targets
}
function isTargetParallel(pkgJSON, target) {
return pkgJSON && pkgJSON.scripty && pkgJSON.scripty.parallel && pkgJSON.scripty.parallel.filter(str => glob(target, str)).length
}
function apply (pkgJSON, targets) {
targets.forEach(target => {
if (!pkgJSON.scripts[target]) {
console.log('Adding target %o...', target)
pkgJSON.scripts[target] = isTargetParallel(pkgJSON, target) ? 'SCRIPTY_PARALLEL=true scripty' : 'scripty'
}
})
}
function findPackageJSON() {
return path.join(process.cwd(), 'package.json')
}
const pkgJSONpath = findPackageJSON()
const pkgJSON = JSON.parse(String(fs.readFileSync(pkgJSONpath)))
const targets = traverse(path.join(path.dirname(pkgJSONpath), 'scripts'))
apply(pkgJSON, targets)
fs.writeFileSync(JSON.stringify(pkgJSON, null, 2)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Idea being: by inspecting the file system of
scripts/
, you should be able to populate the "scripts" section of the package.json so folks don't have to manually keep package.json in sync manually.On the other hand we could seek a more dynamic approach that'd allow scripty to intercept
npm run
without ever changing the package.jsonFor now i think the right thing to do is be explicit like we are being until we get some usage as feedback
The text was updated successfully, but these errors were encountered: