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

Add initial support for --dry-run #58

Merged
merged 20 commits into from
Apr 9, 2018
Merged

Add initial support for --dry-run #58

merged 20 commits into from
Apr 9, 2018

Conversation

aulisius
Copy link
Contributor

Still unsure how to show the actual diff or what kind of tests to write for this. Currently, I've used difflet package but not sure if that is enough.

Closes #6

@aulisius
Copy link
Contributor Author

Added a better differ.

The output looks something like this right now

Before upgrade
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.0",
    "babel-loader": "^7.1.1",
    "babel-plugin-transform-react-jsx-self": "^6.22.0",
    "babel-plugin-transform-react-jsx-source": "^6.22.0",
    "babel-plugin-transform-runtime": "^6.23.0",
    "babel-polyfill": "^6.26.0",
    "babel-preset-env": "^1.6.0",
    "babel-preset-react": "^6.24.1",
    "babel-preset-stage-2": "^6.24.1",

After upgrade
    "@babel/cli": "7.0.0-beta.40",
    "@babel/core": "7.0.0-beta.40",
    "@babel/plugin-transform-react-jsx-self": "7.0.0-beta.40",
    "@babel/plugin-transform-react-jsx-source": "7.0.0-beta.40",
    "@babel/plugin-transform-runtime": "7.0.0-beta.40",
    "@babel/polyfill": "7.0.0-beta.40",
    "@babel/preset-env": "7.0.0-beta.40",
    "@babel/preset-react": "7.0.0-beta.40",
    "@babel/preset-stage-2": "7.0.0-beta.40",
    "babel-loader": "^8.0.0-beta.0",

@noahlemen noahlemen added the enhancement New feature or request label Mar 26, 2018
Copy link
Member

@noahlemen noahlemen left a comment

Choose a reason for hiding this comment

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

looking good to me! will leave some more thoughts later when i've got a chance

do you have any thoughts on testing this? it would be nice if we could assert that we don't write anything when a dry run happens

also, any thoughts on the flag? as mentioned here, it might be safer to flip this and use --write as the flag that does write changes

