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

infra(unicorn): prefer-top-level-await #2680

Merged
merged 2 commits into from
Feb 25, 2024
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
3 changes: 0 additions & 3 deletions .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
12 changes: 2 additions & 10 deletions scripts/apidoc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
26 changes: 12 additions & 14 deletions scripts/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});
137 changes: 63 additions & 74 deletions scripts/generate-locales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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);
17 changes: 7 additions & 10 deletions test/scripts/apidoc/signature.debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}