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

fix(cli): improve handling of forwarding args/flags #101

Merged
merged 1 commit into from
Feb 14, 2017
Merged

Conversation

kentcdodds
Copy link
Collaborator

What: This migrates us from commander and omelette to yargs
and in the process changes how we handle forwarding of arguments and
flags. We no longer just arbitrarily forward arguments to every script.
Also instead of comma separated scripts, they're simply separated by a
space. If you want to pass args/flags to a script, then you put it in
quotes.

Why: Makes this more predictable. Also yargs is more powerful in
general anyway.

How: Magic... Will comment inline...

Closes #70 and #100

@kentcdodds
Copy link
Collaborator Author

Anyone watching the repo can probably tell that I'm at a total loss on why the build is failing. Anyone wanna give it a look and figure out what's up? I think somehow that p-s is spawning processes in an old version of node... Or something. Totally bonkers.

@kentcdodds
Copy link
Collaborator Author

I'm fairly confident that I've narrowed it down to spawn-default-shell (dependency of concurrently) being the problem (cc @kimmobrunfeldt). My guess is that the way that it determines the shell to use ignores nvm which Travis has set up for the correct node version and it uses the pre-existing node version. I'm working on a repro repo right now.

@kentcdodds
Copy link
Collaborator Author

Verified

@kentcdodds
Copy link
Collaborator Author

Looks like my fork fixes the issue. Just waiting on this to be merged and released or finding another solution.

@codecov-io
Copy link

codecov-io commented Feb 12, 2017

Codecov Report

Merging #101 into next will not change coverage.
The diff coverage is 100%.

@@         Coverage Diff         @@
##           next   #101   +/-   ##
===================================
  Coverage   100%   100%           
===================================
  Files        14     10    -4     
  Lines       246    286   +40     
===================================
+ Hits        246    286   +40
Impacted Files Coverage Δ
src/bin-utils/index.js 100% <ø> (ø)
src/bin-utils/parser.js 100% <100%> (ø)
src/get-logger.js 100% <100%> (ø)
src/bin-utils/autocomplete-get-scripts.js 100% <100%> (ø)
src/index.js 100% <100%> (ø)
src/bin-utils/fixtures/fake-es6-module.js
src/bin-utils/fixtures/function-config.js
src/bin-utils/fixtures/my-module.js
src/bin-utils/fixtures/bad-data-type-config.js
...in-utils/fixtures/bad-function-data-type-config.js
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8c04785...67dbed7. Read the comment docs.

@kentcdodds
Copy link
Collaborator Author

Wahoo! This PR is ready for review finally!

@@ -4,11 +4,6 @@ import runNPS from './run-nps'
const fixturesPath = resolve(__dirname, './fixtures')

test(
'without arguments',
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I removed this because yargs help output is non-deterministic and based on the width of the terminal.

@@ -60,24 +60,15 @@ test('options: logLevel sets the log level', () => {
})
})

test('passes on additional arguments', () => {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

We don't worry about this anymore. This is no longer a feature. If you want to pass arguments, put it in quotes. It's less confusing this way

@addityasingh
Copy link

@kentcdodds The changes look fine, but do you think it would look a clean syntax?

nps 'build --foo'

My 2 cents: I would still prefer to not have any special syntax for my cli commands and below would be better than above:

nps build --foo

@kentcdodds
Copy link
Collaborator Author

Thanks for the review @addityasingh (I've added you to the contributors table as a reviewer!),
I'll respond to you on #100

-h, --help Show help [boolean]
-v, --version Show version number [boolean]

Examples:

Choose a reason for hiding this comment

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

Examples really shine through 👍

README.md Outdated
##### scripts

To run a script, you simply provide the name of the script like so:
And you can run multiple scripts in series by simply adding more arguments.

Choose a reason for hiding this comment

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

Maybe change wording to: And you can run multiple scripts in series by simply adding more space-separated arguments.?

@@ -50,6 +52,16 @@ function getShouldLogFn(...acceptableValues) {
acceptableValues = ['', 'debug', ...acceptableValues]

Choose a reason for hiding this comment

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

I think it's preferable to treat params (acceptableValues ) as const and not to override them, to avoid any kind of confusion in readability

}
}

function getLogLevel({silent, logLevel}) {

Choose a reason for hiding this comment

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

This whole function can be refactored as below:

const getLogLevel = ({silent, logLevel}) => silent ? 'disable' : logLevel

**What**: This migrates us from `commander` and `omelette` to `yargs`
and in the process changes how we handle forwarding of arguments and
flags. We no longer just arbitrarily forward arguments to every script.
Also instead of comma separated scripts, they're simply separated by a
space. If you want to pass args/flags to a script, then you put it in
quotes.

**Why**: Makes this more predicatble. Also `yargs` is more powerful in
general anyway.

**How**: Magic... Will comment inline...
@kentcdodds
Copy link
Collaborator Author

I'm going to merge this now and publish a beta for the next branch. I think there's still more to do, but I want to get feedback!

@kentcdodds kentcdodds merged commit a15d56a into next Feb 14, 2017
@kentcdodds kentcdodds deleted the pr/yargs branch February 14, 2017 00:41
kentcdodds pushed a commit that referenced this pull request Feb 18, 2017
**What**: This migrates us from `commander` and `omelette` to `yargs`
and in the process changes how we handle forwarding of arguments and
flags. We no longer just arbitrarily forward arguments to every script.
Also instead of comma separated scripts, they're simply separated by a
space. If you want to pass args/flags to a script, then you put it in
quotes.

**Why**: Makes this more predicatble. Also `yargs` is more powerful in
general anyway.

**How**: Magic... Will comment inline...
@gunnx
Copy link
Collaborator

gunnx commented Feb 22, 2017

I just updated and realised I can no longer pass down args as I was doing before:
e.g.
npm start lint - run linter
npm start lint -- --fix - run linter with fix flag passed

I guess this was just useful sometimes without having to create every possible option in package-scripts

@kentcdodds
Copy link
Collaborator Author

As noted in the release notes

Another significant change here is the change for #100.

before:

nps lint,build "src/**/*.js"

after:

nps "lint src/**/*.js" "build src/**/*.js"

It's kinda sad, but I honestly never really saw anyone utilizing the argument forwarding feature anyway. Where this gets a little more impactful is when you're typing at the command line:

before:

nps test --updateSnapshot

after:

nps "test --updateSnapshot"

Notice the quotes. Those are now required for a command and its arguments to be grouped (otherwise --updateSnapshot will be assumed to be a command you're trying to execute and you'll get an error). Actually, nps will throw an error if you try nps foo --flag because --flag is not a recognized nps flag :)

:)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants