Giter Club home page Giter Club logo

Comments (3)

mkg20001 avatar mkg20001 commented on July 23, 2024 1

🎉

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

from scripty.

dasilvacontin avatar dasilvacontin commented on July 23, 2024

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

from scripty.

searls avatar searls commented on July 23, 2024

Thanks @dasilvacontin!

from scripty.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.