Skip to content

Commit

Permalink
revert to use recursiveDirectoryGlob for link following
Browse files Browse the repository at this point in the history
  • Loading branch information
evantahler committed May 7, 2017
1 parent 311645b commit 1b6b86f
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 20 deletions.
5 changes: 2 additions & 3 deletions actionhero.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

const path = require('path')
const async = require('async')
const glob = require('glob')

// HELPERS ///

Expand Down Expand Up @@ -118,11 +117,11 @@ actionhero.prototype.initialize = function (params, callback) {
let customInitializers = []
let duplicatedInitializers = []
this.api.config.general.paths.initializer.forEach((startPath) => {
customInitializers = customInitializers.concat(glob.sync(path.join(startPath, '**', '*.js'), {follow: true}))
customInitializers = customInitializers.concat(this.api.utils.recursiveDirectoryGlob(startPath))
})
// load all other initializers
this.api.utils.arrayUniqueify(
glob.sync(path.join(__dirname, 'initializers', '**', '*.js'), {follow: true})
this.api.utils.recursiveDirectoryGlob(path.join(__dirname, 'initializers'))
.sort()
.concat(
customInitializers
Expand Down
6 changes: 2 additions & 4 deletions bin/methods/help.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
'use strict'

const glob = require('glob')

module.exports = {
name: 'help',
description: 'get actonhero CLI help; will display this document',
Expand All @@ -10,11 +8,11 @@ module.exports = {
let files = []
let methods = {}

glob.sync(api.actionheroRoot + '/bin/methods/**', {follow: true}).forEach(function (f) {
api.utils.recursiveDirectoryGlob(api.actionheroRoot + '/bin/methods').forEach(function (f) {
files.push(f)
})

glob.sync(api.projectRoot + '/bin/**', {follow: true}).forEach(function (f) {
api.utils.recursiveDirectoryGlob(api.projectRoot + '/bin').forEach(function (f) {
files.push(f)
})

Expand Down
5 changes: 1 addition & 4 deletions initializers/actions.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
'use strict'

const glob = require('glob')
const path = require('path')

module.exports = {
loadPriority: 410,
initialize: function (api, next) {
Expand Down Expand Up @@ -97,7 +94,7 @@ module.exports = {
}

api.config.general.paths.action.forEach(function (p) {
glob.sync(path.join(p, '**', '*.js'), {follow: true}).forEach(function (f) {
api.utils.recursiveDirectoryGlob(p).forEach(function (f) {
api.actions.loadFile(f)
})
})
Expand Down
3 changes: 1 addition & 2 deletions initializers/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const fs = require('fs')
const path = require('path')
const glob = require('glob')
const argv = require('optimist').argv

module.exports = {
Expand Down Expand Up @@ -105,7 +104,7 @@ module.exports = {
}

api.loadConfigDirectory = function (configPath, watch) {
const configFiles = glob.sync(path.join(configPath, '**', '*.js'), {follow: true})
const configFiles = api.utils.recursiveDirectoryGlob(configPath)

let loadRetries = 0
let loadErrors = {}
Expand Down
3 changes: 1 addition & 2 deletions initializers/servers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const glob = require('glob')
const path = require('path')
const async = require('async')

Expand Down Expand Up @@ -28,7 +27,7 @@ module.exports = {
let jobs = []

serverFolders.forEach((p) => {
glob.sync(path.join(p, '**', '*.js'), {follow: true}).forEach((f) => {
api.utils.recursiveDirectoryGlob(p).forEach((f) => {
let parts = f.split(/[/\\]+/)
let serverName = parts[(parts.length - 1)].split('.')[0]
if (api.config.servers[serverName] && api.config.servers[serverName].enabled === true) {
Expand Down
4 changes: 1 addition & 3 deletions initializers/tasks.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
'use strict'

const async = require('async')
const glob = require('glob')
const path = require('path')

module.exports = {
startPriority: 900,
Expand Down Expand Up @@ -348,7 +346,7 @@ module.exports = {

function loadTasks (reload) {
api.config.general.paths.task.forEach((p) => {
glob.sync(path.join(p, '**', '*.js'), {follow: true}).forEach((f) => {
api.utils.recursiveDirectoryGlob(p).forEach((f) => {
api.tasks.loadFile(f, reload)
})
})
Expand Down
73 changes: 73 additions & 0 deletions initializers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,79 @@ module.exports = {
return a
}

// //////////////////////////////////////////////////////////////////////////
// get all .js files in a directory
api.utils.recursiveDirectoryGlob = function (dir, extension, followLinkFiles) {
let results = []

if (!extension) { extension = '.js' }
if (!followLinkFiles) { followLinkFiles = true }

extension = extension.replace('.', '')

if (fs.existsSync(dir)) {
fs.readdirSync(dir).forEach((file) => {
let fullFilePath = path.join(dir, file)
if (file[0] !== '.') { // ignore 'system' files
let stats = fs.statSync(fullFilePath)
let child
if (stats.isDirectory()) {
child = api.utils.recursiveDirectoryGlob(fullFilePath, extension, followLinkFiles)
child.forEach((c) => { results.push(c) })
} else if (stats.isSymbolicLink()) {
let realPath = fs.readlinkSync(fullFilePath)
child = api.utils.recursiveDirectoryGlob(realPath, extension, followLinkFiles)
child.forEach((c) => { results.push(c) })
} else if (stats.isFile()) {
let fileParts = file.split('.')
let ext = fileParts[(fileParts.length - 1)]
// real file match
if (ext === extension) { results.push(fullFilePath) }
// linkfile traversal
if (ext === 'link' && followLinkFiles === true) {
let linkedPath = api.utils.sourceRelativeLinkPath(fullFilePath, api.config.general.paths.plugin)
if (linkedPath) {
child = api.utils.recursiveDirectoryGlob(linkedPath, extension, followLinkFiles)
child.forEach((c) => { results.push(c) })
} else {
try {
api.log(`cannot find linked refrence to \`${file}\``, 'warning')
} catch (e) {
throw new Error('cannot find linked refrence to ' + file)
}
}
}
}
}
})
}

return results.sort()
}

api.utils.sourceRelativeLinkPath = function (linkfile, pluginPaths) {
const type = fs.readFileSync(linkfile).toString()
const pathParts = linkfile.split(path.sep)
const name = pathParts[(pathParts.length - 1)].split('.')[0]
const pathsToTry = pluginPaths.slice(0)
let pluginRoot

// TODO: always also try the local destination's `node_modules` to allow for nested plugins
// This might be a security risk without requiring explicit sourcing

pathsToTry.forEach((pluginPath) => {
let pluginPathAttempt = path.normalize(pluginPath + path.sep + name)
try {
let stats = fs.lstatSync(pluginPathAttempt)
if (!pluginRoot && (stats.isDirectory() || stats.isSymbolicLink())) { pluginRoot = pluginPathAttempt }
} catch (e) { }
})

if (!pluginRoot) { return false }
let pluginSection = path.normalize(pluginRoot + path.sep + type)
return pluginSection
}

// //////////////////////////////////////////////////////////////////////////
// object Clone
api.utils.objClone = function (obj) {
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"author": "Evan Tahler <evantahler@gmail.com>",
"name": "actionhero",
"description": "actionhero.js is a multi-transport API Server with integrated cluster capabilities and delayed tasks",
"version": "17.0.0",
"version": "17.0.1",
"homepage": "http://www.actionherojs.com",
"license": "Apache-2.0",
"repository": {
Expand Down Expand Up @@ -35,7 +35,6 @@
"etag": "^1.7.0",
"fakeredis": "^2.0.0",
"formidable": "^1.0.17",
"glob": "^7.1.1",
"i18n": "^0.8.3",
"ioredis": "^2.4.1",
"is-running": "^2.0.1",
Expand Down

0 comments on commit 1b6b86f

Please sign in to comment.