Permalink
Browse files

Pull config out into module, add npmignore, update build phase for pa…

…ckaging + npm3, fix various url-related bugs
  • Loading branch information...
1 parent 5313990 commit 2fa8531e1d87b8c1f2a6d278210ef83cec46434c @joshuafcole joshuafcole committed Nov 23, 2016
Showing with 96 additions and 60 deletions.
  1. 0 .npmignore
  2. +6 −1 bin/eve.js
  3. +1 −1 index.html
  4. +6 −5 package.json
  5. +1 −1 scripts/build-dist.ts
  6. +4 −0 scripts/build.ts
  7. +10 −2 src/client.ts
  8. +9 −0 src/config.ts
  9. +6 −2 src/ide.ts
  10. +53 −48 src/runtime/server.ts
View
No changes.
View
@@ -3,6 +3,8 @@
var path = require("path");
var fs = require("fs");
var minimist = require("minimist");
+
+var config = require("../build/src/config");
var server = require("../build/src/runtime/server");
const argv = minimist(process.argv.slice(2));
@@ -43,4 +45,7 @@ if(!filepath) {
if(internal) filepath = "examples/quickstart.eve";
}
-server.run({internal: internal, browser: browser, editor: editor, port: port, path: filepath, internal: internal, root: root, eveRoot: eveRoot});
+var opts = {internal: internal, browser: browser, editor: editor, port: port, path: filepath, internal: internal, root: root, eveRoot: eveRoot};
+config.init(opts);
+
+server.run(opts);
View
@@ -19,7 +19,7 @@
<link href="favicon.png" rel="icon" type="image/png" />
</head>
<body>
- <script type="text/javascript" src="build/examples.js"></script>
+ <script type="text/javascript" src="build/workspaces.js"></script>
<script type="text/javascript" src="build/src/uuid.js"></script>
<script type="text/javascript" src="build/src/codemirror.js"></script>
<script type="text/javascript" src="build/src/simplescrollbars.js"></script>
View
@@ -18,12 +18,13 @@
},
"scripts": {
- "install": "./node_modules/.bin/tsc",
- "postinstall": "node ./build/scripts/build.js",
- "build": "node ./build/scripts/build.js",
- "build-dist": "node ./build/scripts/build-dist.js",
- "start": "node ./bin/eve.js",
+ "build": "./node_modules/.bin/tsc && node ./build/scripts/build.js",
+ "start": "./node_modules/.bin/tsc && node ./bin/eve.js",
"server": "node ./bin/eve.js",
+
+ "prepublish": "./node_modules/.bin/tsc && node ./build/scripts/build.js",
+ "build-dist": "node ./build/scripts/build-dist.js",
+
"test": "node ./build/test/all.js | faucet"
},
@@ -31,7 +31,7 @@ function buildDist(callback:() => void) {
fs.writeFileSync("./dist/index.html", index);
//copy("./index.html", "./dist/index.html", tracker.track("copy index"));
- copy("./build/examples.js", "./dist/build/examples.js", tracker.track("copy packaged examples"));
+ copy("./build/workspaces.js", "./dist/build/workspaces.js", tracker.track("copy packaged workspaces"));
for(let pattern of ["build/src/**/*.js", "build/src/**/*.js.map", "src/**/*.css", "css/**/*.css", "examples/**/*.css"]) {
View
@@ -1,3 +1,4 @@
+import * as path from "path";
import * as fs from "fs";
import * as glob from "glob";
import {packageWorkspaces} from "./package-workspaces";
@@ -70,7 +71,10 @@ export function build(callback:() => void) {
"node_modules/chevrotain/lib/chevrotain.js"
];
for(let dep of deps) {
+ dep = path.resolve(dep);
let base = dep.split("/").pop();
+ console.log("COPYING", dep);
+ console.log("NM", fs.readdirSync("node_modules"));
copy(dep, "build/src/" + base, tracker.track("copy node module files"));
}
View
@@ -272,10 +272,19 @@ export class EveClient {
browser.init(data.code);
}
if(this.showIDE) {
+ // Ensure the URL bar is in sync with the server.
+ // @FIXME: This back and forth of control over where we are
+ // is an Escherian nightmare.
+ if(!data.path) {
+ history.pushState({}, "", window.location.pathname);
+ }
+
this.ide = new IDE();
initIDE(this);
this.ide.render();
- this.ide.loadFile(data.path, data.code);
+ if(!this.ide.loadFile(data.path, data.code)) {
+
+ }
}
onHashChange({});
}
@@ -453,7 +462,6 @@ function changeDocument() {
// @FIXME: This is not right in the non-internal case.
let docId = "/examples/quickstart.eve";
let path = "/" + location.hash.split('?')[0].split("#/")[1];
- console.log("PATH", path, location.hash);
if(path) {
if(path[path.length - 1] === "/") path = path.slice(0, -1);
docId = path;
View
@@ -0,0 +1,9 @@
+export interface Config {path?:string, browser?: boolean, port?:number, editor?: boolean, root?: string, eveRoot?: string, internal?: boolean}
+
+export var config:Config = {};
+
+export function init(opts:Config) {
+ for(let key in opts) {
+ config[key] = opts[key];
+ }
+}
View
@@ -2056,16 +2056,18 @@ export class IDE {
}, 1, true);
loadFile(docId:string, content?:string) {
+ if(!docId) return false;
// if we're not in local mode, file content is going to come from
// some other source and we should just load it directly
if(!this.local && content !== undefined) {
this.documentId = docId;
this.editor.reset();
this.notices = [];
this.loading = true;
- return this.onLoadFile(this, docId, content);
+ this.onLoadFile(this, docId, content);
+ return true;
} else if(this.loading || this.documentId === docId) {
- return;
+ return false;
}
// Otherwise we load the file from either localstorage or from the supplied
@@ -2085,6 +2087,8 @@ export class IDE {
this.notices = [];
this.loading = true;
this.onLoadFile(this, docId, code);
+
+ return true;
}
loadWorkspace(directory:string, files:{[filename:string]: string}) {
View
@@ -10,6 +10,7 @@ import * as express from "express";
import * as bodyParser from "body-parser";
import * as minimist from "minimist";
+import {config, Config} from "../config";
import {ActionImplementations} from "./actions";
import {PersistedDatabase} from "./databases/persisted";
import {HttpDatabase} from "./databases/node/http";
@@ -19,8 +20,6 @@ import {RuntimeClient} from "./runtimeClient";
import {BrowserViewDatabase, BrowserEditorDatabase, BrowserInspectorDatabase} from "./databases/browserSession";
import * as eveSource from "./eveSource";
-interface CLIOptions {path?:string, browser?: boolean, port?:number, editor?: boolean, root?: string, eveRoot?: string, internal?: boolean}
-
//---------------------------------------------------------------------
// Constants
//---------------------------------------------------------------------
@@ -39,9 +38,6 @@ const shared = new PersistedDatabase();
global["browser"] = false;
-// @FIXME: Move this to something less kludgy.
-var __kludgeConfig:CLIOptions;
-
//---------------------------------------------------------------------
// HTTPRuntimeClient
//---------------------------------------------------------------------
@@ -75,7 +71,7 @@ class HTTPRuntimeClient extends RuntimeClient {
function handleStatic(request, response) {
let url = request['_parsedUrl'].pathname;
- let roots = [".", __kludgeConfig.eveRoot];
+ let roots = [".", config.eveRoot];
let completed = 0;
let results = {};
for(let root of roots) {
@@ -99,20 +95,18 @@ function handleStatic(request, response) {
};
}
-function createExpressApp(opts:CLIOptions) {
- let filepath = opts.path;
- console.log(filepath, opts.path, opts.internal);
-
+function createExpressApp() {
+ let filepath = config.path;
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
- app.get("/build/examples.js", (request, response) => {
+ app.get("/build/workspaces.js", (request, response) => {
let packaged = eveSource.pack();
// @FIXME: Is rewriting the file really needed here..?
- fs.writeFileSync("build/examples.js", packaged);
+ fs.writeFileSync(path.join(config.eveRoot, "build/workspaces.js"), packaged);
response.setHeader("Content-Type", `application/javascript; charset=utf-8`);
response.end(packaged);
});
@@ -131,7 +125,7 @@ function createExpressApp(opts:CLIOptions) {
client.handle(request, response);
if(!client.server.handling) {
response.setHeader("Content-Type", `${contentTypes["html"]}; charset=utf-8`);
- response.end(fs.readFileSync(path.join(opts.eveRoot, "index.html")));
+ response.end(fs.readFileSync(path.join(config.eveRoot, "index.html")));
}
});
@@ -157,21 +151,19 @@ function createExpressApp(opts:CLIOptions) {
class SocketRuntimeClient extends RuntimeClient {
socket: WebSocket;
- options:CLIOptions;
- constructor(socket:WebSocket, options:CLIOptions) {
+ constructor(socket:WebSocket, inEditor:boolean) {
const dbs = {
"http": new HttpDatabase(),
"shared": shared,
}
- if(options.editor) {
+ if(inEditor) {
dbs["view"] = new BrowserViewDatabase();
dbs["editor"] = new BrowserEditorDatabase();
dbs["inspector"] = new BrowserInspectorDatabase();
}
super(dbs);
this.socket = socket;
- this.options = options;
}
send(json) {
@@ -185,21 +177,35 @@ function IDEMessageHandler(client:SocketRuntimeClient, message) {
let ws = client.socket;
let data = JSON.parse(message);
if(data.type === "init") {
- let {editor, browser} = client.options;
+ let {editor, browser} = config;
let {url, hash} = data;
let path = hash !== "" ? hash : url;
- fs.stat("." + path, (err, stats) => {
- if(err || !stats.isFile()) {
- ws.send(JSON.stringify({type: "initProgram", local: true, withIDE: editor}));
- } else {
- let content = fs.readFileSync("." + path).toString();
- ws.send(JSON.stringify({type: "initProgram", local: browser, path, code: content, withIDE: editor}));
+ // @FIXME: get doesn't really work this way atm.
+ // We need to be able to match the url to any workspace,
+ // not just eve.
+ let content = eveSource.get(path);
+ if(content) {
+ ws.send(JSON.stringify({type: "initProgram", local: browser, path, code: content, withIDE: editor}));
+ if(!browser) {
+ client.load(content, "user");
+ }
+ } else {
+ fs.stat("." + path, (err, stats) => {
+ if(!err && stats.isFile()) {
+ let content = fs.readFileSync("." + path).toString();
+ ws.send(JSON.stringify({type: "initProgram", local: browser, path, code: content, withIDE: editor}));
+ } else if(config.internal) {
+ ws.send(JSON.stringify({type: "initProgram", local: browser, path: "/examples/quickstart.eve", code: eveSource.get("quickstart.eve", "examples"), withIDE: editor}));
+ } else {
+ ws.send(JSON.stringify({type: "initProgram", local: true, withIDE: editor}));
+ }
+
if(!browser) {
client.load(content, "user");
}
- }
- });
+ });
+ }
} else if(data.type === "save"){
fs.stat("." + path.dirname(data.path), (err, stats) => {
console.log(err, stats);
@@ -221,7 +227,7 @@ function MessageHandler(client:SocketRuntimeClient, message) {
let ws = client.socket;
let data = JSON.parse(message);
if(data.type === "init") {
- let {editor, browser, path:filepath} = client.options;
+ let {editor, browser, path:filepath} = config;
// we do nothing here since the server is in charge of handling init.
let content = fs.readFileSync(filepath).toString();
ws.send(JSON.stringify({type: "initProgram", local: browser, path: filepath, code: content, withIDE: editor}));
@@ -238,11 +244,11 @@ function MessageHandler(client:SocketRuntimeClient, message) {
}
}
-function initWebsocket(wss, opts:CLIOptions) {
+function initWebsocket(wss, inEditor:boolean) {
wss.on('connection', function connection(ws) {
- let client = new SocketRuntimeClient(ws, opts);
- let handler = opts.editor ? IDEMessageHandler : MessageHandler;
- if(!opts.editor) {
+ let client = new SocketRuntimeClient(ws, inEditor);
+ let handler = inEditor ? IDEMessageHandler : MessageHandler;
+ if(!inEditor) {
// we need to initialize
}
ws.on('message', (message) => {
@@ -260,44 +266,43 @@ function initWebsocket(wss, opts:CLIOptions) {
// Go!
//---------------------------------------------------------------------
-export function run(opts:CLIOptions) {
- __kludgeConfig = opts;
- console.log(opts);
-
- if(!opts.internal) {
- eveSource.add(opts.root.split(path.sep).pop(), opts.root);
+export function run() {
+ if(!config.internal) {
+ eveSource.add(config.root.split(path.sep).pop(), config.root);
} else {
- eveSource.add("examples", path.join(opts.eveRoot, "examples"));
+ eveSource.add("examples", path.join(config.eveRoot, "examples"));
}
// @FIXME: Split these out!
- eveSource.add("eve", path.join(opts.eveRoot, "examples"));
+ eveSource.add("eve", path.join(config.eveRoot, "examples"));
// If a file was passed in, we need to make sure it actually exists
// now instead of waiting for the user to submit a request and then
// blowing up
- if(opts.path) {
+ if(config.path) {
try {
- fs.statSync(opts.path);
+ fs.statSync(config.path);
} catch(e) {
- throw new Error("Can't load " + opts.path);
+ throw new Error("Can't load " + config.path);
}
}
- let app = createExpressApp(opts);
+ let app = createExpressApp();
let server = http.createServer(app);
let WebSocketServer = require('ws').Server;
let wss = new WebSocketServer({server});
- initWebsocket(wss, opts);
+ initWebsocket(wss, config.editor);
- server.listen(opts.port, function(){
- console.log(`Eve is available at http://localhost:${opts.port}. Point your browser there to access the Eve editor.`);
+ server.listen(config.port, function(){
+ console.log(`Eve is available at http://localhost:${config.port}. Point your browser there to access the Eve editor.`);
});
// If the port is already in use, display an error message
- process.on('uncaughtException', function(err) {
+ process.on('uncaughtException', function handleAddressInUse(err) {
if(err.errno === 'EADDRINUSE') {
- console.log(`ERROR: Eve couldn't start because port ${opts.port} is already in use.\n\nYou can select a different port for Eve using the "port" argument.\nFor example:\n\n> npm start -- --port 1234`);
+ console.log(`ERROR: Eve couldn't start because port ${config.port} is already in use.\n\nYou can select a different port for Eve using the "port" argument.\nFor example:\n\n> npm start -- --port 1234`);
+ } else {
+ console.error(err);
}
process.exit(1);
});

0 comments on commit 2fa8531

Please sign in to comment.