Skip to content

Commit

Permalink
Adding ensureTodoConfig to write default config (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
scalvert authored Jan 26, 2021
1 parent e347866 commit 242816b
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ have a todo lint violation.</p>
Config values can be present in</p>
<p>The package.json</p>
</dd>
<dt><a href="#ensureTodoConfig">ensureTodoConfig(baseDir)</a></dt>
<dd><p>Ensures that a valid todo config exists in the project by writing one to the package.json
if we&#39;re invoking the todos functionality for the first time (there is no .lint-todo directory).</p>
</dd>
<dt><a href="#writeTodoConfig">writeTodoConfig(baseDir, todoConfig)</a></dt>
<dd><p>Writes a todo config to the package.json located at the provided baseDir.</p>
</dd>
Expand Down Expand Up @@ -387,6 +391,18 @@ Environment variables (`TODO_DAYS_TO_WARN` or `TODO_DAYS_TO_ERROR`)

Passed in directly, such as from command line options.
- Passed in options override both env vars and package.json config
<a name="ensureTodoConfig"></a>

## ensureTodoConfig(baseDir)
Ensures that a valid todo config exists in the project by writing one to the package.json
if we're invoking the todos functionality for the first time (there is no .lint-todo directory).

**Kind**: global function

| Param | Description |
| --- | --- |
| baseDir | The base directory that contains the project's package.json. |

<a name="writeTodoConfig"></a>

## writeTodoConfig(baseDir, todoConfig)
Expand Down
73 changes: 72 additions & 1 deletion __tests__/todo-config-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { unlink, readFileSync, writeFileSync } from 'fs-extra';
import { join } from 'path';
import { getTodoConfig, writeTodoConfig } from '../src';
import { getTodoConfig, writeTodoConfig, ensureTodoStorageDirSync } from '../src';
import { FakeProject } from './__utils__/fake-project';
import { ensureTodoConfig } from '../src/todo-config';

describe('todo-config', () => {
let project: FakeProject;
Expand Down Expand Up @@ -160,6 +161,76 @@ describe('todo-config', () => {
});
});

describe('ensureTodoConfig', () => {
it('does not change package.json if lintTodo directory present', () => {
ensureTodoStorageDirSync(project.baseDir);

const originalPkg = readFileSync(join(project.baseDir, 'package.json'), { encoding: 'utf8' });

ensureTodoConfig(project.baseDir);

const pkg = readFileSync(join(project.baseDir, 'package.json'), { encoding: 'utf8' });

expect(pkg).toEqual(originalPkg);
});

it('does not change package.json if todo config already present', () => {
ensureTodoStorageDirSync(project.baseDir);

writeTodoConfig(project.baseDir, {
warn: 10,
error: 20,
});

const originalPkg = readFileSync(join(project.baseDir, 'package.json'), { encoding: 'utf8' });

ensureTodoConfig(project.baseDir);

const pkg = readFileSync(join(project.baseDir, 'package.json'), { encoding: 'utf8' });

expect(pkg).toEqual(originalPkg);
});

it('does not change package.json if todo config is empty object', () => {
ensureTodoStorageDirSync(project.baseDir);

writeTodoConfig(project.baseDir, {});

const originalPkg = readFileSync(join(project.baseDir, 'package.json'), { encoding: 'utf8' });

ensureTodoConfig(project.baseDir);

const pkg = readFileSync(join(project.baseDir, 'package.json'), { encoding: 'utf8' });

expect(pkg).toEqual(originalPkg);
});

it('does change package.json if lintTodo directory not present', () => {
ensureTodoConfig(project.baseDir);

const pkg = readFileSync(join(project.baseDir, 'package.json'), { encoding: 'utf8' });

expect(pkg).toMatchInlineSnapshot(`
"{
\\"name\\": \\"fake-project\\",
\\"version\\": \\"0.0.0\\",
\\"keywords\\": [],
\\"license\\": \\"MIT\\",
\\"description\\": \\"Fake project\\",
\\"repository\\": \\"http://fakerepo.com\\",
\\"dependencies\\": {},
\\"devDependencies\\": {},
\\"lintTodo\\": {
\\"decayDays\\": {
\\"warn\\": 30,
\\"error\\": 60
}
}
}"
`);
});
});

describe('writeTodoConfig', () => {
it('does not change package.json if lintTodo config present', () => {
project.writeTodoConfig({
Expand Down
22 changes: 21 additions & 1 deletion src/todo-config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { join } from 'path';
import { TodoConfig } from './types';
import { readFileSync, writeFileSync } from 'fs-extra';
import { readFileSync, writeFileSync, readJsonSync } from 'fs-extra';
import { todoStorageDirExists } from './io';

const DETECT_TRAILING_WHITESPACE = /\s+$/;

Expand Down Expand Up @@ -53,6 +54,25 @@ export function getTodoConfig(
return mergedConfig;
}

/**
* Ensures that a valid todo config exists in the project by writing one to the package.json
* if we're invoking the todos functionality for the first time (there is no .lint-todo directory).
*
* @param baseDir - The base directory that contains the project's package.json.
*/
export function ensureTodoConfig(baseDir: string): void {
if (!todoStorageDirExists(baseDir)) {
const pkg = readJsonSync(join(baseDir, 'package.json'));

if (!pkg.lintTodo) {
writeTodoConfig(baseDir, {
warn: 30,
error: 60,
});
}
}
}

/**
* Writes a todo config to the package.json located at the provided baseDir.
*
Expand Down

0 comments on commit 242816b

Please sign in to comment.