Skip to content
This repository was archived by the owner on Mar 21, 2025. It is now read-only.
This repository was archived by the owner on Mar 21, 2025. It is now read-only.

Add a CLI command that generates the package.json "scripty"'s for you #1

Open

Description

@searls

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

Activity

  1. dasilvacontin commented on Apr 13, 2016

    @dasilvacontin

    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. :)

  2. searls commented on Apr 13, 2016

    @searls
    MemberAuthor
  3. mkg20001 commented on Mar 22, 2019

    @mkg20001

    🎉

    #!/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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions