Skip to content

Commit

Permalink
Add .cjs file ending for config file locations (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
nnmrts committed Feb 6, 2021
1 parent 0df5574 commit b49b20f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Run `np` without arguments to launch the interactive UI that guides you through

## Config

`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.
`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.

Currently, these are the flags you can configure:

Expand Down Expand Up @@ -120,7 +120,7 @@ For example, this configures `np` to never use Yarn and to use `dist` as the sub
}
```

`.np-config.js`
`.np-config.js` or `.np-config.cjs`
```js
module.exports = {
yarn: false,
Expand Down
2 changes: 1 addition & 1 deletion source/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {cosmiconfig} = require('cosmiconfig');

module.exports = async () => {
const searchDir = isInstalledGlobally ? os.homedir() : await pkgDir();
const searchPlaces = ['.np-config.json', '.np-config.js'];
const searchPlaces = ['.np-config.json', '.np-config.js', '.np-config.cjs'];
if (!isInstalledGlobally) {
searchPlaces.push('package.json');
}
Expand Down
18 changes: 16 additions & 2 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const fixtureBasePath = path.resolve('test', 'fixtures', 'config');
const getConfigsWhenGlobalBinaryIsUsed = async homedirStub => {
const pathsPkgDir = [path.resolve(fixtureBasePath, 'pkg-dir'),
path.resolve(fixtureBasePath, 'local1'),
path.resolve(fixtureBasePath, 'local2')];
path.resolve(fixtureBasePath, 'local2'),
path.resolve(fixtureBasePath, 'local3')];

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

const getConfigsWhenLocalBinaryIsUsed = async pathPkgDir => {
const homedirs = [path.resolve(fixtureBasePath, 'homedir1'),
path.resolve(fixtureBasePath, 'homedir2')];
path.resolve(fixtureBasePath, 'homedir2'),
path.resolve(fixtureBasePath, 'homedir3')];

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

test('returns config from home directory when global binary is used and `.np-config.cjs` exists in home directory', async t => {
const homedirStub = sinon.stub();
homedirStub.returns(path.resolve(fixtureBasePath, 'homedir3'));
const configs = await getConfigsWhenGlobalBinaryIsUsed(homedirStub);
configs.forEach(config => t.deepEqual(config, {source: 'homedir/.np-config.cjs'}));
});

test('returns config from package directory when local binary is used and `package.json` exists in package directory', async t => {
const configs = await getConfigsWhenLocalBinaryIsUsed(path.resolve(fixtureBasePath, 'pkg-dir'));
configs.forEach(config => t.deepEqual(config, {source: 'package.json'}));
Expand All @@ -74,3 +83,8 @@ test('returns config from package directory when local binary is used and `.np-c
const configs = await getConfigsWhenLocalBinaryIsUsed(path.resolve(fixtureBasePath, 'local2'));
configs.forEach(config => t.deepEqual(config, {source: 'packagedir/.np-config.js'}));
});

test('returns config from package directory when local binary is used and `.np-config.cjs` exists in package directory', async t => {
const configs = await getConfigsWhenLocalBinaryIsUsed(path.resolve(fixtureBasePath, 'local3'));
configs.forEach(config => t.deepEqual(config, {source: 'packagedir/.np-config.cjs'}));
});
3 changes: 3 additions & 0 deletions test/fixtures/config/homedir3/.np-config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
source: 'homedir/.np-config.cjs'
};
3 changes: 3 additions & 0 deletions test/fixtures/config/local3/.np-config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
source: 'packagedir/.np-config.cjs'
};

0 comments on commit b49b20f

Please sign in to comment.