Skip to content

Commit

Permalink
Support auto lookup of .mjs and .cjs config files
Browse files Browse the repository at this point in the history
  • Loading branch information
Bert De Block committed Sep 1, 2023
1 parent 3abccb8 commit 7ecfb40
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/get-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const KNOWN_ROOT_PROPERTIES = new Set([
const SUPPORTED_OVERRIDE_KEYS = new Set(['files', 'rules']);
const SUPPORTED_FORMAT_PROPS = new Set(['name', 'outputFile']);

const CONFIG_FILE_NAME = '.template-lintrc.js';
const CONFIG_FILE_NAMES = ['.template-lintrc.js', '.template-lintrc.mjs', '.template-lintrc.cjs'];
const ALLOWED_ERROR_CODES = new Set([
// resolve package error codes
'MODULE_NOT_FOUND',
Expand Down Expand Up @@ -77,9 +77,15 @@ export async function resolveProjectConfig(workingDir, options) {
return {};
} else {
// look for our config file relative to the specified working directory
configPath = findUpSync(CONFIG_FILE_NAME, {
cwd: workingDir,
});
for (const CONFIG_FILE_NAME of CONFIG_FILE_NAMES) {
configPath = findUpSync(CONFIG_FILE_NAME, {
cwd: workingDir,
});

if (configPath) {
break;
}
}

if (configPath === undefined) {
// we weren't given an explicit --config-path argument, and we couldn't
Expand Down
32 changes: 32 additions & 0 deletions test/unit/get-config-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,4 +970,36 @@ describe('getRuleFromString', function () {
);
}
});

it('supports a `.template-lintrc.mjs` config file', async function () {
let project = await Project.defaultSetup();

project.write({
'.template-lintrc.mjs': `module.exports = { extends: 'recommended' };`,
});

try {
const config = await resolveProjectConfig(project.baseDir, {});

expect(config).toEqual({ extends: 'recommended' });
} finally {
project.dispose();
}
});

it('supports a `.template-lintrc.cjs` config file', async function () {
let project = await Project.defaultSetup();

project.write({
'.template-lintrc.cjs': `module.exports = { extends: 'recommended' };`,
});

try {
const config = await resolveProjectConfig(project.baseDir, {});

expect(config).toEqual({ extends: 'recommended' });
} finally {
project.dispose();
}
});
});

0 comments on commit 7ecfb40

Please sign in to comment.