Skip to content

Commit 6f2b2bc

Browse files
authored
Refactor create*/ensure* API to async/await (#1023)
* Refactor `createFile` * Refactor `createLink` * Refactor `createSymlink`
1 parent 426bb46 commit 6f2b2bc

File tree

5 files changed

+160
-173
lines changed

5 files changed

+160
-173
lines changed

lib/ensure/file.js

+31-34
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
11
'use strict'
22

3-
const u = require('universalify').fromCallback
3+
const u = require('universalify').fromPromise
44
const path = require('path')
5-
const fs = require('graceful-fs')
5+
const fs = require('../fs')
66
const mkdir = require('../mkdirs')
77

8-
function createFile (file, callback) {
9-
function makeFile () {
10-
fs.writeFile(file, '', err => {
11-
if (err) return callback(err)
12-
callback()
13-
})
14-
}
8+
async function createFile (file) {
9+
let stats
10+
try {
11+
stats = await fs.stat(file)
12+
} catch { }
13+
if (stats && stats.isFile()) return
1514

16-
fs.stat(file, (err, stats) => { // eslint-disable-line handle-callback-err
17-
if (!err && stats.isFile()) return callback()
18-
const dir = path.dirname(file)
19-
fs.stat(dir, (err, stats) => {
20-
if (err) {
21-
// if the directory doesn't exist, make it
22-
if (err.code === 'ENOENT') {
23-
return mkdir.mkdirs(dir, err => {
24-
if (err) return callback(err)
25-
makeFile()
26-
})
27-
}
28-
return callback(err)
29-
}
15+
const dir = path.dirname(file)
3016

31-
if (stats.isDirectory()) makeFile()
32-
else {
33-
// parent is not a directory
34-
// This is just to cause an internal ENOTDIR error to be thrown
35-
fs.readdir(dir, err => {
36-
if (err) return callback(err)
37-
})
38-
}
39-
})
40-
})
17+
let dirStats = null
18+
try {
19+
dirStats = await fs.stat(dir)
20+
} catch (err) {
21+
// if the directory doesn't exist, make it
22+
if (err.code === 'ENOENT') {
23+
await mkdir.mkdirs(dir)
24+
await fs.writeFile(file, '')
25+
return
26+
} else {
27+
throw err
28+
}
29+
}
30+
31+
if (dirStats.isDirectory()) {
32+
await fs.writeFile(file, '')
33+
} else {
34+
// parent is not a directory
35+
// This is just to cause an internal ENOTDIR error to be thrown
36+
await fs.readdir(dir)
37+
}
4138
}
4239

4340
function createFileSync (file) {
4441
let stats
4542
try {
4643
stats = fs.statSync(file)
47-
} catch {}
44+
} catch { }
4845
if (stats && stats.isFile()) return
4946

5047
const dir = path.dirname(file)

lib/ensure/link.js

+27-27
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
11
'use strict'
22

3-
const u = require('universalify').fromCallback
3+
const u = require('universalify').fromPromise
44
const path = require('path')
5-
const fs = require('graceful-fs')
5+
const fs = require('../fs')
66
const mkdir = require('../mkdirs')
7-
const pathExists = require('../path-exists').pathExists
7+
const { pathExists } = require('../path-exists')
88
const { areIdentical } = require('../util/stat')
99

10-
function createLink (srcpath, dstpath, callback) {
11-
function makeLink (srcpath, dstpath) {
12-
fs.link(srcpath, dstpath, err => {
13-
if (err) return callback(err)
14-
callback(null)
15-
})
10+
async function createLink (srcpath, dstpath) {
11+
let dstStat
12+
try {
13+
dstStat = await fs.lstat(dstpath)
14+
} catch {
15+
// ignore error
1616
}
1717

18-
fs.lstat(dstpath, (_, dstStat) => {
19-
fs.lstat(srcpath, (err, srcStat) => {
20-
if (err) {
21-
err.message = err.message.replace('lstat', 'ensureLink')
22-
return callback(err)
23-
}
24-
if (dstStat && areIdentical(srcStat, dstStat)) return callback(null)
18+
let srcStat
19+
try {
20+
srcStat = await fs.lstat(srcpath)
21+
} catch (err) {
22+
err.message = err.message.replace('lstat', 'ensureLink')
23+
throw err
24+
}
25+
26+
if (dstStat && areIdentical(srcStat, dstStat)) return
27+
28+
const dir = path.dirname(dstpath)
29+
30+
const dirExists = await pathExists(dir)
31+
32+
if (!dirExists) {
33+
await mkdir.mkdirs(dir)
34+
}
2535

26-
const dir = path.dirname(dstpath)
27-
pathExists(dir, (err, dirExists) => {
28-
if (err) return callback(err)
29-
if (dirExists) return makeLink(srcpath, dstpath)
30-
mkdir.mkdirs(dir, err => {
31-
if (err) return callback(err)
32-
makeLink(srcpath, dstpath)
33-
})
34-
})
35-
})
36-
})
36+
await fs.link(srcpath, dstpath)
3737
}
3838

3939
function createLinkSync (srcpath, dstpath) {

lib/ensure/symlink-paths.js

+57-55
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict'
22

33
const path = require('path')
4-
const fs = require('graceful-fs')
5-
const pathExists = require('../path-exists').pathExists
4+
const fs = require('../fs')
5+
const { pathExists } = require('../path-exists')
6+
7+
const u = require('universalify').fromPromise
68

79
/**
810
* Function that returns two types of paths, one relative to symlink, and one
@@ -26,74 +28,74 @@ const pathExists = require('../path-exists').pathExists
2628
* the ability to pass in `relative to current working direcotry` paths.
2729
*/
2830

29-
function symlinkPaths (srcpath, dstpath, callback) {
31+
async function symlinkPaths (srcpath, dstpath) {
3032
if (path.isAbsolute(srcpath)) {
31-
return fs.lstat(srcpath, (err) => {
32-
if (err) {
33-
err.message = err.message.replace('lstat', 'ensureSymlink')
34-
return callback(err)
35-
}
36-
return callback(null, {
37-
toCwd: srcpath,
38-
toDst: srcpath
39-
})
40-
})
41-
} else {
42-
const dstdir = path.dirname(dstpath)
43-
const relativeToDst = path.join(dstdir, srcpath)
44-
return pathExists(relativeToDst, (err, exists) => {
45-
if (err) return callback(err)
46-
if (exists) {
47-
return callback(null, {
48-
toCwd: relativeToDst,
49-
toDst: srcpath
50-
})
51-
} else {
52-
return fs.lstat(srcpath, (err) => {
53-
if (err) {
54-
err.message = err.message.replace('lstat', 'ensureSymlink')
55-
return callback(err)
56-
}
57-
return callback(null, {
58-
toCwd: srcpath,
59-
toDst: path.relative(dstdir, srcpath)
60-
})
61-
})
62-
}
63-
})
33+
try {
34+
await fs.lstat(srcpath)
35+
} catch (err) {
36+
err.message = err.message.replace('lstat', 'ensureSymlink')
37+
throw err
38+
}
39+
40+
return {
41+
toCwd: srcpath,
42+
toDst: srcpath
43+
}
44+
}
45+
46+
const dstdir = path.dirname(dstpath)
47+
const relativeToDst = path.join(dstdir, srcpath)
48+
49+
const exists = await pathExists(relativeToDst)
50+
if (exists) {
51+
return {
52+
toCwd: relativeToDst,
53+
toDst: srcpath
54+
}
55+
}
56+
57+
try {
58+
await fs.lstat(srcpath)
59+
} catch (err) {
60+
err.message = err.message.replace('lstat', 'ensureSymlink')
61+
throw err
62+
}
63+
64+
return {
65+
toCwd: srcpath,
66+
toDst: path.relative(dstdir, srcpath)
6467
}
6568
}
6669

6770
function symlinkPathsSync (srcpath, dstpath) {
68-
let exists
6971
if (path.isAbsolute(srcpath)) {
70-
exists = fs.existsSync(srcpath)
72+
const exists = fs.existsSync(srcpath)
7173
if (!exists) throw new Error('absolute srcpath does not exist')
7274
return {
7375
toCwd: srcpath,
7476
toDst: srcpath
7577
}
76-
} else {
77-
const dstdir = path.dirname(dstpath)
78-
const relativeToDst = path.join(dstdir, srcpath)
79-
exists = fs.existsSync(relativeToDst)
80-
if (exists) {
81-
return {
82-
toCwd: relativeToDst,
83-
toDst: srcpath
84-
}
85-
} else {
86-
exists = fs.existsSync(srcpath)
87-
if (!exists) throw new Error('relative srcpath does not exist')
88-
return {
89-
toCwd: srcpath,
90-
toDst: path.relative(dstdir, srcpath)
91-
}
78+
}
79+
80+
const dstdir = path.dirname(dstpath)
81+
const relativeToDst = path.join(dstdir, srcpath)
82+
const exists = fs.existsSync(relativeToDst)
83+
if (exists) {
84+
return {
85+
toCwd: relativeToDst,
86+
toDst: srcpath
9287
}
9388
}
89+
90+
const srcExists = fs.existsSync(srcpath)
91+
if (!srcExists) throw new Error('relative srcpath does not exist')
92+
return {
93+
toCwd: srcpath,
94+
toDst: path.relative(dstdir, srcpath)
95+
}
9496
}
9597

9698
module.exports = {
97-
symlinkPaths,
99+
symlinkPaths: u(symlinkPaths),
98100
symlinkPathsSync
99101
}

lib/ensure/symlink-type.js

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
'use strict'
22

3-
const fs = require('graceful-fs')
3+
const fs = require('../fs')
4+
const u = require('universalify').fromPromise
45

5-
function symlinkType (srcpath, type, callback) {
6-
callback = (typeof type === 'function') ? type : callback
7-
type = (typeof type === 'function') ? false : type
8-
if (type) return callback(null, type)
9-
fs.lstat(srcpath, (err, stats) => {
10-
if (err) return callback(null, 'file')
11-
type = (stats && stats.isDirectory()) ? 'dir' : 'file'
12-
callback(null, type)
13-
})
14-
}
6+
async function symlinkType (srcpath, type) {
7+
if (type) return type
158

16-
function symlinkTypeSync (srcpath, type) {
179
let stats
10+
try {
11+
stats = await fs.lstat(srcpath)
12+
} catch {
13+
return 'file'
14+
}
1815

16+
return (stats && stats.isDirectory()) ? 'dir' : 'file'
17+
}
18+
19+
function symlinkTypeSync (srcpath, type) {
1920
if (type) return type
21+
22+
let stats
2023
try {
2124
stats = fs.lstatSync(srcpath)
2225
} catch {
@@ -26,6 +29,6 @@ function symlinkTypeSync (srcpath, type) {
2629
}
2730

2831
module.exports = {
29-
symlinkType,
32+
symlinkType: u(symlinkType),
3033
symlinkTypeSync
3134
}

0 commit comments

Comments
 (0)