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: convert internal scripts to .mts #2367

Merged
merged 2 commits into from
Jan 9, 2025
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ jobs:
- name: react-native run-ios
if: ${{ steps.affected.outputs.ios != '' }}
run: |
../scripts/testing/react-native.mjs run-ios
../scripts/testing/react-native.mts run-ios
working-directory: template-example
timeout-minutes: 60
android:
Expand Down Expand Up @@ -326,7 +326,7 @@ jobs:
- name: react-native run-android
if: ${{ steps.affected.outputs.android != '' }}
run: |
../scripts/testing/react-native.mjs run-android
../scripts/testing/react-native.mts run-android
working-directory: template-example
timeout-minutes: 60
macos:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was generated by generate-manifest.mjs.
// This file was generated by generate-manifest.mts.
// DO NOT MODIFY. ALL CHANGES WILL BE OVERWRITTEN.

@file:Suppress("ktlint:standard:trailing-comma-on-declaration-site")
Expand Down
18 changes: 9 additions & 9 deletions eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ module.exports = [
},
{
files: [
"scripts/internal/generate-manifest-docs.mjs",
"scripts/internal/generate-manifest.mjs",
"scripts/internal/generate-schema.mjs",
"scripts/internal/pack.mjs",
"scripts/internal/set-react-version.mjs",
"scripts/internal/test.mjs",
"scripts/internal/generate-manifest-docs.mts",
"scripts/internal/generate-manifest.mts",
"scripts/internal/generate-schema.mts",
"scripts/internal/pack.mts",
"scripts/internal/set-react-version.mts",
"scripts/internal/test.mts",
"scripts/schema.mjs",
"scripts/testing/test-apple.mjs",
"scripts/testing/test-e2e.mjs",
"scripts/testing/test-matrix.mjs",
"scripts/testing/test-apple.mts",
"scripts/testing/test-e2e.mts",
"scripts/testing/test-matrix.mts",
"scripts/utils/colors.mjs",
],
rules: {
Expand Down
2 changes: 1 addition & 1 deletion ios/ReactTestApp/Manifest.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This file was generated by generate-manifest.mjs.
// This file was generated by generate-manifest.mts.
// DO NOT MODIFY. ALL CHANGES WILL BE OVERWRITTEN.

struct Component {
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,24 @@
},
"scripts": {
"format:c": "clang-format -i $(git ls-files '*.cpp' '*.h' '*.m' '*.mm')",
"format:js": "prettier --write --log-level error $(git ls-files '*.[cm]js' '*.[jt]s' '*.tsx' '*.yml' 'CONTRIBUTING.md' 'README.md' 'test/**/*.json' ':!:.yarn/releases')",
"format:js": "prettier --write --log-level error $(git ls-files '*.[cm][jt]s' '*.[jt]s' '*.tsx' '*.yml' 'CONTRIBUTING.md' 'README.md' 'test/**/*.json' ':!:.yarn/releases')",
"format:swift": "swiftformat $(git ls-files '*.swift')",
"generate:code": "node scripts/internal/generate-manifest.mjs",
"generate:docs": "node scripts/internal/generate-manifest-docs.mjs",
"generate:schema": "node scripts/internal/generate-schema.mjs",
"generate:code": "node --experimental-transform-types --no-warnings scripts/internal/generate-manifest.mts",
"generate:docs": "node --experimental-transform-types --no-warnings scripts/internal/generate-manifest-docs.mts",
"generate:schema": "node --experimental-transform-types --no-warnings scripts/internal/generate-schema.mts",
"lint:commit": "git log --format='%s' origin/trunk..HEAD | tail -1 | npx @rnx-kit/[email protected]",
"lint:js": "eslint $(git ls-files '*.[cm]js' '*.[jt]s' '*.tsx' ':!:*.config.js' ':!:.yarn/releases') && tsc && tsc --project tsconfig.cjs.json",
"lint:js": "eslint $(git ls-files '*.[cm][jt]s' '*.[jt]s' '*.tsx' ':!:*.config.js' ':!:.yarn/releases') && tsc && tsc --project tsconfig.cjs.json",
"lint:kt": "ktlint --relative 'android/app/src/**/*.kt'",
"lint:rb": "bundle exec rubocop",
"lint:swift": "swiftlint",
"prepack": "node scripts/internal/pack.mjs pre",
"postpack": "node scripts/internal/pack.mjs post",
"release-notes": "node scripts/internal/release-notes.mjs",
"set-react-version": "node scripts/internal/set-react-version.mjs",
"show-affected": "node --experimental-transform-types --no-warnings scripts/build/affected.ts",
"test": "node scripts/internal/test.mjs",
"prepack": "node --experimental-transform-types --no-warnings scripts/internal/pack.mts pre",
"postpack": "node --experimental-transform-types --no-warnings scripts/internal/pack.mts post",
"release-notes": "node --experimental-transform-types --no-warnings scripts/internal/release-notes.mts",
"set-react-version": "node --experimental-transform-types --no-warnings scripts/internal/set-react-version.mts",
"show-affected": "node --experimental-transform-types --no-warnings scripts/build/affected.mts",
"test": "node --experimental-transform-types --no-warnings scripts/internal/test.mts",
"test:js": "node --experimental-transform-types --no-warnings --test $(git ls-files '*.test.ts')",
"test:matrix": "node scripts/testing/test-matrix.mjs",
"test:matrix": "node --experimental-transform-types --no-warnings scripts/testing/test-matrix.mts",
"test:rb": "bundle exec ruby -Ilib:test -e \"Dir.glob('./test/test_*.rb').each { |file| require(file) }\""
},
"dependencies": {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// @ts-check
// eslint-disable-next-line no-restricted-imports
import type { SchemaObject } from "ajv";
import * as path from "node:path";
import { readDocumentation } from "./generate-schema.mjs";
import { assertDefinition, readDocumentation } from "./generate-schema.mts";
import { generateSchema } from "../schema.mjs";

async function generateManifestDocs() {
Expand All @@ -9,12 +10,13 @@ async function generateManifestDocs() {

/**
* Renders the specified JSON object schema.
* @param {import("ajv").SchemaObject} definition
* @param {string[]} toc
* @param {string[]} lines
* @param {string} scope
*/
const render = (definition, toc, lines, scope = "") => {
const render = (
definition: SchemaObject,
toc: string[],
lines: string[],
scope = ""
) => {
if (Array.isArray(definition.allOf)) {
for (const { $ref } of definition.allOf) {
render(schema.$defs[$ref.replace("#/$defs/", "")], toc, lines, scope);
Expand All @@ -37,6 +39,8 @@ async function generateManifestDocs() {
})();

for (const [key, def] of Object.entries(definition.properties)) {
assertDefinition(def);

const { description, markdownDescription, type } = def;
const text = markdownDescription || description;
if (!text) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
// @ts-check
// eslint-disable-next-line no-restricted-imports
import type { SchemaObject } from "ajv";
import * as fs from "node:fs/promises";
import * as path from "node:path";
import { URL, fileURLToPath } from "node:url";
import { generateSchema } from "../schema.mjs";
import { assertDefinition } from "./generate-schema.mts";

type Language = {
options: {
indent: string;
level: number;
footer?: string;
header?: string;
};
arrayProperty: (name: string, type: string, required: boolean) => string;
objectProperty: (name: string, required: boolean) => string;
stringProperty: (name: string, required: boolean) => string;
structBegin: (name: string) => string;
structEnd: string;
};

/**
* @import { SchemaObject } from "ajv";
* @import { Language } from "../types.js";
*/
const thisScript = fileURLToPath(import.meta.url);

/**
* Returns the struct name of the definition key or reference.
* @param {string} ref
* @returns {string}
*/
function typename(ref) {
function typename(ref: string): string {
const i = ref.lastIndexOf("/") + 1;
return ref[i].toUpperCase() + ref.substring(i + 1);
}
Expand All @@ -25,11 +37,10 @@ function typename(ref) {
* @param {string} output
* @returns {Language}
*/
function getLanguage(output) {
function getLanguage(output: string): Language {
switch (path.extname(output)) {
case ".h": {
/** @type {(type: string, required: boolean) => string} */
const nullable = (type, required) =>
const nullable = (type: string, required: boolean) =>
required ? type : `std::optional<${type}>`;
return {
options: {
Expand Down Expand Up @@ -80,8 +91,7 @@ function getLanguage(output) {
}

case ".kt": {
/** @type {(required: boolean) => "" | "?"} */
const nullable = (required) => (required ? "" : "?");
const nullable = (required: boolean) => (required ? "" : "?");
return {
options: {
indent: " ",
Expand Down Expand Up @@ -110,8 +120,7 @@ function getLanguage(output) {
}

case ".swift": {
/** @type {(required: boolean) => "" | "?"} */
const nullable = (required) => (required ? "" : "?");
const nullable = (required: boolean) => (required ? "" : "?");
return {
options: {
indent: " ",
Expand Down Expand Up @@ -152,12 +161,12 @@ function getLanguage(output) {

/**
* Generates a data model from the specified schema definition.
* @param {string} name
* @param {SchemaObject} definition
* @param {Language} lang
* @returns {string[]}
*/
function generateType(name, definition, lang) {
function generateType(
name: string,
definition: SchemaObject,
lang: Language
): string[] {
const { indent, level } = lang.options;
const outer = indent.repeat(level);
const inner = indent.repeat(level + 1);
Expand All @@ -166,6 +175,8 @@ function generateType(name, definition, lang) {

const { properties, required = [] } = definition;
Object.entries(properties).forEach(([name, prop]) => {
assertDefinition(prop);

const isRequired = required.includes(name);
switch (prop.type) {
case "array":
Expand All @@ -188,10 +199,8 @@ function generateType(name, definition, lang) {

/**
* Generates manifest data models and writes them to specified path.
* @param {SchemaObject} schema
* @param {string} output
*/
async function generate(schema, output) {
async function generate(schema: SchemaObject, output: string) {
const lang = getLanguage(output);
const lines = [
`// This file was generated by ${path.basename(thisScript)}.`,
Expand All @@ -204,15 +213,9 @@ async function generate(schema, output) {
}

Object.entries(schema.$defs).forEach(([key, definition]) => {
assertDefinition(definition);
if (!("exclude-from-codegen" in definition)) {
lines.push(
...generateType(
typename(key),
/** @type {SchemaObject} */ (definition),
lang
),
""
);
lines.push(...generateType(typename(key), definition, lang), "");
}
return lines;
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
// @ts-check
// eslint-disable-next-line no-restricted-imports
import type { SchemaObject } from "ajv";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { URL, fileURLToPath } from "node:url";
import { isMain } from "../helpers.js";
import { generateSchema } from "../schema.mjs";
import type { Docs } from "../types.js";

/** @import { Docs } from "../types.js"; */
type Definition = SchemaObject & {
type: string;
description: string;
markdownDescription?: string;
};

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

/**
* @returns {Promise<Partial<Docs>>}
*/
export async function readDocumentation() {
/** @type {Partial<Docs>} */
const docs = {};
export function assertDefinition(props: unknown): asserts props is Definition {
if (
!props ||
typeof props !== "object" ||
!("type" in props || "allOf" in props || "oneOf" in props)
) {
throw new Error(`Invalid definition in schema: ${JSON.stringify(props)}`);
}
}

export async function readDocumentation(): Promise<Partial<Docs>> {
const docs: Partial<Docs> = {};
const docsDir = fileURLToPath(new URL("../../docs", import.meta.url));

/** @type {(keyof Docs)[]} */
const keys = [
const keys: (keyof Docs)[] = [
"introduction",
"bundleRoot",
"components",
Expand Down Expand Up @@ -67,6 +77,7 @@ if (isMain(import.meta.url)) {
.then((docs) => generateSchema(docs))
.then((schema) => {
for (const def of Object.values(schema.$defs)) {
assertDefinition(def);
delete def["exclude-from-codegen"];
}
return stripCarriageReturn(JSON.stringify(schema, undefined, 2)) + "\n";
Expand Down
5 changes: 1 addition & 4 deletions scripts/internal/pack.mjs → scripts/internal/pack.mts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// @ts-check
import * as fs from "node:fs";
import * as path from "node:path";

const files = ["example/.gitignore", "example/windows/.gitignore"];

/**
* Renames `.dotfile` to `_dotfile`.
* @param {string} p
* @returns {string}
*/
function renameDotFile(p) {
function renameDotFile(p: string): string {
return path.join(path.dirname(p), "_" + path.basename(p).substring(1));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
// @ts-check
#!/usr/bin/env -S node --experimental-transform-types --no-warnings

import { spawnSync } from "node:child_process";
import * as fs from "node:fs";
Expand Down Expand Up @@ -40,11 +39,8 @@ function configureAppManifest() {

/**
* Runs the specified command.
* @param {string} command
* @param {string[]} args
* @param {Record<string, unknown>=} options
*/
function $(command, args, options) {
function $(command: string, args: string[], options?: Record<string, unknown>) {
const { error, status } = spawnSync(command, args, {
cwd: PROJECT_ROOT,
stdio: "inherit",
Expand Down
Loading
Loading