From fca1d7be39166f68007db2b3cf8ba3f72fffd415 Mon Sep 17 00:00:00 2001 From: mawni Date: Sun, 13 Nov 2016 16:59:47 -0800 Subject: [PATCH 1/2] lib/walkSync: enhance walkSync function to return items with path and stats --- lib/walk-sync/__tests__/walkSync.test.js | 34 +++++++++++++++++------- lib/walk-sync/index.js | 17 +++++++----- 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/lib/walk-sync/__tests__/walkSync.test.js b/lib/walk-sync/__tests__/walkSync.test.js index ee3b8675..24290185 100644 --- a/lib/walk-sync/__tests__/walkSync.test.js +++ b/lib/walk-sync/__tests__/walkSync.test.js @@ -1,13 +1,14 @@ var assert = require('assert') var path = require('path') +var assign = require('../../util/assign') var fse = require('../../') /* global describe, it */ var fixturesDir = path.join(__dirname, 'fixtures') -describe('walk-sync', function () { - it('should return an error if the source dir does not exist', function (done) { +describe('+ walkSync()', function () { + it('> should return an error if the source dir does not exist', function (done) { try { fse.walkSync('dirDoesNotExist/') } catch (err) { @@ -17,7 +18,7 @@ describe('walk-sync', function () { } }) - it('should return an error if the source is not a dir', function (done) { + it('> should return an error if the source is not a dir', function (done) { try { fse.walkSync(path.join(fixturesDir, 'dir1/file1_2')) } catch (err) { @@ -27,14 +28,29 @@ describe('walk-sync', function () { } }) - it('should return all files successfully for a dir', function (done) { - var files = fse.walkSync(fixturesDir) - var expectedFiles = ['dir1/file1_2', 'dir2/dir2_1/file2_1_1', 'file1'] - expectedFiles = expectedFiles.map(function (item) { + it('> should return all items of a dir containing path and stats data successfully', function (done) { + var items = fse.walkSync(fixturesDir) + var files = ['dir1/file1_2', 'dir2/dir2_1/file2_1_1', 'file1'] + files = files.map(function (item) { return path.join(fixturesDir, item) }) - files.forEach(function (elem, i) { - assert.equal(elem, expectedFiles[i]) + var dirs = ['dir1', 'dir2', 'dir2/dir2_1'] + dirs = dirs.map(function (item) { + return path.join(fixturesDir, item) + }) + var expectedItems = [ + {path: dirs[0], stats: assign({}, {type: 'dir'}, fse.lstatSync(dirs[0]))}, + {path: files[0], stats: assign({}, {type: 'file'}, fse.lstatSync(files[0]))}, + {path: dirs[1], stats: assign({}, {type: 'dir'}, fse.lstatSync(dirs[1]))}, + {path: dirs[2], stats: assign({}, {type: 'dir'}, fse.lstatSync(dirs[2]))}, + {path: files[1], stats: assign({}, {type: 'file'}, fse.lstatSync(files[1]))}, + {path: files[2], stats: assign({}, {type: 'file'}, fse.lstatSync(files[2]))} + ] + items.forEach(function (elem, i) { + assert.deepEqual(elem, expectedItems[i]) + assert.strictEqual(elem.path, expectedItems[i].path) + assert.deepEqual(elem.stats, expectedItems[i].stats) + assert.strictEqual(elem.stats.type, expectedItems[i].stats.type) }) done() }) diff --git a/lib/walk-sync/index.js b/lib/walk-sync/index.js index 0ebe56ed..2b4b7999 100644 --- a/lib/walk-sync/index.js +++ b/lib/walk-sync/index.js @@ -1,18 +1,21 @@ var fs = require('graceful-fs') var path = require('path') +var assign = require('../util/assign') -var walkSync = function (dir, filelist) { - var files = fs.readdirSync(dir) - filelist = filelist || [] +var walkSync = function (dir, list) { + var files = fs.readdirSync(path.resolve(dir)) + list = list || [] files.forEach(function (file) { var nestedPath = path.join(dir, file) - if (fs.lstatSync(nestedPath).isDirectory()) { - filelist = walkSync(nestedPath, filelist) + var stat = fs.lstatSync(nestedPath) + if (stat.isDirectory()) { + list.push({path: nestedPath, stats: assign({}, {type: 'dir'}, stat)}) + list = walkSync(nestedPath, list) } else { - filelist.push(nestedPath) + list.push({path: nestedPath, stats: assign({}, {type: 'file'}, stat)}) } }) - return filelist + return list } module.exports = { From 9df7ca072157fc97037897d15888e87de800b47e Mon Sep 17 00:00:00 2001 From: mawni Date: Sun, 13 Nov 2016 21:11:13 -0800 Subject: [PATCH 2/2] lib/walkSync: remove unnecessary type field from stats --- lib/walk-sync/__tests__/walkSync.test.js | 14 ++++++-------- lib/walk-sync/index.js | 5 ++--- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/lib/walk-sync/__tests__/walkSync.test.js b/lib/walk-sync/__tests__/walkSync.test.js index 24290185..94a20012 100644 --- a/lib/walk-sync/__tests__/walkSync.test.js +++ b/lib/walk-sync/__tests__/walkSync.test.js @@ -1,6 +1,5 @@ var assert = require('assert') var path = require('path') -var assign = require('../../util/assign') var fse = require('../../') @@ -39,18 +38,17 @@ describe('+ walkSync()', function () { return path.join(fixturesDir, item) }) var expectedItems = [ - {path: dirs[0], stats: assign({}, {type: 'dir'}, fse.lstatSync(dirs[0]))}, - {path: files[0], stats: assign({}, {type: 'file'}, fse.lstatSync(files[0]))}, - {path: dirs[1], stats: assign({}, {type: 'dir'}, fse.lstatSync(dirs[1]))}, - {path: dirs[2], stats: assign({}, {type: 'dir'}, fse.lstatSync(dirs[2]))}, - {path: files[1], stats: assign({}, {type: 'file'}, fse.lstatSync(files[1]))}, - {path: files[2], stats: assign({}, {type: 'file'}, fse.lstatSync(files[2]))} + {path: dirs[0], stats: fse.lstatSync(dirs[0])}, + {path: files[0], stats: fse.lstatSync(files[0])}, + {path: dirs[1], stats: fse.lstatSync(dirs[1])}, + {path: dirs[2], stats: fse.lstatSync(dirs[2])}, + {path: files[1], stats: fse.lstatSync(files[1])}, + {path: files[2], stats: fse.lstatSync(files[2])} ] items.forEach(function (elem, i) { assert.deepEqual(elem, expectedItems[i]) assert.strictEqual(elem.path, expectedItems[i].path) assert.deepEqual(elem.stats, expectedItems[i].stats) - assert.strictEqual(elem.stats.type, expectedItems[i].stats.type) }) done() }) diff --git a/lib/walk-sync/index.js b/lib/walk-sync/index.js index 2b4b7999..e9b27d92 100644 --- a/lib/walk-sync/index.js +++ b/lib/walk-sync/index.js @@ -1,6 +1,5 @@ var fs = require('graceful-fs') var path = require('path') -var assign = require('../util/assign') var walkSync = function (dir, list) { var files = fs.readdirSync(path.resolve(dir)) @@ -9,10 +8,10 @@ var walkSync = function (dir, list) { var nestedPath = path.join(dir, file) var stat = fs.lstatSync(nestedPath) if (stat.isDirectory()) { - list.push({path: nestedPath, stats: assign({}, {type: 'dir'}, stat)}) + list.push({path: nestedPath, stats: stat}) list = walkSync(nestedPath, list) } else { - list.push({path: nestedPath, stats: assign({}, {type: 'file'}, stat)}) + list.push({path: nestedPath, stats: stat}) } }) return list