Skip to content

Commit

Permalink
Move sync and async operations to their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 6, 2018
1 parent 0ab1913 commit a7fe5d7
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 104 deletions.
90 changes: 90 additions & 0 deletions lib/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use strict'

var fs = require('fs')
var path = require('path')
var vfile = require('./core')

exports.read = read
exports.write = write

/* Create a virtual file and read it in, asynchronously. */
function read(description, options, callback) {
var file = vfile(description)

if (!callback && typeof options === 'function') {
callback = options
options = null
}

if (!callback) {
return new Promise(executor)
}

executor(resolve, callback)

function resolve(result) {
callback(null, result)
}

function executor(resolve, reject) {
var fp

try {
fp = path.resolve(file.cwd, file.path)
} catch (err) {
return reject(err)
}

fs.readFile(fp, options, done)

function done(err, res) {
if (err) {
reject(err)
} else {
file.contents = res
resolve(file)
}
}
}
}

/* Create a virtual file and write it out, asynchronously. */
function write(description, options, callback) {
var file = vfile(description)

/* Weird, right? Otherwise `fs` doesn’t accept it. */
if (!callback && typeof options === 'function') {
callback = options
options = undefined
}

if (!callback) {
return new Promise(executor)
}

executor(resolve, callback)

function resolve(result) {
callback(null, result)
}

function executor(resolve, reject) {
var fp

try {
fp = path.resolve(file.cwd, file.path)
} catch (err) {
return reject(err)
}

fs.writeFile(fp, file.contents || '', options, done)

function done(err) {
if (err) {
reject(err)
} else {
resolve()
}
}
}
}
110 changes: 6 additions & 104 deletions lib/fs.js
Original file line number Diff line number Diff line change
@@ -1,110 +1,12 @@
'use strict'

var fs = require('fs')
var path = require('path')
var vfile = require('./core')
var sync = require('./sync')
var async = require('./async')

module.exports = vfile

vfile.read = read
vfile.readSync = readSync
vfile.write = write
vfile.writeSync = writeSync

/* Create a virtual file and read it in, asynchronously. */
function read(description, options, callback) {
var file = vfile(description)

if (!callback && typeof options === 'function') {
callback = options
options = null
}

if (!callback) {
return new Promise(executor)
}

executor(null, callback)

function executor(resolve, reject) {
var fp

try {
fp = path.resolve(file.cwd, file.path)
} catch (err) {
return reject(err)
}

fs.readFile(fp, options, done)

function done(err, res) {
if (err) {
reject(err)
} else {
file.contents = res

if (resolve) {
resolve(file)
} else {
callback(null, file)
}
}
}
}
}

/* Create a virtual file and read it in, synchronously. */
function readSync(description, options) {
var file = vfile(description)
file.contents = fs.readFileSync(path.resolve(file.cwd, file.path), options)
return file
}

/* Create a virtual file and write it out, asynchronously. */
function write(description, options, callback) {
var file = vfile(description)

/* Weird, right? Otherwise `fs` doesn’t accept it. */
if (!callback && typeof options === 'function') {
callback = options
options = undefined
}

if (!callback) {
return new Promise(executor)
}

executor(null, callback)

function executor(resolve, reject) {
var fp

try {
fp = path.resolve(file.cwd, file.path)
} catch (err) {
return reject(err)
}

fs.writeFile(fp, file.contents || '', options, done)

function done(err) {
if (err) {
reject(err)
} else if (resolve) {
resolve()
} else {
callback()
}
}
}
}

/* Create a virtual file and write it out, synchronously. */
function writeSync(description, options) {
var file = vfile(description)
fs.writeFileSync(
path.resolve(file.cwd, file.path),
file.contents || '',
options
)
}
vfile.read = async.read
vfile.readSync = sync.read
vfile.write = async.write
vfile.writeSync = sync.write
25 changes: 25 additions & 0 deletions lib/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

var fs = require('fs')
var path = require('path')
var vfile = require('./core')

exports.read = readSync
exports.write = writeSync

/* Create a virtual file and read it in, synchronously. */
function readSync(description, options) {
var file = vfile(description)
file.contents = fs.readFileSync(path.resolve(file.cwd, file.path), options)
return file
}

/* Create a virtual file and write it out, synchronously. */
function writeSync(description, options) {
var file = vfile(description)
fs.writeFileSync(
path.resolve(file.cwd, file.path),
file.contents || '',
options
)
}

0 comments on commit a7fe5d7

Please sign in to comment.