forked from EvolveLabs/electron-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
220 lines (196 loc) · 7.47 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
const path = require("path");
const fs = require("fs");
const async = require("async");
const AppDirectory = require("appdirectory");
const compareVersions = require( "compare-versions");
var beLoud = true;
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.hasOwnProperty("main") ? plugin.config.main : "index.js";
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);
}
/* FROM https://stackoverflow.com/questions/31645738/how-to-create-full-path-with-nodes-fs-mkdirsync */
function mkDirByPathSync(targetDir, {isRelativeToScript = false} = {}) {
const sep = path.sep;
const initDir = path.isAbsolute(targetDir) ? sep : "";
const baseDir = isRelativeToScript ? __dirname : ".";
targetDir.split(sep).reduce((parentDir, childDir) => {
const curDir = path.resolve(baseDir, parentDir, childDir);
try {
fs.mkdirSync(curDir);
if ( beLoud ) { console.log(`Directory ${curDir} created!`); }
} catch (err) {
if (err.code !== "EEXIST") {
throw err;
}
if ( beLoud ) { console.log(`Directory ${curDir} already exists!`); }
}
return curDir;
}, initDir);
}
function substitutePluginPath( passedPath, appDir, callback ) {
let pluginPath = "";
var packagePath = path.join(appDir, "package.json");
fs.readFile(packagePath, {encoding: "utf8"}, function (err, contents) {
if(err) return callback(err, null);
var config = JSON.parse(contents)
var dirs = new AppDirectory({
appName: config.name,
appAuthor: config.publisher
})
var appData = dirs.userData()
if ( beLoud ) { console.log("[electron-plugins] appData: " + appData); }
pluginPath = passedPath ? passedPath : path.join(appData, "plugins");
// Check to see if this is a "relative path", ASSUME ~ is homedir for all platforms.
if( pluginPath.slice( 0, 1 ) === "~" )
{
pluginPath = path.join( require( "os" ).homedir(), pluginPath.substr(1) );
}
if( pluginPath.slice( -1 ) === ":" )
{
pluginPath = path.join( pluginPath.slice( 0, -1 ), config.name );
}
if ( beLoud ) { console.log( "[electron-plugins] Using provided plugin path. " + pluginPath ); }
return callback( null, pluginPath );
} );
}
function discover( pluginRelPath, appD, useDev, callback ) {
let foundPlugins = {};
var appDir = appD ? appD : path.dirname(process.mainModule.filename);
substitutePluginPath( pluginRelPath, appDir, function ( err, pluginPath ) {
if( fs.existsSync( pluginPath ) ) {
dirContents = fs.readdirSync( pluginPath );
for( var contLoop=0; contLoop < dirContents.length; contLoop++ )
{
if( fs.statSync( path.join(pluginPath, dirContents[ contLoop ])).isDirectory() )
{
const versions = fs.readdirSync( path.join(pluginPath, dirContents[ contLoop ] ) );
var newestVersion = "0.0.0";
for ( var verLoop=0; verLoop < versions.length; verLoop++ ) {
if( fs.statSync( path.join( pluginPath, dirContents[ contLoop ], versions[ verLoop ])).isDirectory() ) {
if( versions[ verLoop ] === "LINK" )
{
if ( useDev )
{
foundPlugins[ dirContents[ contLoop ] ] = "LINK";
}
continue;
}
else if (foundPlugins[ dirContents[ contLoop ] ] )
{
if( compareVersions( newestVersion, versions[ verLoop ] ) < 0 )
{
newestVersion = versions[ verLoop ];
}
}
}
}
if( newestVersion != "0.0.0" ) { foundPlugins[ dirContents[ contLoop ] ] = newestVersion; }
}
}
callback( null, foundPlugins );
} else {
callback( "Plugin Path not found.", null );
}
});
}
function load( appContext, callback) {
let makePluginPath = false;
if ( appContext.context.hasOwnProperty( "makePluginPath" ) ) {
makePluginPath = true;
}
if ( appContext.context.hasOwnProperty( "quiet" ) ) {
beLoud = false;
}
let passedPath = appContext.context.hasOwnProperty( "pluginPath" ) ? appContext.context.pluginPath : "";
var appDir = appContext.context.hasOwnProperty( "appDir") ? appContext.context.appDir : path.dirname(process.mainModule.filename);
substitutePluginPath( passedPath, appDir, function ( err, pluginPath ) {
var currentPath = path.join( pluginPath, ".current" )
// If the plugin path does not exist, log it and bail.
if( !fs.existsSync( pluginPath ) ) {
if ( beLoud ) { console.log( "[electron-plugins] Plugin path does not exist."); }
if ( makePluginPath ) {
mkDirByPathSync( pluginPath );
if ( beLoud ) { console.log( "[electron-plugins] Created plugin path. "); }
}
}
else {
if ( beLoud ) { console.log( "[electron-plugins] Loading plugins from " + pluginPath ); }
var plugins;
if ( appContext.context.hasOwnProperty( "plugins" ) ) {
plugins = appContext.context.plugins;
}
else {
let contents = fs.readFileSync(currentPath, {encoding: "utf8"})
plugins = (!err ? JSON.parse(contents) : config.plugins) || {}
}
var context = {
plugins: plugins,
pluginsDir: pluginPath,
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,
discover: discover
}