Skip to content

Commit

Permalink
Add caching and tests for readJson/readPackageJson (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
artembatura authored Apr 11, 2019
1 parent 61bdc96 commit 5e816b8
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 44 deletions.
1 change: 0 additions & 1 deletion e2e-helpers/run.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { spawn, ChildProcessWithoutNullStreams } from 'child_process';
import * as path from 'path';
import { crossPlatformCommand } from './crossPlatformCommand';
import { waitForHttpStatus } from './waitForHttpStatus';

Expand Down
41 changes: 20 additions & 21 deletions e2e-helpers/waitForHttpStatus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { request } from 'http';

export const waitForHttpStatus = ({
export const waitForHttpStatus = async ({
host,
port,
timeout = 100,
Expand All @@ -16,29 +16,28 @@ export const waitForHttpStatus = ({
method?: string;
expectedStatusCode?: number;
canContinue?: () => boolean;
}) =>
new Promise((resolve, reject) => {
const retry = () => setTimeout(main, timeout);
}) => {
const retry = () => setTimeout(main, timeout);

let totalTimeout = 0;
const main = () => {
const req = request({ port, host }, response => {
if (response.statusCode === expectedStatusCode) {
return resolve();
}
let totalTimeout = 0;
const main = () => {
const req = request({ port, host, method }, response => {
if (response.statusCode === expectedStatusCode) {
return Promise.resolve();
}

if (!canContinue() || totalTimeout > ejectTimeout) {
return reject();
}
if (!canContinue() || totalTimeout > ejectTimeout) {
return Promise.reject();
}

totalTimeout += timeout;
totalTimeout += timeout;

retry();
});
retry();
});

req.on('error', retry);
req.end();
};
req.on('error', retry);
req.end();
};

main();
});
main();
};
2 changes: 1 addition & 1 deletion examples/__tests__/react-typescript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import getPort = require('get-port');
import { run } from '../../e2e-helpers/run';
import * as path from "path";

const workPath = path.join(require.resolve('../react-typescript/package.json'), '..');
const workPath = path.resolve(path.join(__dirname, '..'), 'react-typescript');

describe('example:react-typescript', () => {
beforeAll(() => jest.setTimeout(1000 * 60));
Expand Down
2 changes: 1 addition & 1 deletion examples/__tests__/react.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import getPort = require('get-port');
import { run } from '../../e2e-helpers/run';
import * as path from "path";

const workPath = path.join(require.resolve('../react/package.json'), '..');
const workPath = path.resolve(path.join(__dirname, '..'), 'react');

describe('example:react', () => {
beforeAll(() => jest.setTimeout(1000 * 60));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { extractFirstPropChain } from '../extractFirstPropChain';

describe(extractFirstPropChain.name, () => {
describe('utils:extractFirstPropChain', () => {
it('function', () => {
expect(extractFirstPropChain('function (a) { return a.b.c.f };')).toBe(
'b.c.f'
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/__tests__/getBaseClass.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getBaseClass } from '../getBaseClass';

describe(getBaseClass.name, () => {
describe('utils:getBaseClass', () => {
it('basic', () => {
class Parent {}
class Child extends Parent {}
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/utils/__tests__/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "testPackageName",
"version": "1.0.0",
"dependencies": {}
}
37 changes: 37 additions & 0 deletions packages/core/src/utils/__tests__/readJson.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { readJson } from '../readJson';

type PackageJson = {
name: string;
version: string;
dependencies: object;
};

describe('utils:readJson', () => {
const packageJsonPath = `${__dirname}/package.json`;

it('default', () => {
const json = readJson(packageJsonPath);

expect(json).toEqual({
name: 'testPackageName',
version: '1.0.0',
dependencies: {}
});
});

it('selector', () => {
const packageJsonName = readJson(
packageJsonPath,
({ name }: PackageJson) => name
);

expect(packageJsonName).toBe('testPackageName');
});

it('caching', () => {
const json1 = readJson(packageJsonPath);
const json2 = readJson(packageJsonPath);

expect(json1).toBe(json2);
});
});
26 changes: 26 additions & 0 deletions packages/core/src/utils/__tests__/readPackageJson.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { readPackageJson } from '../readPackageJson';

describe('utils:readPackageJson', () => {
it('relative', () => {
const json = readPackageJson(undefined, {
path: __dirname
});

expect(json).toEqual({
_id: '[email protected]',
name: 'testPackageName',
version: '1.0.0',
readme: 'ERROR: No README data found!',
dependencies: {}
});
});

it('application', () => {
const json = readPackageJson();

expect(json).toMatchObject({
private: true,
name: 'root'
});
});
});
18 changes: 18 additions & 0 deletions packages/core/src/utils/readJson.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { realpathSync, readFileSync } from 'fs';

import { Selector } from '../types';

const _cache: Record<string, any> = {};

export function readJson<T extends Record<string, any>, TSelectedValue>(
_path: string,
selector?: Selector<T, TSelectedValue>
): TSelectedValue | T {
const realPath: string = realpathSync(_path);

if (!_cache[realPath]) {
_cache[realPath] = JSON.parse(readFileSync(realPath, 'utf8'));
}

return selector ? selector(_cache[realPath]) : _cache[realPath];
}
34 changes: 20 additions & 14 deletions packages/core/src/utils/readPackageJson.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
import { Selector } from '../types';
import { Package } from 'normalize-package-data';
import path from 'path';
import { lstatSync } from 'fs';
import { sep } from 'path';

import { Selector } from '../types';
import { readJson } from './readJson';

export type Options = {
cwd: string;
normalize: boolean;
path?: string;
normalize?: boolean;
};

export function readPackageJson<TSelectedValue>(
selector?: Selector<Package, TSelectedValue>,
export function readPackageJson<TPackage extends Package, TSelectedValue>(
selector?: Selector<TPackage, TSelectedValue>,
options?: Options
): TSelectedValue {
const _options: Options = {
cwd: process.cwd(),
): TSelectedValue | TPackage {
const { normalize, path }: Required<Options> = {
path: process.cwd(),
normalize: true,
...(options ? options : {})
};

const filePath = path.resolve(_options.cwd, 'package.json');
const json = require(filePath);
if (!lstatSync(path).isDirectory()) {
throw new Error(`[readPackageJson]: ${path} isn't directory`);
}

const packageJson = readJson(path + sep + 'package.json', selector);

if (_options.normalize) {
require('normalize-package-data')(json);
if (normalize) {
require('normalize-package-data')(packageJson);
}

return selector ? selector(json) : json;
return packageJson;
}
18 changes: 14 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1856,10 +1856,15 @@
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.11.6.tgz#df929d1bb2eee5afdda598a41930fe50b43eaa6a"
integrity sha512-Exw4yUWMBXM3X+8oqzJNRqZSwUAaS4+7NdvHqQuFi/d+synz++xmX3QIf+BFqneW8N31R8Ky+sikfZUXq07ggQ==

"@types/[email protected]":
version "10.12.20"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.20.tgz#f79f959acd3422d0889bd1ead1664bd2d17cd367"
integrity sha512-9spv6SklidqxevvZyOUGjZVz4QRXGu2dNaLyXIFzFYZW0AGDykzPRIUFJXTlQXyfzAucddwTcGtJNim8zqSOPA==
"@types/[email protected]":
version "11.13.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.13.2.tgz#dc85dde46aa8740bb4aed54b8104250f8f849503"
integrity sha512-HOtU5KqROKT7qX/itKHuTtt5fV0iXbheQvrgbLNXFJQBY/eh+VS5vmmTAVlo3qIGMsypm0G4N1t2AXjy1ZicaQ==

"@types/[email protected]":
version "11.13.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-11.13.4.tgz#f83ec3c3e05b174b7241fadeb6688267fe5b22ca"
integrity sha512-+rabAZZ3Yn7tF/XPGHupKIL5EcAbrLxnTr/hgQICxbeuAfWtT0UZSfULE+ndusckBItcv4o6ZeOJplQikVcLvQ==

"@types/[email protected]":
version "2.4.0"
Expand Down Expand Up @@ -11068,6 +11073,11 @@ typedarray@^0.0.6:
resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777"
integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=

[email protected]:
version "3.4.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.2.tgz#9ed4e6475d906f589200193be056f5913caed481"
integrity sha512-Og2Vn6Mk7JAuWA1hQdDQN/Ekm/SchX80VzLhjKN9ETYrIepBFAd8PkOdOTK2nKt0FCkmMZKBJvQ1dV1gIxPu/A==

[email protected]:
version "3.4.3"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.3.tgz#0eb320e4ace9b10eadf5bc6103286b0f8b7c224f"
Expand Down

0 comments on commit 5e816b8

Please sign in to comment.