aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorST-DDT <[email protected]>2024-02-25 22:22:45 +0100
committerGitHub <[email protected]>2024-02-25 21:22:45 +0000
commitc9e817012a507b3545488b7f1cd8a76b6c20c21e (patch)
treee94f67b1dd394a47bf02e44a7ba7de5b3b9b2a52
parenta6eda6fbac913392d617a3a2e12cd283cba4736c (diff)
downloadfaker-c9e817012a507b3545488b7f1cd8a76b6c20c21e.tar.xz
faker-c9e817012a507b3545488b7f1cd8a76b6c20c21e.zip
infra(unicorn): prefer-top-level-await (#2680)
-rw-r--r--.eslintrc.cjs3
-rw-r--r--scripts/apidoc.ts12
-rw-r--r--scripts/diff.ts26
-rw-r--r--scripts/generate-locales.ts137
-rw-r--r--test/scripts/apidoc/signature.debug.ts17
5 files changed, 84 insertions, 111 deletions
diff --git a/.eslintrc.cjs b/.eslintrc.cjs
index e2a3731f..eb5c3e23 100644
--- a/.eslintrc.cjs
+++ b/.eslintrc.cjs
@@ -44,9 +44,6 @@ module.exports = defineConfig({
'unicorn/number-literal-case': 'off', // incompatible with prettier
'unicorn/prefer-ternary': 'off', // ternaries aren't always better
- // TODO @ST-DDT 2023-10-28: The following rule should be turned on when we switch to esm.
- 'unicorn/prefer-top-level-await': 'off',
-
// TODO @Shinigami92 2023-09-23: The following rules currently conflict with our code.
// Each rule should be checked whether it should be enabled/configured and the problems fixed, or stay disabled permanently.
'unicorn/better-regex': 'off',
diff --git a/scripts/apidoc.ts b/scripts/apidoc.ts
index d7c965ae..42eee5c3 100644
--- a/scripts/apidoc.ts
+++ b/scripts/apidoc.ts
@@ -3,13 +3,5 @@
import { generate } from './apidoc/generate';
import { initMarkdownRenderer } from './apidoc/markdown';
-async function build(): Promise<void> {
- await initMarkdownRenderer();
- await generate();
-}
-
-build().catch((error) => {
- // Workaround until top level await is available
- console.error(error);
- process.exit(1);
-});
+await initMarkdownRenderer();
+await generate();
diff --git a/scripts/diff.ts b/scripts/diff.ts
index a420a66c..813154f2 100644
--- a/scripts/diff.ts
+++ b/scripts/diff.ts
@@ -14,19 +14,17 @@ if (!source && !existsSync(pathDocsDiffIndexFile)) {
);
}
-diff(target, source)
- .then((delta) => {
- if (Object.keys(delta).length === 0) {
- console.log('No documentation changes detected');
- return;
- }
+await diff(target, source).then((delta) => {
+ if (Object.keys(delta).length === 0) {
+ console.log('No documentation changes detected');
+ return;
+ }
- console.log('Documentation changes detected:');
- for (const [module, methods] of Object.entries(delta)) {
- console.log(`- ${module}`);
- for (const method of methods) {
- console.log(` - ${method}`);
- }
+ console.log('Documentation changes detected:');
+ for (const [module, methods] of Object.entries(delta)) {
+ console.log(`- ${module}`);
+ for (const method of methods) {
+ console.log(` - ${method}`);
}
- })
- .catch(console.error);
+ }
+});
diff --git a/scripts/generate-locales.ts b/scripts/generate-locales.ts
index 4f35828f..90ea9a34 100644
--- a/scripts/generate-locales.ts
+++ b/scripts/generate-locales.ts
@@ -369,65 +369,61 @@ async function normalizeLocaleFile(filePath: string, definitionKey: string) {
// Start of actual logic
-async function main(): Promise<void> {
- const locales = readdirSync(pathLocales);
- removeIndexTs(locales);
-
- let localeIndexImports = '';
- let localeIndexExportsIndividual = '';
- let localeIndexExportsGrouped = '';
- let localesIndexExports = '';
-
- let localizationLocales =
- '| Locale | Name | Faker |\n| :--- | :--- | :--- |\n';
-
- for (const locale of locales) {
- const pathModules = resolve(pathLocales, locale);
- const pathMetadata = resolve(pathModules, 'metadata.ts');
- let localeTitle = 'No title found';
- try {
- const metadataImport = await import(`file:${pathMetadata}`);
- const metadata: MetadataDefinition = metadataImport.default;
- const { title } = metadata;
- if (!title) {
- throw new Error(
- `No title property found on ${JSON.stringify(metadata)}`
- );
- }
-
- localeTitle = title;
- } catch (error) {
- console.error(
- `Failed to load ${pathMetadata}. Please make sure the file exists and exports a MetadataDefinition.`
- );
- console.error(error);
+const locales = readdirSync(pathLocales);
+removeIndexTs(locales);
+
+let localeIndexImports = '';
+let localeIndexExportsIndividual = '';
+let localeIndexExportsGrouped = '';
+let localesIndexExports = '';
+
+let localizationLocales = '| Locale | Name | Faker |\n| :--- | :--- | :--- |\n';
+
+for (const locale of locales) {
+ const pathModules = resolve(pathLocales, locale);
+ const pathMetadata = resolve(pathModules, 'metadata.ts');
+ let localeTitle = 'No title found';
+ try {
+ const metadataImport = await import(`file:${pathMetadata}`);
+ const metadata: MetadataDefinition = metadataImport.default;
+ const { title } = metadata;
+ if (!title) {
+ throw new Error(`No title property found on ${JSON.stringify(metadata)}`);
}
- const localizedFaker = `faker${locale.replace(/^([a-z]+)/, (part) =>
- part.toUpperCase()
- )}`;
-
- localeIndexImports += `import { faker as ${localizedFaker} } from './${locale}';\n`;
- localeIndexExportsIndividual += ` ${localizedFaker},\n`;
- localeIndexExportsGrouped += ` ${locale}: ${localizedFaker},\n`;
- localesIndexExports += `export { default as ${locale} } from './${locale}';\n`;
- localizationLocales += `| \`${locale}\` | ${localeTitle} | \`${localizedFaker}\` |\n`;
-
- // src/locale/<locale>.ts
- await generateLocaleFile(locale);
-
- // src/locales/**/index.ts
- await generateRecursiveModuleIndexes(
- pathModules,
- locale,
- 'LocaleDefinition',
- 1
+ localeTitle = title;
+ } catch (error) {
+ console.error(
+ `Failed to load ${pathMetadata}. Please make sure the file exists and exports a MetadataDefinition.`
);
+ console.error(error);
}
- // src/locale/index.ts
+ const localizedFaker = `faker${locale.replace(/^([a-z]+)/, (part) =>
+ part.toUpperCase()
+ )}`;
+
+ localeIndexImports += `import { faker as ${localizedFaker} } from './${locale}';\n`;
+ localeIndexExportsIndividual += ` ${localizedFaker},\n`;
+ localeIndexExportsGrouped += ` ${locale}: ${localizedFaker},\n`;
+ localesIndexExports += `export { default as ${locale} } from './${locale}';\n`;
+ localizationLocales += `| \`${locale}\` | ${localeTitle} | \`${localizedFaker}\` |\n`;
+
+ // src/locale/<locale>.ts
+ await generateLocaleFile(locale);
+
+ // src/locales/**/index.ts
+ await generateRecursiveModuleIndexes(
+ pathModules,
+ locale,
+ 'LocaleDefinition',
+ 1
+ );
+}
+
+// src/locale/index.ts
- let localeIndexContent = `
+let localeIndexContent = `
${autoGeneratedCommentHeader}
${localeIndexImports}
@@ -441,34 +437,27 @@ async function main(): Promise<void> {
} as const;
`;
- localeIndexContent = await formatTypescript(localeIndexContent);
- writeFileSync(pathLocaleIndex, localeIndexContent);
+localeIndexContent = await formatTypescript(localeIndexContent);
+writeFileSync(pathLocaleIndex, localeIndexContent);
- // src/locales/index.ts
+// src/locales/index.ts
- let localesIndexContent = `
+let localesIndexContent = `
${autoGeneratedCommentHeader}
${localesIndexExports}
`;
- localesIndexContent = await formatTypescript(localesIndexContent);
- writeFileSync(pathLocalesIndex, localesIndexContent);
+localesIndexContent = await formatTypescript(localesIndexContent);
+writeFileSync(pathLocalesIndex, localesIndexContent);
- // docs/guide/localization.md
+// docs/guide/localization.md
- localizationLocales = await formatMarkdown(localizationLocales);
+localizationLocales = await formatMarkdown(localizationLocales);
- let localizationContent = readFileSync(pathDocsGuideLocalization, 'utf8');
- localizationContent = localizationContent.replaceAll(
- /(^<!-- LOCALES-AUTO-GENERATED-START -->$).*(^<!-- LOCALES-AUTO-GENERATED-END -->$)/gms,
- `$1\n\n<!-- Run '${scriptCommand}' to update. -->\n\n${localizationLocales}\n$2`
- );
- writeFileSync(pathDocsGuideLocalization, localizationContent);
-}
-
-main().catch((error) => {
- // Workaround until top level await is available
- console.error(error);
- process.exit(1);
-});
+let localizationContent = readFileSync(pathDocsGuideLocalization, 'utf8');
+localizationContent = localizationContent.replaceAll(
+ /(^<!-- LOCALES-AUTO-GENERATED-START -->$).*(^<!-- LOCALES-AUTO-GENERATED-END -->$)/gms,
+ `$1\n\n<!-- Run '${scriptCommand}' to update. -->\n\n${localizationLocales}\n$2`
+);
+writeFileSync(pathDocsGuideLocalization, localizationContent);
diff --git a/test/scripts/apidoc/signature.debug.ts b/test/scripts/apidoc/signature.debug.ts
index 9e99e8c6..704b629c 100644
--- a/test/scripts/apidoc/signature.debug.ts
+++ b/test/scripts/apidoc/signature.debug.ts
@@ -8,13 +8,10 @@ import { loadExampleMethods } from './utils';
/* Run with `pnpm tsx test/scripts/apidoc/signature.debug.ts` */
-initMarkdownRenderer()
- .then(async () => {
- const methods = await loadExampleMethods();
- for (const [name, method] of Object.entries(methods)) {
- console.log('Analyzing:', name);
- const result = await analyzeSignature(method, '', method.name);
- console.log('Result:', result);
- }
- })
- .catch(console.error);
+await initMarkdownRenderer();
+const methods = await loadExampleMethods();
+for (const [name, method] of Object.entries(methods)) {
+ console.log('Analyzing:', name);
+ const result = await analyzeSignature(method, '', method.name);
+ console.log('Result:', result);
+}