Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .spellcheckerrc.yml

This file was deleted.

2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

- Add support for configuration in `package.json` ([@gadhagod](https://github.com/gadhagod)).

## [4.8.0] - 2021-06-13

- Convert project to TypeScript.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ Spellchecker CLI can also read configuration from a JSON or YAML file. By defaul

You can specify any command line option in a config file. Just make sure to use camelcase option names in the config file, _e.g._ `frontmatterKeys` instead of `frontmatter-keys`.

Also, you can specify command line options in your `package.json`. All keys under `spellchecker` in `package.json` are rendered the same way as other configuration-from-file methods.

Command line arguments will override any configuration read from a file.

### Globs
Expand Down
5 changes: 2 additions & 3 deletions lib/config/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ export const readConfigFile = (filePathFromArgs: string|undefined): ExternalConf
if (filePathFromArgs) {
return tryLoad(filePathFromArgs);
}

const filePath = [
'/.spellcheckerrc.yaml',
'/.spellcheckerrc.yml',
'./spellcheckerrc.json',
'./spellcheckerrc.jsonc',
'/.spellcheckerrc.json',
'/.spellcheckerrc.jsonc',
]
.map(path => appRootPath.resolve(path))
.find((path) => {
Expand Down
4 changes: 3 additions & 1 deletion lib/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
defaultPlugins, getUsage, readArgs, supportedLanguages, supportedPlugins,
} from './command-line';
import { readConfigFile } from './file';
import { readFromPackage } from './package';
import { InternalConfig } from './types';

const defaultValues = {
Expand All @@ -23,7 +24,8 @@ const defaultValues = {
export const parseConfig = (): InternalConfig => {
const args = readArgs();
const configFile = readConfigFile(args.config);
const parsedArgs = merge({}, defaultValues, configFile, args);
const packageFile = readFromPackage();
const parsedArgs = merge({}, defaultValues, packageFile, configFile, args);

const {
files,
Expand Down
31 changes: 31 additions & 0 deletions lib/config/package.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { accessSync, readFileSync } from 'fs';

import appRootPath from 'app-root-path';

import { printError } from '../print-error';

import { ExternalConfig } from './types';

export function readFromPackage(): ExternalConfig {
const path = appRootPath.resolve('package.json');
try {
accessSync(path);
} catch {
return {};
}
let config;
try {
config = JSON.parse(readFileSync(path, 'utf-8'));
} catch (err) {
if ((err as Error).name === 'SyntaxError') {
printError('Unable to parse package.json');
} else {
printError(`Unable to parse package.json: ${(err as Error).message}`);
}
process.exit(1);
}
if ('spellchecker' in config) {
return config.spellchecker;
}
return {};
}
9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,14 @@
"mocha.parallel": "0.15.5",
"ts-node": "^10.0.0",
"typescript": "^4.3.2"
},
"spellchecker": {
"files": [
"**/*.md",
"!test/**/*.md"
],
"dictionaries": [
"dictionary.txt"
]
}
}
13 changes: 9 additions & 4 deletions test/cli-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ chai.should();

type CommandResult = { stdout: string, stderr: string } & Partial<ExecException>

function runCommand(command: string): Promise<CommandResult> {
function runCommand(command: string, appRootPath?: string): Promise<CommandResult> {
return new Promise((resolve) => {
exec(
command,
// Prevent Spellchecker from picking up .spellcheckerrc.yml in these tests.
{ env: Object.assign({}, process.env, { APP_ROOT_PATH: __dirname }) },
{ env: Object.assign({}, process.env, { APP_ROOT_PATH: appRootPath ?? __dirname }) },
(error, stdout, stderr) => {
if (error) {
resolve(merge({}, error, { stdout, stderr }));
Expand All @@ -37,8 +37,8 @@ function runCommand(command: string): Promise<CommandResult> {
});
}

function runWithArguments(args: string) {
return runCommand(`node build/index.js ${args}`);
function runWithArguments(args: string, appRootPath?: string) {
return runCommand(`node build/index.js ${args}`, appRootPath);
}

const notSpell = (plugin: string) => plugin !== 'spell';
Expand Down Expand Up @@ -477,4 +477,9 @@ parallel('Spellchecker CLI', function testSpellcheckerCLI(this: { timeout(n: num
const result = await runWithArguments('--config test/fixtures/config/basic.jsonc');
result.should.not.have.property('code');
});

it('can read options from `package.json`', async () => {
const result = await runWithArguments('', `${__dirname}/fixtures/config`);
result.should.not.have.property('code');
});
});
11 changes: 11 additions & 0 deletions test/fixtures/config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "spellchecker-cli-test-fixtures",
"version": "1.0.0",
"description": "",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "MIT"
}