Skip to content

Commit

Permalink
feat(bin/p-s.js): Added the abbility to preload a module (#12)
Browse files Browse the repository at this point in the history
* docs(contributing): Fix the ROADMAP.md uri

* feat(bin/p-s.js): Added the abbility to preload a module

You can know add the arguments --require or -r to preload a module before requiring the
package-scripts.js

* fix(bin/p-s.js): Fixed some spelling mistakes (I am so stupid)

* docs(module-preload): Add documentation for the --require cmdline option

Closes #13

* docs(errors_and_warnings): update the docs related to --require

#12 (comment) and
#12 (comment)

* fix(bin): fix local modules with a relative path

It now checks if the preloaded module is a relative path and resolves the path relative to the cwd.
In addition the -r and --require cmdline options are properly get stripped away from the command
which is executed.

* refactor(bin): change from config.default to config.__esModule

* refactor(bin): .substr(0, 1) to [0]

#12 (comment)
  • Loading branch information
Tim McGee authored and Kent C. Dodds committed May 20, 2016
1 parent 8330978 commit 3ecdaf1
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
5 changes: 3 additions & 2 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"avatar_url": "https://avatars.githubusercontent.com/u/14267457?v=3",
"profile": "https://github.com/tim-mcgee",
"contributions": [
"code"
"code",
"doc"
]
},
{
Expand All @@ -77,4 +78,4 @@
]
}
]
}
}
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ Once you are ready to commit the changes, please use the below commands

## Help needed

