sezna / nps

NPM Package Scripts -- All the benefits of npm scripts without the cost of a bloated package.json and limits of json
MIT License
1.43k stars 93 forks source link

yaml include #224

Open airtonix opened 4 years ago

airtonix commented 4 years ago

Scripts file (or at least the relevant bits):

# ./package-scripts.yml

scripts:
  formruntime: >-
    npx nps --config=./packages/FormRuntime/package-scripts.yml --
# ./packages/FormRuntime/package-scripts.yml
scripts:
  dev:
    description: Local Development Mode
    script: >-
      npx parcel dev

  lint: >-
    npx eslint ./

  test: >-
    npx jest

  watch:
    description: run in the amazingly intelligent Jest watch mode
    script: >-
      jest --watch

  build:
    prod:
      script: >-
        parcel build

The command executed:

yarn nps formruntime lint

or

npx nps formruntime lint

The output:

> yarn nps formruntime lint
yarn run v1.22.5
$ C:\Storage\Projects\Forms\node_modules\.bin\nps formruntime lint
nps is executing `formruntime` : npx nps --config=./packages/FormRuntime/package-scripts.yml --
Usage: nps.js [options] <script>...

Commands:
  nps.js init        automatically migrate from npm scripts to nps
  nps.js completion  generate completion script

Options:
  --config, -c      Config file to use (defaults to nearest package-scripts.yml
                    or package-scripts.js)
            [default: "C:\Storage\Projects\Forms\package-scripts.yml"]
  --silent, -s      Silent nps output                 [boolean] [default: false]
  --log-level, -l   The log level to use
                   [choices: "error", "warn", "info", "debug"] [default: "info"]
  --prefix, -p      Prefix for each script name
  --require, -r     Module to preload
  --scripts         Log command text for script        [boolean] [default: true]
  --help-style, -y  Choose the level of detail displayed by the help command
                           [choices: "all", "scripts", "basic"] [default: "all"]
  -v, --version     Show version number                                [boolean]

Examples:
  nps.js test build                     Runs the `test` script then the `build`
                                        script
  nps.js "test --cover" "build --prod"  Runs the `test` script and forwards the
                                        "--cover" flag then the `build` script
                                        and forwards the "--prod" flag
Available scripts (camel or kebab case accepted)

dev - Local Development Mode - npx parcel dev
lint - npx eslint ./
test - npx jest
watch - run in the amazingly intelligent Jest watch mode - jest --watch
build.prod - parcel build
Scripts must resolve to strings. There is no script that can be resolved from "lint" https://github.com/sezna/nps/blob/master/other/ERRORS_AND_WARNINGS.md#missing-script     
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Problem description:

want to split up monorepo tasks among their relevant areas

Suggested solution:

# ./package-scripts.yml

scripts:
  formruntime: !tasks ./packages/FormRuntime/package-scripts.yml

I actually came up with this wrapper:

const path = require('path')
const fs = require('fs')
const YAML = require('js-yaml')

class TaskFile {
  constructor (file) {
    const filepath = path.resolve(file)
    try {
      const data = fs.readFileSync(
        filepath,
        'utf8'
      )
      const tasks = YAML.safeLoad(data, { schema })
      Object.assign(this, tasks)

    } catch (error) {
      console.warn(error)
    }
  }

  toJSON () {
    return JSON.parse(JSON.stringify({...this}))
  }
}

const TasksYamlType = new YAML.Type('!tasks', {
  kind: 'mapping',
  instanceOf: TaskFile,
  construct: ({ file } = {}) => file
    ? new TaskFile(file).scripts || {}
    : {}
})

const schema = YAML.Schema.create([
  TasksYamlType
])

const tasks = (new TaskFile(`${__dirname}/tasks.yml`)).toJSON()

module.exports = tasks

lets me have these yaml files:

scripts:
  formruntime: !tasks
    file: ./packages/FormRuntime/tasks.yml

output becomes:

> npx nps
Usage: nps.js [options] <script>...

Commands:
  nps.js init        automatically migrate from npm scripts to nps
  nps.js completion  generate completion script

Options:
  --config, -c      Config file to use (defaults to nearest package-scripts.yml
                    or package-scripts.js)
             [default: "C:\Storage\Projects\Forms\package-scripts.js"]
  --silent, -s      Silent nps output                 [boolean] [default: false]
  --log-level, -l   The log level to use
                   [choices: "error", "warn", "info", "debug"] [default: "info"]
  --prefix, -p      Prefix for each script name
  --require, -r     Module to preload
  --scripts         Log command text for script        [boolean] [default: true]
  --help-style, -y  Choose the level of detail displayed by the help command
                           [choices: "all", "scripts", "basic"] [default: "all"]
  -v, --version     Show version number                                [boolean]

Examples:
  nps.js test build                     Runs the `test` script then the `build`
                                        script
  nps.js "test --cover" "build --prod"  Runs the `test` script and forwards the
                                        "--cover" flag then the `build` script
                                        and forwards the "--prod" flag
Available scripts (camel or kebab case accepted)

formruntime.dev - Local Development Mode - npx parcel dev
formruntime.lint - npx eslint ./
formruntime.test - npx jest
formruntime.watch - run in the amazingly intelligent Jest watch mode - jest --watch
formruntime.build.prod - parcel build
sezna commented 4 years ago

This seems like a reasonable add. Would you be open to writing a PR for it?