Skip to content

Commit aaf721b

Browse files
authored
Use TS for rebuild-test-project-fixture script (#9804)
1 parent ffe7fb8 commit aaf721b

File tree

5 files changed

+57
-93
lines changed

5 files changed

+57
-93
lines changed

__fixtures__/test-project/web/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424
"@types/react": "18.2.37",
2525
"@types/react-dom": "18.2.15",
2626
"autoprefixer": "^10.4.16",
27-
"postcss": "^8.4.32",
27+
"postcss": "^8.4.33",
2828
"postcss-loader": "^7.3.4",
2929
"prettier-plugin-tailwindcss": "0.4.1",
30-
"tailwindcss": "^3.4.0"
30+
"tailwindcss": "^3.4.1"
3131
}
3232
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"project:deps": "node ./tasks/framework-tools/frameworkDepsToProject.mjs",
2828
"project:sync": "node ./tasks/framework-tools/frameworkSyncToProject.mjs",
2929
"project:tarsync": "node ./tasks/framework-tools/tarsync.mjs",
30-
"rebuild-test-project-fixture": "node ./tasks/test-project/rebuild-test-project-fixture.js",
30+
"rebuild-test-project-fixture": "tsx ./tasks/test-project/rebuild-test-project-fixture.ts",
3131
"release": "node ./tasks/release/release.mjs",
3232
"release:compare": "node ./tasks/release/compare/compare.mjs",
3333
"release:notes": "node ./tasks/release/generateReleaseNotes.mjs",

tasks/test-project/rebuild-test-project-fixture.js tasks/test-project/rebuild-test-project-fixture.ts

+33-50
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,28 @@
1-
#!/usr/bin/env node
2-
/* eslint-env node, es6*/
3-
//@ts-check
4-
const fs = require('fs')
5-
const os = require('os')
6-
const path = require('path')
7-
8-
const chalk = require('chalk')
9-
const fse = require('fs-extra')
10-
const { rimraf } = require('rimraf')
11-
const { hideBin } = require('yargs/helpers')
12-
const yargs = require('yargs/yargs')
13-
14-
const {
15-
RedwoodTUI,
16-
ReactiveTUIContent,
17-
RedwoodStyling,
18-
} = require('@redwoodjs/tui')
19-
20-
const {
1+
import fs from 'node:fs'
2+
import os from 'node:os'
3+
import path from 'node:path'
4+
5+
import chalk from 'chalk'
6+
import fse from 'fs-extra'
7+
import { rimraf } from 'rimraf'
8+
import { hideBin } from 'yargs/helpers'
9+
import yargs from 'yargs/yargs'
10+
11+
import { RedwoodTUI, ReactiveTUIContent, RedwoodStyling } from '@redwoodjs/tui'
12+
13+
import {
2114
addFrameworkDepsToProject,
2215
copyFrameworkPackages,
23-
} = require('./frameworkLinking')
24-
const { webTasks, apiTasks } = require('./tui-tasks')
25-
const { isAwaitable } = require('./typing')
26-
const {
27-
getExecaOptions: utilGetExecaOptions,
16+
} from './frameworkLinking'
17+
import { webTasks, apiTasks } from './tui-tasks'
18+
import { isAwaitable } from './typing'
19+
import type { TuiTaskDef } from './typing'
20+
import {
21+
getExecaOptions as utilGetExecaOptions,
2822
updatePkgJsonScripts,
2923
ExecaError,
3024
exec,
31-
} = require('./util')
25+
} from './util'
3226

3327
const args = yargs(hideBin(process.argv))
3428
.usage('Usage: $0 [option]')
@@ -55,6 +49,7 @@ const args = yargs(hideBin(process.argv))
5549

5650
const { verbose, resume, resumePath, resumeStep } = args
5751

52+
const RW_FRAMEWORK_PATH = path.join(__dirname, '../../')
5853
const OUTPUT_PROJECT_PATH = resumePath
5954
? /* path.resolve(String(resumePath)) */ resumePath
6055
: path.join(
@@ -82,27 +77,18 @@ if (!startStep) {
8277
}
8378
}
8479

85-
const RW_FRAMEWORKPATH = path.join(__dirname, '../../')
86-
8780
const tui = new RedwoodTUI()
8881

