Skip to content

Commit b49b20f

Browse files
authored
Add .cjs file ending for config file locations (#596)
1 parent 0df5574 commit b49b20f

File tree

5 files changed

+25
-5
lines changed

5 files changed

+25
-5
lines changed

readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ Run `np` without arguments to launch the interactive UI that guides you through
8181

8282
## Config
8383

84-
`np` can be configured both locally and globally. When using the global `np` binary, you can configure any of the CLI flags in either a `.np-config.js` or `.np-config.json` file in the home directory. When using the local `np` binary, for example, in a `npm run` script, you can configure `np` by setting the flags in either a top-level `np` field in `package.json` or in a `.np-config.js` or `.np-config.json` file in the project directory. If it exists, the local installation will always take precedence. This ensures any local config matches the version of `np` it was designed for.
84+
`np` can be configured both locally and globally. When using the global `np` binary, you can configure any of the CLI flags in either a `.np-config.js`, `.np-config.cjs` or `.np-config.json` file in the home directory. When using the local `np` binary, for example, in a `npm run` script, you can configure `np` by setting the flags in either a top-level `np` field in `package.json` or in a `.np-config.js`, `.np-config.cjs` or `.np-config.json` file in the project directory. If it exists, the local installation will always take precedence. This ensures any local config matches the version of `np` it was designed for.
8585

8686
Currently, these are the flags you can configure:
8787

@@ -120,7 +120,7 @@ For example, this configures `np` to never use Yarn and to use `dist` as the sub
120120
}
121121
```
122122

123-
`.np-config.js`
123+
`.np-config.js` or `.np-config.cjs`
124124
```js
125125
module.exports = {
126126
yarn: false,

source/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const {cosmiconfig} = require('cosmiconfig');
66

77
module.exports = async () => {
88
const searchDir = isInstalledGlobally ? os.homedir() : await pkgDir();
9-
const searchPlaces = ['.np-config.json', '.np-config.js'];
9+
const searchPlaces = ['.np-config.json', '.np-config.js', '.np-config.cjs'];
1010
if (!isInstalledGlobally) {
1111
searchPlaces.push('package.json');
1212
}

test/config.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ const fixtureBasePath = path.resolve('test', 'fixtures', 'config');
88
const getConfigsWhenGlobalBinaryIsUsed = async homedirStub => {
99
const pathsPkgDir = [path.resolve(fixtureBasePath, 'pkg-dir'),
1010
path.resolve(fixtureBasePath, 'local1'),
11-
path.resolve(fixtureBasePath, 'local2')];
11+
path.resolve(fixtureBasePath, 'local2'),
12+
path.resolve(fixtureBasePath, 'local3')];
1213

1314
const promises = [];
1415
pathsPkgDir.forEach(pathPkgDir => {
@@ -27,7 +28,8 @@ const getConfigsWhenGlobalBinaryIsUsed = async homedirStub => {
2728

2829
const getConfigsWhenLocalBinaryIsUsed = async pathPkgDir => {
2930
const homedirs = [path.resolve(fixtureBasePath, 'homedir1'),
30-
path.resolve(fixtureBasePath, 'homedir2')];
31+
path.resolve(fixtureBasePath, 'homedir2'),
32+
path.resolve(fixtureBasePath, 'homedir3')];
3133

3234
const promises = [];
3335
homedirs.forEach(homedir => {
@@ -60,6 +62,13 @@ test('returns config from home directory when global binary is used and `.np-con
6062
configs.forEach(config => t.deepEqual(config, {source: 'homedir/.np-config.js'}));
6163
});
6264

65+
test('returns config from home directory when global binary is used and `.np-config.cjs` exists in home directory', async t => {
66+
const homedirStub = sinon.stub();
67+
homedirStub.returns(path.resolve(fixtureBasePath, 'homedir3'));
68+
const configs = await getConfigsWhenGlobalBinaryIsUsed(homedirStub);
69+
configs.forEach(config => t.deepEqual(config, {source: 'homedir/.np-config.cjs'}));
70+
});
71+
6372
test('returns config from package directory when local binary is used and `package.json` exists in package directory', async t => {
6473
const configs = await getConfigsWhenLocalBinaryIsUsed(path.resolve(fixtureBasePath, 'pkg-dir'));
6574
configs.forEach(config => t.deepEqual(config, {source: 'package.json'}));
@@ -74,3 +83,8 @@ test('returns config from package directory when local binary is used and `.np-c
7483
const configs = await getConfigsWhenLocalBinaryIsUsed(path.resolve(fixtureBasePath, 'local2'));
7584
configs.forEach(config => t.deepEqual(config, {source: 'packagedir/.np-config.js'}));
7685
});
86+
87+
test('returns config from package directory when local binary is used and `.np-config.cjs` exists in package directory', async t => {
88+
const configs = await getConfigsWhenLocalBinaryIsUsed(path.resolve(fixtureBasePath, 'local3'));
89+
configs.forEach(config => t.deepEqual(config, {source: 'packagedir/.np-config.cjs'}));
90+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
source: 'homedir/.np-config.cjs'
3+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
source: 'packagedir/.np-config.cjs'
3+
};

0 commit comments

Comments
 (0)