Yarn: [feat] yarn add-script

Created on 23 Jun 2018  Â·  4Comments  Â·  Source: yarnpkg/yarn

Proposal:

Add a CLI command to add scripts to package.json without opening a text editor

Reason:

This would make it easier to write install instructions that can be copy-pasted into a shell

Example:

> yarn add babel-cli babel-preset-react-app
> yarn add-script build "babel --presets=react-app/dev src -d lib"
> yarn build

(e.g. https://github.com/reactjs/reactjs.org/pull/992)

cat-feature

Most helpful comment

In the meantime one can use npm-add-script. The example above by the OP would then be:

npx npm-add-script -k build - v "babel --presets=react-app/dev src -d lib"

Yes, this uses npx, which is a bit strange when using Yarn … If I'm not mistaken yarn dlx which will land in Yarn v2 will do about the same.

All 4 comments

any thoughts on this?

This would come in very handy to automate certain installation instructions.

A --overwrite flag would also be needed, in case you want to force overwriting of an existing command that may already be present.

In the meantime one can use npm-add-script. The example above by the OP would then be:

npx npm-add-script -k build - v "babel --presets=react-app/dev src -d lib"

Yes, this uses npx, which is a bit strange when using Yarn … If I'm not mistaken yarn dlx which will land in Yarn v2 will do about the same.

There's a typo in the above command; there shouldn't be a space between - and v.

npx npm-add-script -k build -v "babel --presets=react-app/dev src -d lib"

Also, I'm not sure why, but the command did not work with yarn dlx:

% yarn dlx npm-add-script -k start -v "tsc-node ."
➤ YN0000: ┌ Resolution step
➤ YN0000: └ Completed in 0.48s
➤ YN0000: ┌ Fetch step
➤ YN0013: │ strip-ansi@npm:3.0.1 can't be found in the cache and will be fetched from the remote registry
➤ YN0013: │ window-size@npm:0.1.4 can't be found in the cache and will be fetched from the remote registry
➤ YN0013: │ wrap-ansi@npm:2.1.0 can't be found in the cache and will be fetched from the remote registry
➤ YN0013: │ y18n@npm:3.2.1 can't be found in the cache and will be fetched from the remote registry
➤ YN0013: │ yargs@npm:3.32.0 can't be found in the cache and will be fetched from the remote registry
➤ YN0000: └ Completed in 0.22s
➤ YN0000: ┌ Link step
➤ YN0000: └ Completed
➤ YN0000: Done in 0.74s

Internal Error: Binary not found (npm-add-script) for root-workspace-0b6124@workspace:.
    at Q (/Users/user/project/.yarn/releases/yarn-2.0.0-rc.33.js:16:7693)
    at async Object.t.executeWorkspaceAccessibleBinary (/Users/user/project/.yarn/releases/yarn-2.0.0-rc.33.js:16:12245)
    at async /Users/user/project/.yarn/releases/yarn-2.0.0-rc.33.js:50:306954
    at async s.mktempPromise (/Users/user/project/.yarn/releases/yarn-2.0.0-rc.33.js:2:5648)
    at async c.execute (/Users/user/project/.yarn/releases/yarn-2.0.0-rc.33.js:50:306154)
    at async c.validateAndExecute (/Users/user/project/.yarn/releases/yarn-2.0.0-rc.33.js:16:50176)
    at async c.run (/Users/user/project/.yarn/releases/yarn-2.0.0-rc.33.js:42:55029)
    at async c.runExit (/Users/user/project/.yarn/releases/yarn-2.0.0-rc.33.js:42:55163)

npx worked fine.

Was this page helpful?
0 / 5 - 0 ratings