aboutsummaryrefslogtreecommitdiff
path: root/tools/utils
diff options
context:
space:
mode:
Diffstat (limited to 'tools/utils')
-rw-r--r--tools/utils/server.ts45
-rw-r--r--tools/utils/tasks_tools.ts64
-rw-r--r--tools/utils/template-injectables.ts25
-rw-r--r--tools/utils/template-locals.ts13
4 files changed, 147 insertions, 0 deletions
diff --git a/tools/utils/server.ts b/tools/utils/server.ts
new file mode 100644
index 0000000..9e185d3
--- /dev/null
+++ b/tools/utils/server.ts
@@ -0,0 +1,45 @@
+import * as connectLivereload from 'connect-livereload';
+import * as express from 'express';
+import * as tinylrFn from 'tiny-lr';
+import * as openResource from 'open';
+import * as serveStatic from 'serve-static';
+import {resolve} from 'path';
+import {APP_BASE, APP_DEST, DOCS_DEST, LIVE_RELOAD_PORT, DOCS_PORT, PORT} from '../config';
+
+let tinylr = tinylrFn();
+
+
+export function serveSPA() {
+ let server = express();
+ tinylr.listen(LIVE_RELOAD_PORT);
+
+ server.use(
+ APP_BASE,
+ connectLivereload({ port: LIVE_RELOAD_PORT }),
+ express.static(process.cwd())
+ );
+
+ server.listen(PORT, () =>
+ openResource('http://localhost:' + PORT + APP_BASE + APP_DEST)
+ );
+}
+
+export function notifyLiveReload(e) {
+ let fileName = e.path;
+ tinylr.changed({
+ body: { files: [fileName] }
+ });
+}
+
+export function serveDocs() {
+ let server = express();
+
+ server.use(
+ APP_BASE,
+ serveStatic(resolve(process.cwd(), DOCS_DEST))
+ );
+
+ server.listen(DOCS_PORT, () =>
+ openResource('http://localhost:' + DOCS_PORT + APP_BASE)
+ );
+}
diff --git a/tools/utils/tasks_tools.ts b/tools/utils/tasks_tools.ts
new file mode 100644
index 0000000..af1a069
--- /dev/null
+++ b/tools/utils/tasks_tools.ts
@@ -0,0 +1,64 @@
+import * as gulp from 'gulp';
+import * as util from 'gulp-util';
+import * as chalk from 'chalk';
+import * as gulpLoadPlugins from 'gulp-load-plugins';
+import * as _runSequence from 'run-sequence';
+import {readdirSync, existsSync, lstatSync} from 'fs';
+import {join} from 'path';
+import {TOOLS_DIR} from '../config';
+
+const TASKS_PATH = join(TOOLS_DIR, 'tasks');
+
+// NOTE: Remove if no issues with runSequence function below.
+// export function loadTasks(): void {
+// scanDir(TASKS_PATH, (taskname) => registerTask(taskname));
+// }
+
+export function task(taskname: string, option?: string) {
+ util.log('Loading task', chalk.yellow(taskname, option || ''));
+ return require(join('..', 'tasks', taskname))(gulp, gulpLoadPlugins(), option);
+}
+
+export function runSequence(...sequence: any[]) {
+ let tasks = [];
+ let _sequence = sequence.slice(0);
+ sequence.pop();
+
+ scanDir(TASKS_PATH, taskname => tasks.push(taskname));
+
+ sequence.forEach(task => {
+ if (tasks.indexOf(task) > -1) { registerTask(task); }
+ });
+
+ return _runSequence(..._sequence);
+}
+
+// ----------
+// Private.
+
+function registerTask(taskname: string, filename?: string, option: string = ''): void {
+ gulp.task(taskname, task(filename || taskname, option));
+}
+
+// TODO: add recursive lookup ? or enforce pattern file + folder (ie ./tools/utils & ./tools/utils.ts )
+function scanDir(root: string, cb: (taskname: string) => void) {
+ if (!existsSync(root)) return;
+
+ walk(root);
+
+ function walk(path) {
+ let files = readdirSync(path);
+ for (let i = 0; i < files.length; i += 1) {
+ let file = files[i];
+ let curPath = join(path, file);
+ // if (lstatSync(curPath).isDirectory()) { // recurse
+ // path = file;
+ // walk(curPath);
+ // }
+ if (lstatSync(curPath).isFile() && /\.ts$/.test(file)) {
+ let taskname = file.replace(/(\.ts)/, '');
+ cb(taskname);
+ }
+ }
+ }
+}
diff --git a/tools/utils/template-injectables.ts b/tools/utils/template-injectables.ts
new file mode 100644
index 0000000..83ac315
--- /dev/null
+++ b/tools/utils/template-injectables.ts
@@ -0,0 +1,25 @@
+import * as slash from 'slash';
+import {join} from 'path';
+import {APP_BASE, APP_DEST, ENV} from '../config';
+
+let injectables: string[] = [];
+
+export function injectableAssetsRef() {
+ return injectables;
+}
+
+export function registerInjectableAssetsRef(paths: string[], target: string = '') {
+ injectables = injectables.concat(
+ paths
+ .filter(path => !/(\.map)$/.test(path))
+ .map(path => join(target, slash(path).split('/').pop()))
+ );
+}
+
+export function transformPath(plugins, env) {
+ return function (filepath) {
+ filepath = ENV === 'prod' ? filepath.replace(`/${APP_DEST}`, '') : filepath;
+ arguments[0] = join(APP_BASE, filepath);
+ return slash(plugins.inject.transform.apply(plugins.inject.transform, arguments));
+ };
+}
diff --git a/tools/utils/template-locals.ts b/tools/utils/template-locals.ts
new file mode 100644
index 0000000..be6b669
--- /dev/null
+++ b/tools/utils/template-locals.ts
@@ -0,0 +1,13 @@
+import {APP_BASE, APP_DEST, APP_ROOT, APP_TITLE, SYSTEM_CONFIG, VERSION} from '../config';
+
+// TODO: Add an interface to register more template locals.
+export function templateLocals() {
+ return {
+ APP_BASE,
+ APP_DEST,
+ APP_ROOT,
+ APP_TITLE,
+ SYSTEM_CONFIG,
+ VERSION
+ };
+}