Skip to content

Commit

Permalink
Refactor create*/ensure* API to async/await (#1023)
Browse files Browse the repository at this point in the history
* Refactor `createFile`
* Refactor `createLink`
* Refactor `createSymlink`
  • Loading branch information
SukkaW authored Oct 24, 2023
1 parent 426bb46 commit 6f2b2bc
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 173 deletions.
65 changes: 31 additions & 34 deletions lib/ensure/file.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,47 @@
'use strict'

const u = require('universalify').fromCallback
const u = require('universalify').fromPromise
const path = require('path')
const fs = require('graceful-fs')
const fs = require('../fs')
const mkdir = require('../mkdirs')

function createFile (file, callback) {
function makeFile () {
fs.writeFile(file, '', err => {
if (err) return callback(err)
callback()
})
}
async function createFile (file) {
let stats
try {
stats = await fs.stat(file)
} catch { }
if (stats && stats.isFile()) return

fs.stat(file, (err, stats) => { // eslint-disable-line handle-callback-err
if (!err && stats.isFile()) return callback()
const dir = path.dirname(file)
fs.stat(dir, (err, stats) => {
if (err) {
// if the directory doesn't exist, make it
if (err.code === 'ENOENT') {
return mkdir.mkdirs(dir, err => {
if (err) return callback(err)
makeFile()
})
}
return callback(err)
}
const dir = path.dirname(file)

if (stats.isDirectory()) makeFile()
else {
// parent is not a directory
// This is just to cause an internal ENOTDIR error to be thrown
fs.readdir(dir, err => {
if (err) return callback(err)
})
}
})
})
let dirStats = null
try {
dirStats = await fs.stat(dir)
} catch (err) {
// if the directory doesn't exist, make it
if (err.code === 'ENOENT') {
await mkdir.mkdirs(dir)
await fs.writeFile(file, '')
return
} else {
throw err
}
}

if (dirStats.isDirectory()) {
await fs.writeFile(file, '')
} else {
// parent is not a directory
// This is just to cause an internal ENOTDIR error to be thrown
await fs.readdir(dir)
}
}

function createFileSync (file) {
let stats
try {
stats = fs.statSync(file)
} catch {}
} catch { }
if (stats && stats.isFile()) return

const dir = path.dirname(file)
Expand Down
54 changes: 27 additions & 27 deletions lib/ensure/link.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
'use strict'

const u = require('universalify').fromCallback
const u = require('universalify').fromPromise
const path = require('path')
const fs = require('graceful-fs')
const fs = require('../fs')
const mkdir = require('../mkdirs')
const pathExists = require('../path-exists').pathExists
const { pathExists } = require('../path-exists')
const { areIdentical } = require('../util/stat')

function createLink (srcpath, dstpath, callback) {
function makeLink (srcpath, dstpath) {
fs.link(srcpath, dstpath, err => {
if (err) return callback(err)
callback(null)
})
async function createLink (srcpath, dstpath) {
let dstStat
try {
dstStat = await fs.lstat(dstpath)
} catch {
// ignore error
}

fs.lstat(dstpath, (_, dstStat) => {
fs.lstat(srcpath, (err, srcStat) => {
if (err) {
err.message = err.message.replace('lstat', 'ensureLink')
return callback(err)
}
if (dstStat && areIdentical(srcStat, dstStat)) return callback(null)
let srcStat
try {
srcStat = await fs.lstat(srcpath)
} catch (err) {
err.message = err.message.replace('lstat', 'ensureLink')
throw err
}

if (dstStat && areIdentical(srcStat, dstStat)) return

const dir = path.dirname(dstpath)

const dirExists = await pathExists(dir)

if (!dirExists) {
await mkdir.mkdirs(dir)
}

const dir = path.dirname(dstpath)
pathExists(dir, (err, dirExists) => {
if (err) return callback(err)
if (dirExists) return makeLink(srcpath, dstpath)
mkdir.mkdirs(dir, err => {
if (err) return callback(err)
makeLink(srcpath, dstpath)
})
})
})
})
await fs.link(srcpath, dstpath)
}

function createLinkSync (srcpath, dstpath) {
Expand Down
112 changes: 57 additions & 55 deletions lib/ensure/symlink-paths.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict'

const path = require('path')
const fs = require('graceful-fs')
const pathExists = require('../path-exists').pathExists
const fs = require('../fs')
const { pathExists } = require('../path-exists')

const u = require('universalify').fromPromise

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

function symlinkPaths (srcpath, dstpath, callback) {
async function symlinkPaths (srcpath, dstpath) {
if (path.isAbsolute(srcpath)) {
return fs.lstat(srcpath, (err) => {
if (err) {
err.message = err.message.replace('lstat', 'ensureSymlink')
return callback(err)
}
return callback(null, {
toCwd: srcpath,
toDst: srcpath
})
})
} else {
const dstdir = path.dirname(dstpath)
const relativeToDst = path.join(dstdir, srcpath)
return pathExists(relativeToDst, (err, exists) => {
if (err) return callback(err)
if (exists) {
return callback(null, {
toCwd: relativeToDst,
toDst: srcpath
})
} else {
return fs.lstat(srcpath, (err) => {
if (err) {
err.message = err.message.replace('lstat', 'ensureSymlink')
return callback(err)
}
return callback(null, {
toCwd: srcpath,
toDst: path.relative(dstdir, srcpath)
})
})
}
})
try {
await fs.lstat(srcpath)
} catch (err) {
err.message = err.message.replace('lstat', 'ensureSymlink')
throw err
}

return {
toCwd: srcpath,
toDst: srcpath
}
}

const dstdir = path.dirname(dstpath)
const relativeToDst = path.join(dstdir, srcpath)

const exists = await pathExists(relativeToDst)
if (exists) {
return {
toCwd: relativeToDst,
toDst: srcpath
}
}

try {
await fs.lstat(srcpath)
} catch (err) {
err.message = err.message.replace('lstat', 'ensureSymlink')
throw err
}

return {
toCwd: srcpath,
toDst: path.relative(dstdir, srcpath)
}
}

function symlinkPathsSync (srcpath, dstpath) {
let exists
if (path.isAbsolute(srcpath)) {
exists = fs.existsSync(srcpath)
const exists = fs.existsSync(srcpath)
if (!exists) throw new Error('absolute srcpath does not exist')
return {
toCwd: srcpath,
toDst: srcpath
}
} else {
const dstdir = path.dirname(dstpath)
const relativeToDst = path.join(dstdir, srcpath)
exists = fs.existsSync(relativeToDst)
if (exists) {
return {
toCwd: relativeToDst,
toDst: srcpath
}
} else {
exists = fs.existsSync(srcpath)
if (!exists) throw new Error('relative srcpath does not exist')
return {
toCwd: srcpath,
toDst: path.relative(dstdir, srcpath)
}
}

const dstdir = path.dirname(dstpath)
const relativeToDst = path.join(dstdir, srcpath)
const exists = fs.existsSync(relativeToDst)
if (exists) {
return {
toCwd: relativeToDst,
toDst: srcpath
}
}

const srcExists = fs.existsSync(srcpath)
if (!srcExists) throw new Error('relative srcpath does not exist')
return {
toCwd: srcpath,
toDst: path.relative(dstdir, srcpath)
}
}

module.exports = {
symlinkPaths,
symlinkPaths: u(symlinkPaths),
symlinkPathsSync
}
29 changes: 16 additions & 13 deletions lib/ensure/symlink-type.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
'use strict'

const fs = require('graceful-fs')
const fs = require('../fs')
const u = require('universalify').fromPromise

function symlinkType (srcpath, type, callback) {
callback = (typeof type === 'function') ? type : callback
type = (typeof type === 'function') ? false : type
if (type) return callback(null, type)
fs.lstat(srcpath, (err, stats) => {
if (err) return callback(null, 'file')
type = (stats && stats.isDirectory()) ? 'dir' : 'file'
callback(null, type)
})
}
async function symlinkType (srcpath, type) {
if (type) return type

function symlinkTypeSync (srcpath, type) {
let stats
try {
stats = await fs.lstat(srcpath)
} catch {
return 'file'
}

return (stats && stats.isDirectory()) ? 'dir' : 'file'
}

function symlinkTypeSync (srcpath, type) {
if (type) return type

let stats
try {
stats = fs.lstatSync(srcpath)
} catch {
Expand All @@ -26,6 +29,6 @@ function symlinkTypeSync (srcpath, type) {
}

module.exports = {
symlinkType,
symlinkType: u(symlinkType),
symlinkTypeSync
}
Loading

0 comments on commit 6f2b2bc

Please sign in to comment.