89-
/** @type {(string) => import('execa').Options} */
90-
function getExecaOptions(cwd) {
82+
function getExecaOptions(cwd: string) {
9183
return { ...utilGetExecaOptions(cwd), stdio: 'pipe' }
9284
}
9385

94-
/**
95-
* @param {string} step
96-
*/
97-
function beginStep(step) {
86+
function beginStep(step: string) {
9887
fs.mkdirSync(OUTPUT_PROJECT_PATH, { recursive: true })
9988
fs.writeFileSync(path.join(OUTPUT_PROJECT_PATH, 'step.txt'), '' + step)
10089
}
10190

102-
/**
103-
* @param {import('./typing').TuiTaskDef} taskDef
104-
*/
105-
async function tuiTask({ step, title, content, task, parent }) {
91+
async function tuiTask({ step, title, content, task, parent }: TuiTaskDef) {
10692
const stepId = (parent ? parent + '.' : '') + step
10793

10894
const tuiContent = new ReactiveTUIContent({
@@ -139,7 +125,7 @@ async function tuiTask({ step, title, content, task, parent }) {
139125
return
140126
}
141127

142-
let promise
128+
let promise: void | Promise<unknown>
143129

144130
try {
145131
promise = task()
@@ -251,28 +237,25 @@ if (resumePath && !fs.existsSync(path.join(resumePath, 'redwood.toml'))) {
251237
}
252238

253239
const createProject = () => {
254-
let cmd = `yarn node ./packages/create-redwood-app/dist/create-redwood-app.js ${OUTPUT_PROJECT_PATH}`
240+
const cmd = `yarn node ./packages/create-redwood-app/dist/create-redwood-app.js ${OUTPUT_PROJECT_PATH}`
255241

256242
const subprocess = exec(
257243
cmd,
258244
// We create a ts project and convert using ts-to-js at the end if typescript flag is false
259245
['--no-yarn-install', '--typescript', '--overwrite', '--no-git'],
260-
getExecaOptions(RW_FRAMEWORKPATH)
246+
getExecaOptions(RW_FRAMEWORK_PATH)
261247
)
262248

263249
return subprocess
264250
}
265251

266252
const copyProject = async () => {
267-
const FIXTURE_TESTPROJ_PATH = path.join(
268-
RW_FRAMEWORKPATH,
269-
'__fixtures__/test-project'
270-
)
253+
const fixturePath = path.join(RW_FRAMEWORK_PATH, '__fixtures__/test-project')
271254

272255
// remove existing Fixture
273-
await rimraf(FIXTURE_TESTPROJ_PATH)
256+
await rimraf(fixturePath)
274257
// copy from tempDir to Fixture dir
275-
await fse.copy(OUTPUT_PROJECT_PATH, FIXTURE_TESTPROJ_PATH)
258+
await fse.copy(OUTPUT_PROJECT_PATH, fixturePath)
276259
// cleanup after ourselves
277260
await rimraf(OUTPUT_PROJECT_PATH)
278261
}
@@ -304,7 +287,7 @@ async function runCommand() {
304287
return exec(
305288
'yarn build:clean && yarn build',
306289
[],
307-
getExecaOptions(RW_FRAMEWORKPATH)
290+
getExecaOptions(RW_FRAMEWORK_PATH)
308291
)
309292
},
310293
})
@@ -315,7 +298,7 @@ async function runCommand() {
315298
content: 'Adding framework dependencies to project...',
316299
task: () => {
317300
return addFrameworkDepsToProject(
318-
RW_FRAMEWORKPATH,
301+
RW_FRAMEWORK_PATH,
319302
OUTPUT_PROJECT_PATH,
320303
'pipe' // TODO: Remove this when everything is using @rwjs/tui
321304
)
@@ -362,7 +345,7 @@ async function runCommand() {
362345
title: '[link] Copying framework packages to project',
363346
task: () => {
364347
return copyFrameworkPackages(
365-
RW_FRAMEWORKPATH,
348+
RW_FRAMEWORK_PATH,
366349
OUTPUT_PROJECT_PATH,
367350
'pipe'
368351
)

tasks/test-project/typing.js

-40
This file was deleted.

tasks/test-project/typing.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export interface TuiTaskDef {
2+
/** 0 based step number */
3+
step: number
4+
/** The parent task to this task. */
5+
parent?: string
6+
/** Title of this task. */
7+
title: string
8+
/** Reactive content */
9+
content?: string
10+
/**
11+
* Whether this task is enabled or not. Disabled tasks don't show up in the
12+
* list
13+
*/
14+
enabled?: boolean | (() => boolean)
15+
/** The task to run. Will be passed an instance of TUI when called */
16+
task: () => Promise<unknown> | void
17+
}
18+
19+
export function isAwaitable(promise: unknown): promise is Promise<unknown> {
20+
return typeof promise !== 'undefined' && 'then' in promise
21+
}

0 commit comments

Comments
 (0)