Skip to content

Commit

Permalink
Merge pull request #9 from codelicia/glob
Browse files Browse the repository at this point in the history
Use Glob Pattern
  • Loading branch information
airtonzanon committed Sep 27, 2019
2 parents 096c2c5 + f338de2 commit d647b8a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 46 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
23 changes: 15 additions & 8 deletions lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,32 @@ program.parse(process.argv);
function parse(path) {
getFiles(path).map(processFile);

!program.fix || console.log(fixedFiles);

if (errors.includes(true)) {
process.exit(1);
}

!program.fix || console.log(fixedFiles);
process.exit(0);
}

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

errors.push(fileWithErrors);
errors.push(fileWithErrors);

if (program.fix) {
let fixedFile = fixJsonFile(filePath, formatted);
let fixedFile = fixJsonFile(program.fix, filePath, formatted);
fixedFiles.push(fixedFile);
errors = [];
} 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);
}
}

Expand Down
22 changes: 6 additions & 16 deletions lib/modules.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
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 glob.sync(path);
},
fixJsonFile: (shouldFix, filePath, formatted) => {
if (!shouldFix) {
return;
}

return [path];
},
fixJsonFile: (filePath, formatted) => {
fs.writeFileSync(filePath, formatted);

return filePath + " - has been fixed";
Expand All @@ -37,14 +38,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
20 changes: 18 additions & 2 deletions test/modulesTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,22 @@ const { fixJsonFile, getFiles, showDiff } = require("../lib/modules.js");

test("test write formatted json on the file", (assert) => {
let path = __dirname + "/fixture/actual-json/temp_json_file.json";
let result = fixJsonFile(path, "{a}");
let result = fixJsonFile(true, path, "{a}");
let file = fs.readFileSync(path).toString();

assert.equal(path + " - has been fixed", result);
assert.equal("{a}", file);
assert.end();
});

test("test not write formatted json on the file", (assert) => {
let path = __dirname + "/fixture/actual-json/temp_json_file.json";
let result = fixJsonFile(false, path, "{a}");

assert.equal(undefined, result);
assert.end();
});

test("test get file path", (assert) => {
let path = __dirname + "/fixture/actual-json/temp_json_file.json";
fs.writeFileSync(path, "");
Expand All @@ -23,13 +31,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 d647b8a

Please sign in to comment.