From cc1810341931c28e0f733e28304147f3de61ab72 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Tue, 20 Dec 2022 20:31:53 +0800 Subject: [PATCH] refactor: remove fs-extra (#2695) --- packages/cli/package.json | 1 - packages/cli/src/commands/connector/remove.ts | 5 +++-- packages/cli/src/commands/connector/utils.ts | 13 ++++++------- .../cli/src/commands/database/alteration/utils.ts | 10 +++++----- packages/cli/src/commands/install/utils.ts | 11 ++++------- packages/core/package.json | 1 - pnpm-lock.yaml | 9 +++++---- 7 files changed, 23 insertions(+), 27 deletions(-) diff --git a/packages/cli/package.json b/packages/cli/package.json index 263b658c8..25a4ef5af 100644 --- a/packages/cli/package.json +++ b/packages/cli/package.json @@ -50,7 +50,6 @@ "chalk": "^5.0.0", "decamelize": "^6.0.0", "dotenv": "^16.0.0", - "fs-extra": "^10.1.0", "got": "^12.5.3", "hpagent": "^1.2.0", "inquirer": "^8.2.2", diff --git a/packages/cli/src/commands/connector/remove.ts b/packages/cli/src/commands/connector/remove.ts index fa54265eb..837773fbe 100644 --- a/packages/cli/src/commands/connector/remove.ts +++ b/packages/cli/src/commands/connector/remove.ts @@ -1,5 +1,6 @@ +import fs from 'fs/promises'; + import chalk from 'chalk'; -import fsExtra from 'fs-extra'; import type { CommandModule } from 'yargs'; import { log } from '../../utilities.js'; @@ -39,7 +40,7 @@ const remove: CommandModule<{ path?: string }, { path?: string; packages?: strin const packageInfo = existingPackages.find(({ name }) => name === current); try { - await fsExtra.remove(packageInfo?.path ?? ''); + await fs.rm(packageInfo?.path ?? '', { force: true, recursive: true }); return okSymbol; } catch (error: unknown) { diff --git a/packages/cli/src/commands/connector/utils.ts b/packages/cli/src/commands/connector/utils.ts index 249ec7706..7f88cb4a2 100644 --- a/packages/cli/src/commands/connector/utils.ts +++ b/packages/cli/src/commands/connector/utils.ts @@ -1,12 +1,11 @@ import { exec } from 'child_process'; import { existsSync } from 'fs'; -import { readFile, mkdir, unlink } from 'fs/promises'; +import fs from 'fs/promises'; import path from 'path'; import { promisify } from 'util'; import { assert, conditionalString } from '@silverhand/essentials'; import chalk from 'chalk'; -import fsExtra from 'fs-extra'; import inquirer from 'inquirer'; import pRetry from 'p-retry'; import tar from 'tar'; @@ -36,7 +35,7 @@ const validatePath = async (value: string) => { return buildPathErrorMessage(value); } - const packageJson = await readFile(corePackageJsonPath, { encoding: 'utf8' }); + const packageJson = await fs.readFile(corePackageJsonPath, { encoding: 'utf8' }); const packageName = await z .object({ name: z.string() }) .parseAsync(JSON.parse(packageJson)) @@ -112,7 +111,7 @@ export const addConnectors = async (instancePath: string, packageNames: string[] const cwd = getConnectorDirectory(instancePath); if (!existsSync(cwd)) { - await mkdir(cwd); + await fs.mkdir(cwd, { recursive: true }); } log.info('Fetch connector metadata'); @@ -136,10 +135,10 @@ export const addConnectors = async (instancePath: string, packageNames: string[] const tarPath = path.join(cwd, escapedFilename); const packageDirectory = path.join(cwd, name.replace(/\//g, '-')); - await fsExtra.remove(packageDirectory); - await fsExtra.ensureDir(packageDirectory); + await fs.rm(packageDirectory, { force: true, recursive: true }); + await fs.mkdir(packageDirectory, { recursive: true }); await tar.extract({ cwd: packageDirectory, file: tarPath, strip: 1 }); - await unlink(tarPath); + await fs.unlink(tarPath); log.succeed(`Added ${chalk.green(name)}`); }; diff --git a/packages/cli/src/commands/database/alteration/utils.ts b/packages/cli/src/commands/database/alteration/utils.ts index 6037b4fc2..38f04337c 100644 --- a/packages/cli/src/commands/database/alteration/utils.ts +++ b/packages/cli/src/commands/database/alteration/utils.ts @@ -1,15 +1,15 @@ +import { existsSync } from 'fs'; +import fs from 'fs/promises'; import { fileURLToPath } from 'node:url'; import path from 'path'; import { findPackage } from '@logto/shared'; -import fsExtra from 'fs-extra'; import { getPathInModule } from '../../../utilities.js'; import { metaUrl } from './meta-url.js'; import type { AlterationFile } from './type.js'; const currentDirname = path.dirname(fileURLToPath(metaUrl)); -const { copy, existsSync, remove, readdir } = fsExtra; const alterationFilenameRegex = /-(\d+)-?.*\.js$/; export const getTimestampFromFilename = (filename: string) => { @@ -42,10 +42,10 @@ export const getAlterationFiles = async (): Promise => { } // We need to copy alteration files to execute in the CLI context to make `slonik` available - await remove(localAlterationDirectory); - await copy(alterationDirectory, localAlterationDirectory); + await fs.rm(localAlterationDirectory, { force: true, recursive: true }); + await fs.cp(alterationDirectory, localAlterationDirectory, { recursive: true }); - const directory = await readdir(localAlterationDirectory); + const directory = await fs.readdir(localAlterationDirectory); const files = directory.filter((file) => alterationFilenameRegex.test(file)); return files diff --git a/packages/cli/src/commands/install/utils.ts b/packages/cli/src/commands/install/utils.ts index 4dfdcab1b..71bf30783 100644 --- a/packages/cli/src/commands/install/utils.ts +++ b/packages/cli/src/commands/install/utils.ts @@ -1,12 +1,11 @@ import { execSync } from 'child_process'; import { existsSync } from 'fs'; -import { mkdir } from 'fs/promises'; +import fs from 'fs/promises'; import os from 'os'; import path from 'path'; import { assert } from '@silverhand/essentials'; import chalk from 'chalk'; -import fsExtra from 'fs-extra'; import inquirer from 'inquirer'; import * as semver from 'semver'; import tar from 'tar'; @@ -115,7 +114,7 @@ export const downloadRelease = async (url?: string) => { export const decompress = async (toPath: string, tarPath: string) => { const run = async () => { try { - await mkdir(toPath); + await fs.mkdir(toPath); await tar.extract({ file: tarPath, cwd: toPath, strip: 1 }); } catch (error: unknown) { log.error(error); @@ -140,7 +139,7 @@ export const seedDatabase = async (instancePath: string) => { } catch (error: unknown) { console.error(error); - await oraPromise(fsExtra.remove(instancePath), { + await oraPromise(fs.rm(instancePath, { force: true, recursive: true }), { text: 'Clean up', prefixText: chalk.blue('[info]'), }); @@ -156,9 +155,7 @@ export const seedDatabase = async (instancePath: string) => { export const createEnv = async (instancePath: string, databaseUrl: string) => { const dotEnvPath = path.resolve(instancePath, '.env'); - await fsExtra.writeFile(dotEnvPath, `DB_URL=${databaseUrl}`, { - encoding: 'utf8', - }); + await fs.writeFile(dotEnvPath, `DB_URL=${databaseUrl}`, 'utf8'); log.info(`Saved database URL to ${chalk.blue(dotEnvPath)}`); }; diff --git a/packages/core/package.json b/packages/core/package.json index f4dd523e6..8047998aa 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -42,7 +42,6 @@ "dotenv": "^16.0.0", "etag": "^1.8.1", "find-up": "^6.3.0", - "fs-extra": "^10.1.0", "hash-wasm": "^4.9.0", "i18next": "^21.8.16", "iconv-lite": "0.6.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b9ddc5d6e..3604a32b1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -41,7 +41,6 @@ importers: decamelize: ^6.0.0 dotenv: ^16.0.0 eslint: ^8.21.0 - fs-extra: ^10.1.0 got: ^12.5.3 hpagent: ^1.2.0 inquirer: ^8.2.2 @@ -70,7 +69,6 @@ importers: chalk: 5.1.2 decamelize: 6.0.0 dotenv: 16.0.0 - fs-extra: 10.1.0 got: 12.5.3 hpagent: 1.2.0 inquirer: 8.2.2 @@ -289,7 +287,6 @@ importers: eslint: ^8.21.0 etag: ^1.8.1 find-up: ^6.3.0 - fs-extra: ^10.1.0 hash-wasm: ^4.9.0 http-errors: ^1.6.3 i18next: ^21.8.16 @@ -343,7 +340,6 @@ importers: dotenv: 16.0.0 etag: 1.8.1 find-up: 6.3.0 - fs-extra: 10.1.0 hash-wasm: 4.9.0 i18next: 21.8.16 iconv-lite: 0.6.3 @@ -7408,6 +7404,7 @@ packages: graceful-fs: 4.2.9 jsonfile: 6.1.0 universalify: 2.0.0 + dev: true /fs-extra/7.0.1: resolution: {integrity: sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==} @@ -7730,9 +7727,11 @@ packages: /graceful-fs/4.2.10: resolution: {integrity: sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==} + dev: true /graceful-fs/4.2.9: resolution: {integrity: sha512-NtNxqUcXgpW2iMrfqSfR73Glt39K+BLwWsPs94yR63v45T0Wbej7eRmL5cWfwEgqXnmjQp3zaJTshdRW/qC2ZQ==} + dev: true /grapheme-splitter/1.0.4: resolution: {integrity: sha512-bzh50DW9kTPM00T8y4o8vQg89Di9oLJVLW/KaOGIXJWP/iqCN6WKYkbNOF04vFLJhwcpYUh9ydh/+5vpOqV4YQ==} @@ -9973,6 +9972,7 @@ packages: universalify: 2.0.0 optionalDependencies: graceful-fs: 4.2.10 + dev: true /jsonparse/1.3.1: resolution: {integrity: sha512-POQXvpdL69+CluYsillJ7SUhKvytYjW9vG/GKpnf+xP8UWgYEM/RaMzHHofbALDiKbbP1W8UEYmgGl39WkPZsg==} @@ -14765,6 +14765,7 @@ packages: /universalify/2.0.0: resolution: {integrity: sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==} engines: {node: '>= 10.0.0'} + dev: true /unpipe/1.0.0: resolution: {integrity: sha512-pjy2bYhSsufwWlKwPc+l3cN7+wuJlK6uz0YdJEOlQDbl6jo/YlPi4mb8agUkVC8BF7V8NuzeyPNqRksA3hztKQ==}