Skip to content

Commit b3e1e5e

Browse files
committedApr 15, 2018
refactor: use fs-extra
1 parent 98e1665 commit b3e1e5e

File tree

4 files changed

+23
-25
lines changed

4 files changed

+23
-25
lines changed
 

‎lib/build.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
11
module.exports = async function build (sourceDir, cliOptions = {}) {
22
process.env.NODE_ENV = 'production'
33

4-
const fs = require('fs')
4+
const fs = require('fs-extra')
55
const path = require('path')
66
const chalk = require('chalk')
77
const webpack = require('webpack')
88
const readline = require('readline')
9-
const { promisify } = require('util')
109
const escape = require('escape-html')
11-
const rimraf = promisify(require('rimraf'))
12-
const mkdirp = promisify(require('mkdirp'))
13-
const readFile = promisify(fs.readFile)
14-
const writeFile = promisify(fs.writeFile)
1510

1611
const prepare = require('./prepare')
1712
const createClientConfig = require('./webpack/createClientConfig')
@@ -26,7 +21,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
2621
}
2722

2823
const { outDir } = options
29-
await rimraf(outDir)
24+
await fs.remove(outDir)
3025

3126
let clientConfig = createClientConfig(options, cliOptions).toConfig()
3227
let serverConfig = createServerConfig(options, cliOptions).toConfig()
@@ -48,7 +43,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
4843
const clientManifest = require(path.resolve(outDir, 'manifest/client.json'))
4944

5045
// remove manifests after loading them.
51-
await rimraf(path.resolve(outDir, 'manifest'))
46+
await fs.remove(path.resolve(outDir, 'manifest'))
5247

5348
// find and remove empty style chunk caused by
5449
// https://github.com/webpack-contrib/mini-css-extract-plugin/issues/85
@@ -60,7 +55,7 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
6055
clientManifest,
6156
runInNewContext: false,
6257
inject: false,
63-
template: fs.readFileSync(path.resolve(__dirname, 'app/index.ssr.html'), 'utf-8')
58+
template: await fs.readFile(path.resolve(__dirname, 'app/index.ssr.html'), 'utf-8')
6459
})
6560

6661
// pre-render head tags from user config
@@ -154,8 +149,8 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
154149
}
155150
const filename = pagePath.replace(/\/$/, '/index.html').replace(/^\//, '')
156151
const filePath = path.resolve(outDir, filename)
157-
await mkdirp(path.dirname(filePath))
158-
await writeFile(filePath, html)
152+
await fs.ensureDir(path.dirname(filePath))
153+
await fs.writeFile(filePath, html)
159154
}
160155

161156
function renderPageMeta (meta) {
@@ -174,15 +169,15 @@ module.exports = async function build (sourceDir, cliOptions = {}) {
174169
return /styles\.\w{8}\.js$/.test(a.name)
175170
})
176171
const styleChunkPath = path.resolve(outDir, styleChunk.name)
177-
const styleChunkContent = await readFile(styleChunkPath, 'utf-8')
178-
await rimraf(styleChunkPath)
172+
const styleChunkContent = await fs.readFile(styleChunkPath, 'utf-8')
173+
await fs.remove(styleChunkPath)
179174
// prepend it to app.js.
180175
// this is necessary for the webpack runtime to work properly.
181176
const appChunk = stats.children[0].assets.find(a => {
182177
return /app\.\w{8}\.js$/.test(a.name)
183178
})
184179
const appChunkPath = path.resolve(outDir, appChunk.name)
185-
const appChunkContent = await readFile(appChunkPath, 'utf-8')
186-
await writeFile(appChunkPath, styleChunkContent + appChunkContent)
180+
const appChunkContent = await fs.readFile(appChunkPath, 'utf-8')
181+
await fs.writeFile(appChunkPath, styleChunkContent + appChunkContent)
187182
}
188183
}

‎lib/prepare.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
1-
const fs = require('fs')
1+
const fs = require('fs-extra')
22
const path = require('path')
33
const globby = require('globby')
4-
const mkdirp = require('mkdirp')
5-
const { promisify } = require('util')
6-
const readFile = promisify(fs.readFile)
7-
const writeFile = promisify(fs.writeFile)
84
const yaml = require('yaml-front-matter')
95
const tempPath = path.resolve(__dirname, 'app/.temp')
106
const { inferTitle, extractHeaders } = require('./util')
117

12-
mkdirp(tempPath)
8+
fs.ensureDirSync(tempPath)
139

1410
const tempCache = new Map()
1511
async function writeTemp (file, content) {
1612
// cache write to avoid hitting the dist if it didn't change
1713
const cached = tempCache.get(file)
1814
if (cached !== content) {
19-
await writeFile(path.join(tempPath, file), content)
15+
await fs.writeFile(path.join(tempPath, file), content)
2016
tempCache.set(file, content)
2117
}
2218
}
@@ -150,7 +146,7 @@ async function resolveOptions (sourceDir) {
150146
}
151147

152148
// extract yaml frontmatter
153-
const content = await readFile(path.resolve(sourceDir, file), 'utf-8')
149+
const content = await fs.readFile(path.resolve(sourceDir, file), 'utf-8')
154150
const frontmatter = yaml.loadFront(content)
155151
// infer title
156152
const title = inferTitle(frontmatter)

‎package.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
"es6-promise": "^4.2.4",
5555
"escape-html": "^1.0.3",
5656
"file-loader": "^1.1.11",
57+
"fs-extra": "^5.0.0",
5758
"globby": "^8.0.1",
5859
"html-webpack-plugin": "^3.2.0",
5960
"koa-connect": "^2.0.1",
@@ -67,15 +68,13 @@
6768
"markdown-it-emoji": "^1.4.0",
6869
"markdown-it-table-of-contents": "^0.3.3",
6970
"mini-css-extract-plugin": "^0.4.0",
70-
"mkdirp": "^0.5.1",
7171
"nprogress": "^0.2.0",
7272
"object-assign": "^4.1.1",
7373
"optimize-css-assets-webpack-plugin": "^4.0.0",
7474
"portfinder": "^1.0.13",
7575
"postcss-loader": "^2.1.3",
7676
"prismjs": "^1.13.0",
7777
"register-service-worker": "^1.2.0",
78-
"rimraf": "^2.6.2",
7978
"semver": "^5.5.0",
8079
"stylus": "^0.54.5",
8180
"stylus-loader": "^3.0.2",

‎yarn.lock

+8
Original file line numberDiff line numberDiff line change
@@ -2021,6 +2021,14 @@ fs-extra@^4.0.2:
20212021
jsonfile "^4.0.0"
20222022
universalify "^0.1.0"
20232023

2024+
fs-extra@^5.0.0:
2025+
version "5.0.0"
2026+
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-5.0.0.tgz#414d0110cdd06705734d055652c5411260c31abd"
2027+
dependencies:
2028+
graceful-fs "^4.1.2"
2029+
jsonfile "^4.0.0"
2030+
universalify "^0.1.0"
2031+
20242032
fs-write-stream-atomic@^1.0.8:
20252033
version "1.0.10"
20262034
resolved "https://registry.yarnpkg.com/fs-write-stream-atomic/-/fs-write-stream-atomic-1.0.10.tgz#b47df53493ef911df75731e70a9ded0189db40c9"

0 commit comments

Comments
 (0)
Please sign in to comment.