Skip to content

Commit

Permalink
Add config option "packageFilenames", defaulting to ["package.json"]
Browse files Browse the repository at this point in the history
Specifies filenames that should get the package.json special treatment,
where they're parsed as strict JSON and the packageProp is extracted.
  • Loading branch information
cspotcode authored and Andrew Bradley committed Feb 22, 2019
1 parent 71da326 commit dda579b
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
3 changes: 2 additions & 1 deletion flow-typed/defs.js
Expand Up @@ -14,13 +14,14 @@ type ExplorerOptions = {
stopDir: string,
cache: boolean,
transform: CosmiconfigResult => CosmiconfigResult,
packageFilenames: Array<string>,
packageProp: string,
loaders: Loaders,
searchPlaces: Array<string>,
ignoreEmptySearchPlaces: boolean,
};

type ExplorerContext = ExplorerOptions & {
type ExplorerContext = & {
loadCache: ?Map<string, Promise<CosmiconfigResult>>,
loadSyncCache: ?Map<string, CosmiconfigResult>,
searchCache: ?Map<string, Promise<CosmiconfigResult>>,
Expand Down
6 changes: 5 additions & 1 deletion src/createExplorer.js
Expand Up @@ -69,6 +69,10 @@ class Explorer {
);
}
});

if(config.packageFilenames == null) {
config.packageFilenames = [];
}
}

search(searchFrom?: string): Promise<CosmiconfigResult> {
Expand Down Expand Up @@ -179,7 +183,7 @@ class Explorer {
}

getLoaderEntryForFile(filepath: string): LoaderEntry {
if (path.basename(filepath) === 'package.json') {
if (this.config.packageFilenames.includes(path.basename(filepath))) {
const loader = this.loadPackageProp.bind(this);
return { sync: loader, async: loader };
}
Expand Down
2 changes: 2 additions & 0 deletions src/index.js
Expand Up @@ -10,6 +10,7 @@ module.exports = cosmiconfig;
function cosmiconfig(
moduleName: string,
options: {
packageFilenames?: Array<string>,
packageProp?: string,
loaders?: Object,
searchPlaces?: Array<string>,
Expand All @@ -21,6 +22,7 @@ function cosmiconfig(
) {
options = options || {};
const defaults = {
packageFilenames: ['package.json'],
packageProp: moduleName,
searchPlaces: [
'package.json',
Expand Down
3 changes: 3 additions & 0 deletions test/index.test.js
Expand Up @@ -35,6 +35,7 @@ describe('cosmiconfig', () => {
expect(createExplorerMock).toHaveBeenCalledTimes(1);
const explorerOptions = createExplorerMock.mock.calls[0][0];
expect(explorerOptions).toMatchObject({
packageFilenames: ['package.json'],
packageProp: moduleName,
searchPlaces: [
'package.json',
Expand Down Expand Up @@ -79,6 +80,7 @@ describe('cosmiconfig', () => {
stopDir: __dirname,
cache: false,
searchPlaces: ['.foorc.json', 'wildandfree.js'],
packageFilenames: ['manifest.json'],
packageProp: 'wildandfree',
ignoreEmptySearchPlaces: false,
loaders: {
Expand All @@ -91,6 +93,7 @@ describe('cosmiconfig', () => {

const explorerOptions = createExplorerMock.mock.calls[0][0];
expect(explorerOptions).toMatchObject({
packageFilenames: ['manifest.json'],
packageProp: 'wildandfree',
searchPlaces: ['.foorc.json', 'wildandfree.js'],
ignoreEmptySearchPlaces: false,
Expand Down
57 changes: 55 additions & 2 deletions test/successful-files.test.js
Expand Up @@ -147,12 +147,26 @@ describe('loads package prop when configPath is package.json', () => {
}
}`
);
temp.createFile(
'manifest.json',
`{
"foo": {
"bar": "baz"
},
"isManifest": {"very": "yes"},
"otherPackage": {
"please": "no"
}
}`
);
});

const configPath = temp.absolutePath('package.json');
const checkResult = (result, expectedConfig) => {
const manifestPath = temp.absolutePath('manifest.json');
const checkResult = (result, expectedConfig, filepath) => {
if(filepath === undefined) filepath = configPath;
expect(result.config).toEqual(expectedConfig);
expect(result.filepath).toBe(configPath);
expect(result.filepath).toBe(filepath);
};

describe('default package prop', () => {
Expand Down Expand Up @@ -232,6 +246,45 @@ describe('loads package prop when configPath is package.json', () => {
expect(result).toBeNull();
});
});

describe('load packageProp from alternative packageNames', () => {
const explorer = cosmiconfig('foo', { packageFilenames: ['manifest.json'], packageProp: 'isManifest' });
const expectedConfig = { very: 'yes' };

test('async', () => {
return explorer
.load(manifestPath)
.then(result => checkResult(result, expectedConfig, manifestPath));
});

test('sync', () => {
const result = explorer.loadSync(manifestPath);
checkResult(result, expectedConfig, manifestPath);
});
});

describe('do not load packageProp when package.json is not in packageNames', () => {
const explorer = cosmiconfig('foo', { packageFilenames: ['manifest.json'], packageProp: 'foo' });
const expectedConfig = {
foo: {
bar: "baz"
},
otherPackage: {
please: "no"
}
};

test('async', () => {
return explorer
.load(configPath)
.then(result => checkResult(result, expectedConfig));
});

test('sync', () => {
const result = explorer.loadSync(configPath);
checkResult(result, expectedConfig);
});
});
});

describe('runs transform', () => {
Expand Down

0 comments on commit dda579b

Please sign in to comment.