Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement packageFilenames: specify which files get the package.json special treatment #173

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions flow-typed/defs.js
Expand Up @@ -14,6 +14,7 @@ type ExplorerOptions = {
stopDir: string,
cache: boolean,
transform: CosmiconfigResult => CosmiconfigResult,
packageFilenames: Array<string>,
packageProp: string,
loaders: Loaders,
searchPlaces: Array<string>,
Expand Down
2 changes: 1 addition & 1 deletion src/createExplorer.js
Expand Up @@ -179,7 +179,7 @@ class Explorer {
}

getLoaderEntryForFile(filepath: string): LoaderEntry {
if (path.basename(filepath) === 'package.json') {
if (this.config.packageFilenames.indexOf(path.basename(filepath)) >= 0) {
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