Skip to content

Commit f001281

Browse files
committed
Use ESM
1 parent 805ebae commit f001281

File tree

13 files changed

+211
-136
lines changed

13 files changed

+211
-136
lines changed

packages/tsconfig-reference/package.json

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
"license": "MIT",
55
"private": true,
66
"scripts": {
7-
"generate:json:tsconfig": "yarn ts-node -T scripts/tsconfig/generateJSON.ts",
8-
"generate:json:cli": "yarn ts-node -T scripts/cli/generateJSON.ts",
9-
"generate:json:schema": "yarn ts-node -T scripts/schema/generateJSON.ts",
10-
"generate:json:msbuild": "yarn ts-node -T scripts/msbuild/generateJSON.ts",
7+
"generate:json:tsconfig": "tsc --build && node --experimental-json-modules scripts/tsconfig/generateJSON",
8+
"generate:json:cli": "tsc --build && node --experimental-json-modules scripts/cli/generateJSON",
9+
"generate:json:schema": "tsc --build && node --experimental-json-modules scripts/schema/generateJSON",
10+
"generate:json:msbuild": "tsc --build && node --experimental-json-modules scripts/msbuild/generateJSON",
1111
"generate:json": "yarn generate:json:tsconfig && yarn generate:json:cli && yarn generate:json:schema && yarn generate:json:msbuild",
12-
"generate:md:tsconfig": "yarn ts-node -T scripts/tsconfig/generateMarkdown.ts",
13-
"generate:md:cli": "yarn ts-node -T scripts/cli/generateMarkdown.ts",
14-
"generate:md:msbuild": "yarn ts-node -T scripts/msbuild/generateMarkdown.ts",
12+
"generate:md:tsconfig": "tsc --build && node --experimental-json-modules scripts/tsconfig/generateMarkdown",
13+
"generate:md:cli": "tsc --build && node --experimental-json-modules scripts/cli/generateMarkdown",
14+
"generate:md:msbuild": "tsc --build && node --experimental-json-modules scripts/msbuild/generateMarkdown",
1515
"generate:md": "yarn generate:md:tsconfig && yarn generate:md:cli && yarn generate:md:tsconfig",
1616
"bootstrap": "yarn scripts/schema/downloadSchemaBase.ts",
1717
"build": "yarn generate:json && yarn generate:md",
@@ -26,5 +26,6 @@
2626
"remark": "^11.0.2",
2727
"remark-html": "^10.0.0",
2828
"ts-node": "*"
29-
}
29+
},
30+
"type": "module"
3031
}

packages/tsconfig-reference/scripts/cli/generateJSON.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,33 @@
77
*/
88
console.log("TSConfig Ref: JSON for CLI Opts");
99

10-
import * as ts from "typescript";
10+
import ts from "typescript";
1111

1212
import { CommandLineOptionBase } from "../types";
1313
import { writeFileSync } from "fs";
1414
import { join } from "path";
15-
import { format } from "prettier";
15+
import prettier from "prettier";
1616
import {
1717
deprecated,
1818
internal,
1919
defaultsForOptions,
2020
allowedValues,
2121
configToRelease,
22-
} from "../tsconfigRules";
22+
} from "../tsconfigRules.js";
2323
import { CompilerOptionName } from "../../data/_types";
2424

25-
const toJSONString = (obj) => format(JSON.stringify(obj, null, " "), { filepath: "thing.json" });
25+
const toJSONString = (obj) =>
26+
prettier.format(JSON.stringify(obj, null, " "), { filepath: "thing.json" });
2627
const writeJSON = (name, obj) =>
27-
writeFileSync(join(__dirname, "..", "..", "data", name), toJSONString(obj));
28+
writeFileSync(
29+
new URL(`../../data/${name}`, import.meta.url),
30+
toJSONString(obj)
31+
);
2832
const writeString = (name, text) =>
29-
writeFileSync(join(__dirname, "..", "..", "data", name), format(text, { filepath: name }));
33+
writeFileSync(
34+
new URL(`../../data/${name}`, import.meta.url),
35+
prettier.format(text, { filepath: name })
36+
);
3037

