Skip to content

Commit

Permalink
Load systems plugins from package dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
risq committed Jan 8, 2016
1 parent 91f03f5 commit 8f41df5
Showing 1 changed file with 61 additions and 47 deletions.
108 changes: 61 additions & 47 deletions server/loadSystems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,25 @@ export default function(mainApp: express.Express, buildApp: express.Express, cal
fs.writeFileSync(`${systemPath}/public/templates.json`, JSON.stringify(templatesList, null, 2));

// Load plugins
let pluginsInfo = loadPlugins(systemName, `${systemPath}/plugins`, mainApp, buildApp);

let pluginsInfo = loadLocalPlugins(systemName, `${systemPath}/plugins`, mainApp, buildApp);
let packagePath = `${systemPath}/package.json`;
if (fs.existsSync(packagePath)) {
let packageJSON = JSON.parse(fs.readFileSync(packagePath, { encoding: "utf8" }));
if (packageJSON.superpowers != null && packageJSON.superpowers.publishedPluginBundles != null)
if (packageJSON.superpowers != null && packageJSON.superpowers.publishedPluginBundles != null) {
pluginsInfo.publishedBundles = pluginsInfo.publishedBundles.concat(packageJSON.superpowers.publishedPluginBundles);
}
if (packageJSON.dependencies) {

// Get system dependencies beginning with "superpowers-"
Object.keys(packageJSON.dependencies).forEach((pluginName) => {
if (pluginName.indexOf("superpowers-") === 0) {
let pluginAuthor = packageJSON.dependencies[pluginName].split('/')[0];

// Register plugin
loadPlugin(systemName, `${systemPath}/node_modules/${pluginName}`, pluginAuthor, pluginName, mainApp, buildApp, pluginsInfo);
}
});
}
}
fs.writeFileSync(`${systemPath}/public/plugins.json`, JSON.stringify(pluginsInfo, null, 2));

Expand Down Expand Up @@ -72,7 +84,7 @@ export default function(mainApp: express.Express, buildApp: express.Express, cal
});
}

