From 210b30413040c8f58f5ae501f6038c8aba85cd22 Mon Sep 17 00:00:00 2001 From: trickypr Date: Fri, 3 Dec 2021 16:37:18 +1100 Subject: [PATCH] =?UTF-8?q?=E2=9E=96=20Remove=20fs-extra?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 2 - src/commands/download.ts | 21 +++++---- src/commands/export-file.ts | 15 +++---- src/commands/fix-le.ts | 4 +- src/commands/license-check.ts | 2 +- src/commands/reset.ts | 2 +- src/commands/set-branch.ts | 4 +- src/controllers/brandingPatch.ts | 8 +++- src/controllers/patch.ts | 15 +++---- src/middleware/sha-check.ts | 2 +- src/utils/error-handler.ts | 2 +- src/utils/fs.ts | 77 +++++++++++++++++++++++++++++++- src/utils/import.ts | 17 ++++--- src/utils/write-metadata.ts | 2 +- yarn.lock | 38 +--------------- 15 files changed, 123 insertions(+), 88 deletions(-) diff --git a/package.json b/package.json index c477d99..24f9b7d 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,6 @@ "cli-progress": "^3.9.1", "commander": "^6.2.1", "execa": "^5.1.1", - "fs-extra": "^9.0.1", "glob": "^7.1.7", "linus": "^0.0.6", "listr": "^0.14.3", @@ -51,7 +50,6 @@ }, "devDependencies": { "@types/cli-progress": "^3.9.2", - "@types/fs-extra": "^9.0.6", "@types/listr": "^0.14.4", "@types/node": "^14.14.16", "@types/prompts": "^2.0.14", diff --git a/src/commands/download.ts b/src/commands/download.ts index 65c3484..4d781f6 100644 --- a/src/commands/download.ts +++ b/src/commands/download.ts @@ -3,18 +3,23 @@ import { mkdirSync, readFileSync, rmdirSync, + unlinkSync, writeFileSync, } from 'fs' import { homedir } from 'os' import { basename, join, posix, resolve, sep } from 'path' import execa from 'execa' -import { ensureDirSync, removeSync } from 'fs-extra' import Listr from 'listr' import { bin_name, config, log } from '..' import { ENGINE_DIR, MELON_TMP_DIR } from '../constants' -import { getConfig, walkDirectoryTree, writeMetadata } from '../utils' +import { + ensureDir, + getConfig, + walkDirectoryTree, + writeMetadata, +} from '../utils' import { downloadFileToLocation } from '../utils/download' import { downloadArtifacts } from './download-artifacts' import { discard } from '.' @@ -116,7 +121,7 @@ export const download = async (): Promise => { } if (ctx.firefoxSourceTar) { - removeSync(resolve(cwd, '.dotbuild', 'engines', ctx.firefoxSourceTar)) + unlinkSync(resolve(cwd, '.dotbuild', 'engines', ctx.firefoxSourceTar)) } }, }, @@ -262,10 +267,8 @@ const unpackFirefoxSource = ( task.output = `Unpacking Firefox...` - try { - rmdirSync(ENGINE_DIR) - } catch (e) {} - ensureDirSync(ENGINE_DIR) + if (existsSync(ENGINE_DIR)) rmdirSync(ENGINE_DIR) + mkdirSync(ENGINE_DIR) const tarProc = execa('tar', [ '--transform', @@ -292,7 +295,7 @@ async function downloadAddon( ): Promise { const outFileName = path.replace(/\//g, '-') + basename(url) - ensureDirSync(resolve(process.cwd(), `.dotbuild`, `engines`)) + await ensureDir(resolve(process.cwd(), `.dotbuild`, `engines`)) task.output = `Downloading ${url}` await downloadFileToLocation( @@ -314,7 +317,7 @@ async function downloadFirefoxSource( task.output = `Locating Firefox release ${version}...` - ensureDirSync(resolve(process.cwd(), `.dotbuild`, `engines`)) + await ensureDir(resolve(process.cwd(), `.dotbuild`, `engines`)) if ( existsSync( diff --git a/src/commands/export-file.ts b/src/commands/export-file.ts index 53fd7c6..78ed346 100644 --- a/src/commands/export-file.ts +++ b/src/commands/export-file.ts @@ -1,12 +1,11 @@ import execa from 'execa' import { existsSync, writeFileSync } from 'fs' -import { ensureDirSync } from 'fs-extra' import { resolve } from 'path' import { log } from '..' import { ENGINE_DIR, SRC_DIR } from '../constants' -import { delay } from '../utils' +import { delay, ensureDir } from '../utils' -export const exportFile = async (file: string) => { +export const exportFile = async (file: string): Promise => { log.info(`Exporting ${file}...`) if (!existsSync(resolve(ENGINE_DIR, file))) @@ -28,15 +27,13 @@ export const exportFile = async (file: string) => { stripFinalNewline: false, } ) - const name = - `${file - .split('/') - [file.replace(/\./g, '-').split('/').length - 1].replace(/\./g, '-') - }.patch` + const name = `${file + .split('/') + [file.replace(/\./g, '-').split('/').length - 1].replace(/\./g, '-')}.patch` const patchPath = file.replace(/\./g, '-').split('/').slice(0, -1) - ensureDirSync(resolve(SRC_DIR, ...patchPath)) + await ensureDir(resolve(SRC_DIR, ...patchPath)) if (proc.stdout.length >= 8000) { log.warning('') diff --git a/src/commands/fix-le.ts b/src/commands/fix-le.ts index 39797e3..c93e59e 100644 --- a/src/commands/fix-le.ts +++ b/src/commands/fix-le.ts @@ -1,10 +1,10 @@ -import { existsSync, readdirSync, readFileSync } from 'fs-extra' +import { existsSync, readdirSync, readFileSync } from 'fs' import { resolve } from 'path' import { log } from '..' import { ENGINE_DIR, PATCHES_DIR } from '../constants' import { dispatch } from '../utils' -export const fixLineEndings = async () => { +export const fixLineEndings = async (): Promise => { let patches = readdirSync(PATCHES_DIR) patches = patches.filter((p) => p !== '.index') diff --git a/src/commands/license-check.ts b/src/commands/license-check.ts index 5a5726d..ad43588 100644 --- a/src/commands/license-check.ts +++ b/src/commands/license-check.ts @@ -1,5 +1,5 @@ import chalk from 'chalk' -import { readdirSync, readFileSync } from 'fs-extra' +import { readdirSync, readFileSync } from 'fs' import { resolve } from 'path' import { log } from '..' import { ENGINE_DIR, PATCHES_DIR } from '../constants' diff --git a/src/commands/reset.ts b/src/commands/reset.ts index 5f03b75..0dc318f 100644 --- a/src/commands/reset.ts +++ b/src/commands/reset.ts @@ -1,5 +1,5 @@ import execa from 'execa' -import { existsSync } from 'fs-extra' +import { existsSync } from 'fs' import { resolve } from 'path' import prompts from 'prompts' import rimraf from 'rimraf' diff --git a/src/commands/set-branch.ts b/src/commands/set-branch.ts index e31c607..dc6cc25 100644 --- a/src/commands/set-branch.ts +++ b/src/commands/set-branch.ts @@ -1,9 +1,9 @@ import execa from 'execa' -import { existsSync, readFileSync, writeFileSync } from 'fs-extra' +import { existsSync, readFileSync, writeFileSync } from 'fs' import { resolve } from 'path' import { log } from '..' -export const setBranch = async (branch: string) => { +export const setBranch = async (branch: string): Promise => { if (!existsSync(resolve(process.cwd(), '.dotbuild', 'metadata'))) { return log.error('Cannot find metadata, aborting...') } diff --git a/src/controllers/brandingPatch.ts b/src/controllers/brandingPatch.ts index 5701fc1..b1a59c5 100644 --- a/src/controllers/brandingPatch.ts +++ b/src/controllers/brandingPatch.ts @@ -7,14 +7,18 @@ import { rmdirSync, writeFileSync, } from 'fs' -import { mkdirpSync } from 'fs-extra' import { dirname, extname, join } from 'path' import sharp from 'sharp' import { config, log } from '..' import { templateDir } from '../commands' import { CONFIGS_DIR, ENGINE_DIR } from '../constants' -import { defaultBrandsConfig, stringTemplate, walkDirectory } from '../utils' +import { + defaultBrandsConfig, + mkdirpSync, + stringTemplate, + walkDirectory, +} from '../utils' import { PatchBase } from './patch' export const BRANDING_DIR = join(CONFIGS_DIR, 'branding') diff --git a/src/controllers/patch.ts b/src/controllers/patch.ts index 9727ca7..b100bfd 100644 --- a/src/controllers/patch.ts +++ b/src/controllers/patch.ts @@ -1,14 +1,11 @@ import chalk from 'chalk' -import { info } from 'console' import execa from 'execa' -import { copyFileSync } from 'fs' -import { existsSync, mkdirpSync, rmdirSync, rmSync, statSync } from 'fs-extra' -import { dirname, join, resolve } from 'path' +import { existsSync, rmdirSync, rmSync, statSync } from 'fs' +import { resolve } from 'path' import readline from 'readline' -import sharp from 'sharp' import { log } from '..' -import { CONFIGS_DIR, ENGINE_DIR, PATCH_ARGS } from '../constants' -import { copyManual, walkDirectory } from '../utils' +import { ENGINE_DIR, PATCH_ARGS } from '../constants' +import { copyManual } from '../utils' export abstract class PatchBase { protected name: string @@ -135,10 +132,10 @@ export class ManualPatch extends PatchBase { switch (this.action) { case 'copy': if (typeof this.src === 'string') { - copyManual(this.src, this.options.noIgnore) + await copyManual(this.src, this.options.noIgnore) } else if (Array.isArray(this.src)) { for (const item of this.src) { - copyManual(item, this.options.noIgnore) + await copyManual(item, this.options.noIgnore) } } else { throw new Error( diff --git a/src/middleware/sha-check.ts b/src/middleware/sha-check.ts index 1ed2e5b..c91a356 100644 --- a/src/middleware/sha-check.ts +++ b/src/middleware/sha-check.ts @@ -1,5 +1,5 @@ import execa from 'execa' -import { existsSync, readFileSync } from 'fs-extra' +import { existsSync, readFileSync } from 'fs' import { resolve } from 'path' import { bin_name, log } from '..' diff --git a/src/utils/error-handler.ts b/src/utils/error-handler.ts index dff32bd..a0c8b9e 100644 --- a/src/utils/error-handler.ts +++ b/src/utils/error-handler.ts @@ -1,5 +1,5 @@ import chalk from 'chalk' -import { readFileSync } from 'fs-extra' +import { readFileSync } from 'fs' import { resolve } from 'path' import { log } from '..' diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 05fcc58..ae4c385 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -1,5 +1,6 @@ -import { readdir, stat } from 'fs/promises' -import { join, isAbsolute } from 'path' +import { closeSync, existsSync, mkdirSync, openSync, writeSync } from 'fs' +import { mkdir, readdir, stat, symlink } from 'fs/promises' +import { join, isAbsolute, dirname, relative } from 'path' import { log } from '..' @@ -70,3 +71,75 @@ export async function walkDirectoryTree( return output } + +export async function ensureDir(dirName: string): Promise { + if (!existsSync(dirName)) { + await mkdirp(dirName) + } +} + +export function mkdirp(dirName: string): Promise { + return mkdir(dirName, { recursive: true }) +} + +export function mkdirpSync(dirName: string): string | undefined { + return mkdirSync(dirName, { recursive: true }) +} + +export function appendToFileSync(fileName: string, content: string): void { + const file = openSync(fileName, 'a') + writeSync(file, content) + closeSync(file) +} + +export async function createSymlink( + srcPath: string, + destPath: string, + type?: string +): Promise { + if (existsSync(destPath)) return + + const { toDest: src } = symlinkPaths(srcPath, destPath) + + const dir = dirname(destPath) + const exists = existsSync(dir) + if (exists) return await symlink(src, destPath, type) + await mkdirp(dir) + return await symlink(src, destPath, type) +} + +/** + * Adapted from fs-extra + * @param srcPath + * @param destPath + * @returns + */ +export function symlinkPaths( + srcPath: string, + destPath: string +): { toCwd: string; toDest: string } { + if (isAbsolute(srcPath)) { + if (!existsSync(srcPath)) throw new Error('absolute srcpath does not exist') + + return { + toCwd: srcPath, + toDest: srcPath, + } + } else { + const dstdir = dirname(destPath) + const relativeToDst = join(dstdir, srcPath) + if (existsSync(relativeToDst)) + return { + toCwd: relativeToDst, + toDest: srcPath, + } + else { + if (!existsSync(srcPath)) + throw new Error('relative srcpath does not exist') + return { + toCwd: srcPath, + toDest: relative(dstdir, srcPath), + } + } + } +} diff --git a/src/utils/import.ts b/src/utils/import.ts index 794b8fb..a597036 100644 --- a/src/utils/import.ts +++ b/src/utils/import.ts @@ -1,17 +1,16 @@ import { existsSync } from 'fs' -import { - appendFileSync, - ensureSymlink, - lstatSync, - readFileSync, -} from 'fs-extra' +import { lstatSync, readFileSync } from 'fs' import { resolve } from 'path' import rimraf from 'rimraf' +import { appendToFileSync, createSymlink } from '.' import { ENGINE_DIR, SRC_DIR } from '../constants' const getChunked = (location: string) => location.replace(/\\/g, '/').split('/') -export const copyManual = (name: string, noIgnore?: boolean): void => { +export const copyManual = async ( + name: string, + noIgnore?: boolean +): Promise => { try { // If the file exists and is not a symlink, we want to replace it with a // symlink to our file, so remove it @@ -23,7 +22,7 @@ export const copyManual = (name: string, noIgnore?: boolean): void => { } // Create the symlink - ensureSymlink( + await createSymlink( resolve(SRC_DIR, ...getChunked(name)), resolve(ENGINE_DIR, ...getChunked(name)) ) @@ -32,7 +31,7 @@ export const copyManual = (name: string, noIgnore?: boolean): void => { const gitignore = readFileSync(resolve(ENGINE_DIR, '.gitignore'), 'utf-8') if (!gitignore.includes(getChunked(name).join('/'))) - appendFileSync( + appendToFileSync( resolve(ENGINE_DIR, '.gitignore'), `\n${getChunked(name).join('/')}` ) diff --git a/src/utils/write-metadata.ts b/src/utils/write-metadata.ts index 049e2d4..4c1b930 100644 --- a/src/utils/write-metadata.ts +++ b/src/utils/write-metadata.ts @@ -1,5 +1,5 @@ import execa from 'execa' -import { writeFileSync } from 'fs-extra' +import { writeFileSync } from 'fs' import { resolve } from 'path' import { config } from '..' diff --git a/yarn.lock b/yarn.lock index abc4c96..af36c7f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -134,13 +134,6 @@ dependencies: "@types/node" "*" -"@types/fs-extra@^9.0.6": - version "9.0.12" - resolved "https://registry.yarnpkg.com/@types/fs-extra/-/fs-extra-9.0.12.tgz#9b8f27973df8a7a3920e8461517ebf8a7d4fdfaf" - integrity sha512-I+bsBr67CurCGnSenZZ7v94gd3tc3+Aj2taxMT4yu4ABLuOgOjeFxX3dokG24ztSRg5tnT00sL8BszO7gSMoIw== - dependencies: - "@types/node" "*" - "@types/glob@*": version "7.1.4" resolved "https://registry.yarnpkg.com/@types/glob/-/glob-7.1.4.tgz#ea59e21d2ee5c517914cb4bc8e4153b99e566672" @@ -440,11 +433,6 @@ astral-regex@^2.0.0: resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-2.0.0.tgz#483143c567aeed4785759c0865786dc77d7d2e31" integrity sha512-Z7tMw1ytTXt5jqMcOP+OQteU1VuNK9Y02uuJtKQ1Sv69jXQKKg5cibLwGJow8yzZP+eAc18EmLGPal0bp36rvQ== -at-least-node@^1.0.0: - version "1.0.0" - resolved "https://registry.yarnpkg.com/at-least-node/-/at-least-node-1.0.0.tgz#602cd4b46e844ad4effc92a8011a3c46e0238dc2" - integrity sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg== - axe-core@^4.0.2: version "4.3.3" resolved "https://registry.yarnpkg.com/axe-core/-/axe-core-4.3.3.tgz#b55cd8e8ddf659fe89b064680e1c6a4dceab0325" @@ -1174,16 +1162,6 @@ fs-constants@^1.0.0: resolved "https://registry.yarnpkg.com/fs-constants/-/fs-constants-1.0.0.tgz#6be0de9be998ce16af8afc24497b9ee9b7ccd9ad" integrity sha512-y6OAwoSIf7FyjMIv94u+b5rdheZEjzR63GTyZJm5qh4Bi+2YgwLCcI/fPFZkL5PSixOt6ZNKm+w+Hfp/Bciwow== -fs-extra@^9.0.1: - version "9.1.0" - resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-9.1.0.tgz#5954460c764a8da2094ba3554bf839e6b9a7c86d" - integrity sha512-hcg3ZmepS30/7BSFqRvoo3DOMQu7IjqxO5nCDt+zM9XWjb33Wg7ziNT+Qvqbuc3+gWpzO02JubVyk2G4Zvo1OQ== - dependencies: - at-least-node "^1.0.0" - graceful-fs "^4.2.0" - jsonfile "^6.0.1" - universalify "^2.0.0" - fs.realpath@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" @@ -1283,7 +1261,7 @@ globby@^11.0.3: merge2 "^1.3.0" slash "^3.0.0" -graceful-fs@^4.1.2, graceful-fs@^4.1.6, graceful-fs@^4.2.0: +graceful-fs@^4.1.2: version "4.2.8" resolved "https://registry.yarnpkg.com/graceful-fs/-/graceful-fs-4.2.8.tgz#e412b8d33f5e006593cbd3cee6df9f2cebbe802a" integrity sha512-qkIilPUYcNhJpd33n0GBXTB1MMPp14TxEsEs0pTrsSVucApsYzW5V+Q8Qxhik6KU3evy+qkAAowTByymK0avdg== @@ -1600,15 +1578,6 @@ json5@^1.0.1: dependencies: minimist "^1.2.0" -jsonfile@^6.0.1: - version "6.1.0" - resolved "https://registry.yarnpkg.com/jsonfile/-/jsonfile-6.1.0.tgz#bc55b2634793c679ec6403094eb13698a6ec0aae" - integrity sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ== - dependencies: - universalify "^2.0.0" - optionalDependencies: - graceful-fs "^4.1.6" - "jsx-ast-utils@^2.4.1 || ^3.0.0", jsx-ast-utils@^3.1.0: version "3.2.1" resolved "https://registry.yarnpkg.com/jsx-ast-utils/-/jsx-ast-utils-3.2.1.tgz#720b97bfe7d901b927d87c3773637ae8ea48781b" @@ -2691,11 +2660,6 @@ unbox-primitive@^1.0.1: has-symbols "^1.0.2" which-boxed-primitive "^1.0.2" -universalify@^2.0.0: - version "2.0.0" - resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717" - integrity sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ== - uri-js@^4.2.2: version "4.4.1" resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e"