Skip to content

Commit

Permalink
Merge pull request #2174 from jablko/patch-22
Browse files Browse the repository at this point in the history
Use ESM
  • Loading branch information
Orta Therox committed Dec 2, 2021
2 parents 71d2d3c + f001281 commit 15444fd
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 136 deletions.
17 changes: 9 additions & 8 deletions packages/tsconfig-reference/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
"license": "MIT",
"private": true,
"scripts": {
"generate:json:tsconfig": "yarn ts-node -T scripts/tsconfig/generateJSON.ts",
"generate:json:cli": "yarn ts-node -T scripts/cli/generateJSON.ts",
"generate:json:schema": "yarn ts-node -T scripts/schema/generateJSON.ts",
"generate:json:msbuild": "yarn ts-node -T scripts/msbuild/generateJSON.ts",
"generate:json:tsconfig": "tsc --build && node --experimental-json-modules scripts/tsconfig/generateJSON",
"generate:json:cli": "tsc --build && node --experimental-json-modules scripts/cli/generateJSON",
"generate:json:schema": "tsc --build && node --experimental-json-modules scripts/schema/generateJSON",
"generate:json:msbuild": "tsc --build && node --experimental-json-modules scripts/msbuild/generateJSON",
"generate:json": "yarn generate:json:tsconfig && yarn generate:json:cli && yarn generate:json:schema && yarn generate:json:msbuild",
"generate:md:tsconfig": "yarn ts-node -T scripts/tsconfig/generateMarkdown.ts",
"generate:md:cli": "yarn ts-node -T scripts/cli/generateMarkdown.ts",
"generate:md:msbuild": "yarn ts-node -T scripts/msbuild/generateMarkdown.ts",
"generate:md:tsconfig": "tsc --build && node --experimental-json-modules scripts/tsconfig/generateMarkdown",
"generate:md:cli": "tsc --build && node --experimental-json-modules scripts/cli/generateMarkdown",
"generate:md:msbuild": "tsc --build && node --experimental-json-modules scripts/msbuild/generateMarkdown",
"generate:md": "yarn generate:md:tsconfig && yarn generate:md:cli && yarn generate:md:tsconfig",
"bootstrap": "yarn scripts/schema/downloadSchemaBase.ts",
"build": "yarn generate:json && yarn generate:md",
Expand All @@ -26,5 +26,6 @@
"remark": "^11.0.2",
"remark-html": "^10.0.0",
"ts-node": "*"
}
},
"type": "module"
}
23 changes: 15 additions & 8 deletions packages/tsconfig-reference/scripts/cli/generateJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,33 @@
*/
console.log("TSConfig Ref: JSON for CLI Opts");

import * as ts from "typescript";
import ts from "typescript";

import { CommandLineOptionBase } from "../types";
import { writeFileSync } from "fs";
import { join } from "path";
import { format } from "prettier";
import prettier from "prettier";
import {
deprecated,
internal,
defaultsForOptions,
allowedValues,
configToRelease,
} from "../tsconfigRules";
} from "../tsconfigRules.js";
import { CompilerOptionName } from "../../data/_types";

const toJSONString = (obj) => format(JSON.stringify(obj, null, " "), { filepath: "thing.json" });
const toJSONString = (obj) =>
prettier.format(JSON.stringify(obj, null, " "), { filepath: "thing.json" });
const writeJSON = (name, obj) =>
writeFileSync(join(__dirname, "..", "..", "data", name), toJSONString(obj));
writeFileSync(
new URL(`../../data/${name}`, import.meta.url),
toJSONString(obj)
);
const writeString = (name, text) =>
writeFileSync(join(__dirname, "..", "..", "data", name), format(text, { filepath: name }));
writeFileSync(
new URL(`../../data/${name}`, import.meta.url),
prettier.format(text, { filepath: name })
);

