Skip to content

Commit c2b0aff

Browse files
committed
add qol fixes
1 parent 5141fce commit c2b0aff

4 files changed

Lines changed: 94 additions & 24 deletions

File tree

app/services/LibraryDb.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
"use strict";
22

3+
import fs from "fs";
4+
import path from "path";
35
import _ from "underscore";
46

5-
import StepTemplates from "./step-templates.json";
6-
77
class LibraryDb {
8-
constructor() {
9-
this._items = _.chain(StepTemplates.items)
8+
loadTemplates() {
9+
const templatePath = path.join(__dirname, "step-templates.json");
10+
const stepTemplates = JSON.parse(fs.readFileSync(templatePath, "utf8"));
11+
12+
return _.chain(stepTemplates.items)
1013
.map(function (t) {
1114
if (t.Properties) {
1215
var script = t.Properties["Octopus.Action.Script.ScriptBody"];
@@ -41,16 +44,14 @@ class LibraryDb {
4144
return t.Name.toLowerCase();
4245
})
4346
.value();
44-
45-
this._all = _.indexBy(this._items, "Id");
4647
}
4748

4849
list(cb) {
49-
cb(null, this._items);
50+
cb(null, this.loadTemplates());
5051
}
5152

5253
get(id, cb) {
53-
var item = this._all[id];
54+
var item = _.indexBy(this.loadTemplates(), "Id")[id];
5455
cb(null, item);
5556
}
5657
}

gulpfile.babel.js

Lines changed: 80 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ import jasmineReporters from "jasmine-reporters";
3030
import jasmineTerminalReporter from "jasmine-terminal-reporter";
3131
import eventStream from "event-stream";
3232
import fs from "fs";
33+
import http from "http";
34+
import https from "https";
3335
import jsonlint from "gulp-jsonlint";
3436
import path from "path";
37+
import { spawn } from "child_process";
3538

3639
const sass = gulpSass(dartSass);
3740
const clientDir = "app";
@@ -49,6 +52,54 @@ const $ = gulpLoadPlugins({
4952
const reload = browserSync.reload;
5053
const argv = yargs.argv;
5154

55+
function openBrowser(url) {
56+
if (process.env.CI) {
57+
return;
58+
}
59+
60+
if (process.platform === "darwin") {
61+
spawn("open", [url], { detached: true, stdio: "ignore" }).unref();
62+
return;
63+
}
64+
65+
if (process.platform === "win32") {
66+
spawn("cmd", ["/c", "start", "", url], { detached: true, stdio: "ignore" }).unref();
67+
return;
68+
}
69+
70+
spawn("xdg-open", [url], { detached: true, stdio: "ignore" }).unref();
71+
}
72+
73+
function waitForServer(url, { timeoutMs = 10000, pollIntervalMs = 200 } = {}) {
74+
const parsedUrl = new URL(url);
75+
const client = parsedUrl.protocol === "https:" ? https : http;
76+
const startedAt = Date.now();
77+
78+
return new Promise((resolve) => {
79+
function tryConnect() {
80+
const request = client.get(url, (response) => {
81+
response.resume();
82+
resolve();
83+
});
84+
85+
request.on("error", () => {
86+
if (Date.now() - startedAt >= timeoutMs) {
87+
resolve();
88+
return;
89+
}
90+
91+
setTimeout(tryConnect, pollIntervalMs);
92+
});
93+
94+
request.setTimeout(pollIntervalMs, () => {
95+
request.destroy();
96+
});
97+
}
98+
99+
tryConnect();
100+
});
101+
}
102+
52103
const vendorStyles = [
53104
"node_modules/font-awesome/css/font-awesome.min.css",
54105
"node_modules/font-awesome/css/font-awesome.css.map",
@@ -323,17 +374,16 @@ function provideMissingData() {
323374
});
324375
}
325376

326-
gulp.task(
327-
"step-templates",
328-
gulp.series("tests", () => {
329-
return gulp
330-
.src("./step-templates/*.json")
331-
.pipe(provideMissingData())
332-
.pipe(concat("step-templates.json", { newLine: "," }))
333-
.pipe(insert.wrap('{"items": [', "]}"))
334-
.pipe(argv.production ? gulp.dest(`${publishDir}/app/services`) : gulp.dest(`${buildDir}/app/services`));
335-
})
336-
);
377+
gulp.task("step-templates:data", () => {
378+
return gulp
379+
.src("./step-templates/*.json")
380+
.pipe(provideMissingData())
381+
.pipe(concat("step-templates.json", { newLine: "," }))
382+
.pipe(insert.wrap('{"items": [', "]}"))
383+
.pipe(argv.production ? gulp.dest(`${publishDir}/app/services`) : gulp.dest(`${buildDir}/app/services`));
384+
});
385+
386+
gulp.task("step-templates", gulp.series("tests", "step-templates:data"));
337387

338388
gulp.task("styles:vendor", () => {
339389
return gulp.src(vendorStyles, { base: "node_modules/" }).pipe(argv.production ? gulp.dest(`${publishDir}/public/styles/vendor`) : gulp.dest(`${buildDir}/public/styles/vendor`));
@@ -426,14 +476,28 @@ gulp.task(
426476
server.start();
427477
process.chdir(`../`);
428478

429-
browserSync.init(null, {
430-
proxy: "http://localhost:9000",
431-
});
479+
browserSync.init(
480+
null,
481+
{
482+
proxy: "http://localhost:9000",
483+
open: false,
484+
},
485+
() => {
486+
waitForServer("http://localhost:9000").then(() => {
487+
openBrowser("http://localhost:9000");
488+
});
489+
}
490+
);
491+
492+
function reloadServer(done) {
493+
server.start.bind(server)();
494+
done();
495+
}
432496

433497
gulp.watch(`${clientDir}/**/*.jade`, gulp.series("build:client"));
434-
gulp.watch(`${clientDir}/**/*.jsx`, gulp.series("scripts", "copy:app"));
498+
gulp.watch(`${clientDir}/**/*.jsx`, gulp.series("scripts", "copy:app", reloadServer));
435499
gulp.watch(`${clientDir}/content/styles/**/*.scss`, gulp.series("styles:client"));
436-
gulp.watch("step-templates/*.json", gulp.series("step-templates"));
500+
gulp.watch("step-templates/*.json", gulp.series("step-templates:data"));
437501

438502
gulp.watch(`${buildDir}/**/*.*`).on("change", reload);
439503
})

server/server.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,13 @@ app.get("*", (req, res) => {
7474

7575
LibraryActions.sendTemplates(data, () => {
7676
var libraryAppHtml = ReactDOMServer.renderToStaticMarkup(<RouterContext {...renderProps} />);
77+
const browserSyncClientUrl = process.env.NODE_ENV === "development" ? `${req.protocol}://${req.hostname}:3000/browser-sync/browser-sync-client.js` : null;
7778
res.render("index", {
7879
siteKeywords: config.keywords.join(),
7980
siteDescription: config.description,
8081
reactOutput: libraryAppHtml,
8182
stepTemplates: JSON.stringify(data),
83+
browserSyncClientUrl,
8284
});
8385
});
8486
});

server/views/index.pug

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,8 @@ html
4646

4747
ga('create', 'UA-24461753-5', 'octopusdeploy.com');
4848

49+
if browserSyncClientUrl
50+
script(type='text/javascript', async=true, src=browserSyncClientUrl)
51+
4952
//- inject:js
5053
//- endinject

0 commit comments

Comments
 (0)