Skip to content

Commit

Permalink
refactor: upgrade and reduce usage of fs-extra
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Dec 8, 2022
1 parent 42e17b4 commit cfbd029
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"esbuild": "^0.16.2",
"escape-string-regexp": "^5.0.0",
"etag": "^1.8.1",
"fs-extra": "^10.1.0",
"fs-extra": "^11.1.0",
"globby": "^13.1.2",
"gzip-size": "^7.0.0",
"h3": "^1.0.1",
Expand Down
14 changes: 12 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/presets/azure.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fse from 'fs-extra'
import { readFile } from 'node:fs/promises'
import { join, resolve } from 'pathe'
import { writeFile } from '../utils'
import { defineNitroPreset } from '../preset'
Expand Down Expand Up @@ -26,7 +26,7 @@ async function writeRoutes (nitro: Nitro) {

let nodeVersion = '16'
try {
const currentNodeVersion = fse.readJSONSync(join(nitro.options.rootDir, 'package.json')).engines.node
const currentNodeVersion = JSON.parse(await readFile(join(nitro.options.rootDir, 'package.json'), 'utf8')).engines.node
if (['16', '14'].includes(currentNodeVersion)) {
nodeVersion = currentNodeVersion
}
Expand Down
6 changes: 3 additions & 3 deletions src/presets/cloudflare.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'pathe'
import { move } from 'fs-extra'
import fse from 'fs-extra'
import { writeFile } from '../utils'
import { defineNitroPreset } from '../preset'
import type { Nitro } from '../types'
Expand Down Expand Up @@ -37,8 +37,8 @@ export const cloudflarePages = defineNitroPreset({
},
hooks: {
async 'compiled' (nitro: Nitro) {
await move(resolve(nitro.options.output.serverDir, 'path.js'), resolve(nitro.options.output.serverDir, '[[path]].js'))
await move(resolve(nitro.options.output.serverDir, 'path.js.map'), resolve(nitro.options.output.serverDir, '[[path]].js.map'))
await fse.move(resolve(nitro.options.output.serverDir, 'path.js'), resolve(nitro.options.output.serverDir, '[[path]].js'))
await fse.move(resolve(nitro.options.output.serverDir, 'path.js.map'), resolve(nitro.options.output.serverDir, '[[path]].js.map'))
}
}
})
7 changes: 4 additions & 3 deletions src/presets/firebase.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createRequire } from 'module'
import { existsSync } from 'node:fs'
import { readFile } from 'node:fs/promises'
import { join, relative, resolve } from 'pathe'
import fse from 'fs-extra'
import { globby } from 'globby'
import { readPackageJSON } from 'pkg-types'
import { writeFile } from '../utils'
Expand All @@ -20,7 +21,7 @@ export const firebase = defineNitroPreset({
})

async function writeRoutes (nitro: Nitro) {
if (!fse.existsSync(join(nitro.options.rootDir, 'firebase.json'))) {
if (!existsSync(join(nitro.options.rootDir, 'firebase.json'))) {
const firebase = {
functions: {
source: relative(nitro.options.rootDir, nitro.options.output.serverDir)
Expand Down Expand Up @@ -57,7 +58,7 @@ async function writeRoutes (nitro: Nitro) {

let nodeVersion = '14'
try {
const currentNodeVersion = fse.readJSONSync(join(nitro.options.rootDir, 'package.json')).engines.node
const currentNodeVersion = JSON.parse(await readFile(join(nitro.options.rootDir, 'package.json'), 'utf8')).engines.node
if (['16', '14'].includes(currentNodeVersion)) {
nodeVersion = currentNodeVersion
}
Expand Down
13 changes: 7 additions & 6 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createRequire } from 'module'
import fsp from 'node:fs/promises'
import { existsSync, readFileSync } from 'node:fs'
import { relative, dirname, resolve } from 'pathe'
import fse from 'fs-extra'
import jiti from 'jiti'
import consola from 'consola'
import chalk from 'chalk'
Expand Down Expand Up @@ -38,8 +39,8 @@ export function tryImport (dir: string, path: string) {
}

export async function writeFile (file: string, contents: Buffer | string, log = false) {
await fse.mkdirp(dirname(file))
await fse.writeFile(file, contents, typeof contents === 'string' ? 'utf-8' : undefined)
await fsp.mkdir(dirname(file), { recursive: true })
await fsp.writeFile(file, contents, typeof contents === 'string' ? 'utf-8' : undefined)
if (log) {
consola.info('Generated', prettyPath(file))
}
Expand Down Expand Up @@ -80,7 +81,7 @@ export function detectTarget () {

export async function isDirectory (path: string) {
try {
return (await fse.stat(path)).isDirectory()
return (await fsp.stat(path)).isDirectory()
} catch (_err) {
return false
}
Expand Down Expand Up @@ -117,8 +118,8 @@ export function readPackageJson (
const pkgModulePaths = /^(.*\/node_modules\/).*$/.exec(_require.resolve(packageName))
for (const pkgModulePath of pkgModulePaths || []) {
const path = resolve(pkgModulePath, packageName, 'package.json')
if (fse.existsSync(path)) {
return fse.readJSONSync(path)
if (existsSync(path)) {
return JSON.parse(readFileSync(path, 'utf8'))
}
continue
}
Expand Down

0 comments on commit cfbd029

Please sign in to comment.