@alauda/doom 1.4.1 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/cli/lint.js CHANGED
@@ -1,13 +1,7 @@
1
- import cspellRecommended from '@cspell/eslint-plugin/recommended';
2
- import js from '@eslint/js';
3
- import react from '@eslint-react/eslint-plugin';
4
1
  import { logger } from '@rspress/shared/logger';
5
2
  import { Command } from 'commander';
6
- import { merge } from 'es-toolkit/compat';
7
3
  import { ESLint } from 'eslint';
8
- import * as mdx from 'eslint-plugin-mdx';
9
- import tseslint from 'typescript-eslint';
10
- import { parseTerms } from './helpers.js';
4
+ import doom from '../eslint.js';
11
5
  import { loadConfig } from './load-config.js';
12
6
  export const lintCommand = new Command('lint')
13
7
  .description('Lint the documentation')
@@ -16,59 +10,11 @@ export const lintCommand = new Command('lint')
16
10
  const globalOptions = this.optsWithGlobals();
17
11
  const { config } = await loadConfig(root, globalOptions);
18
12
  const docsDir = config.root;
19
- const parsedTerms = await parseTerms();
20
13
  const eslint = new ESLint({
21
14
  cwd: docsDir,
22
15
  overrideConfigFile: true,
23
16
  // @ts-expect-error -- stronger types
24
- overrideConfig: tseslint.config([
25
- {
26
- extends: [
27
- js.configs.recommended,
28
- react.configs.recommended,
29
- mdx.configs.flat,
30
- ],
31
- },
32
- {
33
- files: ['**/en/**/*.{js,jsx,md,mdx,ts,tsx}'],
34
- extends: [cspellRecommended],
35
- rules: {
36
- '@cspell/spellchecker': [
37
- 'error',
38
- merge({
39
- autoFix: true,
40
- cspell: {
41
- allowCompoundWords: true,
42
- words: parsedTerms.map((it) => it.en),
43
- flagWords: parsedTerms.flatMap(({ badCases }) => badCases?.en ?? []),
44
- },
45
- }, config.lint?.cspellOptions),
46
- ],
47
- },
48
- },
49
- {
50
- files: ['**/*.{ts,tsx}'],
51
- extends: [
52
- tseslint.configs.recommendedTypeChecked,
53
- react.configs['recommended-typescript'],
54
- ],
55
- rules: {
56
- '@typescript-eslint/no-misused-promises': 'off',
57
- '@typescript-eslint/no-non-null-assertion': 'off',
58
- '@typescript-eslint/restrict-template-expressions': [
59
- 'error',
60
- { allowNumber: true },
61
- ],
62
- 'prefer-const': ['error', { destructuring: 'all' }],
63
- },
64
- languageOptions: {
65
- parser: tseslint.parser,
66
- parserOptions: {
67
- projectService: true,
68
- },
69
- },
70
- },
71
- ]),
17
+ overrideConfig: await doom(config.lint?.cspellOptions),
72
18
  });
73
19
  logger.start('Linting...');
74
20
  const results = await eslint.lintFiles('**/*.{js,jsx,ts,tsx,md,mdx}');
@@ -1,7 +1,7 @@
1
1
  import { type UserConfig } from '@rspress/core';
2
2
  import type { GlobalCliOptions } from '../types.js';