export interface CompilerOptionJSON extends CommandLineOptionBase {
releaseVersion?: string;
Expand All @@ -40,8 +47,8 @@ export interface CompilerOptionJSON extends CommandLineOptionBase {
hostObj: string;
}

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

const notCompilerFlags = [
// @ts-ignore
Expand Down
64 changes: 36 additions & 28 deletions packages/tsconfig-reference/scripts/cli/generateMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,32 @@ console.log("TSConfig Ref: MD for CLI Opts");

import { writeFileSync, readdirSync, existsSync, readFileSync } from "fs";
import { join } from "path";
import { read as readMarkdownFile } from "gray-matter";
import * as prettier from "prettier";
import { fileURLToPath } from "url";
import matter from "gray-matter";
import prettier from "prettier";
import { CompilerOptionJSON } from "./generateJSON.js";
import { parseMarkdown } from "../tsconfigRules";
import { parseMarkdown } from "../tsconfigRules.js";

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

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

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

languages.forEach((lang) => {
const locale = join(__dirname, "..", "..", "copy", lang);
const fallbackLocale = join(__dirname, "..", "..", "copy", "en");
const locale = new URL(`../../copy/${lang}/`, import.meta.url);
const fallbackLocale = new URL("../../copy/en/", import.meta.url);

const markdownChunks: string[] = [];

const getPathInLocale = (path: string, optionalExampleContent?: string) => {
if (existsSync(join(locale, path))) return join(locale, path);
if (existsSync(join(fallbackLocale, path))) return join(fallbackLocale, path);
const en = join(fallbackLocale, path);
if (existsSync(new URL(path, locale))) return new URL(path, locale);
if (existsSync(new URL(path, fallbackLocale)))
return new URL(path, fallbackLocale);
const en = new URL(path, fallbackLocale);

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

function renderTable(title: string, options: CompilerOptionJSON[], opts?: { noDefaults: true }) {
function renderTable(
title: string,
options: typeof cliOpts[keyof typeof cliOpts],
opts?: { noDefaults: true }
) {
markdownChunks.push(`<h3>${title}</h3>`);

// Trim leading whitespaces so that it is not rendered as a markdown code block
Expand All @@ -68,13 +70,17 @@ languages.forEach((lang) => {
// CLI description
let description = option.description?.message;
try {
const sectionsPath = getPathInLocale(join("options", option.name + ".md"));
const optionFile = readMarkdownFile(sectionsPath);
const sectionsPath = getPathInLocale(
join("options", option.name + ".md")
);
const optionFile = matter.read(fileURLToPath(sectionsPath));
description = optionFile.data.oneline;
} catch (error) {
try {
const sectionsPath = getPathInLocale(join("cli", option.name + ".md"));
const optionFile = readMarkdownFile(sectionsPath);
const sectionsPath = getPathInLocale(
join("cli", option.name + ".md")
);
const optionFile = matter.read(fileURLToPath(sectionsPath));
description = optionFile.data.oneline;
} catch (error) { }
}
Expand Down Expand Up @@ -122,21 +128,23 @@ languages.forEach((lang) => {
markdownChunks.push(`</tbody></table>\n`);
}

renderTable("CLI Commands", options.cli, { noDefaults: true });
renderTable("Build Options", options.build, { noDefaults: true });
renderTable("Watch Options", options.watch, { noDefaults: true });
renderTable("Compiler Flags", options.options);
renderTable("CLI Commands", cliOpts.cli, { noDefaults: true });
renderTable("Build Options", cliOpts.build, { noDefaults: true });
renderTable("Watch Options", cliOpts.watch, { noDefaults: true });
renderTable("Compiler Flags", cliOpts.options);

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

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

if (existsSync(compOptsPath)) {
const md = readFileSync(compOptsPath, "utf8");
Expand Down
28 changes: 16 additions & 12 deletions packages/tsconfig-reference/scripts/lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@

// yarn workspace tsconfig-reference lint

const chalk = require("chalk");
import chalk from "chalk";

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

const { readdirSync, readFileSync, statSync } = require("fs");
const { join } = require("path");
import { readdirSync, readFileSync, statSync } from "fs";
import { join } from "path";

const remark = require("remark");
const remarkTwoSlash = require("remark-shiki-twoslash");
import remark from "remark";
import remarkTwoSlash from "remark-shiki-twoslash";

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

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

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

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

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

try {
options = readdirSync(join(locale, "options")).filter((f) => !f.startsWith("."));
options = readdirSync(new URL("options", locale)).filter(
(f) => !f.startsWith(".")
);
} catch {
errorReports.push({
path: join(locale, "options"),
error: `Options directory ${join(locale, "options")} doesn't exist`,
path: new URL("options", locale),
error: `Options directory ${new URL("options", locale)} doesn't exist`,
});
continue;
}
Expand All @@ -46,7 +50,7 @@ const go = async () => {
for (const option of options) {
if (filterString.length && !option.includes(filterString)) continue;

const optionPath = join(locale, "options", option);
const optionPath = new URL(`options/${option}`, locale);

const isDir = statSync(optionPath).isDirectory();
if (isDir) continue;
Expand Down
22 changes: 16 additions & 6 deletions packages/tsconfig-reference/scripts/msbuild/generateJSON.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,27 @@

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

import parser = require("xml-js");
import parser from "xml-js";
import { readFileSync, writeFileSync } from "fs";
import { join } from "path";
import { format } from "prettier";
import prettier from "prettier";

const toJSONString = (obj) => format(JSON.stringify(obj, null, " "), { filepath: "thing.json" });
const toJSONString = (obj) =>
prettier.format(JSON.stringify(obj, null, " "), { filepath: "thing.json" });
const writeJSON = (name, obj) =>
writeFileSync(join(__dirname, "..", "..", "data", name), toJSONString(obj));
writeFileSync(
new URL(`../../data/${name}`, import.meta.url),
toJSONString(obj)
);

const targetsXMLText = readFileSync(join(__dirname, "./Microsoft.TypeScript.targets"), "utf8");
const targetJSONtext = parser.xml2json(targetsXMLText, { compact: true, spaces: 4 });
const targetsXMLText = readFileSync(
new URL("./Microsoft.TypeScript.targets", import.meta.url),
"utf8"
);
const targetJSONtext = parser.xml2json(targetsXMLText, {
compact: true,
spaces: 4,
});
const targets = JSON.parse(targetJSONtext) as import("./types").Target;

const config = targets.Project.PropertyGroup.find((f) => f.TypeScriptBuildConfigurations?.length);
Expand Down
39 changes: 22 additions & 17 deletions packages/tsconfig-reference/scripts/msbuild/generateMarkdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,34 @@ console.log("TSConfig Ref: MD for MSBuild");

import { writeFileSync, readdirSync, existsSync, readFileSync } from "fs";
import { join } from "path";
import { read as readMarkdownFile } from "gray-matter";
import * as prettier from "prettier";
import { fileURLToPath } from "url";
import matter from "gray-matter";
import prettier from "prettier";

import * as remark from "remark";
import * as remarkHTML from "remark-html";
import remark from "remark";
import remarkHTML from "remark-html";

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

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

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

languages.forEach((lang) => {
const locale = join(__dirname, "..", "..", "copy", lang);
const fallbackLocale = join(__dirname, "..", "..", "copy", "en");
const locale = new URL(`../../copy/${lang}/`, import.meta.url);
const fallbackLocale = new URL("../../copy/en/", import.meta.url);

const markdownChunks: string[] = [];

const getPathInLocale = (path: string, optionalExampleContent?: string) => {
if (existsSync(join(locale, path))) return join(locale, path);
if (existsSync(join(fallbackLocale, path))) return join(fallbackLocale, path);
const en = join(fallbackLocale, path);
if (existsSync(new URL(path, locale))) return new URL(path, locale);
if (existsSync(new URL(path, fallbackLocale)))
return new URL(path, fallbackLocale);
const en = new URL(path, fallbackLocale);

const localeDesc = lang === "en" ? lang : `either ${lang} or English`;
// prettier-ignore
Expand Down Expand Up @@ -64,12 +67,12 @@ languages.forEach((lang) => {
let description = "";
try {
const sectionsPath = getPathInLocale(join("options", name + ".md"));
const optionFile = readMarkdownFile(sectionsPath);
const optionFile = matter.read(fileURLToPath(sectionsPath));
description = optionFile.data.oneline;
} catch (error) {
try {
const sectionsPath = getPathInLocale(join("msbuild", name + ".md"));
const optionFile = readMarkdownFile(sectionsPath);
const optionFile = matter.read(fileURLToPath(sectionsPath));
description = optionFile.data.oneline;
} catch (error) { }
}
Expand All @@ -94,15 +97,17 @@ languages.forEach((lang) => {
renderTable("CLI Mappings", options.flags);

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

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

if (existsSync(compOptsPath)) {
const md = readFileSync(compOptsPath, "utf8");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

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

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

const getFileAndStoreLocally = async (url, path, editFunc) => {
const editingFunc = editFunc ? editFunc : (text) => text;
const packageJSON = await nodeFetch(url);
const contents = await packageJSON.text();
writeFileSync(join(__dirname, path), editingFunc(contents), "utf8");
writeFileSync(new URL(path, import.meta.url), editingFunc(contents), "utf8");
};

getFileAndStoreLocally(
Expand Down
Loading

0 comments on commit 15444fd

Please sign in to comment.