Skip to content

Commit

Permalink
fix(vue-tsc): clean fixture building steps
Browse files Browse the repository at this point in the history
  • Loading branch information
fi3ework committed Jan 24, 2023
1 parent 0816f95 commit c133cb2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 77 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@tsconfig/node16-strictest-esm": "^1.0.3",
"@types/babel__code-frame": "^7.0.2",
"@types/debug": "^4.1.5",
"@types/fs-extra": "^9.0.11",
"@types/fs-extra": "^11.0.1",
"@types/klaw": "^3.0.3",
"@types/minimist": "^1.2.2",
"@types/node": "^16.0.0",
Expand All @@ -60,7 +60,7 @@
"execa": "^5.1.1",
"fast-glob": "^3.2.7",
"fast-json-stable-stringify": "^2.1.0",
"fs-extra": "^10.0.0",
"fs-extra": "^11.1.0",
"jest-serializer-path": "^0.1.15",
"klaw": "^4.0.1",
"lint-staged": "^11.0.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/vite-plugin-checker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"chokidar": "^3.5.1",
"commander": "^8.0.0",
"fast-glob": "^3.2.7",
"fs-extra": "^11.1.0",
"lodash.debounce": "^4.0.8",
"lodash.pick": "^4.4.0",
"npm-run-path": "^4.0.1",
Expand Down Expand Up @@ -95,6 +96,7 @@
}
},
"devDependencies": {
"@types/fs-extra": "^11.0.1",
"@types/eslint": "^7.2.14",
"@types/lodash.debounce": "^4.0.6",
"@types/lodash.pick": "^4.4.6",
Expand Down
87 changes: 28 additions & 59 deletions packages/vite-plugin-checker/src/checkers/vueTsc/prepareVueTsc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import fs from 'fs'
import fsExtra from 'fs-extra'
import { createRequire } from 'module'
import path, { dirname } from 'path'
import { fileURLToPath } from 'url'
import { writeFile, access, readFile, rm } from 'fs/promises'

const { copy, mkdir } = fsExtra
const _require = createRequire(import.meta.url)

// isomorphic __dirname https://antfu.me/posts/isomorphic-dirname
Expand Down Expand Up @@ -52,84 +55,50 @@ const textToReplace: { target: string; replacement: string }[] = [
},
]

export function prepareVueTsc() {
export async function prepareVueTsc() {
// 1. copy typescript to folder
const targetTsDir = path.resolve(_dirname, 'typescript-vue-tsc')
const vueTscFlagFile = path.resolve(targetTsDir, 'vue-tsc-resolve-path')

let shouldPrepare = true
const targetDirExist = fs.existsSync(targetTsDir)
if (targetDirExist) {
try {
const targetTsVersion = _require(path.resolve(targetTsDir, 'package.json')).version
const currTsVersion = _require('typescript/package.json').version
// check fixture versions before re-use
if (
targetTsVersion === currTsVersion &&
fs.existsSync(vueTscFlagFile) &&
fs.readFileSync(vueTscFlagFile, 'utf8') === proxyPath
) {
shouldPrepare = true
}
} catch {
shouldPrepare = true
let shouldBuildFixture = true
try {
await access(targetTsDir)
const targetTsVersion = _require(path.resolve(targetTsDir, 'package.json')).version
const currTsVersion = _require('typescript/package.json').version
// check fixture versions before re-use
await access(vueTscFlagFile)
const fixtureFlagContent = await readFile(vueTscFlagFile, 'utf8')
if (targetTsVersion === currTsVersion && fixtureFlagContent === proxyPath) {
shouldBuildFixture = false
}
} catch (e) {
// no matter what error, we should rebuild the fixture
shouldBuildFixture = true
}

if (shouldPrepare) {
rimraf(targetTsDir)
fs.mkdirSync(targetTsDir)
if (shouldBuildFixture) {
await rm(targetTsDir, { force: true, recursive: true })
await mkdir(targetTsDir)
const sourceTsDir = path.resolve(_require.resolve('typescript'), '../..')
copyDirRecursively(sourceTsDir, targetTsDir)
fs.writeFileSync(vueTscFlagFile, proxyPath)
await copy(sourceTsDir, targetTsDir)
await writeFile(vueTscFlagFile, proxyPath)

// 2. sync modification of lib/tsc.js with vue-tsc
const tscJs = _require.resolve(path.resolve(targetTsDir, 'lib/tsc.js'))
modifyFileText(tscJs, textToReplace)
await modifyFileText(tscJs, textToReplace)
}

return { targetTsDir: targetTsDir }
return { targetTsDir }
}

function modifyFileText(
async function modifyFileText(
filePath: string,
textToReplace: { target: string; replacement: string }[]
) {
const text = fs.readFileSync(filePath, 'utf8')
const text = await readFile(filePath, 'utf8')
let newText = text
for (const { target, replacement } of textToReplace) {
newText = newText.replace(target, replacement)
}
fs.writeFileSync(filePath, newText)
}

function copyDirRecursively(src: string, dest: string) {
const files = fs.readdirSync(src, { withFileTypes: true })
for (const file of files) {
const srcPath = path.join(src, file.name)
const destPath = path.join(dest, file.name)
if (file.isDirectory()) {
fs.mkdirSync(destPath, { recursive: true })
copyDirRecursively(srcPath, destPath)
} else {
fs.copyFileSync(srcPath, destPath)
}
}
}

/**
* https://stackoverflow.com/a/42505874
*/
function rimraf(dir_path: string) {
if (fs.existsSync(dir_path)) {
fs.readdirSync(dir_path).forEach((entry) => {
const entry_path = path.join(dir_path, entry)
if (fs.lstatSync(entry_path).isDirectory()) {
rimraf(entry_path)
} else {
fs.unlinkSync(entry_path)
}
})
fs.rmdirSync(dir_path)
}
await writeFile(filePath, newText)
}
43 changes: 27 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c133cb2

Please sign in to comment.