Skip to content
This repository has been archived by the owner on Feb 15, 2023. It is now read-only.

Commit

Permalink
➖ Remove fs-extra
Browse files Browse the repository at this point in the history
  • Loading branch information
trickypr committed Dec 3, 2021
1 parent 387a05b commit 210b304
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 88 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
21 changes: 12 additions & 9 deletions src/commands/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 '.'
Expand Down Expand Up @@ -116,7 +121,7 @@ export const download = async (): Promise<void> => {
}

if (ctx.firefoxSourceTar) {
removeSync(resolve(cwd, '.dotbuild', 'engines', ctx.firefoxSourceTar))
unlinkSync(resolve(cwd, '.dotbuild', 'engines', ctx.firefoxSourceTar))
}
},
},
Expand Down Expand Up @@ -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',
Expand All @@ -292,7 +295,7 @@ async function downloadAddon(
): Promise<string> {
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(
Expand All @@ -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(
Expand Down
15 changes: 6 additions & 9 deletions src/commands/export-file.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
log.info(`Exporting ${file}...`)

if (!existsSync(resolve(ENGINE_DIR, file)))
Expand All @@ -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('')
Expand Down
4 changes: 2 additions & 2 deletions src/commands/fix-le.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
let patches = readdirSync(PATCHES_DIR)

patches = patches.filter((p) => p !== '.index')
Expand Down
2 changes: 1 addition & 1 deletion src/commands/license-check.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/commands/reset.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
4 changes: 2 additions & 2 deletions src/commands/set-branch.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
if (!existsSync(resolve(process.cwd(), '.dotbuild', 'metadata'))) {
return log.error('Cannot find metadata, aborting...')
}
Expand Down
8 changes: 6 additions & 2 deletions src/controllers/brandingPatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
15 changes: 6 additions & 9 deletions src/controllers/patch.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/middleware/sha-check.ts
Original file line number Diff line number Diff line change
@@ -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 '..'

Expand Down
2 changes: 1 addition & 1 deletion src/utils/error-handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chalk from 'chalk'
import { readFileSync } from 'fs-extra'
import { readFileSync } from 'fs'
import { resolve } from 'path'
import { log } from '..'

Expand Down
77 changes: 75 additions & 2 deletions src/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -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 '..'

Expand Down Expand Up @@ -70,3 +71,75 @@ export async function walkDirectoryTree(

return output
}

export async function ensureDir(dirName: string): Promise<void> {
if (!existsSync(dirName)) {
await mkdirp(dirName)
}
}

export function mkdirp(dirName: string): Promise<string | undefined> {
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<void> {
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),
}
}
}
}
17 changes: 8 additions & 9 deletions src/utils/import.ts
Original file line number Diff line number Diff line change
@@ -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<void> => {
try {
// If the file exists and is not a symlink, we want to replace it with a
// symlink to our file, so remove it
Expand All @@ -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))
)
Expand All @@ -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('/')}`
)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/write-metadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import execa from 'execa'
import { writeFileSync } from 'fs-extra'
import { writeFileSync } from 'fs'
import { resolve } from 'path'
import { config } from '..'

Expand Down
Loading

0 comments on commit 210b304

Please sign in to comment.