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

yaml include #224

Open
airtonix opened this issue Sep 4, 2020 · 1 comment
Open

yaml include #224

airtonix opened this issue Sep 4, 2020 · 1 comment

Comments

@airtonix
Copy link

airtonix commented Sep 4, 2020

  • nps version:
...
    "nps": "^5.10.0",
...
  • node version:
> node -v
v13.14.0
  • npm version:
> npm -v
6.1.0

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
Copy link
Owner

sezna commented Sep 14, 2020

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

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

2 participants