Skip to content

Commit 921a451

Browse files
authored
Merge branch 'main' into refactor-datatype-number
2 parents 1e9d211 + 0dfe9a3 commit 921a451

28 files changed

+2528
-383
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"build": "run-s build:clean build:code build:types",
5858
"generate:api-docs": "esno ./scripts/apidoc.ts",
5959
"generate:locales": "esno ./scripts/generateLocales.ts",
60+
"copy:mime-types": "esno ./scripts/copyMimeTypes.ts",
6061
"docs:build": "run-s docs:prepare docs:build:run",
6162
"docs:build:run": "vitepress build docs",
6263
"docs:build:ci": "run-s build docs:build",
@@ -108,6 +109,7 @@
108109
"eslint-plugin-prettier": "~4.0.0",
109110
"esno": "~0.14.1",
110111
"lint-staged": "~12.3.7",
112+
"mime-db": "~1.52.0",
111113
"npm-run-all": "~4.1.5",
112114
"picocolors": "~1.0.0",
113115
"prettier": "2.6.2",

pnpm-lock.yaml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/apidoc/apiDocsWriter.ts

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { writeFileSync } from 'node:fs';
22
import { resolve } from 'node:path';
3-
import type { Options } from 'prettier';
4-
import { format } from 'prettier';
5-
import prettierConfig from '../../.prettierrc.cjs';
63
import type { Method } from '../../docs/.vitepress/components/api-docs/method';
74
import type { PageIndex } from './utils';
8-
import { pathDocsDir, pathOutputDir } from './utils';
5+
import {
6+
formatMarkdown,
7+
formatTypescript,
8+
pathDocsDir,
9+
pathOutputDir,
10+
} from './utils';
911

1012
const pathDocsApiPages = resolve(pathDocsDir, '.vitepress', 'api-pages.ts');
1113

@@ -18,21 +20,6 @@ editLink: false
1820
1921
`;
2022

21-
const prettierMarkdown: Options = {
22-
...prettierConfig,
23-
parser: 'markdown',
24-
};
25-
26-
const prettierTypescript: Options = {
27-
...prettierConfig,
28-
parser: 'typescript',
29-
};
30-
31-
const prettierBabel: Options = {
32-
...prettierConfig,
33-
parser: 'babel',
34-
};
35-
3623
/**
3724
* Writes the api page for the given module to the correct location.
3825
*
@@ -75,7 +62,7 @@ export function writeApiDocsModulePage(
7562
<ApiDocsMethod v-for="method of methods" :key="method.name" :method="method" v-once />
7663
`.replace(/\n +/g, '\n');
7764

78-
content = vitePressInFileOptions + format(content, prettierMarkdown);
65+
content = vitePressInFileOptions + formatMarkdown(content);
7966