3138
export interface CompilerOptionJSON extends CommandLineOptionBase {
3239
releaseVersion?: string;
@@ -40,8 +47,8 @@ export interface CompilerOptionJSON extends CommandLineOptionBase {
4047
hostObj: string;
4148
}
4249

43-
const tsconfigOpts = require(join(__dirname, "../../data/tsconfigOpts.json"))
44-
.options as CompilerOptionJSON[];
50+
// @ts-ignore
51+
import tsconfigOpts from "../../data/tsconfigOpts.json";
4552

4653
const notCompilerFlags = [
4754
// @ts-ignore

packages/tsconfig-reference/scripts/cli/generateMarkdown.ts

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,32 @@ console.log("TSConfig Ref: MD for CLI Opts");
99

1010
import { writeFileSync, readdirSync, existsSync, readFileSync } from "fs";
1111
import { join } from "path";
12-
import { read as readMarkdownFile } from "gray-matter";
13-
import * as prettier from "prettier";
12+
import { fileURLToPath } from "url";
13+
import matter from "gray-matter";
14+
import prettier from "prettier";
1415
import { CompilerOptionJSON } from "./generateJSON.js";
15-
import { parseMarkdown } from "../tsconfigRules";
16+
import { parseMarkdown } from "../tsconfigRules.js";
1617

17-
const options = require(join(__dirname, "../../data/cliOpts.json")) as {
18-
options: CompilerOptionJSON[];
19-
build: CompilerOptionJSON[];
20-
watch: CompilerOptionJSON[];
21-
cli: CompilerOptionJSON[];
22-
};
18+
// @ts-ignore
19+
import cliOpts from "../../data/cliOpts.json";
2320

2421
const knownTypes: Record<string, string> = {};
2522

26-
const languages = readdirSync(join(__dirname, "..", "..", "copy")).filter(
23+
const languages = readdirSync(new URL("../../copy", import.meta.url)).filter(
2724
(f) => !f.startsWith(".")
2825
);
2926

3027
languages.forEach((lang) => {
31-
const locale = join(__dirname, "..", "..", "copy", lang);
32-
const fallbackLocale = join(__dirname, "..", "..", "copy", "en");
28+
const locale = new URL(`../../copy/${lang}/`, import.meta.url);
29+
const fallbackLocale = new URL("../../copy/en/", import.meta.url);
3330

3431
const markdownChunks: string[] = [];
3532

3633
const getPathInLocale = (path: string, optionalExampleContent?: string) => {
37-
if (existsSync(join(locale, path))) return join(locale, path);
38-
if (existsSync(join(fallbackLocale, path))) return join(fallbackLocale, path);
39-
const en = join(fallbackLocale, path);
34+
if (existsSync(new URL(path, locale))) return new URL(path, locale);
35+
if (existsSync(new URL(path, fallbackLocale)))
36+
return new URL(path, fallbackLocale);
37+
const en = new URL(path, fallbackLocale);
4038

4139
const localeDesc = lang === "en" ? lang : `either ${lang} or English`;
4240
// prettier-ignore
@@ -45,7 +43,11 @@ languages.forEach((lang) => {
4543
);
4644
};
4745

48-
function renderTable(title: string, options: CompilerOptionJSON[], opts?: { noDefaults: true }) {
46+
function renderTable(
47+
title: string,
48+
options: typeof cliOpts[keyof typeof cliOpts],
49+
opts?: { noDefaults: true }
50+
) {
4951
markdownChunks.push(`<h3>${title}</h3>`);
5052

5153
// Trim leading whitespaces so that it is not rendered as a markdown code block
@@ -68,13 +70,17 @@ languages.forEach((lang) => {
6870
// CLI description
6971
let description = option.description?.message;
7072
try {
71-
const sectionsPath = getPathInLocale(join("options", option.name + ".md"));
72-
const optionFile = readMarkdownFile(sectionsPath);
73+
const sectionsPath = getPathInLocale(
74+
join("options", option.name + ".md")
75+
);
76+
const optionFile = matter.read(fileURLToPath(sectionsPath));
7377
description = optionFile.data.oneline;
7478
} catch (error) {
7579
try {
76-
const sectionsPath = getPathInLocale(join("cli", option.name + ".md"));
77-
const optionFile = readMarkdownFile(sectionsPath);
80+
const sectionsPath = getPathInLocale(
81+
join("cli", option.name + ".md")
82+
);
83+
const optionFile = matter.read(fileURLToPath(sectionsPath));
7884
description = optionFile.data.oneline;
7985
} catch (error) { }
8086
}
@@ -122,21 +128,23 @@ languages.forEach((lang) => {
122128
markdownChunks.push(`</tbody></table>\n`);
123129
}
124130

125-
renderTable("CLI Commands", options.cli, { noDefaults: true });
126-
renderTable("Build Options", options.build, { noDefaults: true });
127-
renderTable("Watch Options", options.watch, { noDefaults: true });
128-
renderTable("Compiler Flags", options.options);
131+
renderTable("CLI Commands", cliOpts.cli, { noDefaults: true });
132+
renderTable("Build Options", cliOpts.build, { noDefaults: true });
133+
renderTable("Watch Options", cliOpts.watch, { noDefaults: true });
134+
renderTable("Compiler Flags", cliOpts.options);
129135

130136
// Write the Markdown and JSON
131-
const markdown = prettier.format(markdownChunks.join("\n"), { filepath: "index.md" });
132-
const mdPath = join(__dirname, "..", "..", "output", lang + "-cli.md");
137+
const markdown = prettier.format(markdownChunks.join("\n"), {
138+
filepath: "index.md",
139+
});
140+
const mdPath = new URL(`../../output/${lang}-cli.md`, import.meta.url);
133141
writeFileSync(mdPath, markdown);
134142
});
135143

136144
languages.forEach((lang) => {
137-
const mdCLI = join(__dirname, "..", "..", "output", lang + "-cli.md");
145+
const mdCLI = new URL(`../../output/${lang}-cli.md`, import.meta.url);
138146
// prettier-ignore
139-
const compOptsPath = join(__dirname, "..", "..", "..", `documentation/copy/${lang}/project-config/Compiler Options.md`);
147+
const compOptsPath = new URL(`../../../documentation/copy/${lang}/project-config/Compiler Options.md`, import.meta.url);
140148

141149
if (existsSync(compOptsPath)) {
142150
const md = readFileSync(compOptsPath, "utf8");

packages/tsconfig-reference/scripts/lint.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33

44
// yarn workspace tsconfig-reference lint
55

6-
const chalk = require("chalk");
6+
import chalk from "chalk";
77

88
const tick = chalk.bold.greenBright("✓");
99
const cross = chalk.bold.redBright("⤫");
1010

11-
const { readdirSync, readFileSync, statSync } = require("fs");
12-
const { join } = require("path");
11+
import { readdirSync, readFileSync, statSync } from "fs";
12+
import { join } from "path";
1313

14-
const remark = require("remark");
15-
const remarkTwoSlash = require("remark-shiki-twoslash");
14+
import remark from "remark";
15+
import remarkTwoSlash from "remark-shiki-twoslash";
1616

17-
const { read } = require("gray-matter");
17+
import matter from "gray-matter";
1818

19-
const languages = readdirSync(join(__dirname, "..", "copy")).filter((f) => !f.startsWith("."));
19+
const languages = readdirSync(new URL("../copy", import.meta.url)).filter(
20+
(f) => !f.startsWith(".")
21+
);
2022

2123
console.log("Linting the sample code which uses twoslasher in ts-config");
2224

@@ -29,15 +31,17 @@ const go = async () => {
2931
for (const lang of languages) {
3032
console.log("\n\nLanguage: " + chalk.bold(lang) + "\n");
3133

32-
const locale = join(__dirname, "..", "copy", lang);
34+
const locale = new URL(`../copy/${lang}/`, import.meta.url);
3335
let options;
3436

3537
try {
36-
options = readdirSync(join(locale, "options")).filter((f) => !f.startsWith("."));
38+
options = readdirSync(new URL("options", locale)).filter(
39+
(f) => !f.startsWith(".")
40+
);
3741
} catch {
3842
errorReports.push({
39-
path: join(locale, "options"),
40-
error: `Options directory ${join(locale, "options")} doesn't exist`,
43+
path: new URL("options", locale),
44+
error: `Options directory ${new URL("options", locale)} doesn't exist`,
4145
});
4246
continue;
4347
}
@@ -46,7 +50,7 @@ const go = async () => {
4650
for (const option of options) {
4751
if (filterString.length && !option.includes(filterString)) continue;
4852

49-
const optionPath = join(locale, "options", option);
53+
const optionPath = new URL(`options/${option}`, locale);
5054

5155
const isDir = statSync(optionPath).isDirectory();
5256
if (isDir) continue;

packages/tsconfig-reference/scripts/msbuild/generateJSON.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@
22

33
console.log("TSConfig Ref: JSON for MSBuild");
44

5-
import parser = require("xml-js");
5+
import parser from "xml-js";
66
import { readFileSync, writeFileSync } from "fs";
77
import { join } from "path";
8-
import { format } from "prettier";
8+
import prettier from "prettier";
99

10-
const toJSONString = (obj) => format(JSON.stringify(obj, null, " "), { filepath: "thing.json" });
10+
const toJSONString = (obj) =>
11+
prettier.format(JSON.stringify(obj, null, " "), { filepath: "thing.json" });
1112
const writeJSON = (name, obj) =>
12-
writeFileSync(join(__dirname, "..", "..", "data", name), toJSONString(obj));
13+
writeFileSync(
14+
new URL(`../../data/${name}`, import.meta.url),
15+
toJSONString(obj)
16+
);
1317

14-
const targetsXMLText = readFileSync(join(__dirname, "./Microsoft.TypeScript.targets"), "utf8");
15-
const targetJSONtext = parser.xml2json(targetsXMLText, { compact: true, spaces: 4 });
18+
const targetsXMLText = readFileSync(
19+
new URL("./Microsoft.TypeScript.targets", import.meta.url),
20+
"utf8"
21+
);
22+
const targetJSONtext = parser.xml2json(targetsXMLText, {
23+
compact: true,
24+
spaces: 4,
25+
});
1626
const targets = JSON.parse(targetJSONtext) as import("./types").Target;
1727

1828
const config = targets.Project.PropertyGroup.find((f) => f.TypeScriptBuildConfigurations?.length);

packages/tsconfig-reference/scripts/msbuild/generateMarkdown.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,34 @@ console.log("TSConfig Ref: MD for MSBuild");
1010

1111
import { writeFileSync, readdirSync, existsSync, readFileSync } from "fs";
1212
import { join } from "path";
13-
import { read as readMarkdownFile } from "gray-matter";
14-
import * as prettier from "prettier";
13+
import { fileURLToPath } from "url";
14+
import matter from "gray-matter";
15+
import prettier from "prettier";
1516

16-
import * as remark from "remark";
17-
import * as remarkHTML from "remark-html";
17+
import remark from "remark";
18+
import remarkHTML from "remark-html";
1819

19-
const options = require(join(__dirname, "../../data/msbuild-flags.json"));
20+
// @ts-ignore
21+
import options from "../../data/msbuild-flags.json";
2022
const parseMarkdown = (md: string) => remark().use(remarkHTML).processSync(md);
2123

2224
const knownTypes: Record<string, string> = {};
2325

24-
const languages = readdirSync(join(__dirname, "..", "..", "copy")).filter(
26+
const languages = readdirSync(new URL("../../copy", import.meta.url)).filter(
2527
(f) => !f.startsWith(".")
2628
);
2729

2830
languages.forEach((lang) => {
29-
const locale = join(__dirname, "..", "..", "copy", lang);
30-
const fallbackLocale = join(__dirname, "..", "..", "copy", "en");
31+
const locale = new URL(`../../copy/${lang}/`, import.meta.url);
32+
const fallbackLocale = new URL("../../copy/en/", import.meta.url);
3133

3234
const markdownChunks: string[] = [];
3335

3436
const getPathInLocale = (path: string, optionalExampleContent?: string) => {
35-
if (existsSync(join(locale, path))) return join(locale, path);
36-
if (existsSync(join(fallbackLocale, path))) return join(fallbackLocale, path);
37-
const en = join(fallbackLocale, path);
37+
if (existsSync(new URL(path, locale))) return new URL(path, locale);
38+
if (existsSync(new URL(path, fallbackLocale)))
39+
return new URL(path, fallbackLocale);
40+
const en = new URL(path, fallbackLocale);
3841

3942
const localeDesc = lang === "en" ? lang : `either ${lang} or English`;
4043
// prettier-ignore
@@ -64,12 +67,12 @@ languages.forEach((lang) => {
6467
let description = "";
6568
try {
6669
const sectionsPath = getPathInLocale(join("options", name + ".md"));
67-
const optionFile = readMarkdownFile(sectionsPath);
70+
const optionFile = matter.read(fileURLToPath(sectionsPath));
6871
description = optionFile.data.oneline;
6972
} catch (error) {
7073
try {
7174
const sectionsPath = getPathInLocale(join("msbuild", name + ".md"));
72-
const optionFile = readMarkdownFile(sectionsPath);
75+
const optionFile = matter.read(fileURLToPath(sectionsPath));
7376
description = optionFile.data.oneline;
7477
} catch (error) { }
7578
}
@@ -94,15 +97,17 @@ languages.forEach((lang) => {
9497
renderTable("CLI Mappings", options.flags);
9598

9699
// Write the Markdown and JSON
97-
const markdown = prettier.format(markdownChunks.join("\n"), { filepath: "index.md" });
98-
const mdPath = join(__dirname, "..", "..", "output", lang + "-msbuild.md");
100+
const markdown = prettier.format(markdownChunks.join("\n"), {
101+
filepath: "index.md",
102+
});
103+
const mdPath = new URL(`../../output/${lang}-msbuild.md`, import.meta.url);
99104
writeFileSync(mdPath, markdown);
100105
});
101106

102107
languages.forEach((lang) => {
103-
const mdCLI = join(__dirname, "..", "..", "output", lang + "-msbuild.md");
108+
const mdCLI = new URL(`../../output/${lang}-msbuild.md`, import.meta.url);
104109
// prettier-ignore
105-
const compOptsPath = join(__dirname, "..", "..", "..", `documentation/copy/${lang}/project-config/Compiler Options in MSBuild.md`);
110+
const compOptsPath = new URL(`../../../documentation/copy/${lang}/project-config/Compiler Options in MSBuild.md`, import.meta.url);
106111

107112
if (existsSync(compOptsPath)) {
108113
const md = readFileSync(compOptsPath, "utf8");

packages/tsconfig-reference/scripts/schema/downloadSchemaBase.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
// https://json.schemastore.org/tsconfig.json
44

5-
const nodeFetch = require("node-fetch").default;
6-
const { writeFileSync, existsSync, mkdirSync } = require("fs");
7-
const { join } = require("path");
5+
import nodeFetch from "node-fetch";
6+
import { writeFileSync, existsSync, mkdirSync } from "fs";
7+
import { join } from "path";
88

99
const getFileAndStoreLocally = async (url, path, editFunc) => {
1010
const editingFunc = editFunc ? editFunc : (text) => text;
1111
const packageJSON = await nodeFetch(url);
1212
const contents = await packageJSON.text();
13-
writeFileSync(join(__dirname, path), editingFunc(contents), "utf8");
13+
writeFileSync(new URL(path, import.meta.url), editingFunc(contents), "utf8");
1414
};
1515

1616
getFileAndStoreLocally(

0 commit comments

Comments
 (0)