Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle lowercase readme.md #106

Merged
merged 1 commit into from
Oct 10, 2022
Merged
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
31 changes: 16 additions & 15 deletions lib/generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { existsSync, readFileSync, writeFileSync } from 'node:fs';
import { join, resolve, relative } from 'node:path';
import { join, relative, resolve } from 'node:path';
import { getAllNamedOptions, hasOptions } from './rule-options.js';
import { loadPlugin, getPluginPrefix, getPluginRoot } from './package-json.js';
import {
loadPlugin,
getPluginPrefix,
getPluginRoot,
getPathWithExactFileNameCasing,
} from './package-json.js';
import { updateRulesList } from './rule-list.js';
import { generateRuleHeaderLines } from './rule-notices.js';
import { END_RULE_HEADER_MARKER } from './markers.js';
Expand Down Expand Up @@ -80,11 +85,6 @@ export async function generate(
const pluginPrefix = getPluginPrefix(path);
const configsToRules = await resolveConfigsToRules(plugin);

const pathTo = {
readme: resolve(path, 'README.md'),
docs: resolve(path, 'docs'),
};

if (!plugin.rules) {
throw new Error('Could not find exported `rules` object in ESLint plugin.');
}
Expand Down Expand Up @@ -123,7 +123,7 @@ export async function generate(

// Update rule doc for each rule.
for (const { name, description, schema } of details) {
const pathToDoc = join(pathTo.docs, 'rules', `${name}.md`);
const pathToDoc = join(resolve(path, 'docs'), 'rules', `${name}.md`);

if (!existsSync(pathToDoc)) {
throw new Error(
Expand Down Expand Up @@ -177,22 +177,23 @@ export async function generate(
}
}

if (!existsSync(pathTo.readme)) {
throw new Error(
`Could not find README: ${relative(getPluginRoot(path), pathTo.readme)}`
);
// Find the README.
const pathToReadme = getPathWithExactFileNameCasing(path, 'README.md');
if (!pathToReadme || !existsSync(pathToReadme)) {
throw new Error('Could not find README.md in ESLint plugin root.');
}

// Update the rules list in the README.
const readme = await updateRulesList(
details,
readFileSync(pathTo.readme, 'utf8'),
readFileSync(pathToReadme, 'utf8'),
plugin,
configsToRules,
pluginPrefix,
pathTo.readme,
pathToReadme,
path,
options?.ignoreConfig,
options?.urlConfigs
);
writeFileSync(pathTo.readme, readme, 'utf8');
writeFileSync(pathToReadme, readme, 'utf8');
}
23 changes: 21 additions & 2 deletions lib/package-json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { join } from 'node:path';
import { existsSync, readFileSync } from 'node:fs';
import { join, resolve } from 'node:path';
import { existsSync, readFileSync, readdirSync } from 'node:fs';
import { importAbs } from './import.js';
import type { Plugin } from './types.js';
import type { PackageJson } from 'type-fest';
Expand Down Expand Up @@ -44,3 +44,22 @@ export function getPluginPrefix(path: string): string {
? pluginPackageJson.name.split('/')[0] // Scoped plugin name like @my-scope/eslint-plugin.
: pluginPackageJson.name.replace('eslint-plugin-', ''); // Unscoped name like eslint-plugin-foo.
}

/**
* Resolve the path to a file but with the exact filename-casing present on disk.
*/
export function getPathWithExactFileNameCasing(
dir: string,
fileNameToSearch: string
) {
const filenames = readdirSync(dir, { withFileTypes: true });
for (const dirent of filenames) {
if (
dirent.isFile() &&
dirent.name.toLowerCase() === fileNameToSearch.toLowerCase()
) {
return resolve(dir, dirent.name);
}
}
return undefined; // eslint-disable-line unicorn/no-useless-undefined
}
8 changes: 7 additions & 1 deletion lib/rule-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import {
import { getConfigsForRule, hasAnyConfigs } from './configs.js';
import { COLUMN_TYPE, getColumns, COLUMN_HEADER } from './rule-list-columns.js';
import { findSectionHeader, format } from './markdown.js';
import { getPluginRoot } from './package-json.js';
import { generateLegend } from './legend.js';
import { relative } from 'node:path';
import type { Plugin, RuleDetails, ConfigsToRules } from './types.js';

function getConfigurationColumnValueForRule(
Expand Down Expand Up @@ -132,6 +134,7 @@ export async function updateRulesList(
configsToRules: ConfigsToRules,
pluginPrefix: string,
pathToReadme: string,
pathToPlugin: string,
ignoreConfig?: string[],
urlConfigs?: string
): Promise<string> {
Expand Down Expand Up @@ -160,7 +163,10 @@ export async function updateRulesList(

if (listStartIndex === -1 || listEndIndex === -1) {
throw new Error(
`README.md is missing rules list markers: ${BEGIN_RULE_LIST_MARKER}${END_RULE_LIST_MARKER}`
`${relative(
getPluginRoot(pathToPlugin),
pathToReadme
)} is missing rules list markers: ${BEGIN_RULE_LIST_MARKER}${END_RULE_LIST_MARKER}`
);
}

Expand Down
10 changes: 10 additions & 0 deletions test/lib/__snapshots__/generator-test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ exports[`generator #generate deprecated rules updates the documentation 4`] = `
"
`;

exports[`generator #generate lowercase README file generates the documentation 1`] = `
"<!-- begin rules list -->

| Name |
| :----------------------------- |
| [no-foo](docs/rules/no-foo.md) |

<!-- end rules list -->"
`;

exports[`generator #generate no existing comment markers - minimal doc content generates the documentation 1`] = `
"## Rules
<!-- begin rules list -->
Expand Down
39 changes: 38 additions & 1 deletion test/lib/generator-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,48 @@ describe('generator', function () {

it('throws an error', async function () {
await expect(generate('.')).rejects.toThrow(
'Could not find README: README.md'
'Could not find README.md in ESLint plugin root.'
);
});
});

describe('lowercase README file', function () {
beforeEach(function () {
mockFs({
'package.json': JSON.stringify({
name: 'eslint-plugin-test',
main: 'index.js',
type: 'module',
}),

'index.js': `
export default {
rules: {
'no-foo': { meta: { }, create(context) {} },
},
};`,

'readme.md': '<!-- begin rules list --><!-- end rules list -->',
'docs/rules/no-foo.md': '',

// Needed for some of the test infrastructure to work.
node_modules: mockFs.load(
resolve(__dirname, '..', '..', 'node_modules')
),
});
});

afterEach(function () {
mockFs.restore();
jest.resetModules();
});

it('generates the documentation', async function () {
await generate('.');
expect(readFileSync('readme.md', 'utf8')).toMatchSnapshot();
});
});

describe('adds extra column to rules table for TypeScript rules', function () {
beforeEach(function () {
mockFs({
Expand Down