8067
writeFileSync(resolve(pathOutputDir, lowerModuleName + '.md'), content);
8168
}
@@ -98,7 +85,7 @@ export function writeApiDocsDirectPage(methodName: string): void {
9885
<ApiDocsMethod v-for="method of methods" :key="method.name" :method="method" v-once />
9986
`.replace(/\n +/g, '\n');
10087

101-
content = vitePressInFileOptions + format(content, prettierMarkdown);
88+
content = vitePressInFileOptions + formatMarkdown(content);
10289

10390
writeFileSync(resolve(pathOutputDir, methodName + '.md'), content);
10491
}
@@ -122,7 +109,7 @@ export const ${lowerModuleName}: Method[] = ${JSON.stringify(
122109
2
123110
)}`;
124111

125-
contentTs = format(contentTs, prettierTypescript);
112+
contentTs = formatTypescript(contentTs);
126113

127114
writeFileSync(resolve(pathOutputDir, lowerModuleName + '.ts'), contentTs);
128115
}
@@ -142,7 +129,7 @@ export function writeApiPagesIndex(pages: PageIndex): void {
142129
export const apiPages = ${JSON.stringify(pages)};
143130
`.replace(/\n +/, '\n');
144131

145-
apiPagesContent = format(apiPagesContent, prettierBabel);
132+
apiPagesContent = formatTypescript(apiPagesContent);
146133

147134
writeFileSync(pathDocsApiPages, apiPagesContent);
148135
}

scripts/apidoc/moduleMethods.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as TypeDoc from 'typedoc';
22
import type { Method } from '../../docs/.vitepress/components/api-docs/method';
3-
import faker from '../../src';
3+
import { faker } from '../../src';
44
import { writeApiDocsData, writeApiDocsModulePage } from './apiDocsWriter';
55
import { analyzeSignature, toBlock } from './signature';
66
import type { PageIndex } from './utils';

scripts/apidoc/signature.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import type {
1414
Method,
1515
MethodParameter,
1616
} from '../../docs/.vitepress/components/api-docs/method';
17-
import faker from '../../src';
18-
import { pathOutputDir } from './utils';
17+
import { faker } from '../../src';
18+
import { formatTypescript, pathOutputDir } from './utils';
1919
// TODO ST-DDT 2022-02-20: Actually import this/fix module import errors
2020
// import vitepressConfig from '../../docs/.vitepress/config';
2121

@@ -228,6 +228,11 @@ function typeToText(type_: Type, short = false): string {
228228
case 'reference':
229229
if (!type.typeArguments || !type.typeArguments.length) {
230230
return type.name;
231+
} else if (type.name === 'LiteralUnion') {
232+
return [
233+
typeToText(type.typeArguments[0]),
234+
typeToText(type.typeArguments[1]),
235+
].join(' | ');
231236
} else {
232237
return `${type.name}<${type.typeArguments
233238
.map((t) => typeToText(t, short))
@@ -240,6 +245,8 @@ function typeToText(type_: Type, short = false): string {
240245
type.indexType,
241246
short
242247
)}]`;
248+
case 'literal':
249+
return formatTypescript(type.toString()).replace(/;\n$/, '');
243250
default:
244251
return type.toString();
245252
}

scripts/apidoc/utils.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import { resolve } from 'node:path';
2+
import type { Options } from 'prettier';
3+
import { format } from 'prettier';
24
import * as TypeDoc from 'typedoc';
5+
import prettierConfig from '../../.prettierrc.cjs';
36
import {
47
DefaultParameterAwareSerializer,
58
parameterDefaultReader,
@@ -44,3 +47,31 @@ export function newTypeDocApp(): TypeDoc.Application {
4447
export function patchProject(project: TypeDoc.ProjectReflection): void {
4548
patchProjectParameterDefaults(project);
4649
}
50+
51+
/**
52+
* Formats markdown contents.
53+
*
54+
* @param text The text to format.
55+
*/
56+
export function formatMarkdown(text: string): string {
57+
return format(text, prettierMarkdown);
58+
}
59+
60+
/**
61+
* Formats typedoc contents.
62+
*
63+
* @param text The text to format.
64+
*/
65+
export function formatTypescript(text: string): string {
66+
return format(text, prettierTypescript);
67+
}
68+
69+
const prettierMarkdown: Options = {
70+
...prettierConfig,
71+
parser: 'markdown',
72+
};
73+
74+
const prettierTypescript: Options = {
75+
...prettierConfig,
76+
parser: 'typescript',
77+
};

scripts/copyMimeTypes.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import type { Options } from 'prettier';
4+
import { format } from 'prettier';
5+
import options from '../.prettierrc.cjs';
6+
7+
const rootPath = path.resolve(__dirname, '..');
8+
const mimeDbPath = path.resolve(rootPath, 'node_modules/mime-db/db.json');
9+
const mimeDbLicencePath = path.resolve(
10+
rootPath,
11+
'node_modules/mime-db/LICENSE'
12+
);
13+
const mimeTypesTsPath = path.resolve(
14+
rootPath,
15+
'src/locales/en/system/mimeTypes.ts'
16+
);
17+
const prettierTsOptions: Options = { ...options, parser: 'typescript' };
18+
fs.readFile(mimeDbPath, 'utf8', (err, data) => {
19+
if (err) {
20+
throw err;
21+
}
22+
23+
const licence = fs.readFileSync(mimeDbLicencePath, { encoding: 'utf8' });
24+
const mimeTypeFileContent = `// This file is generated by scripts/copyMimeTypes.ts\n// Do not edit this file directly. Instead, update mime-db and run \`pnpm run copy:mime-types\`\n\n/*\n${
25+
licence as string
26+
}*/\n\nexport default ${data as string};\n`;
27+
28+
fs.writeFile(
29+
mimeTypesTsPath,
30+
format(mimeTypeFileContent, prettierTsOptions),
31+
(err) => {
32+
if (err) {
33+
throw err;
34+
}
35+
36+
console.log(`Mime types copied to ${mimeTypesTsPath as string}`);
37+
}
38+
);
39+
});

src/address.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export class Address {
128128
if (zipRange) {
129129
return String(this.faker.datatype.number(zipRange));
130130
}
131-
return this.faker.address.zipCode();
131+
return this.zipCode();
132132
}
133133

134134
/**
@@ -228,7 +228,7 @@ export class Address {
228228
*/
229229
streetName(): string {
230230
let result: string;
231-
let suffix = this.faker.address.streetSuffix();
231+
let suffix = this.streetSuffix();
232232
if (suffix !== '') {
233233
suffix = ' ' + suffix;
234234
}
@@ -498,7 +498,7 @@ export class Address {
498498
): [string, string] {
499499
// If there is no coordinate, the best we can do is return a random GPS coordinate.
500500
if (coordinate === undefined) {
501-
return [this.faker.address.latitude(), this.faker.address.longitude()];
501+
return [this.latitude(), this.longitude()];
502502
}
503503

504504
radius = radius || 10.0;

src/commerce.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ export class Commerce {
4646
*/
4747
productName(): string {
4848
return (
49-
this.faker.commerce.productAdjective() +
49+
this.productAdjective() +
5050
' ' +
51-
this.faker.commerce.productMaterial() +
51+
this.productMaterial() +
5252
' ' +
53-
this.faker.commerce.product()
53+
this.product()
5454
);
5555
}
5656

src/company.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
import type { Faker } from '.';
2-
import type { Fake } from './fake';
3-
4-
let f: Fake['fake'];
52

63
/**
74
* Module to generate company related entries.
85
*/
96
export class Company {
107
constructor(private readonly faker: Faker) {
11-
f = this.faker.fake;
12-
138
// Bind `this` so namespaced is working correctly
149
for (const name of Object.getOwnPropertyNames(Company.prototype)) {
1510
if (name === 'constructor' || typeof this[name] !== 'function') {
@@ -49,7 +44,7 @@ export class Company {
4944
format = this.faker.datatype.number(formats.length - 1);
5045
}
5146

52-
return f(formats[format]);
47+
return this.faker.fake(formats[format]);
5348
}
5449

5550
/**
@@ -59,7 +54,7 @@ export class Company {
5954
* faker.company.companySuffix() // 'and Sons'
6055
*/
6156
companySuffix(): string {
62-
return this.faker.random.arrayElement(this.faker.company.suffixes());
57+
return this.faker.random.arrayElement(this.suffixes());
6358
}
6459

6560
/**
@@ -69,7 +64,7 @@ export class Company {
6964
* faker.company.catchPhrase() // 'Upgradable systematic flexibility'
7065
*/
7166
catchPhrase(): string {
72-
return f(
67+
return this.faker.fake(
7368
'{{company.catchPhraseAdjective}} {{company.catchPhraseDescriptor}} {{company.catchPhraseNoun}}'
7469
);
7570
}
@@ -81,7 +76,9 @@ export class Company {
8176
* faker.company.bs() // 'cultivate synergistic e-markets'
8277
*/
8378
bs(): string {
84-
return f('{{company.bsBuzz}} {{company.bsAdjective}} {{company.bsNoun}}');
79+
return this.faker.fake(
80+
'{{company.bsBuzz}} {{company.bsAdjective}} {{company.bsNoun}}'
81+
);
8582
}
8683

8784
/**

0 commit comments

Comments
 (0)