Skip to content
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

Open
searls opened this issue Apr 9, 2016 · 3 comments
Open

Comments

@searls
Copy link
Member

searls commented Apr 9, 2016

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.json

For now i think the right thing to do is be explicit like we are being until we get some usage as feedback

@dasilvacontin
Copy link

Populating the "scripts" section of the package.json def has some niceties. Mainly:

  • no need to know about scripty to be able to tell what a script does
  • you can easily check what all the scripts do via npm run.

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 scripty and report back in case I ever feel the need for this populater feature. :)

@searls
Copy link
Member Author

searls commented Apr 13, 2016

Thanks @dasilvacontin!

@mkg20001
Copy link

🎉

#!/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
Projects
None yet
Development

No branches or pull requests

3 participants