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 1 commit
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
7 changes: 7 additions & 0 deletions cli.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/usr/bin/env node
require('colors')
const fs = require('fs')
const globby = require('globby')
const Diff = require('diff')
const sortPackageJson = require('.')

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

Choose a reason for hiding this comment

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

I prefer use a small color lib like yoctocolors instead of extending String.prototype

Copy link
Author

Choose a reason for hiding this comment

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

@fisker
Thanks for the review.

I prefer use a small color lib like yoctocolors instead

Since require() was not supported for yoctocolors, I did not introduce it, but introduced picocolors.
Snapshot changes have also occurred with the lib changes, so we have updated it again.

})
}
}
})
Expand Down
35 changes: 24 additions & 11 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 @@ -70,8 +70,10 @@
"verbose": true
},
"dependencies": {
"colors": "^1.4.0",
"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",
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.