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

chore: migrate schema.js to ESM #1988

Merged
merged 1 commit into from
Apr 29, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"scripts/embed-manifest",
"scripts/init.mjs",
"scripts/parseargs.mjs",
"scripts/schema.mjs",
"scripts/template.mjs",
"test-app.gradle",
"test_app.rb",
Expand Down
2 changes: 0 additions & 2 deletions scripts/affected.mjs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env node
// @ts-check

import yaml from "js-yaml";
import { Minimatch } from "minimatch";
import { spawnSync } from "node:child_process";
Expand Down
2 changes: 0 additions & 2 deletions scripts/copy-yarnrc.mjs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env node
// @ts-check

import yaml from "js-yaml";
import * as fs from "node:fs";
import * as path from "node:path";
Expand Down
2 changes: 1 addition & 1 deletion scripts/embed-manifest/validate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import Ajv from "ajv";
import * as nodefs from "node:fs";
import { readJSONFile } from "../helpers.js";
import { generateSchema } from "../schema.js";
import { generateSchema } from "../schema.mjs";

const APP_JSON = "app.json";

Expand Down
4 changes: 1 addition & 3 deletions scripts/generate-manifest-docs.mjs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#!/usr/bin/env node
// @ts-check

import * as path from "node:path";
import { readDocumentation } from "./generate-schema.mjs";
import { generateSchema } from "./schema.js";
import { generateSchema } from "./schema.mjs";

async function generateManifestDocs() {
const docs = await readDocumentation();
Expand Down
4 changes: 1 addition & 3 deletions scripts/generate-manifest.mjs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#!/usr/bin/env node
// @ts-check

import * as fs from "node:fs/promises";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { generateSchema } from "./schema.js";
import { generateSchema } from "./schema.mjs";

/**
* @typedef {import("ajv").SchemaObject} SchemaObject
Expand Down
34 changes: 17 additions & 17 deletions scripts/generate-schema.mjs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
#!/usr/bin/env node
// @ts-check

import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { fileURLToPath } from "node:url";
import { generateSchema } from "./schema.js";
import { URL, fileURLToPath } from "node:url";
import { isMain } from "./helpers.js";
import { generateSchema } from "./schema.mjs";

/** @typedef {import("./types").Docs} Docs */

const thisScript = fileURLToPath(import.meta.url);
const docsDir = path.join(path.dirname(thisScript), "docs");
/** @type {(str: string) => string} */
const stripCarriageReturn =
os.EOL === "\r\n" ? (str) => str.replace(/\r/g, "") : (str) => str;

/**
* @returns {Promise<import("./schema.js").Docs>}
* @returns {Promise<Partial<Docs>>}
*/
export async function readDocumentation() {
/** @type {(keyof import("./schema.js").Docs)[]} */
/** @type {Partial<Docs>} */
const docs = {};
const docsDir = fileURLToPath(new URL("docs", import.meta.url));

/** @type {(keyof Docs)[]} */
const keys = [
"introduction",
"bundleRoot",
Expand All @@ -40,29 +46,23 @@ export async function readDocumentation() {
"windows.certificateThumbprint",
];

const docs = /** @type {import("./schema.js").Docs} */ ({});

for (const name of keys) {
const filename = path.join(docsDir, name + ".md");
const md = await fs.readFile(filename, { encoding: "utf-8" });
docs[name] = trimCarriageReturn(md).trim();
docs[name] = stripCarriageReturn(md).trim();
}

return docs;
}

/** @type {(s: string) => string} */
const trimCarriageReturn =
os.EOL === "\r\n" ? (str) => str.replace(/\r/g, "") : (str) => str;

if (process.argv[1] === thisScript) {
if (isMain(import.meta.url)) {
readDocumentation()
.then((docs) => generateSchema(docs))
.then((schema) => {
for (const def of Object.values(schema.$defs)) {
delete def["exclude-from-codegen"];
}
return trimCarriageReturn(JSON.stringify(schema, undefined, 2)) + "\n";
return stripCarriageReturn(JSON.stringify(schema, undefined, 2)) + "\n";
})
.then((schema) => fs.writeFile("schema.json", schema))
.catch(console.error);
Expand Down
19 changes: 12 additions & 7 deletions scripts/schema.js → scripts/schema.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/**
* @typedef {import("./types").Docs} Docs
*/
// @ts-check
import { URL, fileURLToPath } from "node:url";
import { readJSONFile } from "./helpers.js";

/** @typedef {import("./types").Docs} Docs */

/**
* @param {string} content
Expand All @@ -11,12 +13,17 @@ function extractBrief(content = "") {
return endBrief > 0 ? content.substring(0, endBrief) : content;
}

function readManifest() {
const manifest = fileURLToPath(new URL("../package.json", import.meta.url));
return /** @type {import("../package.json")} */ (readJSONFile(manifest));
}

/**
* @param {Partial<Docs>=} docs App manifest documentation
* @returns {import("ajv").SchemaObject}
*/
function generateSchema(docs = {}) {
const { defaultPlatformPackages } = require("../package.json");
export function generateSchema(docs = {}) {
const { defaultPlatformPackages } = readManifest();
return {
$defs: {
appIconSet: {
Expand Down Expand Up @@ -330,5 +337,3 @@ function generateSchema(docs = {}) {
},
};
}

exports.generateSchema = generateSchema;
1 change: 0 additions & 1 deletion scripts/set-react-version.mjs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env node
// @ts-check

/**
Expand Down
2 changes: 2 additions & 0 deletions scripts/template.mjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

/**
* Joins all specified lines into a single string.
* @param {...string} lines
Expand Down
1 change: 0 additions & 1 deletion scripts/test-e2e.mjs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env node
// @ts-check

/**
Expand Down
1 change: 0 additions & 1 deletion scripts/test-matrix.mjs
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/usr/bin/env node
// @ts-check

/**
Expand Down
2 changes: 1 addition & 1 deletion test/pack.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ describe("npm pack", () => {
"scripts/helpers.js",
"scripts/init.mjs",
"scripts/parseargs.mjs",
"scripts/schema.js",
"scripts/schema.mjs",
"scripts/template.mjs",
"test-app.gradle",
"test_app.rb",
Expand Down