Permalink
Browse files

Add build-dist script

  • Loading branch information...
1 parent 5ab94bd commit 5c58f6f3d0a0dcb31a5eb1ea5bd27bdde5fbc626 @joshuafcole joshuafcole committed Oct 28, 2016
Showing with 77 additions and 34 deletions.
  1. +1 −0 .gitignore
  2. +3 −3 package.json
  3. +35 −0 scripts/build-dist.ts
  4. +38 −31 scripts/build.ts
View
@@ -42,6 +42,7 @@ luac.out
lua_modules/
*~
+dist/
build/
node_modules/
View
@@ -4,6 +4,7 @@
"@types/commonmark": "^0.22.29",
"@types/express": "^4.0.33",
"@types/glob": "^5.0.30",
+ "@types/mkdirp": "^0.3.29",
"@types/node": "^6.0.41",
"@types/request": "0.0.31",
"@types/tape": "^4.2.28",
@@ -13,6 +14,7 @@
"commonmark": "^0.26.0",
"express": "^4.14.0",
"glob": "^7.1.1",
+ "mkdirp": "^0.5.1",
"request": "^2.75.0",
"typescript": "^2.0.3",
"ws": "^1.1.1"
@@ -23,13 +25,11 @@
},
"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 ./build/src/runtime/server.js",
"server": "node ./build/src/runtime/server.js",
-
"test": "node ./build/test/all.js | faucet"
}
}
View
@@ -0,0 +1,35 @@
+import * as fs from "fs";
+import * as glob from "glob";
+import * as mkdirp from "mkdirp";
+import {build, Tracker, copy, onError} from "./build";
+
+function buildDist(callback:() => void) {
+ let tracker = new Tracker(callback);
+ build(() => {
+ mkdirp.sync("dist/build");
+ mkdirp.sync("dist/css");
+
+ copy("./index.html", "./dist/index.html", tracker.track("copy index"));
+ copy("./build/examples.js", "./dist/build/examples.js", tracker.track("copy packaged examples"));
+
+
+ for(let pattern of ["build/src/**/*.js", "build/src/**/*.js.map", "src/**/*.css", "css/**/*.css", "examples/**/*.css"]) {
+ let matches = glob.sync(pattern);
+ for(let match of matches) {
+ let pathname = match.split("/").slice(0, -1).join("/");
+
+ // @NOTE: Arghhh
+ mkdirp.sync("dist/" + pathname);
+ copy(match, "dist/" + match, tracker.track("copy build artifacts"));
+ }
+ }
+ tracker.finishedStartingTasks();
+ });
+}
+
+if(require.main === module) {
+ console.log("Building distribution folder...")
+ buildDist(() => {
+ console.log("done!")
+ });
+}
View
@@ -2,11 +2,11 @@ import * as fs from "fs";
import * as glob from "glob";
import {packageExamples} from "./package-examples";
-function onError(err) {
+export function onError(err) {
throw err;
}
-function copy(src, dest, callback) {
+export function copy(src, dest, callback) {
let destStream = fs.createWriteStream(dest)
.on("error", onError)
.on("close", callback);
@@ -18,59 +18,66 @@ function copy(src, dest, callback) {
return destStream;
}
-// old school
-// ./node_modules/.bin/tsc && cp src/*.js build/src/ && cp ./node_modules/chevrotain/lib/chevrotain.js build/src/ && npm run examples
+export class Tracker {
+ inProgress = {};
+ protected allTasksStarted = false;
-export function build(callback:() => void) {
- let inProgress = {};
- let allTasksStarted = false;
+ constructor(public callback:() => void) { }
- function checkCompletion(finishedStartingTasks = false) {
- if(finishedStartingTasks) allTasksStarted = true;
- if(!allTasksStarted) return;
+ finishedStartingTasks() {
+ this.allTasksStarted = true;
+ this.checkCompletion();
+ }
- for(let phase in inProgress) {
- if(inProgress[phase] !== 0) return;
+ checkCompletion() {
+ if(!this.allTasksStarted) return;
+
+ for(let phase in this.inProgress) {
+ if(this.inProgress[phase] !== 0) return;
}
- callback();
+ this.callback();
}
- function track(phase:string) {
- if(!inProgress[phase]) {
- inProgress[phase] = 1;
+ track(phase:string) {
+ if(!this.inProgress[phase]) {
+ this.inProgress[phase] = 1;
} else {
- inProgress[phase] += 1;
+ this.inProgress[phase] += 1;
}
return () => {
- inProgress[phase] -= 1;
- if(inProgress[phase] === 0) console.log(" - " + phase + "... done.");
- checkCompletion();
+ this.inProgress[phase] -= 1;
+ if(this.inProgress[phase] === 0) console.log(" - " + phase + "... done.");
+ this.checkCompletion();
};
}
+}
- // Copy static JS files into build.
- glob("src/*.js", (err, matches) => {
- if(err) throw err;
+// old school
+// ./node_modules/.bin/tsc && cp src/*.js build/src/ && cp ./node_modules/chevrotain/lib/chevrotain.js build/src/ && npm run examples
- for(let match of matches) {
- let relative = match.split("/").slice(1).join("/");
- copy(match, "build/src/" + relative, track("copy static files"));
- }
- })
+export function build(callback:() => void) {
+ let tracker = new Tracker(callback);
+
+ // Copy static JS files into build.
+ let matches = glob.sync("src/*.js");
+ for(let match of matches) {
+ let relative = match.split("/").slice(1).join("/");
+ copy(match, "build/src/" + relative, tracker.track("copy static files"));
+ }
// Copy node dependencies required by the browser.
let deps = [
"node_modules/chevrotain/lib/chevrotain.js"
];
for(let dep of deps) {
let base = dep.split("/").pop();
- copy(dep, "build/src/" + base, track("copy node module files"));
+ copy(dep, "build/src/" + base, tracker.track("copy node module files"));
}
// Package examples.
- packageExamples(track("package examples"));
+ packageExamples(tracker.track("package examples"));
- checkCompletion(true);
+ tracker.finishedStartingTasks();
}
if(require.main === module) {

0 comments on commit 5c58f6f

Please sign in to comment.