-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move sync and async operations to their own files
- Loading branch information
Showing
3 changed files
with
121 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) | ||
} |