Please checkout the [ROADMAP.md](https://github.com/kentcdodds/p-s/blob/master/ROADMAP.md) and raise an issue to discuss
Please checkout the [other/ROADMAP.md](https://github.com/kentcdodds/p-s/blob/master/ROADMAP.md) and raise an issue to discuss
any of the items in the want to do or might do list.

Also, please watch the repo and respond to questions/bug reports/feature requests! Thanks!
Expand Down
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ $ p-s --help
-p, --parallel <script-name1,script-name2> Scripts to run in parallel (comma seprated)
-c, --config <filepath> Config file to use (defaults to nearest package-scripts.js)
-l, --log-level <level> The log level to use (error, warn, info [default])
-r, --require <module> Module to preload

Available scripts (camel or kebab case accepted)

Expand Down Expand Up @@ -164,6 +165,10 @@ use that file instead.

Specify the log level to use

#### require

You can specify a module which will be loaded before the config file is loaded. This allows you to preload for example babel-register so you can use all babel presets you like.

#### args

You can pass additional arguments to the script(s) that are being spawned:
Expand Down Expand Up @@ -278,7 +283,7 @@ benefits of npm scripts I wanted to keep).

## In the wild

- [react-component-template](https://github.com/nkbt/react-component-template) uses `p-s` to implement shareable npm scripts. See then how dependent [react-swap](https://github.com/nkbt/react-swap) can reuse them.
- [react-component-template](https://github.com/nkbt/react-component-template) uses `p-s` to implement shareable npm scripts. See then how dependent [react-swap](https://github.com/nkbt/react-swap) can reuse them.

GOTCHAS:
- use `process.cwd()` as the base for all paths
Expand All @@ -290,7 +295,7 @@ benefits of npm scripts I wanted to keep).
Thanks goes to these people ([emoji key][emojis]):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](http://kent.doddsfamily.us)<br />[💻](https://github.com/kentcdodds/p-s/commits?author=kentcdodds) [📖](https://github.com/kentcdodds/p-s/commits?author=kentcdodds) 🚇 💡 | [<img src="https://avatars.githubusercontent.com/u/532272?v=3" width="100px;"/><br /><sub>David Wells</sub>](http://davidwells.io)<br />[💻](https://github.com/kentcdodds/p-s/commits?author=DavidWells) | [<img src="https://avatars.githubusercontent.com/u/802242?v=3" width="100px;"/><br /><sub>Abhishek Shende</sub>](https://twitter.com/abhishekisnot)<br />[💻](https://github.com/kentcdodds/p-s/commits?author=abhishekisnot) [⚠️](https://github.com/kentcdodds/p-s/commits?author=abhishekisnot) | [<img src="https://avatars.githubusercontent.com/u/185649?v=3" width="100px;"/><br /><sub>Rowan Oulton</sub>](http://travelog.io)<br />[💻](https://github.com/kentcdodds/p-s/commits?author=rowanoulton) [📖](https://github.com/kentcdodds/p-s/commits?author=rowanoulton) [⚠️](https://github.com/kentcdodds/p-s/commits?author=rowanoulton) | [<img src="https://avatars.githubusercontent.com/u/1915716?v=3" width="100px;"/><br /><sub>Gilad Goldberg</sub>](https://github.com/giladgo)<br />[💻](https://github.com/kentcdodds/p-s/commits?author=giladgo) | [<img src="https://avatars.githubusercontent.com/u/14267457?v=3" width="100px;"/><br /><sub>Tim McGee</sub>](https://github.com/tim-mcgee)<br />[💻](https://github.com/kentcdodds/p-s/commits?author=tim-mcgee) | [<img src="https://avatars.githubusercontent.com/u/175264?v=3" width="100px;"/><br /><sub>Nik Butenko</sub>](http://butenko.me)<br />💡 |
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub>Kent C. Dodds</sub>](http://kent.doddsfamily.us)<br />[💻](https://github.com/kentcdodds/p-s/commits?author=kentcdodds) [📖](https://github.com/kentcdodds/p-s/commits?author=kentcdodds) 🚇 💡 | [<img src="https://avatars.githubusercontent.com/u/532272?v=3" width="100px;"/><br /><sub>David Wells</sub>](http://davidwells.io)<br />[💻](https://github.com/kentcdodds/p-s/commits?author=DavidWells) | [<img src="https://avatars.githubusercontent.com/u/802242?v=3" width="100px;"/><br /><sub>Abhishek Shende</sub>](https://twitter.com/abhishekisnot)<br />[💻](https://github.com/kentcdodds/p-s/commits?author=abhishekisnot) [⚠️](https://github.com/kentcdodds/p-s/commits?author=abhishekisnot) | [<img src="https://avatars.githubusercontent.com/u/185649?v=3" width="100px;"/><br /><sub>Rowan Oulton</sub>](http://travelog.io)<br />[💻](https://github.com/kentcdodds/p-s/commits?author=rowanoulton) [📖](https://github.com/kentcdodds/p-s/commits?author=rowanoulton) [⚠️](https://github.com/kentcdodds/p-s/commits?author=rowanoulton) | [<img src="https://avatars.githubusercontent.com/u/1915716?v=3" width="100px;"/><br /><sub>Gilad Goldberg</sub>](https://github.com/giladgo)<br />[💻](https://github.com/kentcdodds/p-s/commits?author=giladgo) | [<img src="https://avatars.githubusercontent.com/u/14267457?v=3" width="100px;"/><br /><sub>Tim McGee</sub>](https://github.com/tim-mcgee)<br />[💻](https://github.com/kentcdodds/p-s/commits?author=tim-mcgee) [📖](https://github.com/kentcdodds/p-s/commits?author=tim-mcgee) | [<img src="https://avatars.githubusercontent.com/u/175264?v=3" width="100px;"/><br /><sub>Nik Butenko</sub>](http://butenko.me)<br />💡 |
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->

Expand Down
25 changes: 24 additions & 1 deletion bin/p-s.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ program
.option('-p, --parallel <script-name1,script-name2>', 'Scripts to run in parallel (comma seprated)')
.option('-c, --config <filepath>', 'Config file to use (defaults to nearest package-scripts.js)')
.option('-l, --log-level <level>', 'The log level to use (error, warn, info [default])')
.option('-r, --require <module>', 'Module to preload')
.on('--help', onHelp)
.parse(process.argv)

Expand Down Expand Up @@ -55,7 +56,29 @@ runPackageScript({
function getPSConfig() {
var psConfigFilename = program.config ? resolve(process.cwd(), program.config) : findUp.sync('package-scripts.js')
try {
return require(psConfigFilename)
// Require a module like babel-register
if (program.require) {
var requirePath = (program.require[0] === '.'
? resolve(process.cwd(), program.require)
: program.require)

try {
require(requirePath)
} catch (e) {
log.warn({
message: colors.yellow('Unable to preload ' + program.require),
ref: 'unable-to-preload-module'
})
}
}

var config = require(psConfigFilename)
// babel-register is loading the default package into a object with the prop
// default so if it exist use it as the config
if (config.__esModule) {
config = config.default
}
return config
} catch(e) {
log.error({
message: colors.yellow('Unable to find config at ' + psConfigFilename),
Expand Down
9 changes: 9 additions & 0 deletions other/ERRORS_AND_WARNINGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,12 @@ point to a file that actually exists.

This happens if you specify a script name that can't be resolved to a script. See the docs/examples for how you can
define scripts and how they can be referenced.

## Unable to preload module

This happens when you use the `--require` flag and the module you specify cannot be resolved.

### To fix:

1. Check that you spelled the module correctly
2. Check that the module you wish to require is require-able
2 changes: 1 addition & 1 deletion src/bin-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function getScriptsAndArgs(program) {

function getArgs(args, rawArgs, scripts) {
const allArgs = rawArgs.slice(2)
const psArgs = ['-p', '--parallel', '-c', '--config']
const psArgs = ['-p', '--parallel', '-c', '--config', '-r', '--require']
const psFlags = ['-s', '--silent']
const cleanedArgs = remove(allArgs, (item, index, arry) => {
const isArgOrFlag = contains(psArgs, item) || contains(psFlags, item)
Expand Down

5 comments on commit 3ecdaf1

@nkbt
Copy link
Collaborator

@nkbt nkbt commented on 3ecdaf1 May 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either this (most likely) or efb2645 broke external config loading.

See https://circleci.com/gh/nkbt/react-swap/71 or https://circleci.com/gh/nkbt/react-element-resize/7

@nkbt
Copy link
Collaborator

@nkbt nkbt commented on 3ecdaf1 May 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because p-s uses Nodes require() internally. In your example it resolves the config exactly like other require calls and node_modules/react-com... would mean the file react-component-template/package-scripts.js in the node_modules package.

Yep exactly. I used node_modules/react-component-template/package-scripts.js because otherwise it was not working =).

I will change to react-component-template/package-scripts.js of course since it is now working fine =)

@nkbt
Copy link
Collaborator

@nkbt nkbt commented on 3ecdaf1 May 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep just checked.

Before p-s --config react-component-template/package-script did not work, but node_modules/react-component-template/package-scripts.js did. Now it is the other way around.

It is sort of a breaking change, thus major bump would be preferable. It's not a big issue, seems like I am the only one who uses p-s this way at the moment =)

@nkbt
Copy link
Collaborator

@nkbt nkbt commented on 3ecdaf1 May 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I forgot it is not major yet =)

@kentcdodds
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @nkbt, really sorry about this. This was totally unintentional (also, it was efb2645 that broke you I think).

I've been meaning to publish 1.0.0. Normally that's where I start. I'll try to get something out at 1.0.0 soon so you can rely on proper semver and not worry about breakages. Thanks for your patience 👍

Please sign in to comment.