src/index.js Outdated
pkg.babel = upgradeConfig(pkg.babel, options);
updatedPkg.babel = upgradeConfig(pkg.babel, options);
}
if (options.dryRun) {
Copy link
Member

Choose a reason for hiding this comment

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

maybe this block should happen whether or not we're doing a dry run? eg, have dry run only control whether writeJsonFile gets called

@hzoo
Copy link
Member

hzoo commented Mar 26, 2018

Just wondering, is there a way to show the diff like in git? like with + and -? or like jest snapshots?

I think showing the files one after the other would be difficult to read? Wonder if we could just render a github type diff on a website even. Although maybe I'd just tell them to run it and run git diff themselves 😄

@aulisius
Copy link
Contributor Author

@hzoo Yup, the diff package supports that. The current output looks something like this.

Index: package.json
===================================================================
--- package.json        Before Upgrade
+++ package.json        After Upgrade
@@ -62,23 +62,23 @@
     "string-format": "^0.5.0",
     "traverse": "^0.6.6"
   },
   "devDependencies": {
+    "@babel/cli": "7.0.0-beta.40",
+    "@babel/core": "7.0.0-beta.40",
+    "@babel/plugin-transform-react-jsx-self": "7.0.0-beta.40",
+    "@babel/plugin-transform-react-jsx-source": "7.0.0-beta.40",
+    "@babel/plugin-transform-runtime": "7.0.0-beta.40",
+    "@babel/polyfill": "7.0.0-beta.40",
+    "@babel/preset-env": "7.0.0-beta.40",
+    "@babel/preset-react": "7.0.0-beta.40",
+    "@babel/preset-stage-2": "7.0.0-beta.40",
     "autoprefixer": "8.1.0",
-    "babel-cli": "^6.26.0",
-    "babel-core": "^6.26.0",
     "babel-eslint": "^7.1.0",
-    "babel-loader": "^7.1.1",
+    "babel-loader": "^8.0.0-beta.0",
     "babel-plugin-lodash": "^3.3.2",
     "babel-plugin-transform-imports": "^1.4.1",
-    "babel-plugin-transform-react-jsx-self": "^6.22.0",
-    "babel-plugin-transform-react-jsx-source": "^6.22.0",
     "babel-plugin-transform-react-remove-prop-types": "^0.4.10",
-    "babel-plugin-transform-runtime": "^6.23.0",
-    "babel-polyfill": "^6.26.0",
-    "babel-preset-env": "^1.6.0",
-    "babel-preset-react": "^6.24.1",
-    "babel-preset-stage-2": "^6.24.1",
     "bitbar-webpack-progress-plugin": "^0.1.3",
     "clean-webpack-plugin": "^0.1.17",
     "copy-webpack-plugin": "^4.5.0",
     "css-loader": "^0.28.9",

@kedromelon I'm not really sure about the tests myself, as I don't have enough experience writing them. Would love more input on it.

@aulisius
Copy link
Contributor Author

Rolled back some of my changes and found a simpler way of showing the diff. Following your review @kedromelon, the diff shows up always and only the write action is controlled by the --dry-run flag.

Still unsure of how a test would look like. Will think about it over the weekend.

Copy link
Member

@noahlemen noahlemen left a comment

Choose a reason for hiding this comment

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

@aulisius changes look fantastic, definitely an improvement in clarity!

i'm still thinking about how best to do tests here as well...

src/index.js Outdated

await writeJsonFile(path, pkg, { detectIndent: true });
if (!options.dryRun) {
await writeJsonFile(path, pkg, { detectIndent: true });
Copy link
Member

Choose a reason for hiding this comment

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

for testing this, perhaps we could stub the default export of write-json-file with jest and then assert that it is called (with the correct arguments) when options.dryRun is true and assert that it is not called when options.dryRun isn't?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was thinking along the same lines. I can get started on that. What about the diffs? Is there any value to testing the diff output itself?

@@ -1,6 +1,6 @@
{
"name": "babel-upgrade",
"version": "0.0.15",
"version": "0.0.16",
Copy link
Member

@ljharb ljharb Apr 1, 2018

Choose a reason for hiding this comment

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

Typically version numbers should never be bumped in PRs (or at least, not in PRs that do anything else)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Forgot to rebase before pushing, the master branch actually has version as 0.0.16 not sure why the diff isn't updated with master.

Copy link
Member

Choose a reason for hiding this comment

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

ah i was just looking at the "view changes" diff, no worries

@aulisius
Copy link
Contributor Author

aulisius commented Apr 4, 2018

@hzoo @kedromelon Added a test. Please do tell if that is enough.

@@ -74,3 +74,21 @@ test('jest babel-core bridge', async () => {
test('webpack v1 compatibility', async () => {
expect(await updatePackageJSON(webpackV1Fixture)).toMatchSnapshot();
});

test('respects dry run', async () => {
Copy link
Member

@noahlemen noahlemen Apr 4, 2018

Choose a reason for hiding this comment

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

this is great! thanks for adding!!

just two suggestions:

  • could we make __tests__/index.test.js and do this test there?
  • could we also test that writeJsonFile is called when the dry run is not used?

@aulisius
Copy link
Contributor Author

aulisius commented Apr 5, 2018

Updated the tests. I think the last thing to discuss is with regards to the flag. Do we make --dry-run the default or leave things as it is?

@futagoza
Copy link

futagoza commented Apr 5, 2018

How about:

  1. By default, the CLI does the dry-run and shows the diff, then asks if it should apply the changes
  2. With the --dry-run or -d flags supplied, the CLI exits after showing the diff
  3. With the --apply or -a flags the CLI will apply the upgrade and show the diff then
  • By default this avoids surprises, helps to review changes and involves the upgrader more.
  • The flags help in automating tasks (scripts, watchers, ci, etc)

Also, if it hasn't been implemented (sorry, just found this repo so haven't had a chance to dig into the codebase), how about adding a cache system (mtime?). This would be very helpful on monorepos 😄

Copy link
Member

@noahlemen noahlemen left a comment

Choose a reason for hiding this comment

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

awesome! tests look great. regarding flag itself and its defaults i think it does make sense to dry run by default, and require a --write flag to allow it to write, as discussed in #6

if we are to do that, however, we need to be sure to update the readme with this new detail. given that, i think it would be OK to have this PR just add the support for the functionality, and let the behavior change and documentation updates be handled in a separate PR? open to whatever though, curious to hear others' thoughts!

@aulisius
Copy link
Contributor Author

aulisius commented Apr 9, 2018

Made --dry-run the default. User would have to pass --write to apply the changes.

@futagoza I do like your suggestion but I think it can be done as part of another PR because it requires quite some changes.

@kedromelon Yea, I also think it'd be better if those documentation and/or README updates happened in another PR.

noahlemen added a commit that referenced this pull request Apr 9, 2018
as discussed in #58 (comment), `babel-upgrade` will make no changes unless a `--write` or `-w` flag is provided after #58 is merged. this PR updates the readme to communicate this change in functionality.
@noahlemen
Copy link
Member

noahlemen commented Apr 9, 2018

@aulisius awesome! started #62 to continue coordinating the documentation aspect of this change.

src/bin.js Outdated
@@ -38,7 +50,7 @@ async function hasFlow() {
await writePackageJSON(upgradeOptions);

// TODO: add smarter CLI option handling if we support more options
if (process.argv[2] === '--install') {
if (cliOptions.write && cliOptions.installDeps) {
Copy link
Member

@noahlemen noahlemen Apr 9, 2018

Choose a reason for hiding this comment

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

thought of this after your comment on #62

do you think we should provide a warning when the user gives the --install flag but does not include --write? otherwise it might be difficult for them to understand what is going on in that situation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, agreed. Will add that

@aulisius
Copy link
Contributor Author

aulisius commented Apr 9, 2018

Added the note about install w/o write! @kedromelon we're good to go.

@noahlemen noahlemen merged commit e72f80c into babel:master Apr 9, 2018
@aulisius aulisius deleted the feat/add-dry-run-option branch April 9, 2018 23:28
hzoo pushed a commit that referenced this pull request Apr 13, 2018
* add --write flag to readme (followup for #58)

as discussed in #58 (comment), `babel-upgrade` will make no changes unless a `--write` or `-w` flag is provided after #58 is merged. this PR updates the readme to communicate this change in functionality.
@ctsstc
Copy link

ctsstc commented Aug 14, 2018

Is this in v7 yet? I'm trying to do the dry run flag but I still get files generated. I'm using Babel 7.0.0-rc.1 I also didn't notice anything in my --help flag when running ./node_modules/.bin/babel src --help

I'm trying to see the plugins and target browsers being used when debug is set to true. Is there a better way to do this or a more direct command for such? I know npx browserslist will show you what your target browsers are if you are using a .browserslistrc file. But I would also like to see the plugins/polyfills being used given the target browsers.

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

Successfully merging this pull request may close these issues.

6 participants