Skip to content

Commit

Permalink
Merge pull request #19 from tillig/feature/diff-info
Browse files Browse the repository at this point in the history
Diff to determine what kind of change was made.
  • Loading branch information
tillig authored Nov 17, 2023
2 parents bf24a56 + b26243c commit 0f4e010
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"cSpell.words": [
"autofix"
"autofix",
"jsdiff"
]
}
35 changes: 35 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,38 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

---
License for jsdiff <https://github.com/kpdecker/jsdiff>:

Software License Agreement (BSD License)

Copyright (c) 2009-2015, Kevin Decker <kpdecker@gmail.com>

All rights reserved.

Redistribution and use of this software in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above
copyright notice, this list of conditions and the
following disclaimer.

* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the
following disclaimer in the documentation and/or other
materials provided with the distribution.

* Neither the name of Kevin Decker nor the names of its
contributors may be used to endorse or promote products
derived from this software without specific prior
written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@ If `json-sort` detects any changes in content due to sorting, it will exit with
By default, the formatter is non-destructive - it will _warn you_ if the sorted content is different from the original content. This allows for easier integration with pre-commit hooks and build scripts where you don't want these things automatically changing content.

If you want the formatter to overwrite the existing file with the sorted content, specify the `--autofix` argument.

Console output from the command will tell you if the difference is _structural_ or _whitespace_. Since the sort command obeys `.editorconfig` there are sometimes non-obvious differences - file encoding, tabs vs. spaces, etc. The messages will tell you what the sort thinks needs changing.
5 changes: 3 additions & 2 deletions json-sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,18 @@ async function main() {
const originalHash = await fileHash.create(originalFilePath);
const formattedHash = await fileHash.create(formattedFilePath);
if (originalHash !== formattedHash) {
const isStructuralDifference = await formatter.isStructuralDifference(originalFilePath, formattedFilePath);
exitCode = 1;
if (argv.autofix) {
console.error(`Updating file ${originalFilePath}.`);
console.error(`Updating file ${originalFilePath} (${isStructuralDifference ? 'structure' : 'whitespace'}).`);
try {
await fs.cp(formattedFilePath, originalFilePath, { force: true });
} catch (e) {
writeError(`Error copying sorted data to autofix ${originalFilePath}.`, e);
continue;
}
} else {
console.error(`${originalFilePath} is not properly sorted.`);
console.error(`${originalFilePath} is not properly sorted (${isStructuralDifference ? 'structure' : 'whitespace'}).`);
}
}
}
Expand Down
17 changes: 13 additions & 4 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"url": "https://github.com/tillig/json-sort-cli/issues"
},
"dependencies": {
"diff": "^5.1.0",
"editorconfig": "^2.0.0",
"glob": "^10.3.10",
"json-stable-stringify": "^1.0.2",
Expand Down
11 changes: 11 additions & 0 deletions src/formatter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const crypto = require('crypto');
const diff = require('diff');
const editorconfig = require('editorconfig');
const fs = require('fs').promises;
const fsSync = require('fs');
Expand Down Expand Up @@ -52,3 +53,13 @@ exports.formatJson = function (originalContents, formatOptions) {

return sortedContents;
};

exports.isStructuralDifference = async function (originalPath, formattedPath) {
const originalContents = (await fs.readFile(originalPath)).toString();
const formattedContents = (await fs.readFile(formattedPath)).toString();
const diffResult = diff.diffTrimmedLines(originalContents, formattedContents);

// If the differences are structural we'll have results; if it's whitespace or
// file encoding we'll see nothing here.
return diffResult && diffResult.length && diffResult.find((dr) => dr.added || dr.removed);
};

0 comments on commit 0f4e010

Please sign in to comment.