Skip to content

Commit

Permalink
Use Glob Pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Airton Zanon <me@airton.dev>
  • Loading branch information
airtonzanon committed Sep 13, 2019
1 parent 5c1d063 commit 3e7762d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 41 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,15 @@ Install jsoncs with npm to use the command line interface:

Verify if the code style is fine:

./node_modules/.bin/jsoncs my/file.json
./node_modules/.bin/jsoncs 'my/file.json'

or to multiple files

./node_modules/.bin/jsoncs 'my/*.json'

Fix the code style of a file or multiple files:

./node_modules/.bin/jsoncs --fix my/directory/
./node_modules/.bin/jsoncs --fix 'my/directory/*'

### Options

Expand Down
19 changes: 15 additions & 4 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,22 @@ function parse(path) {
}

function processFile(filePath) {
let source = fs.readFileSync(filePath, "utf8");
let formatted = formatter.formatJson(source);
let fileWithErrors = showDiff(program.quiet, source, formatted, filePath);
let formatted, source, fileWithErrors;

errors.push(fileWithErrors);
try {
source = fs.readFileSync(filePath, "utf8");
formatted = formatter.formatJson(source);
fileWithErrors = showDiff(program.quiet, source, formatted, filePath);

errors.push(fileWithErrors);
} catch (e) {
console.log("Couldn't read the file / directory");
console.log("Try to:");
console.log(" - use quotes (eg: jsoncs 'my/file.json')");
console.log(" - use glob pattern (eg: jsoncs 'my/*.json')");
console.log(" - use glob pattern (eg: jsoncs 'my/*/*.json')");
process.exit(1);
}

if (program.fix) {
let fixedFile = fixJsonFile(filePath, formatted);
Expand Down
18 changes: 2 additions & 16 deletions lib/modules.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
const fs = require("fs");
const jsdiff = require("diff");
const glob = require("glob")

const blue = "\x1b[34m";
const red = "\x1b[31m";
const withe = "\x1b[37m";

module.exports = {
getFiles: (path) => {
if (fs.lstatSync(path).isDirectory()) {
return readDirectory(path);
}

return [path];
return glob.sync(path);
},
fixJsonFile: (filePath, formatted) => {
fs.writeFileSync(filePath, formatted);
Expand All @@ -37,14 +34,3 @@ module.exports = {
return fileWithErrors;
}
};

function readDirectory(directoryPath) {
let files = fs.readdirSync(directoryPath);
let filesPath = [];

files.forEach((file) => {
filesPath.push(directoryPath + file);
});

return filesPath;
}
23 changes: 6 additions & 17 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
},
"dependencies": {
"commander": "^3.0.0",
"diff": "^4.0.1"
"diff": "^4.0.1",
"glob": "^7.1.4"
},
"devDependencies": {
"eslint": "^6.2.1",
Expand Down
10 changes: 9 additions & 1 deletion test/modulesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,21 @@ test("test get file path", (assert) => {
});

test("test get directory path", (assert) => {
let path = __dirname + "/fixture/expected-json/";
let path = __dirname + "/fixture/expected-json/*.json";
let result = getFiles(path);

assert.equal(result.length, 2);
assert.end();
});

test("test get all files inside a directory path", (assert) => {
let path = __dirname + "/fixture/*/*.json";
let result = getFiles(path);

assert.equal(result.length, 5);
assert.end();
});

test("test show diff between two different json objects", (assert) => {
let json1 = {foo:"bar"};
let json2 = {ble:"bar"};
Expand Down

0 comments on commit 3e7762d

Please sign in to comment.