Skip to content

Commit

Permalink
Add commander
Browse files Browse the repository at this point in the history
  • Loading branch information
bidoubiwa committed May 28, 2021
1 parent c1295b3 commit c064244
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 56 deletions.
24 changes: 12 additions & 12 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
version: 2
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: weekly
time: "04:00"
open-pull-requests-limit: 10
labels:
- skip-changelog
- dependencies
versioning-strategy: increase
rebase-strategy: disabled
ignore:
- package-ecosystem: npm
directory: "/"
schedule:
interval: weekly
time: "04:00"
open-pull-requests-limit: 10
labels:
- skip-changelog
- dependencies
versioning-strategy: increase
rebase-strategy: disabled
ignore:
- dependency-name: "prettier*"
- dependency-name: "chalk"
- dependency-name: "jest"
22 changes: 11 additions & 11 deletions .github/release-draft-template.yml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
name-template: 'v$RESOLVED_VERSION 🌻'
tag-template: 'v$RESOLVED_VERSION'
name-template: "v$RESOLVED_VERSION 🌻"
tag-template: "v$RESOLVED_VERSION"
exclude-labels:
- 'skip-changelog'
- "skip-changelog"
version-resolver:
minor:
labels:
- 'breaking-change'
- "breaking-change"
default: patch
categories:
- title: 'Breaking changes ⚠️'
label: 'breaking-change'
- title: "Breaking changes ⚠️"
label: "breaking-change"
template: |
## Changes
$CHANGES
Thanks again to $CONTRIBUTORS! 🎉
no-changes-template: 'Changes are coming soon 😎'
sort-direction: 'ascending'
no-changes-template: "Changes are coming soon 😎"
sort-direction: "ascending"
replacers:
- search: '/(?:and )?@dependabot-preview(?:\[bot\])?,?/g'
replace: ''
replace: ""
- search: '/(?:and )?@dependabot(?:\[bot\])?,?/g'
replace: ''
replace: ""
- search: '/(?:and )?@bors(?:\[bot\])?,?/g'
replace: ''
replace: ""
42 changes: 24 additions & 18 deletions __tests__/basic.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ beforeAll(() => {
});
// doesnt work with one file
test('Tests on 1 empty file', async () => {
const res = await combineJson(
'misc/one_empty',
'test-output/combine_empty.json'
);
const res = await combineJson({
inputDir: 'misc/one_empty',
outputFile: 'test-output/combine_empty.json',
});

const data = JSON.parse(
fs.readFileSync(`${process.cwd()}/test-output/combine_empty.json`, 'utf-8')
Expand All @@ -25,10 +25,10 @@ test('Tests on 1 empty file', async () => {
});

test('Tests on multiple empty files', async () => {
const res = await combineJson(
'misc/multiple_empty',
'test-output/combine_multiple_empty.json'
);
const res = await combineJson({
inputDir: 'misc/multiple_empty',
outputFile: 'test-output/combine_multiple_empty.json',
});
const data = JSON.parse(
fs.readFileSync(
`${process.cwd()}/test-output/combine_multiple_empty.json`,
Expand All @@ -41,23 +41,26 @@ test('Tests on multiple empty files', async () => {
});

test('Tests if on 1 file containing one primitive', async () => {
const res = await combineJson(
'misc/one_primitive',
'test-output/combine_a_single_primitive.json'
);
const res = await combineJson({
inputDir: 'misc/one_primitive',
outputFile: 'test-output/combine_a_single_primitive.json',
});
const data = JSON.parse(
fs.readFileSync(`${process.cwd()}/test-output/combine_a_single_primitive.json`, 'utf-8')
fs.readFileSync(
`${process.cwd()}/test-output/combine_a_single_primitive.json`,
'utf-8'
)
);
const expected = [1];
expect(res).toBe(1);
expect(data).toEqual(expected);
});

test('Tests if on 1 files', async () => {
const res = await combineJson(
'misc/one_file',
'test-output/combine_one.json'
);
const res = await combineJson({
inputDir: 'misc/one_file',
outputFile: 'test-output/combine_one.json',
});
const data = JSON.parse(
fs.readFileSync(`${process.cwd()}/test-output/combine_one.json`, 'utf-8')
);
Expand All @@ -67,7 +70,10 @@ test('Tests if on 1 files', async () => {
});

test('Tests if on all files', async () => {
const res = await combineJson('misc', 'test-output/combine_all.json');
const res = await combineJson({
inputDir: 'misc',
outputFile: 'test-output/combine_all.json',
});
const data = JSON.parse(
fs.readFileSync(`${process.cwd()}/test-output/combine_all.json`, 'utf-8')
);
Expand Down
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ module.exports = async () => {
verbose: true,
watchPathIgnorePatterns: ['<rootDir>/test-output'],
rootDir: '.',
testEnvironment: 'node'
testEnvironment: 'node',
};
};
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
{
"name": "combine-json",
"version": "1.0.0",
"version": "0.1.0",
"description": "",
"repository": "git://github.com/bidoubiwa/combine-json.git",
"main": "src/index.js",
"preferGlobal": true,
"bin": "./src/cli.js",
"bin": {
"json-combine": "./src/cli.js"
},
"scripts": {
"link": "npm link",
"lint": "prettier --write .",
"test": "jest",
"test:watch": "npx jest --watch"
},
"author": "",
"author": "Charlotte Vermandel",
"license": "ISC",
"dependencies": {
"chalk": "^3.0.0"
"chalk": "^3.0.0",
"commander": "^7.2.0"
},
"devDependencies": {
"jest": "^26.6.3",
Expand Down
23 changes: 18 additions & 5 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
#!/usr/bin/env node
const pkg = require('../package.json');
const combineJson = require('./combine-json');
const { program } = require('commander');

program
.version(pkg.version)
.requiredOption(
'-i, --input-dir <dir-path>',
'Directory in which the json files are present'
)
.option(
'-o, --output-file <file-name>',
'File name in which all the json files will be merged',
'combine.json'
)
.parse(process.argv);

(async () => {
try {
if (process.argv.length === 2) {
console.log(chalk.red('Error: Missing path argument'));
} else {
await combineJson(process.argv[2], process.argv[3]);
}
const options = program.opts();
console.log(options);
await combineJson(options);
} catch (e) {
console.error(e);
throw e;
Expand Down
12 changes: 7 additions & 5 deletions src/combine-json.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const {
const { jsonRootType, closingArrayIndex } = require('./json-root-type');

const BUFFER_SIZE = 8;
// TODO be able to improve buffer size

async function combine({ inputFiles, inputDirPath, outputFilePath }) {
createOutputArrayFile(outputFilePath);
Expand All @@ -26,6 +27,7 @@ async function combine({ inputFiles, inputDirPath, outputFilePath }) {
fd: inputFileFd,
bufferSize: BUFFER_SIZE,
});

let lastBracket = undefined;

if (isArray) {
Expand Down Expand Up @@ -65,8 +67,8 @@ async function combine({ inputFiles, inputDirPath, outputFilePath }) {

await new Promise(function (resolve) {
comaWrite.write(',', () => {
resolve('')
})
resolve('');
});
});
} else if (last) {
let closingBracketWrite = fs.createWriteStream(outputFilePath, {
Expand All @@ -75,8 +77,8 @@ async function combine({ inputFiles, inputDirPath, outputFilePath }) {

await new Promise(function (resolve) {
closingBracketWrite.write(']', () => {
resolve('')
})
resolve('');
});
});
}

Expand All @@ -91,7 +93,7 @@ async function combine({ inputFiles, inputDirPath, outputFilePath }) {
return 1;
}

async function combineJson(inputDir, outputFile = undefined) {
async function combineJson({ inputDir, outputFile = 'combine.json' }) {
try {
const { inputDirPath, filesName } = inputFilesAndDir({ inputDir });
const outputFilePath = resolveOutputFilePath({ fileName: outputFile });
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1036,6 +1036,11 @@ combined-stream@^1.0.6, combined-stream@~1.0.6:
dependencies:
delayed-stream "~1.0.0"

commander@^7.2.0:
version "7.2.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-7.2.0.tgz#a36cb57d0b501ce108e4d20559a150a391d97ab7"
integrity sha512-QrWXB+ZQSVPmIWIhtEO9H+gwHaMGYiF5ChvoJ+K9ZGHG/sVsa6yiesAD1GC/x46sET00Xlwo1u49RVVVzvcSkw==

component-emitter@^1.2.1:
version "1.3.0"
resolved "https://registry.yarnpkg.com/component-emitter/-/component-emitter-1.3.0.tgz#16e4070fba8ae29b679f2215853ee181ab2eabc0"
Expand Down

0 comments on commit c064244

Please sign in to comment.