Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parallelizing file existance check using promises #188

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion jingo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var fs = require('fs')
var os = require('os')
var semver = require('semver')
var pkg = require('./package')
var fileExists = require('./lib/file_exists')

global.Git = require('./lib/gitmech')

Expand All @@ -35,7 +36,7 @@ if (program.hashString) {
process.exit(0)
}

if (!program.config || !fs.existsSync(program.config)) {
if (!program.config || !fileExists.sync(program.config)) {
program.help()
process.exit(-1)
}
Expand Down
3 changes: 2 additions & 1 deletion lib/components.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var models = require('./models')
var Promiser = require('bluebird')
var renderer = require('./renderer')
var Configurable = require('./configurable')
var fileExists = require('./file_exists')

models.use(Git)

Expand Down Expand Up @@ -34,7 +35,7 @@ Component.prototype.exists = function () {
return this._exists
}

this._exists = fs.existsSync(Git.absPath(this.file))
this._exists = fileExists.sync(Git.absPath(this.file))

if (!this._exists) {
this.cache = null
Expand Down
30 changes: 30 additions & 0 deletions lib/file_exists.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var fs = require('fs')
var Promiser = require('bluebird')
// using fs.access instead of the deprecated fs.exists:
// https://nodejs.org/api/fs.html#fs_fs_exists_path_callback
var exists = Promiser.promisify(fs.access)

module.exports = {
sync: function (file) {
try {
fs.accessSync(file)
// If it didn't throw, the file exists
return true
} catch (err) {
return catchEnoent(err)
}
},
async: function (file) {
return exists(file)
.then(function (res) { return true })
.catch(catchEnoent)
}
}

function catchEnoent (err) {
if (err.code === 'ENOENT') {
return false
} else {
throw err
}
}
3 changes: 2 additions & 1 deletion lib/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var namer = require('./namer')
var fs = require('fs')
var Configurable = require('./configurable')
var locker = require('./locker')
var fileExists = require('./file_exists')

var gitmech

Expand Down Expand Up @@ -61,7 +62,7 @@ Page.prototype.renameTo = function (newName) {

return new Promiserr(function (resolve, reject) {
// Cannot rename if the file already exists
if (fs.existsSync(gitmech.absPath(newFilename))) {
if (fileExists.sync(gitmech.absPath(newFilename))) {
reject()
return
}
Expand Down
28 changes: 14 additions & 14 deletions routes/misc.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/* global Git */
var router = require('express').Router()
var renderer = require('../lib/renderer')
var fs = require('fs')
var models = require('../lib/models')
var Promiser = require('bluebird')
var fileExists = require('../lib/file_exists')

models.use(Git)

Expand All @@ -22,24 +23,23 @@ function _postPreview (req, res) {

function _getExistence (req, res) {
if (!req.query.data) {
res.send(JSON.stringify({data: []}))
res.json({data: []})
return
}

var result = []
var page
var n = req.query.data.length

req.query.data.forEach(function (pageName, idx) {
(function (name, index) {
page = new models.Page(name)
if (!fs.existsSync(page.pathname)) {
result.push(name)
}
if (index === (n - 1)) {
res.send(JSON.stringify({data: result}))

Promiser.all(req.query.data.map(function (pageName) {
var page = new models.Page(pageName)
return fileExists.async(page.pathname)
.then(function (exists) {
if (!exists) {
result.push(pageName)
}
}(pageName, idx))
})
}))
.then(function () {
res.json({data: result})
})
}

Expand Down