-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add caching and tests for readJson/readPackageJson (#245)
- Loading branch information
1 parent
61bdc96
commit 5e816b8
Showing
12 changed files
with
144 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/core/src/utils/__tests__/extractFirstPropChain.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "testPackageName", | ||
"version": "1.0.0", | ||
"dependencies": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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" | ||
|