From 461d37b9472ab0f12f9da5bf9aa6f98ece1f1962 Mon Sep 17 00:00:00 2001 From: Bjorn Lu Date: Mon, 29 Jul 2024 19:20:03 +0800 Subject: [PATCH] chore: remove fs-extra dev dependency (#17782) --- package.json | 2 -- packages/create-vite/__tests__/cli.spec.ts | 20 +++++++++---------- playground/resolve-config/__tests__/serve.ts | 15 +++++++++----- playground/vitestGlobalSetup.ts | 13 +++++++----- playground/vitestSetup.ts | 18 ++++++++--------- pnpm-lock.yaml | 21 -------------------- scripts/releaseUtils.ts | 18 ++++++++++------- 7 files changed, 48 insertions(+), 59 deletions(-) diff --git a/package.json b/package.json index 09eae39c5540b1..c94baee6db14e8 100644 --- a/package.json +++ b/package.json @@ -48,7 +48,6 @@ "@types/debug": "^4.1.12", "@types/estree": "^1.0.5", "@types/etag": "^1.8.3", - "@types/fs-extra": "^11.0.4", "@types/less": "^3.0.6", "@types/micromatch": "^4.0.9", "@types/node": "^20.14.11", @@ -62,7 +61,6 @@ "eslint-plugin-n": "^17.9.0", "eslint-plugin-regexp": "^2.6.0", "execa": "^9.3.0", - "fs-extra": "^11.2.0", "globals": "^15.8.0", "lint-staged": "^15.2.7", "npm-run-all2": "^6.2.2", diff --git a/packages/create-vite/__tests__/cli.spec.ts b/packages/create-vite/__tests__/cli.spec.ts index 8f03bf7d2df0c0..b23f881a34b123 100644 --- a/packages/create-vite/__tests__/cli.spec.ts +++ b/packages/create-vite/__tests__/cli.spec.ts @@ -1,13 +1,13 @@ -import { join } from 'node:path' +import fs from 'node:fs' +import path from 'node:path' import type { SyncOptions, SyncResult } from 'execa' import { execaCommandSync } from 'execa' -import fs from 'fs-extra' import { afterEach, beforeAll, expect, test } from 'vitest' -const CLI_PATH = join(__dirname, '..') +const CLI_PATH = path.join(__dirname, '..') const projectName = 'test-app' -const genPath = join(__dirname, projectName) +const genPath = path.join(__dirname, projectName) const run = ( args: string[], @@ -19,22 +19,22 @@ const run = ( // Helper to create a non-empty directory const createNonEmptyDir = () => { // Create the temporary directory - fs.mkdirpSync(genPath) + fs.mkdirSync(genPath, { recursive: true }) // Create a package.json file - const pkgJson = join(genPath, 'package.json') + const pkgJson = path.join(genPath, 'package.json') fs.writeFileSync(pkgJson, '{ "foo": "bar" }') } // Vue 3 starter template const templateFiles = fs - .readdirSync(join(CLI_PATH, 'template-vue')) + .readdirSync(path.join(CLI_PATH, 'template-vue')) // _gitignore is renamed to .gitignore .map((filePath) => (filePath === '_gitignore' ? '.gitignore' : filePath)) .sort() -beforeAll(() => fs.remove(genPath)) -afterEach(() => fs.remove(genPath)) +beforeAll(() => fs.rmSync(genPath, { recursive: true, force: true })) +afterEach(() => fs.rmSync(genPath, { recursive: true, force: true })) test('prompts for the project name if none supplied', () => { const { stdout } = run([]) @@ -42,7 +42,7 @@ test('prompts for the project name if none supplied', () => { }) test('prompts for the framework if none supplied when target dir is current directory', () => { - fs.mkdirpSync(genPath) + fs.mkdirSync(genPath, { recursive: true }) const { stdout } = run(['.'], { cwd: genPath }) expect(stdout).toContain('Select a framework:') }) diff --git a/playground/resolve-config/__tests__/serve.ts b/playground/resolve-config/__tests__/serve.ts index 76172577f690c3..93b1f10e683e8e 100644 --- a/playground/resolve-config/__tests__/serve.ts +++ b/playground/resolve-config/__tests__/serve.ts @@ -1,8 +1,8 @@ // this is automatically detected by playground/vitestSetup.ts and will replace // the default e2e test serve behavior +import fs from 'node:fs/promises' import path from 'node:path' -import fs from 'fs-extra' import { isBuild, rootDir } from '~utils' const configNames = ['js', 'cjs', 'mjs', 'ts', 'mts', 'cts'] @@ -17,7 +17,9 @@ export async function serve() { for (const configName of configNames) { const pathToConf = fromTestDir(configName, `vite.config.${configName}`) - await fs.copy(fromTestDir('root'), fromTestDir(configName)) + await fs.cp(fromTestDir('root'), fromTestDir(configName), { + recursive: true, + }) await fs.rename(fromTestDir(configName, 'vite.config.ts'), pathToConf) if (['cjs', 'cts'].includes(configName)) { @@ -35,9 +37,12 @@ export async function serve() { } // copy directory and add package.json with "type": "module" - await fs.copy(fromTestDir(configName), fromTestDir(`${configName}-module`)) - await fs.writeJSON(fromTestDir(`${configName}-module`, 'package.json'), { - type: 'module', + await fs.cp(fromTestDir(configName), fromTestDir(`${configName}-module`), { + recursive: true, }) + await fs.writeFile( + fromTestDir(`${configName}-module`, 'package.json'), + '{ "type": "module" }', + ) } } diff --git a/playground/vitestGlobalSetup.ts b/playground/vitestGlobalSetup.ts index 3a8288ab34ba57..b1bc77b32fcaf2 100644 --- a/playground/vitestGlobalSetup.ts +++ b/playground/vitestGlobalSetup.ts @@ -1,5 +1,5 @@ +import fs from 'node:fs/promises' import path from 'node:path' -import fs from 'fs-extra' import type { GlobalSetupContext } from 'vitest/node' import type { BrowserServer } from 'playwright-chromium' import { chromium } from 'playwright-chromium' @@ -21,10 +21,11 @@ export async function setup({ provide }: GlobalSetupContext): Promise { provide('wsEndpoint', browserServer.wsEndpoint()) const tempDir = path.resolve(__dirname, '../playground-temp') - await fs.ensureDir(tempDir) - await fs.emptyDir(tempDir) + await fs.rm(tempDir, { recursive: true, force: true }) + await fs.mkdir(tempDir, { recursive: true }) await fs - .copy(path.resolve(__dirname, '../playground'), tempDir, { + .cp(path.resolve(__dirname, '../playground'), tempDir, { + recursive: true, dereference: false, filter(file) { file = file.replace(/\\/g, '/') @@ -45,6 +46,8 @@ export async function setup({ provide }: GlobalSetupContext): Promise { export async function teardown(): Promise { await browserServer?.close() if (!process.env.VITE_PRESERVE_BUILD_ARTIFACTS) { - fs.removeSync(path.resolve(__dirname, '../playground-temp')) + await fs.rm(path.resolve(__dirname, '../playground-temp'), { + recursive: true, + }) } } diff --git a/playground/vitestSetup.ts b/playground/vitestSetup.ts index ff2303dc498569..62ee5a3ee9405f 100644 --- a/playground/vitestSetup.ts +++ b/playground/vitestSetup.ts @@ -1,6 +1,6 @@ import type * as http from 'node:http' -import path, { dirname, resolve } from 'node:path' -import fs from 'fs-extra' +import fs from 'node:fs' +import path from 'node:path' import { chromium } from 'playwright-chromium' import type { ConfigEnv, @@ -25,7 +25,7 @@ import { beforeAll, inject } from 'vitest' // #region env -export const workspaceRoot = resolve(__dirname, '../') +export const workspaceRoot = path.resolve(__dirname, '../') export const isBuild = !!process.env.VITE_TEST_BUILD export const isServe = !isBuild @@ -125,20 +125,20 @@ beforeAll(async (s) => { testPath = suite.filepath! testName = slash(testPath).match(/playground\/([\w-]+)\//)?.[1] - testDir = dirname(testPath) + testDir = path.dirname(testPath) // if this is a test placed under playground/xxx/__tests__ // start a vite server in that directory. if (testName) { - testDir = resolve(workspaceRoot, 'playground-temp', testName) + testDir = path.resolve(workspaceRoot, 'playground-temp', testName) // when `root` dir is present, use it as vite's root - const testCustomRoot = resolve(testDir, 'root') + const testCustomRoot = path.resolve(testDir, 'root') rootDir = fs.existsSync(testCustomRoot) ? testCustomRoot : testDir const testCustomServe = [ - resolve(dirname(testPath), 'serve.ts'), - resolve(dirname(testPath), 'serve.js'), + path.resolve(path.dirname(testPath), 'serve.ts'), + path.resolve(path.dirname(testPath), 'serve.js'), ].find((i) => fs.existsSync(i)) if (testCustomServe) { @@ -182,7 +182,7 @@ async function loadConfig(configEnv: ConfigEnv) { let config: UserConfig | null = null // config file named by convention as the *.spec.ts folder - const variantName = path.basename(dirname(testPath)) + const variantName = path.basename(path.dirname(testPath)) if (variantName !== '__tests__') { const configVariantPath = path.resolve( rootDir, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 62131352ec51e9..8bef347be89f78 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -49,9 +49,6 @@ importers: '@types/etag': specifier: ^1.8.3 version: 1.8.3 - '@types/fs-extra': - specifier: ^11.0.4 - version: 11.0.4 '@types/less': specifier: ^3.0.6 version: 3.0.6 @@ -91,9 +88,6 @@ importers: execa: specifier: ^9.3.0 version: 9.3.0 - fs-extra: - specifier: ^11.2.0 - version: 11.2.0 globals: specifier: ^15.8.0 version: 15.8.0 @@ -3039,18 +3033,12 @@ packages: '@types/express@4.17.21': resolution: {integrity: sha512-ejlPM315qwLpaQlQDTjPdsUFSc6ZsP4AN6AlWnogPjQ7CVi7PYF3YVz+CY3jE2pwYf7E/7HlDAN0rV2GxTG0HQ==} - '@types/fs-extra@11.0.4': - resolution: {integrity: sha512-yTbItCNreRooED33qjunPthRcSjERP1r4MqCZc7wv0u2sUkzTFp45tgUfS5+r7FrZPdmCCNflLhVSP/o+SemsQ==} - '@types/hast@3.0.4': resolution: {integrity: sha512-WPs+bbQw5aCj+x6laNGWLH3wviHtoCv/P3+otBhbOhJgG8qtpdAMlTCxLtsTWA7LH1Oh/bFCHsBn0TPS5m30EQ==} '@types/http-errors@2.0.4': resolution: {integrity: sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==} - '@types/jsonfile@6.1.4': - resolution: {integrity: sha512-D5qGUYwjvnNNextdU59/+fI+spnwtTFmyQP0h+PfIOSkNfpU6AOICUOkm4i0OnSk+NyjdPJrxCDro0sJsWlRpQ==} - '@types/less@3.0.6': resolution: {integrity: sha512-PecSzorDGdabF57OBeQO/xFbAkYWo88g4Xvnsx7LRwqLC17I7OoKtA3bQB9uXkY6UkMWCOsA8HSVpaoitscdXw==} @@ -8171,21 +8159,12 @@ snapshots: '@types/qs': 6.9.12 '@types/serve-static': 1.15.5 - '@types/fs-extra@11.0.4': - dependencies: - '@types/jsonfile': 6.1.4 - '@types/node': 20.14.11 - '@types/hast@3.0.4': dependencies: '@types/unist': 3.0.2 '@types/http-errors@2.0.4': {} - '@types/jsonfile@6.1.4': - dependencies: - '@types/node': 20.14.11 - '@types/less@3.0.6': {} '@types/linkify-it@5.0.0': {} diff --git a/scripts/releaseUtils.ts b/scripts/releaseUtils.ts index fbbe7c5b780f4f..586bbb5713a270 100644 --- a/scripts/releaseUtils.ts +++ b/scripts/releaseUtils.ts @@ -1,9 +1,8 @@ -import { readdirSync, writeFileSync } from 'node:fs' +import fs from 'node:fs/promises' import path from 'node:path' import colors from 'picocolors' import type { Options as ExecaOptions, ResultPromise } from 'execa' import { execa } from 'execa' -import fs from 'fs-extra' export function run( bin: string, @@ -14,7 +13,9 @@ export function run( } export async function getLatestTag(pkgName: string): Promise { - const pkgJson = await fs.readJson(`packages/${pkgName}/package.json`) + const pkgJson = JSON.parse( + await fs.readFile(`packages/${pkgName}/package.json`, 'utf-8'), + ) const version = pkgJson.version return pkgName === 'vite' ? `v${version}` : `${pkgName}@${version}` } @@ -48,17 +49,20 @@ export async function logRecentCommits(pkgName: string): Promise { } export async function updateTemplateVersions(): Promise { - const viteVersion = fs.readJSONSync('packages/vite/package.json').version + const vitePkgJson = JSON.parse( + await fs.readFile('packages/vite/package.json', 'utf-8'), + ) + const viteVersion = vitePkgJson.version if (/beta|alpha|rc/.test(viteVersion)) return const dir = 'packages/create-vite' - const templates = readdirSync(dir).filter((dir) => + const templates = (await fs.readdir(dir)).filter((dir) => dir.startsWith('template-'), ) for (const template of templates) { const pkgPath = path.join(dir, template, `package.json`) - const pkg = fs.readJSONSync(pkgPath) + const pkg = JSON.parse(await fs.readFile(pkgPath, 'utf-8')) pkg.devDependencies.vite = `^` + viteVersion - writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n') + await fs.writeFile(pkgPath, JSON.stringify(pkg, null, 2) + '\n') } }