3
- export declare function loadConfig(root?: string, { config: configFile, base, prefix, v: version, download, export: export_, ignore, force, open, lazy, include, exclude, outDir, redirect, editRepo, algolia, siteUrl, }?: GlobalCliOptions): Promise<{
3
+ export declare function loadConfig(root?: string | URL, { config: configFile, base, prefix, v: version, download, export: export_, ignore, force, open, lazy, include, exclude, outDir, redirect, editRepo, algolia, siteUrl, }?: GlobalCliOptions): Promise<{
4
4
  config: UserConfig;
5
5
  filepath?: string;
6
6
  }>;
7
- export declare function resolveDocRoot(cwd: string, cliRoot?: string, configRoot?: string): string;
7
+ export declare function resolveDocRoot(cliRoot?: string, configRoot?: string): string;
@@ -1,5 +1,6 @@
1
1
  import fs from 'node:fs/promises';
2
2
  import path from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
3
4
  import { pluginReact } from '@rsbuild/plugin-react';
4
5
  import { pluginSass } from '@rsbuild/plugin-sass';
5
6
  import { pluginSvgr } from '@rsbuild/plugin-svgr';
@@ -14,7 +15,7 @@ import { cyan } from 'yoctocolors';
14
15
  import { apiPlugin, autoSidebarPlugin, autoTocPlugin, createTransformerCallouts, directivesPlugin, globalPlugin, mermaidPlugin, permissionPlugin, replacePlugin, sitemapPlugin, } from '../plugins/index.js';
15
16
  import { isExplicitlyUnversioned, UNVERSIONED, } from '../shared/index.js';
16
17
  import { pathExists, pkgResolve, resolveStaticConfig } from '../utils/index.js';
17
- import { CWD, DEFAULT_CONFIG_NAME, DEFAULT_EXTENSIONS, I18N_FILE, SITES_FILE, YAML_EXTENSIONS, } from './constants.js';
18
+ import { DEFAULT_CONFIG_NAME, DEFAULT_EXTENSIONS, I18N_FILE, SITES_FILE, YAML_EXTENSIONS, } from './constants.js';
18
19
  import { defaultGitHubUrl } from './helpers.js';
19
20
  const DEFAULT_LOGO = '/logo.svg';
20
21
  const KNOWN_LOCALE_CONFIGS = {
@@ -61,7 +62,7 @@ const ALAUDA_RU_ALGOLIA_OPTIONS = {
61
62
  };
62
63
  const getCommonConfig = async ({ config, root, configFilePath, base, version, download, export: export_, ignore, force, open, lazy, include, exclude, redirect, editRepo, algolia, siteUrl, }) => {
63
64
  const fallbackToZh = 'lang' in config && !config.lang;
64
- root = resolveDocRoot(CWD, root, config.root);
65
+ root = resolveDocRoot(root, config.root);
65
66
  const localBasePath = configFilePath ? path.dirname(configFilePath) : root;
66
67
  const userBase = (base = addLeadingSlash(addTrailingSlash(base || config.base || '/')));
67
68
  if (version && !isExplicitlyUnversioned(version)) {
@@ -246,6 +247,10 @@ export async function loadConfig(root, { config: configFile, base, prefix, v: ve
246
247
  }
247
248
  else {
248
249
  if (root) {
250
+ root = root.toString();
251
+ if (root.startsWith('file:')) {
252
+ root = fileURLToPath(root);
253
+ }
249
254
  configFilePath = await findConfig(path.resolve(root, DEFAULT_CONFIG_NAME));
250
255
  }
251
256
  if (!configFilePath) {
@@ -292,7 +297,7 @@ export async function loadConfig(root, { config: configFile, base, prefix, v: ve
292
297
  const commonConfig = await getCommonConfig({
293
298
  config,
294
299
  configFilePath,
295
- root,
300
+ root: root,
296
301
  base,
297
302
  version,
298
303
  download,
@@ -353,15 +358,15 @@ export async function loadConfig(root, { config: configFile, base, prefix, v: ve
353
358
  filepath: configFilePath,
354
359
  };
355
360
  }
356
- export function resolveDocRoot(cwd, cliRoot, configRoot) {
361
+ export function resolveDocRoot(cliRoot, configRoot) {
357
362
  // CLI root has highest priority
358
363
  if (cliRoot) {
359
- return path.join(cwd, cliRoot);
364
+ return path.resolve(cliRoot);
360
365
  }
361
366
  // Config root is next in priority
362
367
  if (configRoot) {
363
- return path.isAbsolute(configRoot) ? configRoot : path.join(cwd, configRoot);
368
+ return path.isAbsolute(configRoot) ? configRoot : path.resolve(configRoot);
364
369
  }
365
370
  // Default to 'docs' if no root is specified
366
- return path.join(cwd, 'docs');
371
+ return path.resolve('docs');
367
372
  }
@@ -0,0 +1,2 @@
1
+ import type { CSpellSettings } from '@cspell/cspell-types';
2
+ export default function doom(): Promise<CSpellSettings>;
package/lib/cspell.js ADDED
@@ -0,0 +1,67 @@
1
+ import fs from 'node:fs/promises';
2
+ import path from 'node:path';
3
+ import { parseTerms } from './cli/helpers.js';
4
+ const resolveDictionaries = async () => {
5
+ const dictionariesDir = path.resolve('.cspell');
6
+ try {
7
+ if (!(await fs.stat(dictionariesDir)).isDirectory()) {
8
+ return;
9
+ }
10
+ }
11
+ catch {
12
+ return;
13
+ }
14
+ const dictionaries = [];
15
+ const dictionaryDefinitions = [];
16
+ for (const file of await fs.readdir(dictionariesDir, {
17
+ withFileTypes: true,
18
+ })) {
19
+ if (!file.isFile()) {
20
+ continue;
21
+ }
22
+ const dictionary = path.parse(file.name).name;
23
+ dictionaries.push(dictionary);
24
+ dictionaryDefinitions.push({
25
+ name: dictionary,
26
+ path: `.cspell/${file.name}`,
27
+ addWords: true,
28
+ });
29
+ }
30
+ return { dictionaries, dictionaryDefinitions };
31
+ };
32
+ const CASE_SENSITIVE_DICTIONARY = '$$case-sensitive$$';
33
+ export default async function doom() {
34
+ const parsedTerms = await parseTerms();
35
+ const words = [];
36
+ const caseSensitiveWords = [];
37
+ const flagWords = parsedTerms.flatMap((it) => {
38
+ const word = it.en;
39
+ const badCases = it.badCases?.en ?? [];
40
+ if (badCases.some((c) => word !== c && word.toLowerCase() === c.toLowerCase())) {
41
+ caseSensitiveWords.push(it);
42
+ return [];
43
+ }
44
+ words.push(word);
45
+ return badCases.map((c) => `${c}->${word}`);
46
+ });
47
+ const { dictionaries = [], dictionaryDefinitions = [] } = (await resolveDictionaries()) ?? {};
48
+ if (caseSensitiveWords.length) {
49
+ dictionaries.push(CASE_SENSITIVE_DICTIONARY);
50
+ dictionaryDefinitions.push({
51
+ name: CASE_SENSITIVE_DICTIONARY,
52
+ words: caseSensitiveWords.flatMap((it) => [
53
+ it.en,
54
+ ...(it.badCases?.en ?? []).map((c) => `!${c}`),
55
+ ]),
56
+ });
57
+ }
58
+ return {
59
+ allowCompoundWords: true,
60
+ language: 'en,en-GB,en-US',
61
+ useGitignore: true,
62
+ words,
63
+ flagWords,
64
+ dictionaries,
65
+ dictionaryDefinitions,
66
+ };
67
+ }
@@ -0,0 +1,5 @@
1
+ import type { Options } from '@cspell/eslint-plugin';
2
+ import tseslint from 'typescript-eslint';
3
+ declare function doom(cspellOptions?: Options): Promise<tseslint.ConfigArray>;
4
+ declare function doom(root: string | URL): Promise<tseslint.ConfigArray>;
5
+ export default doom;
package/lib/eslint.js ADDED
@@ -0,0 +1,76 @@
1
+ import cspellRecommended from '@cspell/eslint-plugin/recommended';
2
+ import js from '@eslint/js';
3
+ import react from '@eslint-react/eslint-plugin';
4
+ import { merge } from 'es-toolkit/compat';
5
+ import * as mdx from 'eslint-plugin-mdx';
6
+ import globals from 'globals';
7
+ import tseslint from 'typescript-eslint';
8
+ import { loadConfig } from './cli/load-config.js';
9
+ async function doom(cspellOptionsOrRoot) {
10
+ let cspellOptions;
11
+ if (typeof cspellOptionsOrRoot === 'string' ||
12
+ cspellOptionsOrRoot instanceof URL) {
13
+ const { config } = await loadConfig(cspellOptionsOrRoot);
14
+ cspellOptions = config.lint?.cspellOptions;
15
+ }
16
+ else {
17
+ cspellOptions = cspellOptionsOrRoot;
18
+ }
19
+ return tseslint.config([
20
+ {
21
+ ignores: ['**/.yarn', '**/dist', '**/lib', '**/node_modules'],
22
+ },
23
+ {
24
+ extends: [
25
+ js.configs.recommended,
26
+ react.configs.recommended,
27
+ mdx.configs.flat,
28
+ ],
29
+ languageOptions: {
30
+ globals: globals.node,
31
+ },
32
+ },
33
+ {
34
+ files: ['**/en/**/*.{js,jsx,md,mdx,ts,tsx}'],
35
+ extends: [cspellRecommended],
36
+ languageOptions: {
37
+ globals: globals.browser,
38
+ },
39
+ rules: {
40
+ '@cspell/spellchecker': [
41
+ 'error',
42
+ merge({ autoFix: true }, cspellOptions),
43
+ ],
44
+ },
45
+ },
46
+ {
47
+ files: ['**/*.mdx'],
48
+ rules: {
49
+ 'no-unused-expressions': 'off',
50
+ },
51
+ },
52
+ {
53
+ files: ['**/*.{ts,tsx}'],
54
+ extends: [
55
+ tseslint.configs.recommendedTypeChecked,
56
+ react.configs['recommended-typescript'],
57
+ ],
58
+ rules: {
59
+ '@typescript-eslint/no-misused-promises': 'off',
60
+ '@typescript-eslint/no-non-null-assertion': 'off',
61
+ '@typescript-eslint/restrict-template-expressions': [
62
+ 'error',
63
+ { allowNumber: true },
64
+ ],
65
+ 'prefer-const': ['error', { destructuring: 'all' }],
66
+ },
67
+ languageOptions: {
68
+ parser: tseslint.parser,
69
+ parserOptions: {
70
+ projectService: true,
71
+ },
72
+ },
73
+ },
74
+ ]);
75
+ }
76
+ export default doom;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alauda/doom",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "type": "module",
5
5
  "description": "Doctor Doom making docs.",
6
6
  "repository": "git+https://github.com/alauda/doom.git",
@@ -13,6 +13,8 @@
13
13
  "exports": {
14
14
  "./package.json": "./package.json",
15
15
  "./config": "./lib/config.js",
16
+ "./cspell": "./lib/cspell.js",
17
+ "./eslint": "./lib/eslint.js",
16
18
  "./runtime": "./lib/runtime/index.js",
17
19
  "./theme": "./lib/theme.js",
18
20
  "./types": "./lib/types.js"
@@ -56,6 +58,7 @@
56
58
  "es-toolkit": "^1.39.3",
57
59
  "eslint": "^9.29.0",
58
60
  "eslint-plugin-mdx": "^3.5.0",
61
+ "globals": "^16.2.0",
59
62
  "html-tag-names": "^2.1.0",
60
63
  "mdast-util-mdx": "^3.0.0",
61
64
  "mdast-util-mdx-jsx": "^3.2.0",