From eafe7c3251e6903c2b108bbf181bc8c50f72b7c4 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 4 Jan 2024 14:37:57 +0800 Subject: [PATCH] feat: support patchFiles option --- tests/vuetify.ts | 17 +++++++++-------- types.d.ts | 1 + utils.ts | 12 ++++++++++++ 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/tests/vuetify.ts b/tests/vuetify.ts index 74d60cf..d0b985e 100644 --- a/tests/vuetify.ts +++ b/tests/vuetify.ts @@ -1,22 +1,23 @@ import { runInRepo } from '../utils' import { RunOptions } from '../types' -import path from 'node:path' -import fs from 'node:fs' export async function test(options: RunOptions) { await runInRepo({ ...options, repo: 'vuetifyjs/vuetify', branch: 'master', - beforeBuild: async () => { - const dir = path.resolve(options.workspace, 'vuetify') - const filePath = path.resolve(dir, 'packages/vuetify/src/globals.d.ts') - const file = fs.readFileSync(filePath, 'utf-8') - fs.writeFileSync(filePath, `import 'vue/jsx'\n` + file) - }, build: 'yarn workspace vuetify run build', // there's also an e2e test script in vuetify, // but it seems flaky, so I skipped it for now test: ['yarn lerna run test:coverage -- -- -i'], + patchFiles: { + 'packages/vuetify/src/globals.d.ts': (content) => { + if (!content.includes('vue/jsx')) { + return `import 'vue/jsx'\n${content}` + } else { + return content + } + }, + }, }) } diff --git a/types.d.ts b/types.d.ts index 985ade1..479c0bb 100644 --- a/types.d.ts +++ b/types.d.ts @@ -22,6 +22,7 @@ export interface RunOptions { beforeInstall?: Task | Task[] beforeBuild?: Task | Task[] beforeTest?: Task | Task[] + patchFiles?: Record string> } type Task = string | (() => Promise) diff --git a/utils.ts b/utils.ts index b1c9107..ac32924 100644 --- a/utils.ts +++ b/utils.ts @@ -215,6 +215,7 @@ export async function runInRepo(options: RunOptions & RepoOptions) { beforeInstall, beforeBuild, beforeTest, + patchFiles, } = options const dir = path.resolve( options.workspace, @@ -285,6 +286,17 @@ export async function runInRepo(options: RunOptions & RepoOptions) { } } await applyPackageOverrides(dir, pkg, overrides) + + if (patchFiles) { + for (const fileName in patchFiles) { + const filePath = path.resolve(dir, fileName) + const patchFn = patchFiles[fileName] + const content = fs.readFileSync(filePath, 'utf-8') + fs.writeFileSync(filePath, patchFn(content)) + console.log(`patched file: ${fileName}`) + } + } + await beforeBuildCommand?.(pkg.scripts) await buildCommand?.(pkg.scripts) if (test) {