refactor: migrate scripts to typescript and organize imports

triage
Divyansh Singh 3 weeks ago
parent e6e90ed5a9
commit 1ade7bd422

@ -1,4 +1,4 @@
import fs from 'node:fs/promises'
import { writeFile, unlink } from 'node:fs/promises'
import { fileURLToPath } from 'node:url'
describe('static data file support in vite 3', () => {
@ -48,20 +48,20 @@ describe('static data file support in vite 3', () => {
const b = fileURLToPath(new URL('./data/b.json', import.meta.url))
try {
await fs.writeFile(a, JSON.stringify({ a: false }, null, 2) + '\n')
await writeFile(a, JSON.stringify({ a: false }, null, 2) + '\n')
await page.waitForFunction(
() =>
document.querySelector('pre#basic')?.textContent ===
JSON.stringify([{ a: false }, { b: true }], null, 2)
)
} finally {
await fs.writeFile(a, JSON.stringify({ a: true }, null, 2) + '\n')
await writeFile(a, JSON.stringify({ a: true }, null, 2) + '\n')
}
let err = true
try {
await fs.unlink(b)
await unlink(b)
await page.waitForFunction(
() =>
document.querySelector('pre#basic')?.textContent ===
@ -70,19 +70,19 @@ describe('static data file support in vite 3', () => {
err = false
} finally {
if (err) {
await fs.writeFile(b, JSON.stringify({ b: true }, null, 2) + '\n')
await writeFile(b, JSON.stringify({ b: true }, null, 2) + '\n')
}
}
try {
await fs.writeFile(b, JSON.stringify({ b: false }, null, 2) + '\n')
await writeFile(b, JSON.stringify({ b: false }, null, 2) + '\n')
await page.waitForFunction(
() =>
document.querySelector('pre#basic')?.textContent ===
JSON.stringify([{ a: true }, { b: false }], null, 2)
)
} finally {
await fs.writeFile(b, JSON.stringify({ b: true }, null, 2) + '\n')
await writeFile(b, JSON.stringify({ b: true }, null, 2) + '\n')
}
})

@ -1,6 +1,6 @@
import fs from 'node:fs'
import getPort from 'get-port'
import { nanoid } from 'nanoid'
import { rm } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath, URL } from 'node:url'
import { chromium } from 'playwright-chromium'
@ -33,12 +33,12 @@ const variations = themes.flatMap((theme) =>
afterAll(async () => {
await page.close()
await browser.close()
await fs.promises.rm(tempDir, { recursive: true, force: true })
await rm(tempDir, { recursive: true, force: true })
})
test.each(variations)('init %s', async (_, { theme, useTs }) => {
const root = getTempRoot()
await fs.promises.rm(root, { recursive: true, force: true })
await rm(root, { recursive: true, force: true })
scaffold({ root, theme, useTs, injectNpmScripts: false })
const port = await getPort()

@ -1,7 +1,7 @@
import { ref } from 'vue'
import { resolveLocaleLink } from 'client/theme-default/composables/langs'
import type { VitePressData } from 'vitepress'
import type { DefaultTheme } from 'vitepress/theme'
import { resolveLocaleLink } from 'client/theme-default/composables/langs'
import { ref } from 'vue'
function createData(
themeConfig: DefaultTheme.Config,

@ -1,5 +1,5 @@
import { mergeConfig } from 'node/config'
import type { MarkdownItAsync } from 'markdown-it-async'
import { mergeConfig } from 'node/config'
describe('node/config', () => {
test('merges markdown config hooks from extended configs', async () => {

@ -1,8 +1,8 @@
import { resolveConfig } from 'node/config'
import { createMarkdownToVueRenderFn } from 'node/markdownToVue'
import { mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { resolveConfig } from 'node/config'
import { createMarkdownToVueRenderFn } from 'node/markdownToVue'
describe('node/markdownToVue', () => {
let root: string | undefined

@ -1,9 +1,9 @@
import MiniSearch from 'minisearch'
import { resolveConfig } from 'node/config'
import { localSearchPlugin } from 'node/plugins/localSearchPlugin'
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import path from 'node:path'
import { resolveConfig } from 'node/config'
import { localSearchPlugin } from 'node/plugins/localSearchPlugin'
import MiniSearch from 'minisearch'
describe('node/plugins/localSearchPlugin', () => {
let root: string | undefined

@ -56,11 +56,11 @@
"dev:start": "pnpm --stream '/^dev:(client|node|watch)$/'",
"dev:client": "tsc --sourcemap -w --preserveWatchOutput -p src/client",
"dev:node": "DEV=true pnpm build:node -w",
"dev:shared": "node scripts/copyShared",
"dev:watch": "node scripts/watchAndCopy",
"dev:shared": "node scripts/copyShared.ts",
"dev:watch": "node scripts/watchAndCopy.ts",
"build": "pnpm build:prepare && pnpm build:client && pnpm build:node",
"build:prepare": "pnpm clean && node scripts/copyShared",
"build:client": "vue-tsc --noEmit -p src/client && tsc -p src/client && node scripts/copyClient",
"build:prepare": "pnpm clean && node scripts/copyShared.ts",
"build:client": "vue-tsc --noEmit -p src/client && tsc -p src/client && node scripts/copyClient.ts",
"build:node": "tsc -p src/node --noEmit && rollup --config rollup.config.ts --configPlugin esbuild",
"test": "pnpm --aggregate-output --reporter=append-only '/^test:(unit|e2e|init)$/'",
"test:unit": "vitest run -r __tests__/unit",
@ -87,7 +87,7 @@
"format:fail": "prettier --experimental-cli --check .",
"check": "pnpm format:fail && pnpm build && pnpm test",
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s",
"release": "node scripts/release.js"
"release": "node scripts/release.ts"
},
"simple-git-hooks": {
"pre-commit": "pnpm lint-staged"
@ -144,6 +144,7 @@
"@types/node": "^25.9.4",
"@types/picomatch": "^4.0.3",
"@types/prompts": "^2.4.9",
"@types/semver": "^7.7.1",
"chokidar": "^5.0.0",
"conventional-changelog": "^7.2.1",
"conventional-changelog-angular": "^8.3.1",

@ -164,6 +164,9 @@ importers:
'@types/prompts':
specifier: ^2.4.9
version: 2.4.9
'@types/semver':
specifier: ^7.7.1
version: 7.7.1
chokidar:
specifier: ^5.0.0
version: 5.0.0
@ -1101,6 +1104,9 @@ packages:
'@types/sax@1.2.7':
resolution: {integrity: sha512-rO73L89PJxeYM3s3pPPjiPgVVcymqU490g0YO5n5By0k2Erzj6tay/4lr1CHAAU4JyOWd1rpQ8bCf6cZfHU96A==}
'@types/semver@7.7.1':
resolution: {integrity: sha512-FmgJfu+MOcQ370SD0ev7EI8TlCAfKYU+B4m5T3yXc1CiRN94g/SZPtsCkk506aUDtlMnFZvasDwHHUcZUEaYuA==}
'@types/unist@3.0.3':
resolution: {integrity: sha512-ko/gIFJRv177XgZsZcBwnqJN5x/Gien8qNOn0D5bQU/zAzVf9Zt3BlcUiLqhV9y4ARk0GbT3tnUiPNgnTXzc/Q==}
@ -3593,6 +3599,8 @@ snapshots:
dependencies:
'@types/node': 25.9.4
'@types/semver@7.7.1': {}
'@types/unist@3.0.3': {}
'@types/web-bluetooth@0.0.21': {}

@ -3,7 +3,7 @@ import commonjs from '@rollup/plugin-commonjs'
import json from '@rollup/plugin-json'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import replace from '@rollup/plugin-replace'
import * as fs from 'node:fs/promises'
import { rm } from 'node:fs/promises'
import { builtinModules, createRequire } from 'node:module'
import { type RollupOptions, defineConfig } from 'rollup'
import dts from 'rollup-plugin-dts'
@ -88,7 +88,7 @@ const clientTypes: RollupOptions = {
name: 'cleanup',
async closeBundle() {
if (PROD) {
await fs.rm('dist/client-types', { recursive: true })
await rm('dist/client-types', { recursive: true })
}
}
}

@ -1,7 +1,7 @@
import { cp } from 'node:fs/promises'
import { globSync } from 'tinyglobby'
function toDest(file) {
function toDest(file: string) {
return file.replace(/^src\//, 'dist/')
}

@ -1,5 +1,6 @@
import { spawn } from 'node:child_process'
import { readFileSync, writeFileSync } from 'node:fs'
import { spawn } from 'cross-spawn'
import type { SpawnOptions } from 'node:child_process'
import fs from 'node:fs'
import { createRequire } from 'node:module'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
@ -12,17 +13,16 @@ const { version: currentVersion } = createRequire(import.meta.url)(
)
const { inc: _inc, valid } = semver
const versionIncrements = ['patch', 'minor', 'major']
const versionIncrements = ['patch', 'minor', 'major'] as const
const tags = ['latest', 'next']
const tags = ['latest', 'next'] as const
const dir = fileURLToPath(new URL('.', import.meta.url))
const inc = (i) => _inc(currentVersion, i)
const run = (bin, args, opts = {}) =>
new Promise((resolve, reject) => {
const inc = (i: semver.ReleaseType) => _inc(currentVersion, i)
const run = (bin: string, args: string[], opts: SpawnOptions = {}) =>
new Promise<void>((resolve, reject) => {
const child = spawn(bin, args, {
stdio: 'inherit',
shell: process.platform === 'win32',
...opts
})
@ -37,10 +37,10 @@ const run = (bin, args, opts = {}) =>
}
})
})
const step = (msg) => console.log(c.cyan(msg))
const step = (msg: string) => console.log(c.cyan(msg))
async function main() {
let targetVersion
let targetVersion: string
const versions = versionIncrements
.map((i) => `${i} (${inc(i)})`)
@ -50,7 +50,7 @@ async function main() {
type: 'select',
name: 'release',
message: 'Select release type',
choices: versions
choices: versions.map((title, value) => ({ title, value }))
})
if (release === 3) {
@ -63,7 +63,7 @@ async function main() {
})
).version
} else {
targetVersion = versions[release].match(/\((.*)\)/)[1]
targetVersion = versions[release].match(/\((.*)\)/)![1]
}
if (!valid(targetVersion)) {
@ -74,7 +74,7 @@ async function main() {
type: 'select',
name: 'tag',
message: 'Select tag type',
choices: tags
choices: tags.map((title, value) => ({ title, value }))
})
const { yes: tagOk } = await prompts({
@ -132,13 +132,13 @@ async function main() {
await run('git', ['push'])
}
function updatePackage(version) {
function updatePackage(version: string) {
const pkgPath = resolve(resolve(dir, '..'), 'package.json')
const pkg = JSON.parse(readFileSync(pkgPath, 'utf-8'))
const pkg = JSON.parse(fs.readFileSync(pkgPath, 'utf-8'))
pkg.version = version
writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n')
}
main().catch((err) => console.error(err))

@ -2,7 +2,7 @@ import { watch } from 'chokidar'
import { cp, rm } from 'node:fs/promises'
import { normalizePath } from 'vite'
function toClientAndNode(method, file) {
function toClientAndNode(method: 'copy' | 'remove', file: string) {
file = normalizePath(file)
if (method === 'copy') {
cp(file, file.replace(/^src\/shared\//, 'src/node/'))
@ -13,13 +13,13 @@ function toClientAndNode(method, file) {
}
}
function toDist(file) {
function toDist(file: string) {
return normalizePath(file).replace(/^src\//, 'dist/')
}
// copy shared files to the client and node directory whenever they change.
watch('src/shared', {
ignored: (path, stats) => stats?.isFile() && !path.endsWith('.ts')
ignored: (path, stats) => !!stats?.isFile() && !path.endsWith('.ts')
})
.on('change', (file) => toClientAndNode('copy', file))
.on('add', (file) => toClientAndNode('copy', file))
@ -29,7 +29,8 @@ watch('src/shared', {
// they change.
watch('src/client', {
ignored: (path, stats) =>
stats?.isFile() && (path.endsWith('.ts') || path.endsWith('tsconfig.json'))
!!stats?.isFile() &&
(path.endsWith('.ts') || path.endsWith('tsconfig.json'))
})
.on('change', (file) => cp(file, toDist(file)))
.on('add', (file) => cp(file, toDist(file)))

@ -1,4 +1,5 @@
import fs from 'node:fs'
import { cp } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import {
@ -202,7 +203,7 @@ export async function bundle(
if (!chunk.fileName.endsWith('.js')) {
const tempPath = path.resolve(config.tempDir, chunk.fileName)
const outPath = path.resolve(config.outDir, chunk.fileName)
await fs.promises.cp(tempPath, outPath)
await cp(tempPath, outPath)
}
})
)
@ -211,7 +212,7 @@ export async function bundle(
if (fs.existsSync(publicDir)) {
// dereference symlinks like vite's own publicDir copy does, and so that
// copying over an existing symlinked file does not fail with EEXIST
await fs.promises.cp(publicDir, config.outDir, {
await cp(publicDir, config.outDir, {
recursive: true,
dereference: true
})

@ -1,5 +1,6 @@
import { isBooleanAttr } from '@vue/shared'
import fs from 'node:fs'
import { mkdir, writeFile } from 'node:fs/promises'
import path from 'node:path'
import { minify, normalizePath, type Rolldown } from 'vite'
import { version } from '../../../package.json'
@ -191,7 +192,7 @@ export async function renderPage(
</html>`
const htmlFileName = path.join(config.outDir, page.replace(/\.md$/, '.html'))
await fs.promises.mkdir(path.dirname(htmlFileName), { recursive: true })
await mkdir(path.dirname(htmlFileName), { recursive: true })
const transformedHtml = await config.transformHtml?.(html, htmlFileName, {
page,
siteConfig: config,
@ -203,7 +204,7 @@ export async function renderPage(
content,
assets
})
await fs.promises.writeFile(htmlFileName, transformedHtml || html)
await writeFile(htmlFileName, transformedHtml || html)
}
function resolvePageImports(

@ -1,5 +1,6 @@
import MiniSearch from 'minisearch'
import fs from 'node:fs'
import { readFile } from 'node:fs/promises'
import path from 'node:path'
import { createDebug } from 'obug'
import type { Plugin, ViteDevServer } from 'vite'
@ -54,7 +55,7 @@ export async function localSearchPlugin(
const { srcDir, cleanUrls = false } = siteConfig
const relativePath = slash(path.relative(srcDir, file))
const env: MarkdownEnv = { path: file, relativePath, cleanUrls }
const md_raw = await fs.promises.readFile(file, 'utf-8')
const md_raw = await readFile(file, 'utf-8')
const md_src = processIncludes(md, srcDir, md_raw, file, [], cleanUrls)
if (options._render) {
return await options._render(md_src, env, md)

@ -17,7 +17,6 @@
"**/dist/**",
"template",
"bin",
"docs/snippets",
"scripts"
"docs/snippets"
]
}

Loading…
Cancel
Save