Skip to content

Commit

Permalink
fix: handle missing prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Oct 9, 2022
1 parent b7884d6 commit 9138249
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 26 deletions.
28 changes: 17 additions & 11 deletions lib/markdown.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
// General helpers for dealing with markdown files / content.

import prettier from 'prettier'; // eslint-disable-line node/no-extraneous-import -- prettier is included by eslint-plugin-square

export async function format(str: string, filePath: string): Promise<string> {
const options = await prettier.resolveConfig(filePath);
return prettier.format(str, {
...options,
parser: 'markdown',
});
try {
const { default: prettier } = await import('prettier');
const options = await prettier.resolveConfig(filePath);
return prettier
.format(str, {
...options,
parser: 'markdown',
})
.replace(/\s+$/, ''); // Trim the ending newline that prettier may add.
} catch {
// Skip prettier formatting if not installed.
/* istanbul ignore next -- TODO: Figure out how to test when prettier is not installed. */
return str;
}
}

/**
Expand All @@ -32,10 +39,9 @@ export async function replaceOrCreateHeader(
lines.splice(0, 1);
}

return (
(await format(newHeader, pathToDoc)) +
lines.slice(markerLineIndex + 1).join('\n')
);
return `${await format(newHeader, pathToDoc)}\n${lines
.slice(markerLineIndex + 1)
.join('\n')}`;
}

/**
Expand Down
17 changes: 9 additions & 8 deletions lib/rule-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,17 @@ export async function updateRulesList(
const columns = getColumns(details, plugin, configsToRules);

// New legend.
const legend = await format(
generateLegend(columns, urlConfigs),
pathToReadme
);
const legend = generateLegend(columns, urlConfigs);

// New rule list.
const list = await format(
generateRulesListMarkdown(columns, details, configsToRules, pluginPrefix),
pathToReadme
const list = generateRulesListMarkdown(
columns,
details,
configsToRules,
pluginPrefix
);

return `${preList}${BEGIN_RULE_LIST_MARKER}\n\n${legend}\n${list}\n${END_RULE_LIST_MARKER}${postList}`;
const newContent = await format(`${legend}\n\n${list}`, pathToReadme);

return `${preList}${BEGIN_RULE_LIST_MARKER}\n\n${newContent}\n\n${END_RULE_LIST_MARKER}${postList}`;
}
1 change: 1 addition & 0 deletions lib/rule-notices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ export function generateRuleHeaderLines(
pluginPrefix,
urlConfigs
),
'',
END_RULE_HEADER_MARKER,
].join('\n');
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
"typescript": "^4.8.4"
},
"peerDependencies": {
"eslint": ">= 7"
"eslint": ">= 7",
"prettier": ">= 2"
},
"engines": {
"node": "^14.18.0 || ^16.0.0 || >=18.0.0"
Expand Down
5 changes: 0 additions & 5 deletions test/lib/__snapshots__/generator-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
exports[`generator #generate CJS (non-ESM) generates the documentation 1`] = `
"<!-- begin rules list -->
| Rule | Description |
| ------------------------------ | ------------- |
| [no-foo](docs/rules/no-foo.md) | disallow foo. |
Expand All @@ -25,7 +24,6 @@ exports[`generator #generate Missing plugin package.json throws an error 1`] = `
exports[`generator #generate No configs found omits the config column 1`] = `
"<!-- begin rules list -->
| Rule | Description |
| ------------------------------ | ------------- |
| [no-foo](docs/rules/no-foo.md) | disallow foo. |
Expand All @@ -52,7 +50,6 @@ Foo.
## Rules
<!-- begin rules list -->
| Rule | Description |
| ------------------------------ | ---------------------- |
| [no-foo](docs/rules/no-foo.md) | Description of no-foo. |
Expand Down Expand Up @@ -344,7 +341,6 @@ exports[`generator #generate no rules with description generates the documentati
"## Rules
<!-- begin rules list -->
| Rule |
| ------------------------------ |
| [no-foo](docs/rules/no-foo.md) |
Expand All @@ -364,7 +360,6 @@ exports[`generator #generate one rule missing description generates the document
"## Rules
<!-- begin rules list -->
| Rule | Description |
| ------------------------------ | ----------------------- |
| [no-bar](docs/rules/no-bar.md) | |
Expand Down
2 changes: 1 addition & 1 deletion test/lib/generator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { dirname, join, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { readFileSync } from 'node:fs';
import { jest } from '@jest/globals';
import prettier from 'prettier'; // eslint-disable-line node/no-extraneous-import -- prettier is included by eslint-plugin-square
import prettier from 'prettier';
import * as sinon from 'sinon';

const __dirname = dirname(fileURLToPath(import.meta.url));
Expand Down

0 comments on commit 9138249

Please sign in to comment.