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

feat: show diff after sorting #268

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env node
const Diff = require('diff')
const fs = require('fs')
const globby = require('globby')
const pc = require('picocolors')
const sortPackageJson = require('.')

const isCheckFlag = (argument) => argument === '--check' || argument === '-c'
Expand Down Expand Up @@ -34,6 +36,15 @@ files.forEach((file) => {
} else {
fs.writeFileSync(file, sorted, 'utf8')
console.log(`${file} is sorted!`)
const diff = Diff.diffLines(packageJson, sorted)
diff.forEach((part) => {
const colorValue = part.added
? pc.green(part.value)
: part.removed
? pc.red(part.value)
: pc.gray(part.value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Put +/- before the line? For environment doesn't support color.

Copy link
Author

Choose a reason for hiding this comment

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

@fisker

Put +/- before the line?

I agree. +/- is now displayed before the line.

Result

Sorting items

Sorting items

If there are unnecessary row

If there are unnecessary row

Copy link
Collaborator

Choose a reason for hiding this comment

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

Forgot to commit?

Copy link
Author

Choose a reason for hiding this comment

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

Sorry...
I will contact you again when I push commit.

Copy link
Owner

Choose a reason for hiding this comment

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

Please also add a for lines without a - or + so that the file is all inline.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we add space after +?

Copy link
Collaborator

Choose a reason for hiding this comment

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

package.json are special normally they are already indented.

Copy link
Author

Choose a reason for hiding this comment

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

Forgot to commit?

I've pushed the current one.
I will check again on the comments I received.

process.stderr.write(colorValue)
})
}
}
})
Expand Down
39 changes: 33 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,11 @@
"dependencies": {
"detect-indent": "^6.0.0",
"detect-newline": "3.1.0",
"diff": "^5.1.0",
"git-hooks-list": "1.0.3",
"globby": "10.0.0",
"is-plain-obj": "2.1.0",
"picocolors": "^1.0.0",
"sort-object-keys": "^1.1.3"
},
"devDependencies": {
Expand Down
45 changes: 40 additions & 5 deletions tests/snapshots/cli.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ Generated by [AVA](https://avajs.dev).
],
result: {
errorCode: null,
stderr: '',
stderr: `{␊
"version": "1.0.0",␊
"name": "sort-package-json"␊
"name": "sort-package-json",␊
"version": "1.0.0"␊
}`,
stdout: `package.json is sorted!␊
`,
},
Expand Down Expand Up @@ -116,7 +121,12 @@ Generated by [AVA](https://avajs.dev).
],
result: {
errorCode: null,
stderr: '',
stderr: `{␊
"version": "1.0.0",␊
"name": "sort-package-json"␊
"name": "sort-package-json",␊
"version": "1.0.0"␊
}`,
stdout: `bad/package.json is sorted!␊
`,
},
Expand Down Expand Up @@ -188,7 +198,17 @@ Generated by [AVA](https://avajs.dev).
],
result: {
errorCode: null,
stderr: '',
stderr: `{␊
"version": "1.0.0",␊
"name": "sort-package-json"␊
"name": "sort-package-json",␊
"version": "1.0.0"␊
}{␊
"version": "1.0.0",␊
"name": "sort-package-json"␊
"name": "sort-package-json",␊
"version": "1.0.0"␊
}`,
stdout: `bad-1/package.json is sorted!␊
bad-2/package.json is sorted!␊
`,
Expand Down Expand Up @@ -295,7 +315,17 @@ Generated by [AVA](https://avajs.dev).
],
result: {
errorCode: null,
stderr: '',
stderr: `{␊
"version": "1.0.0",␊
"name": "sort-package-json"␊
"name": "sort-package-json",␊
"version": "1.0.0"␊
}{␊
"version": "1.0.0",␊
"name": "sort-package-json"␊
"name": "sort-package-json",␊
"version": "1.0.0"␊
}`,
stdout: `bad-1/package.json is sorted!␊
bad-2/package.json is sorted!␊
`,
Expand Down Expand Up @@ -475,7 +505,12 @@ Generated by [AVA](https://avajs.dev).
],
result: {
errorCode: null,
stderr: '',
stderr: `{␊
"version": "1.0.0",␊
"name": "sort-package-json"␊
"name": "sort-package-json",␊
"version": "1.0.0"␊
}`,
stdout: `bad-1/package.json is sorted!␊
`,
},
Expand Down
Binary file modified tests/snapshots/cli.js.snap
Binary file not shown.