Skip to content

Commit

Permalink
Use fs.promises instead of promisify (vercel#12026)
Browse files Browse the repository at this point in the history
  • Loading branch information
matamatanot authored May 2, 2020
1 parent 8259612 commit 05e93e1
Show file tree
Hide file tree
Showing 15 changed files with 102 additions and 137 deletions.
67 changes: 36 additions & 31 deletions packages/next/build/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import crypto from 'crypto'
import fs from 'fs'
import { promises, writeFileSync } from 'fs'
import Worker from 'jest-worker'
import chalk from 'next/dist/compiled/chalk'
import devalue from 'next/dist/compiled/devalue'
Expand All @@ -8,7 +8,6 @@ import findUp from 'next/dist/compiled/find-up'
import nanoid from 'next/dist/compiled/nanoid/index.js'
import { pathToRegexp } from 'next/dist/compiled/path-to-regexp'
import path from 'path'
import { promisify } from 'util'
import formatWebpackMessages from '../client/dev/error-overlay/format-webpack-messages'
import checkCustomRoutes, {
getRedirectStatus,
Expand Down Expand Up @@ -73,15 +72,6 @@ import {
import getBaseWebpackConfig from './webpack-config'
import { writeBuildId } from './write-build-id'

const fsAccess = promisify(fs.access)
const fsUnlink = promisify(fs.unlink)
const fsRmdir = promisify(fs.rmdir)
const fsStat = promisify(fs.stat)
const fsMove = promisify(fs.rename)
const fsReadFile = promisify(fs.readFile)
const fsWriteFile = promisify(fs.writeFile)
const mkdir = promisify(fs.mkdir)

const staticCheckWorker = require.resolve('./utils')

export type SsgRoute = {
Expand Down Expand Up @@ -140,7 +130,8 @@ export default async function build(dir: string, conf = null): Promise<void> {

if (ciEnvironment.isCI) {
const cacheDir = path.join(distDir, 'cache')
const hasCache = await fsAccess(cacheDir)
const hasCache = await promises
.access(cacheDir)
.then(() => true)
.catch(() => false)

Expand Down Expand Up @@ -182,7 +173,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
await verifyTypeScriptSetup(dir, pagesDir)

try {
await fsStat(publicDir)
await promises.stat(publicDir)
hasPublicDir = true
} catch (_) {}

Expand Down Expand Up @@ -237,7 +228,7 @@ export default async function build(dir: string, conf = null): Promise<void> {

if (hasPublicDir) {
try {
await fsStat(path.join(publicDir, '_next'))
await promises.stat(path.join(publicDir, '_next'))
throw new Error(PUBLIC_DIR_MIDDLEWARE_CONFLICT)
} catch (err) {}
}
Expand Down Expand Up @@ -310,10 +301,14 @@ export default async function build(dir: string, conf = null): Promise<void> {
}),
}

await mkdir(distDir, { recursive: true })
await promises.mkdir(distDir, { recursive: true })
// We need to write the manifest with rewrites before build
// so serverless can import the manifest
await fsWriteFile(routesManifestPath, JSON.stringify(routesManifest), 'utf8')
await promises.writeFile(
routesManifestPath,
JSON.stringify(routesManifest),
'utf8'
)

const configs = await Promise.all([
getBaseWebpackConfig(dir, {
Expand Down Expand Up @@ -451,8 +446,12 @@ export default async function build(dir: string, conf = null): Promise<void> {
const serverPropsPages = new Set<string>()
const additionalSsgPaths = new Map<string, Array<string>>()
const pageInfos = new Map<string, PageInfo>()
const pagesManifest = JSON.parse(await fsReadFile(manifestPath, 'utf8'))
const buildManifest = JSON.parse(await fsReadFile(buildManifestPath, 'utf8'))
const pagesManifest = JSON.parse(
await promises.readFile(manifestPath, 'utf8')
)
const buildManifest = JSON.parse(
await promises.readFile(buildManifestPath, 'utf8')
)

let customAppGetInitialProps: boolean | undefined

Expand Down Expand Up @@ -563,10 +562,12 @@ export default async function build(dir: string, conf = null): Promise<void> {
'pages',
actualPage + '.js'
)
await fsUnlink(clientBundle)
await promises.unlink(clientBundle)

if (config.experimental.modern) {
await fsUnlink(clientBundle.replace(/\.js$/, '.module.js'))
await promises.unlink(
clientBundle.replace(/\.js$/, '.module.js')
)
}
} catch (err) {
if (err.code !== 'ENOENT') {
Expand Down Expand Up @@ -672,7 +673,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
}
})

await fsWriteFile(
await promises.writeFile(
routesManifestPath,
JSON.stringify(routesManifest),
'utf8'
Expand Down Expand Up @@ -776,7 +777,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
// remove server bundles that were exported
for (const page of staticPages) {
const { serverBundle } = pageInfos.get(page)!
await fsUnlink(serverBundle)
await promises.unlink(serverBundle)
}

const moveExportedPage = async (
Expand All @@ -803,8 +804,8 @@ export default async function build(dir: string, conf = null): Promise<void> {
if (page === '/') pagesManifest['/index'] = relativeDest
if (page === '/.amp') pagesManifest['/index.amp'] = relativeDest
}
await mkdir(path.dirname(dest), { recursive: true })
await fsMove(orig, dest)
await promises.mkdir(path.dirname(dest), { recursive: true })
await promises.rename(orig, dest)
}

// Only move /404 to /404 when there is no custom 404 as in that case we don't know about the 404 page
Expand Down Expand Up @@ -866,8 +867,12 @@ export default async function build(dir: string, conf = null): Promise<void> {

// remove temporary export folder
await recursiveDelete(exportOptions.outdir)
await fsRmdir(exportOptions.outdir)
await fsWriteFile(manifestPath, JSON.stringify(pagesManifest), 'utf8')
await promises.rmdir(exportOptions.outdir)
await promises.writeFile(
manifestPath,
JSON.stringify(pagesManifest),
'utf8'
)
}

if (postBuildSpinner) postBuildSpinner.stopAndPersist()
Expand Down Expand Up @@ -915,7 +920,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
preview: previewProps,
}

await fsWriteFile(
await promises.writeFile(
path.join(distDir, PRERENDER_MANIFEST),
JSON.stringify(prerenderManifest),
'utf8'
Expand All @@ -932,7 +937,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
dynamicRoutes: {},
preview: previewProps,
}
await fsWriteFile(
await promises.writeFile(
path.join(distDir, PRERENDER_MANIFEST),
JSON.stringify(prerenderManifest),
'utf8'
Expand All @@ -941,7 +946,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
// await generateClientSsgManifest(prerenderManifest, { distDir, buildId })
}

await fsWriteFile(
await promises.writeFile(
path.join(distDir, EXPORT_MARKER),
JSON.stringify({
version: 1,
Expand All @@ -950,7 +955,7 @@ export default async function build(dir: string, conf = null): Promise<void> {
}),
'utf8'
)
await fsUnlink(path.join(distDir, EXPORT_DETAIL)).catch(err => {
await promises.unlink(path.join(distDir, EXPORT_DETAIL)).catch(err => {
if (err.code === 'ENOENT') {
return Promise.resolve()
}
Expand Down Expand Up @@ -1065,7 +1070,7 @@ function generateClientSsgManifest(
ssgPages
)};self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`
clientSsgManifestPaths.forEach(clientSsgManifestPath =>
fs.writeFileSync(
writeFileSync(
path.join(distDir, clientSsgManifestPath),
clientSsgManifestContent
)
Expand Down
5 changes: 1 addition & 4 deletions packages/next/build/is-writeable.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import fs from 'fs'
import { promisify } from 'util'

const access = promisify(fs.access)

export async function isWriteable(directory: string): Promise<boolean> {
try {
await access(directory, (fs.constants || fs).W_OK)
await fs.promises.access(directory, (fs.constants || fs).W_OK)
return true
} catch (err) {
return false
Expand Down
7 changes: 2 additions & 5 deletions packages/next/build/plugins/collect-plugins.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import findUp from 'next/dist/compiled/find-up'
import fs from 'fs'
import { promises } from 'fs'
import path from 'path'
import { promisify } from 'util'
import resolve from 'next/dist/compiled/resolve/index.js'
import { execOnce } from '../../next-server/lib/utils'

const readdir = promisify(fs.readdir)

export type PluginMetaData = {
requiredEnv: string[]
middleware: string[]
Expand Down Expand Up @@ -65,7 +62,7 @@ async function collectPluginMeta(
// TODO: add err.sh explaining requirements
let middleware: string[] = []
try {
middleware = await readdir(path.join(pkgDir, 'src'))
middleware = await promises.readdir(path.join(pkgDir, 'src'))
} catch (err) {
if (err.code !== 'ENOENT') {
console.error(err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { join } from 'path'
import { promisify } from 'util'
import fs from 'fs'
import { promises } from 'fs'
import { IS_BUNDLED_PAGE_REGEX } from '../../../next-server/lib/constants'
import { Compiler } from 'webpack'

const unlink = promisify(fs.unlink)

// Makes sure removed pages are removed from `.next` in development
export class UnlinkRemovedPagesPlugin {
prevAssets: any
Expand All @@ -27,7 +24,7 @@ export class UnlinkRemovedPagesPlugin {
removed.map(async f => {
const path = join((compiler as any).outputPath, f)
try {
await unlink(path)
await promises.unlink(path)
} catch (err) {
if (err.code === 'ENOENT') return
throw err
Expand Down
7 changes: 2 additions & 5 deletions packages/next/build/write-build-id.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import fs from 'fs'
import { promisify } from 'util'
import { promises } from 'fs'
import { join } from 'path'
import { BUILD_ID_FILE } from '../next-server/lib/constants'

const writeFile = promisify(fs.writeFile)

export async function writeBuildId(
distDir: string,
buildId: string
): Promise<void> {
const buildIdPath = join(distDir, BUILD_ID_FILE)
await writeFile(buildIdPath, buildId, 'utf8')
await promises.writeFile(buildIdPath, buildId, 'utf8')
}
19 changes: 8 additions & 11 deletions packages/next/export/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import chalk from 'next/dist/compiled/chalk'
import findUp from 'next/dist/compiled/find-up'
import {
copyFile as copyFileOrig,
promises,
existsSync,
exists as existsOrig,
mkdir as mkdirOrig,
readFileSync,
writeFileSync,
} from 'fs'
Expand Down Expand Up @@ -37,8 +36,6 @@ import { Telemetry } from '../telemetry/storage'
import { normalizePagePath } from '../next-server/server/normalize-page-path'
import { loadEnvConfig } from '../lib/load-env-config'

const copyFile = promisify(copyFileOrig)
const mkdir = promisify(mkdirOrig)
const exists = promisify(existsOrig)

const createProgress = (total: number, label = 'Exporting') => {
Expand Down Expand Up @@ -189,7 +186,7 @@ export default async function(
}

await recursiveDelete(join(outDir))
await mkdir(join(outDir, '_next', buildId), { recursive: true })
await promises.mkdir(join(outDir, '_next', buildId), { recursive: true })

writeFileSync(
join(distDir, EXPORT_DETAIL),
Expand Down Expand Up @@ -393,14 +390,14 @@ export default async function(
)
const jsonDest = join(pagesDataDir, `${route}.json`)

await mkdir(dirname(htmlDest), { recursive: true })
await mkdir(dirname(jsonDest), { recursive: true })
await copyFile(`${orig}.html`, htmlDest)
await copyFile(`${orig}.json`, jsonDest)
await promises.mkdir(dirname(htmlDest), { recursive: true })
await promises.mkdir(dirname(jsonDest), { recursive: true })
await promises.copyFile(`${orig}.html`, htmlDest)
await promises.copyFile(`${orig}.json`, jsonDest)

if (await exists(`${orig}.amp.html`)) {
await mkdir(dirname(ampHtmlDest), { recursive: true })
await copyFile(`${orig}.amp.html`, ampHtmlDest)
await promises.mkdir(dirname(ampHtmlDest), { recursive: true })
await promises.copyFile(`${orig}.amp.html`, ampHtmlDest)
}
})
)
Expand Down
24 changes: 12 additions & 12 deletions packages/next/export/worker.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { promisify } from 'util'
import url from 'url'
import { extname, join, dirname, sep } from 'path'
import { renderToHTML } from '../next-server/server/render'
import { access, mkdir as mkdirOrig, writeFile } from 'fs'
import { promises } from 'fs'
import AmpHtmlValidator from 'next/dist/compiled/amphtml-validator'
import { loadComponents } from '../next-server/server/load-components'
import { isDynamicRoute } from '../next-server/lib/router/utils/is-dynamic'
Expand All @@ -20,9 +19,6 @@ if (!global.fetch) {
}

const envConfig = require('../next-server/lib/runtime-config')
const writeFileP = promisify(writeFile)
const accessP = promisify(access)
const mkdir = promisify(mkdirOrig)

global.__NEXT_DATA__ = {
nextExport: true,
Expand Down Expand Up @@ -121,7 +117,7 @@ export default async function({
const baseDir = join(outDir, dirname(htmlFilename))
let htmlFilepath = join(outDir, htmlFilename)

await mkdir(baseDir, { recursive: true })
await promises.mkdir(baseDir, { recursive: true })
let html
let curRenderOpts = {}
let renderMethod = renderToHTML
Expand Down Expand Up @@ -243,7 +239,7 @@ export default async function({
const ampHtmlFilepath = join(outDir, ampHtmlFilename)

try {
await accessP(ampHtmlFilepath)
await promises.access(ampHtmlFilepath)
} catch (_) {
// make sure it doesn't exist from manual mapping
let ampHtml
Expand All @@ -263,8 +259,8 @@ export default async function({
if (!curRenderOpts.ampSkipValidation) {
await validateAmp(ampHtml, page + '?amp=1')
}
await mkdir(ampBaseDir, { recursive: true })
await writeFileP(ampHtmlFilepath, ampHtml, 'utf8')
await promises.mkdir(ampBaseDir, { recursive: true })
await promises.writeFile(ampHtmlFilepath, ampHtml, 'utf8')
}
}

Expand All @@ -274,12 +270,16 @@ export default async function({
htmlFilename.replace(/\.html$/, '.json')
)

await mkdir(dirname(dataFile), { recursive: true })
await writeFileP(dataFile, JSON.stringify(curRenderOpts.pageData), 'utf8')
await promises.mkdir(dirname(dataFile), { recursive: true })
await promises.writeFile(
dataFile,
JSON.stringify(curRenderOpts.pageData),
'utf8'
)
}
results.fromBuildExportRevalidate = curRenderOpts.revalidate

await writeFileP(htmlFilepath, html, 'utf8')
await promises.writeFile(htmlFilepath, html, 'utf8')
return results
} catch (error) {
console.error(
Expand Down
Loading

0 comments on commit 05e93e1

Please sign in to comment.