Skip to content

Commit 39094de

Browse files
authored
chore: convert internal scripts to .mts (#2367)
1 parent a213de5 commit 39094de

22 files changed

+188
-270
lines changed

.github/workflows/build.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ jobs:
239239
- name: react-native run-ios
240240
if: ${{ steps.affected.outputs.ios != '' }}
241241
run: |
242-
../scripts/testing/react-native.mjs run-ios
242+
../scripts/testing/react-native.mts run-ios
243243
working-directory: template-example
244244
timeout-minutes: 60
245245
android:
@@ -326,7 +326,7 @@ jobs:
326326
- name: react-native run-android
327327
if: ${{ steps.affected.outputs.android != '' }}
328328
run: |
329-
../scripts/testing/react-native.mjs run-android
329+
../scripts/testing/react-native.mts run-android
330330
working-directory: template-example
331331
timeout-minutes: 60
332332
macos:

android/app/src/main/java/com/microsoft/reacttestapp/manifest/Manifest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file was generated by generate-manifest.mjs.
1+
// This file was generated by generate-manifest.mts.
22
// DO NOT MODIFY. ALL CHANGES WILL BE OVERWRITTEN.
33

44
@file:Suppress("ktlint:standard:trailing-comma-on-declaration-site")

eslint.config.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@ module.exports = [
2929
},
3030
{
3131
files: [
32-
"scripts/internal/generate-manifest-docs.mjs",
33-
"scripts/internal/generate-manifest.mjs",
34-
"scripts/internal/generate-schema.mjs",
35-
"scripts/internal/pack.mjs",
36-
"scripts/internal/set-react-version.mjs",
37-
"scripts/internal/test.mjs",
32+
"scripts/internal/generate-manifest-docs.mts",
33+
"scripts/internal/generate-manifest.mts",
34+
"scripts/internal/generate-schema.mts",
35+
"scripts/internal/pack.mts",
36+
"scripts/internal/set-react-version.mts",
37+
"scripts/internal/test.mts",
3838
"scripts/schema.mjs",
39-
"scripts/testing/test-apple.mjs",
40-
"scripts/testing/test-e2e.mjs",
41-
"scripts/testing/test-matrix.mjs",
39+
"scripts/testing/test-apple.mts",
40+
"scripts/testing/test-e2e.mts",
41+
"scripts/testing/test-matrix.mts",
4242
"scripts/utils/colors.mjs",
4343
],
4444
rules: {

ios/ReactTestApp/Manifest.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// This file was generated by generate-manifest.mjs.
1+
// This file was generated by generate-manifest.mts.
22
// DO NOT MODIFY. ALL CHANGES WILL BE OVERWRITTEN.
33

44
struct Component {

package.json

+12-12
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,24 @@
6767
},
6868
"scripts": {
6969
"format:c": "clang-format -i $(git ls-files '*.cpp' '*.h' '*.m' '*.mm')",
70-
"format:js": "prettier --write --log-level error $(git ls-files '*.[cm]js' '*.[jt]s' '*.tsx' '*.yml' 'CONTRIBUTING.md' 'README.md' 'test/**/*.json' ':!:.yarn/releases')",
70+
"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')",
7171
"format:swift": "swiftformat $(git ls-files '*.swift')",
72-
"generate:code": "node scripts/internal/generate-manifest.mjs",
73-
"generate:docs": "node scripts/internal/generate-manifest-docs.mjs",
74-
"generate:schema": "node scripts/internal/generate-schema.mjs",
72+
"generate:code": "node --experimental-transform-types --no-warnings scripts/internal/generate-manifest.mts",
73+
"generate:docs": "node --experimental-transform-types --no-warnings scripts/internal/generate-manifest-docs.mts",
74+
"generate:schema": "node --experimental-transform-types --no-warnings scripts/internal/generate-schema.mts",
7575
"lint:commit": "git log --format='%s' origin/trunk..HEAD | tail -1 | npx @rnx-kit/[email protected]",
76-
"lint:js": "eslint $(git ls-files '*.[cm]js' '*.[jt]s' '*.tsx' ':!:*.config.js' ':!:.yarn/releases') && tsc && tsc --project tsconfig.cjs.json",
76+
"lint:js": "eslint $(git ls-files '*.[cm][jt]s' '*.[jt]s' '*.tsx' ':!:*.config.js' ':!:.yarn/releases') && tsc && tsc --project tsconfig.cjs.json",
7777
"lint:kt": "ktlint --relative 'android/app/src/**/*.kt'",
7878
"lint:rb": "bundle exec rubocop",
7979
"lint:swift": "swiftlint",
80-
"prepack": "node scripts/internal/pack.mjs pre",
81-
"postpack": "node scripts/internal/pack.mjs post",
82-
"release-notes": "node scripts/internal/release-notes.mjs",
83-
"set-react-version": "node scripts/internal/set-react-version.mjs",
84-
"show-affected": "node --experimental-transform-types --no-warnings scripts/build/affected.ts",
85-
"test": "node scripts/internal/test.mjs",
80+
"prepack": "node --experimental-transform-types --no-warnings scripts/internal/pack.mts pre",
81+
"postpack": "node --experimental-transform-types --no-warnings scripts/internal/pack.mts post",
82+
"release-notes": "node --experimental-transform-types --no-warnings scripts/internal/release-notes.mts",
83+
"set-react-version": "node --experimental-transform-types --no-warnings scripts/internal/set-react-version.mts",
84+
"show-affected": "node --experimental-transform-types --no-warnings scripts/build/affected.mts",
85+
"test": "node --experimental-transform-types --no-warnings scripts/internal/test.mts",
8686
"test:js": "node --experimental-transform-types --no-warnings --test $(git ls-files '*.test.ts')",
87-
"test:matrix": "node scripts/testing/test-matrix.mjs",
87+
"test:matrix": "node --experimental-transform-types --no-warnings scripts/testing/test-matrix.mts",
8888
"test:rb": "bundle exec ruby -Ilib:test -e \"Dir.glob('./test/test_*.rb').each { |file| require(file) }\""
8989
},
9090
"dependencies": {
File renamed without changes.

scripts/internal/generate-manifest-docs.mjs renamed to scripts/internal/generate-manifest-docs.mts

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
// @ts-check
1+
// eslint-disable-next-line no-restricted-imports
2+
import type { SchemaObject } from "ajv";
23
import * as path from "node:path";
3-
import { readDocumentation } from "./generate-schema.mjs";
4+
import { assertDefinition, readDocumentation } from "./generate-schema.mts";
45
import { generateSchema } from "../schema.mjs";
56

67
async function generateManifestDocs() {
@@ -9,12 +10,13 @@ async function generateManifestDocs() {
910

1011
/**
1112
* Renders the specified JSON object schema.
12-
* @param {import("ajv").SchemaObject} definition
13-
* @param {string[]} toc
14-
* @param {string[]} lines
15-
* @param {string} scope
1613
*/
17-
const render = (definition, toc, lines, scope = "") => {
14+
const render = (
15+
definition: SchemaObject,
16+
toc: string[],
17+
lines: string[],
18+
scope = ""
19+
) => {
1820
if (Array.isArray(definition.allOf)) {
1921
for (const { $ref } of definition.allOf) {
2022
render(schema.$defs[$ref.replace("#/$defs/", "")], toc, lines, scope);
@@ -37,6 +39,8 @@ async function generateManifestDocs() {
3739
})();
3840

3941
for (const [key, def] of Object.entries(definition.properties)) {
42+
assertDefinition(def);
43+
4044
const { description, markdownDescription, type } = def;
4145
const text = markdownDescription || description;
4246
if (!text) {

scripts/internal/generate-manifest.mjs renamed to scripts/internal/generate-manifest.mts

+32-29
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,33 @@
1-
// @ts-check
1+
// eslint-disable-next-line no-restricted-imports
2+
import type { SchemaObject } from "ajv";
23
import * as fs from "node:fs/promises";
34
import * as path from "node:path";
45
import { URL, fileURLToPath } from "node:url";
56
import { generateSchema } from "../schema.mjs";
7+
import { assertDefinition } from "./generate-schema.mts";
8+
9+
type Language = {
10+
options: {
11+
indent: string;
12+
level: number;
13+
footer?: string;
14+
header?: string;
15+
};
16+
arrayProperty: (name: string, type: string, required: boolean) => string;
17+
objectProperty: (name: string, required: boolean) => string;
18+
stringProperty: (name: string, required: boolean) => string;
19+
structBegin: (name: string) => string;
20+
structEnd: string;
21+
};
622

7-
/**
8-
* @import { SchemaObject } from "ajv";
9-
* @import { Language } from "../types.js";
10-
*/
1123
const thisScript = fileURLToPath(import.meta.url);
1224

1325
/**
1426
* Returns the struct name of the definition key or reference.
1527
* @param {string} ref
1628
* @returns {string}
1729
*/
18-
function typename(ref) {
30+
function typename(ref: string): string {
1931
const i = ref.lastIndexOf("/") + 1;
2032
return ref[i].toUpperCase() + ref.substring(i + 1);
2133
}
@@ -25,11 +37,10 @@ function typename(ref) {
2537
* @param {string} output
2638
* @returns {Language}
2739
*/
28-
function getLanguage(output) {
40+
function getLanguage(output: string): Language {
2941
switch (path.extname(output)) {
3042
case ".h": {
31-
/** @type {(type: string, required: boolean) => string} */
32-
const nullable = (type, required) =>
43+
const nullable = (type: string, required: boolean) =>
3344
required ? type : `std::optional<${type}>`;
3445
return {
3546
options: {
@@ -80,8 +91,7 @@ function getLanguage(output) {
8091
}
8192

8293
case ".kt": {
83-
/** @type {(required: boolean) => "" | "?"} */
84-
const nullable = (required) => (required ? "" : "?");
94+
const nullable = (required: boolean) => (required ? "" : "?");
8595
return {
8696
options: {
8797
indent: " ",
@@ -110,8 +120,7 @@ function getLanguage(output) {
110120
}
111121

112122
case ".swift": {
113-
/** @type {(required: boolean) => "" | "?"} */
114-
const nullable = (required) => (required ? "" : "?");
123+
const nullable = (required: boolean) => (required ? "" : "?");
115124
return {
116125
options: {
117126
indent: " ",
@@ -152,12 +161,12 @@ function getLanguage(output) {
152161

153162
/**
154163
* Generates a data model from the specified schema definition.
155-
* @param {string} name
156-
* @param {SchemaObject} definition
157-
* @param {Language} lang
158-
* @returns {string[]}
159164
*/
160-
function generateType(name, definition, lang) {
165+
function generateType(
166+
name: string,
167+
definition: SchemaObject,
168+
lang: Language
169+
): string[] {
161170
const { indent, level } = lang.options;
162171
const outer = indent.repeat(level);
163172
const inner = indent.repeat(level + 1);
@@ -166,6 +175,8 @@ function generateType(name, definition, lang) {
166175

167176
const { properties, required = [] } = definition;
168177
Object.entries(properties).forEach(([name, prop]) => {
178+
assertDefinition(prop);
179+
169180
const isRequired = required.includes(name);
170181
switch (prop.type) {
171182
case "array":
@@ -188,10 +199,8 @@ function generateType(name, definition, lang) {
188199

189200
/**
190201
* Generates manifest data models and writes them to specified path.
191-
* @param {SchemaObject} schema
192-
* @param {string} output
193202
*/
194-
async function generate(schema, output) {
203+
async function generate(schema: SchemaObject, output: string) {
195204
const lang = getLanguage(output);
196205
const lines = [
197206
`// This file was generated by ${path.basename(thisScript)}.`,
@@ -204,15 +213,9 @@ async function generate(schema, output) {
204213
}
205214

206215
Object.entries(schema.$defs).forEach(([key, definition]) => {
216+
assertDefinition(definition);
207217
if (!("exclude-from-codegen" in definition)) {
208-
lines.push(
209-
...generateType(
210-
typename(key),
211-
/** @type {SchemaObject} */ (definition),
212-
lang
213-
),
214-
""
215-
);
218+
lines.push(...generateType(typename(key), definition, lang), "");
216219
}
217220
return lines;
218221
});

scripts/internal/generate-schema.mjs renamed to scripts/internal/generate-schema.mts

+23-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1-
// @ts-check
1+
// eslint-disable-next-line no-restricted-imports
2+
import type { SchemaObject } from "ajv";
23
import * as fs from "node:fs/promises";
34
import * as os from "node:os";
45
import * as path from "node:path";
56
import { URL, fileURLToPath } from "node:url";
67
import { isMain } from "../helpers.js";
78
import { generateSchema } from "../schema.mjs";
9+
import type { Docs } from "../types.js";
810

9-
/** @import { Docs } from "../types.js"; */
11+
type Definition = SchemaObject & {
12+
type: string;
13+
description: string;
14+
markdownDescription?: string;
15+
};
1016

11-
/** @type {(str: string) => string} */
12-
const stripCarriageReturn =
17+
const stripCarriageReturn: (str: string) => string =
1318
os.EOL === "\r\n" ? (str) => str.replaceAll("\r", "") : (str) => str;
1419

15-
/**
16-
* @returns {Promise<Partial<Docs>>}
17-
*/
18-
export async function readDocumentation() {
19-
/** @type {Partial<Docs>} */
20-
const docs = {};
20+
export function assertDefinition(props: unknown): asserts props is Definition {
21+
if (
22+
!props ||
23+
typeof props !== "object" ||
24+
!("type" in props || "allOf" in props || "oneOf" in props)
25+
) {
26+
throw new Error(`Invalid definition in schema: ${JSON.stringify(props)}`);
27+
}
28+
}
29+
30+
export async function readDocumentation(): Promise<Partial<Docs>> {
31+
const docs: Partial<Docs> = {};
2132
const docsDir = fileURLToPath(new URL("../../docs", import.meta.url));
2233

23-
/** @type {(keyof Docs)[]} */
24-
const keys = [
34+
const keys: (keyof Docs)[] = [
2535
"introduction",
2636
"bundleRoot",
2737
"components",
@@ -67,6 +77,7 @@ if (isMain(import.meta.url)) {
6777
.then((docs) => generateSchema(docs))
6878
.then((schema) => {
6979
for (const def of Object.values(schema.$defs)) {
80+
assertDefinition(def);
7081
delete def["exclude-from-codegen"];
7182
}
7283
return stripCarriageReturn(JSON.stringify(schema, undefined, 2)) + "\n";

scripts/internal/pack.mjs renamed to scripts/internal/pack.mts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
// @ts-check
21
import * as fs from "node:fs";
32
import * as path from "node:path";
43

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

76
/**
87
* Renames `.dotfile` to `_dotfile`.
9-
* @param {string} p
10-
* @returns {string}
118
*/
12-
function renameDotFile(p) {
9+
function renameDotFile(p: string): string {
1310
return path.join(path.dirname(p), "_" + path.basename(p).substring(1));
1411
}
1512

scripts/internal/prepare-viewfinder.mjs renamed to scripts/internal/prepare-viewfinder.mts

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#!/usr/bin/env node
2-
// @ts-check
1+
#!/usr/bin/env -S node --experimental-transform-types --no-warnings
32

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

4140
/**
4241
* Runs the specified command.
43-
* @param {string} command
44-
* @param {string[]} args
45-
* @param {Record<string, unknown>=} options
4642
*/
47-
function $(command, args, options) {
43+
function $(command: string, args: string[], options?: Record<string, unknown>) {
4844
const { error, status } = spawnSync(command, args, {
4945
cwd: PROJECT_ROOT,
5046
stdio: "inherit",

0 commit comments

Comments
 (0)