-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
100 lines (93 loc) · 3.25 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var path = require('path'),
fs = require('fs'),
async = require('async'),
AppDirectory = require('appdirectory')
function getPlugins(plugins) {
var mapped = []
Object.getOwnPropertyNames(plugins).forEach(function (name) {
mapped.push({
name: name,
version: plugins[name]
})
})
return mapped
}
function getPluginPackage(plugin, callback) {
var context = this
var name = plugin.name
var version = plugin.version
// Load the package.json in either the linked dev directory or from the downloaded plugin
async.map(
['link', version],
function (version, callback) {
var packagePath = path.join(context.pluginsDir, name, version, 'package.json')
fs.readFile(packagePath, {encoding:'utf8'}, function (err, result) {
if(err) return callback()
callback(null, {
name: name,
version: version,
config: JSON.parse(result)
})
})
},
function (err, results) {
// If neither file is found, or there was an unexpected error then fail
var result = results[0] || results[1]
if (err || !result) return callback(err || 'ENOENT')
callback(null, result)
})
}
function loadPlugin(context, results, callback) {
var modules = []
var dependencies = []
try {
for(var i = 0, n = results.length; i < n; i++) {
var plugin = results[i]
var main = plugin.config.main
var name = plugin.name
var version = plugin.version
var depName = name.replace(/-/g, '.')
var file = path.resolve(path.join(context.pluginsDir, name, version), main)
var Plugin = require(file)
var mod = new Plugin(context.appContext)
modules.push(mod)
dependencies.push(depName)
}
} catch (err) {
return callback(err)
}
callback(null, dependencies, modules)
}
function load(appContext, callback) {
var appDir = path.dirname(process.mainModule.filename)
var packagePath = path.join(appDir, 'package.json')
fs.readFile(packagePath, {encoding: 'utf8'}, function (err, contents) {
if(err) return callback(err);
var config = JSON.parse(contents)
var dirs = new AppDirectory({
appName: config.name,
appAuthor: config.publisher
})
var appData = dirs.userData()
console.log('appData: ' + appData)
var currentPath = path.join(appData, '.current')
fs.readFile(currentPath, {encoding: 'utf8'}, function (err, contents) {
var plugins = (!err ? JSON.parse(contents) : config.plugins) || {}
var context = {
plugins: plugins,
pluginsDir: path.join(appData, 'plugins'),
appContext: appContext
}
async.map(
getPlugins(context.plugins),
getPluginPackage.bind(context),
function (err, results) {
if(err) return callback(err)
loadPlugin(context, results, callback)
})
})
})
}
module.exports = {
load: load
}