Skip to content

Commit

Permalink
refactor: replace 'm-io' by 'fs-extra' and 'glob'
Browse files Browse the repository at this point in the history
  • Loading branch information
nknapp committed Feb 16, 2019
1 parent f44963f commit 02760c5
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 71 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ coverage
*.iml
/test-output/
/test/fixtures/scenarios/*/actual
/.nyc_output
/.nyc_output
/.idea
test-output
4 changes: 2 additions & 2 deletions examples/run-thought.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* This script generates the documentation for all example-projects.
*/

var qfs = require('m-io/fs')
var fs = require('fs-extra')
var exeq = require('../lib/utils/exeq')
var deep = require('deep-aplus')(Promise)
process.chdir(__dirname)

// Run thought in all example projects
qfs.list(__dirname)
fs.readdir(__dirname)
.then(list => list.filter(dir => dir.match(/^example-project.*$/)))
.then(list => list.map(dir => {
return exeq('thought', ['run', '-a'], { cwd: dir })
Expand Down
16 changes: 8 additions & 8 deletions handlebars/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const _ = {
const debug = require('debug')('thought:helpers')
const { resolvePackageRoot } = require('../../lib/utils/resolve-package-root')
const Handlebars = require('handlebars')
const qfs = require('m-io/fs')
const fs = require('fs-extra')
const util = require('util')
const Q = require('q')
const got = require('got')
Expand Down Expand Up @@ -63,7 +63,7 @@ function json (obj) {
* @memberOf helpers
*/
function include (filename, language) {
return qfs.read(filename).then(function (contents) {
return fs.readFile(filename,'utf-8').then(function (contents) {
return '```' +
(typeof language === 'string' ? language : path.extname(filename).substr(1)) +
'\n' +
Expand All @@ -80,7 +80,7 @@ function include (filename, language) {
* @memberOf helpers
*/
function includeRaw (filename) {
return qfs.read(filename)
return fs.readFile(filename, 'utf-8')
}

/**
Expand All @@ -106,7 +106,7 @@ function includeRaw (filename) {
* @memberOf helpers
*/
function example (filename, options) {
return qfs.read(filename)
return fs.readFile(filename, 'utf-8')
.then(function (contents) {
// Relative path to the current module (e.g. "../"). This path must be replaced
// by the module name in the
Expand All @@ -130,12 +130,12 @@ function example (filename, options) {
* Return true if a file exists
*
* @param {string} filename the path to the file
* @return {boolean} true, if the file or diectory exists
* @return {Promise<boolean>} true, if the file or diectory exists
* @access public
* @memberOf helpers
*/
function exists (filename) {
return qfs.exists(filename)
return fs.exists(filename)
}

/**
Expand Down Expand Up @@ -318,8 +318,8 @@ function hasCodecov () {
* @private
*/
function _searchCiConfig (searchString) {
const travis = qfs.read('.travis.yml')
const appveyor = qfs.read('appveyor.yml')
const travis = fs.readFile('.travis.yml','utf-8')
const appveyor = fs.readFile('appveyor.yml','utf-8')
return Q.allSettled([travis, appveyor]).then(function (files) {
let i
for (i = 0; i < files.length; i++) {
Expand Down
6 changes: 3 additions & 3 deletions handlebars/preprocessor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var qfs = require('m-io/fs')
var fs = require('fs-extra')
var path = require('path')
var deep = require('deep-aplus')(Promise)
var debug = require('debug')('thought:preprocessor')
Expand All @@ -15,7 +15,7 @@ module.exports = function (data) {

// Detect license file and read contents
debug('workingdir', data.workingDir)
data.licenseFile = qfs.list(data.workingDir)
data.licenseFile = fs.readdir(data.workingDir)
.then(function (files) {
debug('project files', files)
var licenseFiles = files.filter(function (filename) {
Expand All @@ -24,7 +24,7 @@ module.exports = function (data) {
if (licenseFiles.length > 0) {
return {
filename: licenseFiles[0],
contents: qfs.read(path.join(data.workingDir, licenseFiles[0])),
contents: fs.readFile(path.join(data.workingDir, licenseFiles[0]),'utf-8'),
fences: path.extname(licenseFiles[0]) !== '.md'
}
}
Expand Down
6 changes: 3 additions & 3 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Copyright (c) 2015 Nils Knappmeier.
* Released under the MIT license.
*/
var qfs = require('m-io/fs')
var fs = require('fs-extra')
var debug = require('debug')('thought:init')
var exec = require('./utils/exeq')
var thoughtPackageJson = require('../package.json')
Expand All @@ -13,7 +13,7 @@ module.exports = function () {
var packageJson
return checkPackageJsonInGit()
.then(() => npmInstallThought())
.then(() => qfs.read('package.json'))
.then(() => fs.readFile('package.json', 'utf-8'))
.then(function (contents) {
packageJson = JSON.parse(contents)
if (packageJson.scripts && packageJson.scripts.thought) {
Expand All @@ -29,7 +29,7 @@ module.exports = function () {
packageJson.scripts.version = 'npm run thought'
}
packageJson.devDependencies.thought = `^${thoughtPackageJson.version}`
return qfs.write('package.json', JSON.stringify(packageJson, null, 2))
return fs.writeFile('package.json', JSON.stringify(packageJson, null, 2))
})
.then(function () {
return exec('git', ['commit', 'package.json', '-m', '[Thought] Added scripts to run thought on version-bumps'])
Expand Down
32 changes: 15 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"customize-write-files": "^3.0.0",
"debug": "^4.1.1",
"deep-aplus": "^1.0.4",
"fs-extra": "^7.0.1",
"glob": "^7.1.3",
"got": "^9.6.0",
"handlebars": "^4.1.0",
Expand All @@ -54,7 +55,6 @@
"lodash.groupby": "^4.6.0",
"lodash.isplainobject": "^4.0.6",
"lodash.map": "^4.6.0",
"m-io": "^0.5.0",
"semver": "^5.6.0",
"simple-git": "^1.107.0",
"trace-and-clarify-if-possible": "^1.0.5"
Expand Down
28 changes: 14 additions & 14 deletions test/init-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ var chai = require('chai')
chai.use(require('chai-as-promised'))
chai.use(require('dirty-chai'))

var qfs = require('m-io/fs')
var fs = require('fs-extra')

var expect = chai.expect

Expand All @@ -46,12 +46,12 @@ describe('The "init" option', function () {
return git.initAsync()
.then(() => git.addAsync('package.json'))
.then(() => git.commitAsync('Initial checkin'))
.then(() => qfs.write('.gitignore', 'node_modules'))
.then(() => fs.writeFile('.gitignore', 'node_modules'))
.then(() => init())
.then(() => git.logAsync())
// Check only which files have been added to the index
.then(log => expect(log.latest.message, 'package.json must have been committed').to.match(/Added scripts to run thought on version-bumps/))
.then(() => qfs.read('package.json'))
.then(() => fs.readFile('package.json','utf-8'))
.then(pkgJson => expect(JSON.parse(pkgJson), 'Checking package.json').to.deep.equals({
'author': '',
'description': 'A simple description',
Expand All @@ -73,7 +73,7 @@ describe('The "init" option', function () {
},
'version': '1.0.0'
}))
.then(() => expect(qfs.exists('node_modules/thought'), 'Thought dependency must be installed').to.be.ok())
.then(() => expect(fs.existsSync('node_modules/thought'), 'Thought dependency must be installed').to.be.true())
})
})

Expand All @@ -83,20 +83,20 @@ describe('The "init" option', function () {
var git = bluebird.promisifyAll(simpleGit(scenario.actual))

return git.initAsync()
.then(() => qfs.read('package.json'))
.then(() => fs.readFile('package.json','utf-8'))
.then(JSON.parse)
.then((pkgJson) => {
delete pkgJson.scripts
return qfs.write('package.json', JSON.stringify(pkgJson))
return fs.writeFile('package.json', JSON.stringify(pkgJson))
})
.then(() => git.addAsync('package.json'))
.then(() => git.commitAsync('Initial checkin'))
.then(() => qfs.write('.gitignore', 'node_modules'))
.then(() => fs.writeFile('.gitignore', 'node_modules'))
.then(() => init())
.then(() => git.logAsync())
// Check only which files have been added to the index
.then(log => expect(log.latest.message, 'package.json must have been committed').to.match(/Added scripts to run thought on version-bumps/))
.then(() => qfs.read('package.json'))
.then(() => fs.readFile('package.json','utf-8'))
.then(pkgJson => expect(JSON.parse(pkgJson), 'Checking package.json').to.deep.equals({
'author': '',
'description': 'A simple description',
Expand All @@ -117,7 +117,7 @@ describe('The "init" option', function () {
},
'version': '1.0.0'
}))
.then(() => expect(qfs.exists('node_modules/thought'), 'Thought dependency must be installed').to.be.ok())
.then(() => expect(fs.existsSync('node_modules/thought'), 'Thought dependency must be installed').to.be.ok())
})
})

Expand All @@ -136,12 +136,12 @@ describe('The "init" option', function () {
var git = bluebird.promisifyAll(simpleGit(scenario.actual))

return git.initAsync()
.then(() => qfs.read('package.json'))
.then(() => fs.readFile('package.json','utf-8'))

.then(JSON.parse)
.then((pkgJson) => {
pkgJson.scripts.thought = 'thought run -a'
return qfs.write('package.json', JSON.stringify(pkgJson))
return fs.writeFile('package.json', JSON.stringify(pkgJson))
})
.then(() => git.addAsync('package.json'))
.then(() => git.commitAsync('Initial checkin'))
Expand All @@ -155,17 +155,17 @@ describe('The "init" option', function () {
var git = bluebird.promisifyAll(simpleGit(scenario.actual))

return git.initAsync()
.then(() => qfs.read('package.json'))
.then(() => fs.readFile('package.json','utf-8'))

.then(JSON.parse)
.then((pkgJson) => {
pkgJson.scripts.version = 'run something'
return qfs.write('package.json', JSON.stringify(pkgJson))
return fs.writeFile('package.json', JSON.stringify(pkgJson))
})
.then(() => git.addAsync('package.json'))
.then(() => git.commitAsync('Initial checkin'))
.then(() => init())
.then(() => qfs.read('package.json'))
.then(() => fs.readFile('package.json','utf-8'))
.then(JSON.parse)
.then((pkgJson) => expect(pkgJson.scripts.version).to.equal('npm run thought && run something'))
})
Expand Down
9 changes: 3 additions & 6 deletions test/lib/scenarios.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
var fs = require('fs')
var fs = require('fs-extra')
var path = require('path')

var qfs = require('m-io/fs')
var copy = require('recursive-copy')

var basedir = path.resolve('test', 'fixtures', 'scenarios')

/**
Expand All @@ -22,8 +19,8 @@ class Scenario {
* Prepare setup of the scenario
*/
prepare () {
return qfs.removeTree(this.actual)
.then(() => copy(this.input, this.actual, { dot: true }))
return fs.remove(this.actual)
.then(() => fs.copy(this.input, this.actual, { dot: true }))
.then(() => this)
}

Expand Down
22 changes: 6 additions & 16 deletions test/scenarios-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

'use strict'

var fs = require('fs')
var qfs = require('m-io/fs')
var fs = require('fs-extra')
var pify = require('pify')
var glob = pify(require('glob'))
var deep = require('deep-aplus')(Promise)
var path = require('path')
var chai = require('chai')
Expand All @@ -20,25 +21,14 @@ var expect = chai.expect
var thought = require('../')
var Scenario = require('./lib/scenarios')

function listTreeRelative (baseDir, filter) {
return qfs.listTree(baseDir, filter)
.then((result) => {
const relativeFiles = result.map((_path) => {
return path.relative(baseDir, _path)
})
relativeFiles.sort()
return relativeFiles
})
}

/**
* Travers all files and subdirs of a base directory and
* call the callback for each of them
* @param baseDir the base directory
* @param relativeDir the current directory within the base directory (only for recursive calls)
* @param visitor the callback / visitor
*/
function walk (baseDir, relativeDir, visitor) {
function walk(baseDir, relativeDir, visitor) {
var dirEntries = fs.readdirSync(path.join(baseDir, relativeDir))
dirEntries.forEach(function (fileOrDir) {
const relativePath = path.join(relativeDir, fileOrDir)
Expand Down Expand Up @@ -69,8 +59,8 @@ describe('the integration test: ', function () {

it('the generated files in "actual" should be should match in "expected"', function () {
var filter = (name, stats) => stats.isFile()
var expected = listTreeRelative(scenario.expected, filter)
var actual = listTreeRelative(scenario.actual, filter)
var expected = glob('**/*', { root: scenario.expected, nodir: true, dot: true })
var actual = glob('**/*', { root: scenario.actual, nodir: true, dot: true })
return deep({ expected, actual })
.then(function (result) {
expect(result.actual).to.deep.equal(result.expected)
Expand Down

0 comments on commit 02760c5

Please sign in to comment.