function loadPlugins (systemName: string, pluginsPath: string, mainApp: express.Express, buildApp: express.Express): SupCore.PluginsInfo {
function loadLocalPlugins (systemName: string, pluginsPath: string, mainApp: express.Express, buildApp: express.Express): SupCore.PluginsInfo {
let pluginNamesByAuthor: { [author: string]: string[] } = {};
let pluginsInfo: SupCore.PluginsInfo = { list: [], paths: { editors: {}, tools: {} }, publishedBundles: [] };

Expand Down Expand Up @@ -108,57 +120,59 @@ function loadPlugins (systemName: string, pluginsPath: string, mainApp: express.

pluginNames.forEach((pluginName) => {
let pluginPath = `${pluginAuthorPath}/${pluginName}`;
loadPlugin(systemName, pluginPath, pluginAuthor, pluginName, mainApp, buildApp, pluginsInfo);
});
});

// Load data module
let dataModulePath = `${pluginPath}/data/index.js`;
if (fs.existsSync(dataModulePath)) require(dataModulePath);

// Collect plugin info
pluginsInfo.list.push(`${pluginAuthor}/${pluginName}`);
if (fs.existsSync(`${pluginPath}/public/editors`)) {
let editors = fs.readdirSync(`${pluginPath}/public/editors`);
editors.forEach((editorName) => {
if (SupCore.system.data.assetClasses[editorName] != null) {
pluginsInfo.paths.editors[editorName] = `${pluginAuthor}/${pluginName}`;
} else {
pluginsInfo.paths.tools[editorName] = `${pluginAuthor}/${pluginName}`;
}
return pluginsInfo;
}

mainApp.get(`/systems/${systemName}/plugins/${pluginAuthor}/${pluginName}/editors/${editorName}`, (req, res) => {
let language = req.cookies["supLanguage"];
let editorPath = path.join(pluginPath, "public/editors", editorName);
let localizedIndexFilename = getLocalizedFilename("index.html", language);
fs.exists(path.join(editorPath, localizedIndexFilename), (exists) => {
if (exists) res.sendFile(path.join(editorPath, localizedIndexFilename));
else res.sendFile(path.join(editorPath, `index.html`));
});
});
});
function loadPlugin(systemName: string, pluginPath: string, pluginAuthor: string, pluginName: string, mainApp: express.Express, buildApp: express.Express, pluginsInfo: SupCore.PluginsInfo) {
// Load data module
let dataModulePath = `${pluginPath}/data/index.js`;
if (fs.existsSync(dataModulePath)) require(dataModulePath);
// Collect plugin info
pluginsInfo.list.push(`${pluginAuthor}/${pluginName}`);
if (fs.existsSync(`${pluginPath}/public/editors`)) {
let editors = fs.readdirSync(`${pluginPath}/public/editors`);
editors.forEach((editorName) => {
if (SupCore.system.data.assetClasses[editorName] != null) {
pluginsInfo.paths.editors[editorName] = `${pluginAuthor}/${pluginName}`;
} else {
pluginsInfo.paths.tools[editorName] = `${pluginAuthor}/${pluginName}`;
}

// Expose public stuff
mainApp.get(`/systems/${systemName}/plugins/${pluginAuthor}/${pluginName}/locales/*.json`, (req, res) => {
let localeFile = req.path.split("/locales/")[1];
let localePath = path.join(pluginPath, "public/locales", localeFile);
fs.exists(localePath, (exists) => {
if (exists) res.sendFile(localePath);
else res.send("{}");
mainApp.get(`/systems/${systemName}/plugins/${pluginAuthor}/${pluginName}/editors/${editorName}`, (req, res) => {
let language = req.cookies["supLanguage"];
let editorPath = path.join(pluginPath, "public/editors", editorName);
let localizedIndexFilename = getLocalizedFilename("index.html", language);
fs.exists(path.join(editorPath, localizedIndexFilename), (exists) => {
if (exists) res.sendFile(path.join(editorPath, localizedIndexFilename));
else res.sendFile(path.join(editorPath, `index.html`));
});
});
});
}

for (let app of [mainApp, buildApp]) {
app.get(`/systems/${systemName}/plugins/${pluginAuthor}/${pluginName}/bundles/*.js`, (req, res) => {
let bundleFile = req.path.split("/bundles/")[1];
let bundlePath = path.join(pluginPath, "public/bundles", bundleFile);
fs.exists(bundlePath, (exists) => {
if (exists) res.sendFile(bundlePath);
else res.send("");
});
});
app.use(`/systems/${systemName}/plugins/${pluginAuthor}/${pluginName}`, express.static(`${pluginPath}/public`));
}
// Expose public stuff
mainApp.get(`/systems/${systemName}/plugins/${pluginAuthor}/${pluginName}/locales/*.json`, (req, res) => {
let localeFile = req.path.split("/locales/")[1];
let localePath = path.join(pluginPath, "public/locales", localeFile);
fs.exists(localePath, (exists) => {
if (exists) res.sendFile(localePath);
else res.send("{}");
});
});

return pluginsInfo;
for (let app of [mainApp, buildApp]) {
app.get(`/systems/${systemName}/plugins/${pluginAuthor}/${pluginName}/bundles/*.js`, (req, res) => {
let bundleFile = req.path.split("/bundles/")[1];
let bundlePath = path.join(pluginPath, "public/bundles", bundleFile);
fs.exists(bundlePath, (exists) => {
if (exists) res.sendFile(bundlePath);
else res.send("");
});
});
app.use(`/systems/${systemName}/plugins/${pluginAuthor}/${pluginName}`, express.static(`${pluginPath}/public`));
}
}

0 comments on commit 8f41df5

Please sign in to comment.