From 8a629980d90825f65b8726dab1514427812f7e58 Mon Sep 17 00:00:00 2001 From: Jun Yang Date: Mon, 1 Oct 2018 22:06:03 +0800 Subject: [PATCH] refactor: move fs util into template.js --- src/template.js | 8 ++++++-- src/util/fs.js | 15 --------------- test/unit/template.js | 4 +++- 3 files changed, 9 insertions(+), 18 deletions(-) delete mode 100644 src/util/fs.js diff --git a/src/template.js b/src/template.js index 66dd05f4c..4586d6059 100644 --- a/src/template.js +++ b/src/template.js @@ -1,7 +1,11 @@ import * as _ from './util/underscore.js' import path from 'path' import {anySeries} from './util/promise.js' -import {statFileAsync, readFileAsync} from './util/fs.js' +import fs from 'fs' +import {promisify} from 'util' + +const statFileAsync = promisify(fs.stat) +const readFileAsync = promisify(fs.readFile) export async function resolve (filepath, root, options) { if (!path.extname(filepath)) { @@ -22,5 +26,5 @@ export async function resolve (filepath, root, options) { } export async function read (filepath) { - return readFileAsync(filepath) + return readFileAsync(filepath, 'utf8') } diff --git a/src/util/fs.js b/src/util/fs.js deleted file mode 100644 index 276824139..000000000 --- a/src/util/fs.js +++ /dev/null @@ -1,15 +0,0 @@ -import fs from 'fs' - -export function readFileAsync (filepath) { - return new Promise(function (resolve, reject) { - fs.readFile(filepath, 'utf8', function (err, content) { - err ? reject(err) : resolve(content) - }) - }) -}; - -export function statFileAsync (path) { - return new Promise(function (resolve, reject) { - fs.stat(path, (err, stat) => err ? reject(err) : resolve(stat)) - }) -}; diff --git a/test/unit/template.js b/test/unit/template.js index 4b9900a26..0d1a8f6f0 100644 --- a/test/unit/template.js +++ b/test/unit/template.js @@ -1,6 +1,7 @@ import {resolve} from '../../src/template.js' import mock from 'mock-fs' import chai from 'chai' +import path from 'path' import chaiAsPromised from 'chai-as-promised' const expect = chai.expect @@ -15,7 +16,8 @@ describe('template', function () { describe('#resolve()', function () { it('should resolve based on root', function () { const filepath = resolve('bar.html', '/foo', {root: []}) - return expect(filepath).to.eventually.equal('/foo/bar.html') + const expected = path.resolve('/foo/bar.html') + return expect(filepath).to.eventually.equal(expected) }) it('should resolve based on root', function () { return expect(resolve('foo.html', '/foo', {root: []}))