Skip to content

Commit

Permalink
fix(Core): Adding beautifier for JSON output and using relative pathe…
Browse files Browse the repository at this point in the history
…s in console
  • Loading branch information
Igmat committed Jan 22, 2018
1 parent 09ac776 commit bc81077
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"postinstall": "lerna bootstrap",
"build": "lerna run build",
"watch": "lerna run watch --parallel",
"test": "node ./packages/baset/bin/index.js -s tests/*.spec.js",
"test": "lerna link && node ./packages/baset/bin/index.js -s tests/*.spec.js",
"accept": "node ./packages/baset/bin/index.js a -b tests/*.base",
"doctoc": "doctoc README.md CONTRIBUTING.md",
"commit": "git-cz"
Expand Down
3 changes: 1 addition & 2 deletions packages/baset-cli/src/commands/accept.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { accept } from 'baset-core';
import * as glob from 'glob-promise';
import * as path from 'path';
import { CommandModule } from 'yargs';

const acceptCommand: CommandModule = {
Expand All @@ -17,7 +16,7 @@ const acceptCommand: CommandModule = {
},
handler: async argv => {
const baselines = await glob(argv.bases + '.tmp');
accept(baselines.map(base => path.resolve(base)));
accept(baselines);
},
};
export = acceptCommand;
5 changes: 1 addition & 4 deletions packages/baset-cli/src/commands/test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { test } from 'baset-core';
import * as glob from 'glob-promise';
import * as path from 'path';
import { CommandModule } from 'yargs';

const testCommand: CommandModule = {
Expand All @@ -23,9 +22,7 @@ const testCommand: CommandModule = {
},
handler: async argv => {
const [specs, baselines] = await Promise.all([glob(argv.specs), glob(argv.bases)]);
test(
specs.map(spec => path.resolve(spec)),
baselines.map(base => path.resolve(base)));
test(specs, baselines);
},
};
export = testCommand;
7 changes: 5 additions & 2 deletions packages/baset-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
"license": "MIT",
"devDependencies": {
"@types/node": "^9.3.0",
"baset": "^0.2.1",
"tslint": "^5.9.1",
"typescript": "^2.6.2",
"baset": "^0.2.1"
"typescript": "^2.6.2"
},
"dependencies": {
"json-beautify": "^1.0.1"
}
}
14 changes: 8 additions & 6 deletions packages/baset-core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import * as fs from 'fs';
import * as beautify from 'json-beautify';
import * as path from 'path';

export function test(specs: string[], baselines: string[]) {
const files = specs.map(name => ({
name,
output: JSON.stringify(require(name)),
output: beautify(require(path.resolve(name)), undefined, 4, 20),
}));
files.forEach(file => {
const baselinePath = file.name.replace(/.spec.js$/, '.base');
const baselinePath = path.resolve(file.name.replace(/.spec.js$/, '.base'));
const baseline = fs.existsSync(baselinePath)
? fs.readFileSync(baselinePath, { encoding: 'utf-8'})
: false;
fs.writeFile(
file.name.replace(/.spec.js$/, '.base.tmp'),
path.resolve(file.name.replace(/.spec.js$/, '.base.tmp')),
file.output,
err => {
if (err) return console.log(err);
Expand All @@ -26,14 +28,14 @@ export function test(specs: string[], baselines: string[]) {

export function accept(files: string[]) {
files.forEach(file => {
const baseline = fs.readFileSync(file, { encoding: 'utf-8' });
const baseline = fs.readFileSync(path.resolve(file), { encoding: 'utf-8' });
const filePath = file.replace(/.tmp$/, '');
fs.writeFile(
filePath,
path.resolve(filePath),
baseline,
err => {
console.log(err || `Baseline ${filePath} is written.`);
fs.unlinkSync(file);
fs.unlinkSync(path.resolve(file));
});
});
}
5 changes: 5 additions & 0 deletions packages/baset-core/src/types/json-beautify.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare interface beautify {
(value: any, replacer?: (key: string, value: any) => any, space?: string | number, maxWidth?: number): string;
}
declare const beautify: beautify;
export = beautify;

0 comments on commit bc81077

Please sign in to comment.