Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Add writable data path next to the possibly read-only data path #158

Open
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion SupClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ if ((global as any).SupApp == null) {
}

// Initialize empty system
SupCore.system = new SupCore.System("", "");
SupCore.system = new SupCore.System("", "", "");

const plugins: { [contextName: string]: { [pluginName: string]: { path: string; content: any; } } } = {};

Expand Down
4 changes: 3 additions & 1 deletion SupCore/SupCore.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,13 +322,14 @@ declare namespace SupCore {
}

class System {
systemPath: string;
id: string;
folderName: string;
data: SystemData;
pluginsInfo: PluginsInfo;
serverBuild: (server: ProjectServer, buildPath: string, callback: (err: string) => void) => void;

constructor(id: string, folderName: string);
constructor(systemPath: string, id: string, folderName: string);
requireForAllPlugins(filePath: string): void;
registerPlugin<T>(contextName: string, pluginName: string, plugin: T): void;
getPlugins<T>(contextName: string): { [pluginName: string]: T };
Expand All @@ -337,6 +338,7 @@ declare namespace SupCore {
// All loaded systems (server-side only)
export const systems: { [system: string]: System };
export const systemsPath: string;
export const rwSystemsPath: string;
// The currently active system
export let system: System;

Expand Down
4 changes: 3 additions & 1 deletion SupCore/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import * as Data from "./Data";
export { Data };

export let systemsPath: string;
export let rwSystemsPath: string;

export function setSystemsPath(path: string) {
export function setSystemsPath(path: string, rwPath: string) {
systemsPath = path;
rwSystemsPath = rwPath;
}

export * from "./systems";
Expand Down
4 changes: 2 additions & 2 deletions SupCore/systems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ export class System {
pluginsInfo: SupCore.PluginsInfo;
serverBuild: (server: ProjectServer, buildPath: string, callback: (err: string) => void) => void;

constructor(public id: string, public folderName: string) {
constructor(public systemPath: string, public id: string, public folderName: string) {
this.data = new SystemData(this);
}

requireForAllPlugins(filePath: string) {
const pluginsPath = path.resolve(`${SupCore.systemsPath}/${this.folderName}/plugins`);
const pluginsPath = path.resolve(`${this.systemPath}/${this.folderName}/plugins`);

for (const pluginAuthor of fs.readdirSync(pluginsPath)) {
const pluginAuthorPath = `${pluginsPath}/${pluginAuthor}`;
Expand Down
6 changes: 3 additions & 3 deletions server/ProjectHub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export default class ProjectHub {
serversById: { [serverId: string]: ProjectServer } = {};
loadingProjectFolderName: string;

constructor(globalIO: SocketIO.Server, dataPath: string, callback: (err: Error) => any) {
constructor(globalIO: SocketIO.Server, dataPath: string, rwDataPath: string, callback: (err: Error) => any) {
this.globalIO = globalIO;
this.projectsPath = path.join(dataPath, "projects");
this.buildsPath = path.join(dataPath, "builds");
this.projectsPath = path.join(rwDataPath, "projects");
this.buildsPath = path.join(rwDataPath, "builds");

const serveProjects = (callback: ErrorCallback<NodeJS.ErrnoException>) => {
async.eachSeries(fs.readdirSync(this.projectsPath), (folderName: string, cb: (err: Error) => any) => {
Expand Down
5 changes: 4 additions & 1 deletion server/RemoteHubClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export default class RemoteHubClient extends BaseRemoteClient {
let formatVersion = SupCore.Data.ProjectManifest.currentFormatVersion;
let templatePath: string;
if (details.template != null) {
templatePath = `${SupCore.systemsPath}/${details.systemId}/public/templates/${details.template}`;
templatePath = `${SupCore.rwSystemsPath}/${details.systemId}/public/templates/${details.template}`;
if (!fs.existsSync(templatePath)) {
templatePath = `${SupCore.systemsPath}/${details.systemId}/public/templates/${details.template}`;
}
formatVersion = JSON.parse(fs.readFileSync(path.join(templatePath, `manifest.json`), { encoding: "utf8" })).formatVersion;
}

Expand Down
20 changes: 12 additions & 8 deletions server/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require("module").Module._initPaths();
/* tslint:enable */

let dataPath: string;
let rwDataPath: string;
let hub: ProjectHub = null;
let mainApp: express.Express = null;
let mainHttpServer: http.Server;
Expand All @@ -46,24 +47,25 @@ function onUncaughtException(err: Error) {
process.exit(1);
}

export default function start(serverDataPath: string) {
export default function start(serverDataPath: string, serverRwDataPath: string) {
dataPath = serverDataPath;
SupCore.log(`Using data from ${dataPath}.`);
rwDataPath = serverRwDataPath;
SupCore.log(`Using data from ${dataPath} and ${rwDataPath}.`);
process.on("uncaughtException", onUncaughtException);

loadConfig();

const { version, superpowers: { appApiVersion: appApiVersion } } = JSON.parse(fs.readFileSync(`${__dirname}/../../package.json`, { encoding: "utf8" }));
SupCore.log(`Server v${version} starting...`);
fs.writeFileSync(`${__dirname}/../../public/superpowers.json`, JSON.stringify({
fs.writeFileSync(`${rwDataPath}/superpowers.json`, JSON.stringify({
serverName: config.server.serverName,
version, appApiVersion,
hasPassword: config.server.password.length !== 0
}, null, 2));

// SupCore
(global as any).SupCore = SupCore;
SupCore.setSystemsPath(path.join(dataPath, "systems"));
SupCore.setSystemsPath(path.join(dataPath, "systems"), path.join(rwDataPath, "systems"));

// List available languages
languageIds = fs.readdirSync(`${__dirname}/../../public/locales`);
Expand All @@ -77,7 +79,7 @@ export default function start(serverDataPath: string) {
memoryStore = new expressSession.MemoryStore();

try {
const sessionsJSON = fs.readFileSync(`${__dirname}/../../sessions.json`, { encoding: "utf8" });
const sessionsJSON = fs.readFileSync(`${rwDataPath}/sessions.json`, { encoding: "utf8" });
(memoryStore as any).sessions = JSON.parse(sessionsJSON);
} catch (err) {
// Ignore
Expand Down Expand Up @@ -112,6 +114,7 @@ export default function start(serverDataPath: string) {
mainApp.get("/serverBuild", enforceAuth, serveServerBuildIndex);

mainApp.use("/projects/:projectId/*", serveProjectWildcard);
mainApp.use("/superpowers.json", express.static(`${rwDataPath}/superpowers.json`));
mainApp.use("/", express.static(`${__dirname}/../../public`));

mainHttpServer = http.createServer(mainApp);
Expand All @@ -131,6 +134,7 @@ export default function start(serverDataPath: string) {
buildApp.get("/", redirectToHub);
buildApp.get("/systems/:systemId/SupCore.js", serveSystemSupCore);

buildApp.use("/superpowers.json", express.static(`${rwDataPath}/superpowers.json`));
buildApp.use("/", express.static(`${__dirname}/../../public`));

buildApp.use((req, res, next) => {
Expand Down Expand Up @@ -166,7 +170,7 @@ export default function start(serverDataPath: string) {
function loadConfig() {
let mustWriteConfig = false;

const serverConfigPath = `${dataPath}/config.json`;
const serverConfigPath = `${rwDataPath}/config.json`;
if (fs.existsSync(serverConfigPath)) {
config.setServerConfig(JSON.parse(fs.readFileSync(serverConfigPath, { encoding: "utf8" })));
schemas.validate(config, "config");
Expand Down Expand Up @@ -277,7 +281,7 @@ function onSystemsLoaded() {
buildApp.use(handle404);

// Project hub
hub = new ProjectHub(io, dataPath, (err: Error) => {
hub = new ProjectHub(io, dataPath, rwDataPath, (err: Error) => {
if (err != null) { SupCore.log(`Failed to start server:\n${(err as any).stack}`); return; }

SupCore.log(`Loaded ${Object.keys(hub.serversById).length} projects from ${hub.projectsPath}.`);
Expand Down Expand Up @@ -323,7 +327,7 @@ function onExit() {
SupCore.log("Saving sessions...");
try {
const sessionsJSON = JSON.stringify((memoryStore as any).sessions, null, 2);
fs.writeFileSync(`${__dirname}/../../sessions.json`, sessionsJSON);
fs.writeFileSync(`${rwDataPath}/sessions.json`, sessionsJSON);
} catch (err) {
SupCore.log(`Failed to save sessions:\n${(err as any).stack}`);
hadError = true;
Expand Down
113 changes: 61 additions & 52 deletions server/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ export const pluginNameRegex = /^[A-Za-z0-9]+\/[A-Za-z0-9]+$/;

// Data path
const argv = yargs
.describe("data-path", "Path to store/read data files from, including config and projects")
.describe("data-path", "Path to read data files from")
.describe("rw-data-path", "Path to store/read data files from, including config and projects")
.describe("download-url", "Url to download a release")
.boolean("force")
.argv;
export const dataPath = argv["data-path"] != null ? path.resolve(argv["data-path"]) : path.resolve(`${__dirname}/../..`);
mkdirp.sync(dataPath);
mkdirp.sync(`${dataPath}/projects`);
mkdirp.sync(`${dataPath}/builds`);
export const rwDataPath = argv["rw-data-path"] != null ? path.resolve(argv["rw-data-path"]) : path.resolve(`${__dirname}/../..`);
mkdirp.sync(rwDataPath);
mkdirp.sync(`${rwDataPath}/projects`);
mkdirp.sync(`${rwDataPath}/builds`);
export const systemsPath = `${dataPath}/systems`;
mkdirp.sync(systemsPath);
export const rwSystemsPath = `${rwDataPath}/systems`;
mkdirp.sync(rwSystemsPath);

export const force = argv["force"];
export const downloadURL = argv["download-url"];
Expand All @@ -39,56 +42,62 @@ export const systemsById: { [id: string]: {
plugins: { [authorName: string]: { [pluginName: string]: { version: string; isDev: boolean; } } };
} } = {};

for (const entry of fs.readdirSync(systemsPath)) {
if (!folderNameRegex.test(entry)) continue;
if (!fs.statSync(`${systemsPath}/${entry}`).isDirectory) continue;

let systemId: string;
let systemVersion: string;
const systemPath = `${systemsPath}/${entry}`;
try {
const packageDataFile = fs.readFileSync(`${systemPath}/package.json`, { encoding: "utf8" });
const packageData = JSON.parse(packageDataFile);
systemId = packageData.superpowers.systemId;
systemVersion = packageData.version;
} catch (err) {
emitError(`Could not load system id from systems/${entry}/package.json:`, err.stack);
}
const allSystemsPaths = [`${rwSystemsPath}`, `${systemsPath}`];

let isDev = true;
try { if (!fs.lstatSync(`${systemPath}/.git`).isDirectory()) isDev = false; }
catch (err) { isDev = false; }

systemsById[systemId] = { folderName: entry, version: systemVersion, isDev, plugins: {} };
let pluginAuthors: string[];
try { pluginAuthors = fs.readdirSync(`${systemPath}/plugins`); } catch (err) { /* Ignore */ }
if (pluginAuthors == null) continue;

for (const pluginAuthor of pluginAuthors) {
if (builtInPluginAuthors.indexOf(pluginAuthor) !== -1) continue;
if (!folderNameRegex.test(pluginAuthor)) continue;

systemsById[systemId].plugins[pluginAuthor] = {};
for (const pluginName of fs.readdirSync(`${systemPath}/plugins/${pluginAuthor}`)) {
if (!folderNameRegex.test(pluginName)) continue;

const pluginPath = `${systemPath}/plugins/${pluginAuthor}/${pluginName}`;
if (!fs.statSync(pluginPath).isDirectory) continue;

let pluginVersion: string;
try {
const packageDataFile = fs.readFileSync(`${pluginPath}/package.json`, { encoding: "utf8" });
const packageData = JSON.parse(packageDataFile);
pluginVersion = packageData.version;
} catch (err) {
emitError(`Could not load plugin verson from systems/${entry}/${pluginAuthor}/${pluginName}/package.json:`, err.stack);
}
for (const sysPath of allSystemsPaths) {
for (const entry of fs.readdirSync(sysPath)) {
if (!folderNameRegex.test(entry)) continue;

const systemPath = `${sysPath}/${entry}`;
if (!fs.existsSync(`${systemPath}/package.json`)) continue;

let systemId: string;
let systemVersion: string;
try {
const packageDataFile = fs.readFileSync(`${systemPath}/package.json`, { encoding: "utf8" });
const packageData = JSON.parse(packageDataFile);
systemId = packageData.superpowers.systemId;
systemVersion = packageData.version;
} catch (err) {
emitError(`Could not load system id from systems/${entry}/package.json:`, err.stack);
}

let isDev = true;
try { if (!fs.lstatSync(`${systemPath}/.git`).isDirectory()) isDev = false; }
catch (err) { isDev = false; }

let isDev = true;
try { if (!fs.lstatSync(`${pluginPath}/.git`).isDirectory()) isDev = false; }
catch (err) { isDev = false; }
if (systemId in systemsById) { continue; }
systemsById[systemId] = { folderName: entry, version: systemVersion, isDev, plugins: {} };
let pluginAuthors: string[];
try { pluginAuthors = fs.readdirSync(`${systemPath}/plugins`); } catch (err) { /* Ignore */ }
if (pluginAuthors == null) continue;

systemsById[systemId].plugins[pluginAuthor][pluginName] = { version: pluginVersion, isDev };
for (const pluginAuthor of pluginAuthors) {
if (builtInPluginAuthors.indexOf(pluginAuthor) !== -1) continue;
if (!folderNameRegex.test(pluginAuthor)) continue;

systemsById[systemId].plugins[pluginAuthor] = {};
for (const pluginName of fs.readdirSync(`${systemPath}/plugins/${pluginAuthor}`)) {
if (!folderNameRegex.test(pluginName)) continue;

const pluginPath = `${systemPath}/plugins/${pluginAuthor}/${pluginName}`;
if (!fs.statSync(pluginPath).isDirectory) continue;

let pluginVersion: string;
try {
const packageDataFile = fs.readFileSync(`${pluginPath}/package.json`, { encoding: "utf8" });
const packageData = JSON.parse(packageDataFile);
pluginVersion = packageData.version;
} catch (err) {
emitError(`Could not load plugin verson from systems/${entry}/${pluginAuthor}/${pluginName}/package.json:`, err.stack);
}

let isDev = true;
try { if (!fs.lstatSync(`${pluginPath}/.git`).isDirectory()) isDev = false; }
catch (err) { isDev = false; }

systemsById[systemId].plugins[pluginAuthor][pluginName] = { version: pluginVersion, isDev };
}
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions server/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/// <reference path="index.d.ts" />

import * as yargs from "yargs";
import { dataPath } from "./commands/utils";
import { dataPath, rwDataPath } from "./commands/utils";

// Command line interface
const argv = yargs
.usage("Usage: $0 <command> [options]")
.demand(1, "Enter a command")
.describe("data-path", "Path to store/read data files from, including config and projects")
.describe("data-path", "Path to read data files from")
.describe("rw-data-path", "Path to store/read data files from, including config and projects")
.command("start", "Start the server", (yargs) => {
yargs.demand(1, 1, `The "start" command doesn't accept any arguments`).argv;
})
Expand All @@ -33,7 +34,7 @@ const command = argv._[0];
const [ systemId, pluginFullName ] = argv._[1] != null ? argv._[1].split(":") : [ null, null ];
switch (command) {
/* tslint:disable */
case "start": require("./commands/start").default(dataPath); break;
case "start": require("./commands/start").default(dataPath, rwDataPath); break;
case "registry": require("./commands/registry").default(); break;
case "install": require("./commands/install").default(systemId, pluginFullName); break;
case "uninstall": require("./commands/uninstall").default(systemId, pluginFullName); break;
Expand Down
Loading