Skip to content

Commit c9ea2d9

Browse files
authored
switch scripts package to pure typescript (#4106)
1 parent ad85949 commit c9ea2d9

23 files changed

Lines changed: 92 additions & 164 deletions

.yarnrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ catalogs:
239239
react-native-windows: ^0.81.0
240240
react-test-renderer: 19.1.4
241241

242-
dynamicPackageExtensions: ./scripts/dynamic.extensions.mjs
242+
dynamicPackageExtensions: ./scripts/dynamic.extensions.mts
243243

244244
enableGlobalCache: false
245245

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
],
1717
"scripts": {
1818
"build": "lage build-all",
19-
"clean-all": "node ./scripts/src/preinstall/clean-all.js",
19+
"clean-all": "node ./scripts/src/preinstall/clean-all.ts",
2020
"docs": "yarn workspace fluent-rn-website start",
2121
"bundle:repo": "lage bundle",
2222
"clean": "lage clean",
2323
"change": "node .github/scripts/change.mts",
2424
"changeset:version": "node .github/scripts/changeset-version-with-postbump.mts",
2525
"change:check": "node .github/scripts/change.mts --check",
26-
"check-publishing": "node ./scripts/src/cli.mjs check-publishing",
26+
"check-publishing": "node ./scripts/src/cli.ts check-publishing",
2727
"lint-fix": "cross-env FURN_FIX_MODE=true lage lint",
2828
"lint-package-fix": "cross-env FURN_FIX_MODE=true lage lint-package",
29-
"preinstall": "node ./scripts/src/preinstall/use-yarn-please.js",
29+
"preinstall": "node ./scripts/src/preinstall/use-yarn-please.ts",
3030
"format": "oxfmt",
3131
"format:check": "oxfmt --check",
3232
"lint-lockfile": "lint-lockfile",
Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
// @ts-check
2-
31
import fs from 'node:fs';
42
import path from 'node:path';
5-
import { getToolVersion, hasJestConfig } from './src/preinstall/tool-versions.js';
3+
import { getToolVersion, hasJestConfig } from './src/preinstall/tool-versions.ts';
4+
import type { PackageManifest } from './src/utils/projectRoot.ts';
65

7-
/**
8-
* @typedef {() => boolean} ConditionalCheck
9-
*/
6+
type ConditionalCheck = () => boolean;
107

118
/**
129
* Conditionally add a dependency to the given dependencies object if it is not already present
13-
* @param {string[]} depsToAdd
14-
* @param {import('./src/utils/projectRoot.ts').PackageManifest} manifest
15-
* @param {ConditionalCheck | boolean | undefined} condition
16-
* @returns {Record<string, string>}
1710
*/
18-
function conditionallyAdd(depsToAdd, manifest, condition) {
19-
/** @type {Record<string, string>} */
20-
const newDeps = {};
11+
function conditionallyAdd(
12+
depsToAdd: string[],
13+
manifest: PackageManifest,
14+
condition: ConditionalCheck | boolean | undefined,
15+
): Record<string, string> {
16+
const newDeps: Record<string, string> = {};
2117
if (!condition || (typeof condition === 'function' ? condition() : condition)) {
2218
for (const dep of depsToAdd) {
2319
if (!manifest.dependencies?.[dep] && !manifest.devDependencies?.[dep]) {
@@ -36,29 +32,23 @@ function conditionallyAdd(depsToAdd, manifest, condition) {
3632
}
3733

3834
/**
39-
* @param {import('./src/utils/projectRoot.ts').PackageManifest} manifest - The package manifest.
40-
* @returns {boolean} true if oxfmt should be added based on the manifest's prettier scripts
35+
* Returns true if oxfmt should be added based on the manifest's prettier scripts.
4136
*/
42-
function addOxfmt(manifest) {
37+
function addOxfmt(manifest: PackageManifest): boolean {
4338
return Boolean(manifest && manifest.scripts && (manifest.scripts.format || manifest.scripts['format:fix']));
4439
}
4540

4641
/**
4742
* Check if Jest is already in the manifest or if a Jest script is defined.
48-
* @param {string} cwd - The current working directory.
49-
* @param {import('./src/utils/projectRoot.ts').PackageManifest} manifest - The package manifest.
50-
* @returns {boolean} - True if Jest should be added, false otherwise.
5143
*/
52-
function addJest(cwd, manifest) {
44+
function addJest(cwd: string, manifest: PackageManifest): boolean {
5345
return Boolean(manifest.scripts?.test && hasJestConfig(cwd));
5446
}
5547

5648
/**
5749
* Get the dynamic dependencies for the given package given the package root directory and its manifest.
58-
* @param {{cwd: string, manifest: import('./src/utils/projectRoot.ts').PackageManifest}} param0
59-
* @returns { { dependencies: Record<string, string> } }
6050
*/
61-
export default function ({ cwd, manifest }) {
51+
export default function ({ cwd, manifest }: { cwd: string; manifest: PackageManifest }): { dependencies: Record<string, string> } {
6252
const enableLinting = Boolean(manifest.scripts && manifest.scripts.lint);
6353

6454
return {

scripts/package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
"directory": "scripts"
1010
},
1111
"bin": {
12-
"fluentui-scripts": "./src/cli.mjs"
12+
"fluentui-scripts": "./src/cli.ts"
1313
},
1414
"type": "module",
15-
"main": "./src/index.js",
15+
"main": "./src/index.ts",
1616
"exports": {
1717
".": {
18-
"require": "./src/index.js",
19-
"default": "./src/index.js"
18+
"require": "./src/index.ts",
19+
"default": "./src/index.ts"
2020
},
2121
"./babel-config": "./configs/jest/babel.config.cjs",
2222
"./jest-config": "./configs/jest/jest.config.cjs",
@@ -29,10 +29,10 @@
2929
"build": "tsgo",
3030
"build-core": "tsgo",
3131
"bundlesize": "bundlesize --debug",
32-
"depcheck": "node ./src/cli.mjs depcheck",
33-
"format": "node ./src/cli.mjs format",
34-
"lint": "node ./src/cli.mjs lint",
35-
"lint-package": "node ./src/cli.mjs lint-package"
32+
"depcheck": "node ./src/cli.ts depcheck",
33+
"format": "node ./src/cli.ts format",
34+
"lint": "node ./src/cli.ts lint",
35+
"lint-package": "node ./src/cli.ts lint-package"
3636
},
3737
"dependencies": {
3838
"@babel/core": "catalog:",
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#!/usr/bin/env node
2-
// @ts-check
32

43
import { Builtins, Cli } from 'clipanion';
54
import { BuildCommand } from './tasks/build.ts';
6-
import { CleanCommand } from './tasks/clean.js';
7-
import { FormatCommand } from './tasks/format.js';
8-
import { LintCommand } from './tasks/lint.js';
5+
import { CleanCommand } from './tasks/clean.ts';
6+
import { FormatCommand } from './tasks/format.ts';
7+
import { LintCommand } from './tasks/lint.ts';
98
import { LintPackageCommand } from './tasks/lintPackage.ts';
10-
import { JestCommand } from './tasks/jest.js';
11-
import { CheckPublishingCommand } from './tasks/checkPublishingTask.js';
9+
import { JestCommand } from './tasks/jest.ts';
10+
import { CheckPublishingCommand } from './tasks/checkPublishingTask.ts';
1211
import { DepcheckCommand } from './tasks/depcheck.ts';
1312

1413
const cli = new Cli({
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
// @ts-check
21
export { isPnpmMode } from './utils/ispnpm.ts';
Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
// @ts-check
2-
3-
const child_process = require('child_process');
4-
const fs = require('fs');
5-
const path = require('path');
6-
const os = require('os');
1+
import child_process from 'node:child_process';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import os from 'node:os';
75
// This script MUST NOT have any deps aside from Node built-ins, because it deletes all node_modules!
86

9-
/**
10-
* @param {string} question - The question to prompt the user with
11-
* @returns {Promise<string>}
12-
* */
13-
function prompt(question) {
7+
function prompt(question: string): Promise<string> {
148
return new Promise((resolve) => {
159
process.stdin.resume();
1610
process.stdout.write(question);
@@ -22,10 +16,7 @@ function prompt(question) {
2216
});
2317
}
2418

25-
/**
26-
* @param {string} itemPath - The path to the item to check
27-
*/
28-
function deleteIfSymlink(itemPath) {
19+
function deleteIfSymlink(itemPath: string): void {
2920
itemPath = path.resolve(itemPath);
3021
try {
3122
// Compare realpath since fs.statSync(itemPath).isSymbolicLink() doesn't work on Windows
@@ -40,10 +31,8 @@ function deleteIfSymlink(itemPath) {
4031

4132
/**
4233
* Deletes symlinks in the specified node_modules directory.
43-
* @param {string} nodeModulesPath - The path to the node_modules directory
44-
* @returns {void}
4534
*/
46-
function deleteNodeModulesSymlinks(nodeModulesPath) {
35+
function deleteNodeModulesSymlinks(nodeModulesPath: string): void {
4736
if (!fs.existsSync(nodeModulesPath)) {
4837
return;
4938
}
@@ -62,10 +51,8 @@ function deleteNodeModulesSymlinks(nodeModulesPath) {
6251

6352
/**
6453
* Deletes symlinks in the specified parent folder.
65-
* @param {string} parentFolder - The path to the parent folder
66-
* @returns {void}
6754
*/
68-
function deleteSymlinks(parentFolder) {
55+
function deleteSymlinks(parentFolder: string): void {
6956
const parentPath = path.join(process.cwd(), parentFolder);
7057
if (!fs.existsSync(parentPath)) {
7158
return;
Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ import fs from 'node:fs';
22
import path from 'node:path';
33
import { fileURLToPath } from 'node:url';
44

5+
import type { PackageManifest } from '../utils/projectRoot.ts';
6+
57
/**
68
* Get the package.json manifest for a given folder.
7-
* @param {string} folder
8-
* @returns {import('../utils/projectRoot.ts').PackageManifest}
99
*/
10-
function getPackageManifest(folder) {
10+
function getPackageManifest(folder: string): PackageManifest {
1111
const manifestPath = path.join(folder, 'package.json');
1212
return JSON.parse(fs.readFileSync(manifestPath, 'utf-8'));
1313
}
@@ -17,8 +17,7 @@ const scriptFolder = path.join(path.dirname(fileURLToPath(import.meta.url)), '..
1717
const scriptManifest = getPackageManifest(scriptFolder);
1818
const rootManifest = getPackageManifest(path.dirname(scriptFolder));
1919

20-
/** @type {Record<string, string>} */
21-
const devToolVersions = {
20+
const devToolVersions: Record<string, string> = {
2221
'@eslint/js': '^9.0.0',
2322
'@microsoft/eslint-plugin-sdl': '^1.1.0',
2423
'@react-native-community/cli': '^13.6.4',
@@ -58,27 +57,21 @@ const devToolVersions = {
5857
...rootManifest.devDependencies,
5958
};
6059

61-
/** @type {Record<string, string>} */
62-
const workspaceToolVersions = {
60+
const workspaceToolVersions: Record<string, string> = {
6361
'@fluentui-react-native/scripts': 'workspace:*',
6462
};
6563

66-
/**
67-
*
68-
* @param {string} packageName
69-
* @returns {string | null}
70-
*/
71-
export function getToolVersion(packageName) {
64+
export function getToolVersion(packageName: string): string | null {
7265
return devToolVersions[packageName] ?? workspaceToolVersions[packageName] ?? null;
7366
}
7467

7568
const CONFIG_EXTENSIONS = ['.js', '.cjs', '.mjs', '.ts', '.cts', '.mts'];
7669

7770
/**
78-
* @param {string} cwd - The current working directory.
79-
* @returns {boolean} - True if a Jest config file exists in the cwd, false otherwise.
71+
* @param cwd the current working directory.
72+
* @returns true if a Jest config file exists in the cwd, false otherwise.
8073
*/
81-
export function hasJestConfig(cwd) {
74+
export function hasJestConfig(cwd: string): boolean {
8275
for (const ext of CONFIG_EXTENSIONS) {
8376
if (fs.existsSync(path.join(cwd, `jest.config${ext}`))) {
8477
return true;

scripts/src/tasks/build.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
// @ts-check
2-
31
import { Command } from 'clipanion';
4-
import { runYarn } from '../utils/runScript.js';
2+
import { runYarn } from '../utils/runScript.ts';
53
import { getProjectRoot } from '../utils/projectRoot.ts';
64
import { getResolvedConfig } from '../utils/buildConfig.ts';
75

0 commit comments

Comments
 (0)