diff --git a/e2e-helpers/run.ts b/e2e-helpers/run.ts index 11652220..a484588f 100644 --- a/e2e-helpers/run.ts +++ b/e2e-helpers/run.ts @@ -1,5 +1,4 @@ import { spawn, ChildProcessWithoutNullStreams } from 'child_process'; -import * as path from 'path'; import { crossPlatformCommand } from './crossPlatformCommand'; import { waitForHttpStatus } from './waitForHttpStatus'; diff --git a/e2e-helpers/waitForHttpStatus.ts b/e2e-helpers/waitForHttpStatus.ts index 1af818ff..9341bef2 100644 --- a/e2e-helpers/waitForHttpStatus.ts +++ b/e2e-helpers/waitForHttpStatus.ts @@ -1,6 +1,6 @@ import { request } from 'http'; -export const waitForHttpStatus = ({ +export const waitForHttpStatus = async ({ host, port, timeout = 100, @@ -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(); +}; diff --git a/examples/__tests__/react-typescript.test.ts b/examples/__tests__/react-typescript.test.ts index 8ee7af8f..03082de4 100644 --- a/examples/__tests__/react-typescript.test.ts +++ b/examples/__tests__/react-typescript.test.ts @@ -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)); diff --git a/examples/__tests__/react.test.ts b/examples/__tests__/react.test.ts index 7e3178ba..147015cc 100644 --- a/examples/__tests__/react.test.ts +++ b/examples/__tests__/react.test.ts @@ -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)); diff --git a/packages/core/src/utils/__tests__/extractFirstPropChain.test.ts b/packages/core/src/utils/__tests__/extractFirstPropChain.test.ts index 89cdf9c6..ee33b5ea 100644 --- a/packages/core/src/utils/__tests__/extractFirstPropChain.test.ts +++ b/packages/core/src/utils/__tests__/extractFirstPropChain.test.ts @@ -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' diff --git a/packages/core/src/utils/__tests__/getBaseClass.test.ts b/packages/core/src/utils/__tests__/getBaseClass.test.ts index 6e6b0c4f..765544e6 100644 --- a/packages/core/src/utils/__tests__/getBaseClass.test.ts +++ b/packages/core/src/utils/__tests__/getBaseClass.test.ts @@ -1,6 +1,6 @@ import { getBaseClass } from '../getBaseClass'; -describe(getBaseClass.name, () => { +describe('utils:getBaseClass', () => { it('basic', () => { class Parent {} class Child extends Parent {} diff --git a/packages/core/src/utils/__tests__/package.json b/packages/core/src/utils/__tests__/package.json new file mode 100644 index 00000000..cac63c00 --- /dev/null +++ b/packages/core/src/utils/__tests__/package.json @@ -0,0 +1,5 @@ +{ + "name": "testPackageName", + "version": "1.0.0", + "dependencies": {} +} diff --git a/packages/core/src/utils/__tests__/readJson.test.ts b/packages/core/src/utils/__tests__/readJson.test.ts new file mode 100644 index 00000000..7cd08b8b --- /dev/null +++ b/packages/core/src/utils/__tests__/readJson.test.ts @@ -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); + }); +}); diff --git a/packages/core/src/utils/__tests__/readPackageJson.test.ts b/packages/core/src/utils/__tests__/readPackageJson.test.ts new file mode 100644 index 00000000..f2fcf8db --- /dev/null +++ b/packages/core/src/utils/__tests__/readPackageJson.test.ts @@ -0,0 +1,26 @@ +import { readPackageJson } from '../readPackageJson'; + +describe('utils:readPackageJson', () => { + it('relative', () => { + const json = readPackageJson(undefined, { + path: __dirname + }); + + expect(json).toEqual({ + _id: 'testPackageName@1.0.0', + 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' + }); + }); +}); diff --git a/packages/core/src/utils/readJson.ts b/packages/core/src/utils/readJson.ts new file mode 100644 index 00000000..34873184 --- /dev/null +++ b/packages/core/src/utils/readJson.ts @@ -0,0 +1,18 @@ +import { realpathSync, readFileSync } from 'fs'; + +import { Selector } from '../types'; + +const _cache: Record = {}; + +export function readJson, TSelectedValue>( + _path: string, + selector?: Selector +): TSelectedValue | T { + const realPath: string = realpathSync(_path); + + if (!_cache[realPath]) { + _cache[realPath] = JSON.parse(readFileSync(realPath, 'utf8')); + } + + return selector ? selector(_cache[realPath]) : _cache[realPath]; +} diff --git a/packages/core/src/utils/readPackageJson.ts b/packages/core/src/utils/readPackageJson.ts index a4298897..f9d36b2f 100644 --- a/packages/core/src/utils/readPackageJson.ts +++ b/packages/core/src/utils/readPackageJson.ts @@ -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( - selector?: Selector, +export function readPackageJson( + selector?: Selector, options?: Options -): TSelectedValue { - const _options: Options = { - cwd: process.cwd(), +): TSelectedValue | TPackage { + const { normalize, path }: Required = { + 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; } diff --git a/yarn.lock b/yarn.lock index 6a2d715a..52911cb7 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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/node@10.12.20": - version "10.12.20" - resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.20.tgz#f79f959acd3422d0889bd1ead1664bd2d17cd367" - integrity sha512-9spv6SklidqxevvZyOUGjZVz4QRXGu2dNaLyXIFzFYZW0AGDykzPRIUFJXTlQXyfzAucddwTcGtJNim8zqSOPA== +"@types/node@11.13.2": + version "11.13.2" + resolved "https://registry.yarnpkg.com/@types/node/-/node-11.13.2.tgz#dc85dde46aa8740bb4aed54b8104250f8f849503" + integrity sha512-HOtU5KqROKT7qX/itKHuTtt5fV0iXbheQvrgbLNXFJQBY/eh+VS5vmmTAVlo3qIGMsypm0G4N1t2AXjy1ZicaQ== + +"@types/node@11.13.4": + version "11.13.4" + resolved "https://registry.yarnpkg.com/@types/node/-/node-11.13.4.tgz#f83ec3c3e05b174b7241fadeb6688267fe5b22ca" + integrity sha512-+rabAZZ3Yn7tF/XPGHupKIL5EcAbrLxnTr/hgQICxbeuAfWtT0UZSfULE+ndusckBItcv4o6ZeOJplQikVcLvQ== "@types/normalize-package-data@2.4.0": version "2.4.0" @@ -11068,6 +11073,11 @@ typedarray@^0.0.6: resolved "https://registry.yarnpkg.com/typedarray/-/typedarray-0.0.6.tgz#867ac74e3864187b1d3d47d996a78ec5c8830777" integrity sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c= +typescript@3.4.2: + version "3.4.2" + resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.2.tgz#9ed4e6475d906f589200193be056f5913caed481" + integrity sha512-Og2Vn6Mk7JAuWA1hQdDQN/Ekm/SchX80VzLhjKN9ETYrIepBFAd8PkOdOTK2nKt0FCkmMZKBJvQ1dV1gIxPu/A== + typescript@3.4.3: version "3.4.3" resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.4.3.tgz#0eb320e4ace9b10eadf5bc6103286b0f8b7c224f"