Skip to content
Draft
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
7 changes: 4 additions & 3 deletions lib/blank.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ blankJson.read(configPath);

switch (argv._[0]) {
case "server":
require("./server")(false);
break;
case "one":
if (!process.env.BLANK_SERVICE_REGISTRY_PORT) {
process.env.BLANK_SERVICE_REGISTRY_PORT = "2345";
}
if (!process.env.BLANK_FILE_STORE_PORT) {
process.env.BLANK_FILE_STORE_PORT = "8082";
}

require("./server")(true);
require("./server")();
break;
case "init":
require("./init")(argv._[1], force);
Expand Down
71 changes: 23 additions & 48 deletions lib/buildConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,14 @@ let lessVars = null;
let scripts = null;
let commit = "no git";
let buildTime = new Date();
// let loadModule = false;

const gitShort = () => {
return new Promise(resolve => {
git.short(res => resolve(res));
return new Promise((resolve) => {
git.short((res) => resolve(res));
});
};

// const isWinSep = path.sep === "\\";
// const normalizePath = path => {
// if (isWinSep) {
// return path.replace(/\\/g, "/");
// }

// return path;
// };

module.exports = function(configPath, watch, oneServer) {
module.exports = function (configPath, watch) {
babelRegister({
only: [new RegExp(path.resolve(configPath))],
plugins: [reactJSX],
Expand All @@ -68,7 +58,7 @@ module.exports = function(configPath, watch, oneServer) {
}

usedModules = Object.keys(require.cache);
settings = loadSettings(configPath, oneServer);
settings = loadSettings(configPath);

console.log(`Building blank from: ${configPath}`);
return prepareConfig(configPath)
Expand All @@ -80,7 +70,7 @@ module.exports = function(configPath, watch, oneServer) {
return createWatchers(configPath);
}
})
.catch(err => {
.catch((err) => {
console.error(err);
throw new Error(err);
});
Expand All @@ -104,7 +94,7 @@ module.exports.makeConfig = function makeConfig(configPath, output) {
.then(() => {
return postLib();
})
.catch(err => {
.catch((err) => {
console.error(err);
throw new Error("[makeConfig]: " + err);
});
Expand All @@ -121,24 +111,26 @@ function createWatchers(configPath) {
ignoreInitial: true,
ignored: [/var\//, /tests\//, /lib\//, /interfaces\//, /assets\//, /dist\//],
});
configWatcher.on("change", function() {
configWatcher.on("change", function () {
clearTimeout(configTimer);
configTimer = setTimeout(() => prepareConfig(configPath), 500);
});

const libWatcher = chokidar.watch(settings.libPaths, { persistent: true, ignoreInitial: true });
libWatcher.on("change", function(_path) {
libWatcher.on("change", function (_path) {
clearTimeout(libTimer);
libTimer = setTimeout(() => postLib(), 500);
});

const assetsWatchPaths = settings.assetsPaths.map(p => p.path || (typeof p === "string" && p) || "").filter(p => p);
const assetsWatchPaths = settings.assetsPaths
.map((p) => p.path || (typeof p === "string" && p) || "")
.filter((p) => p);
console.log("[assetsWatchPaths]", assetsWatchPaths);
const assetsWatcher = chokidar.watch(assetsWatchPaths, {
persistent: true,
ignoreInitial: true,
});
assetsWatcher.on("change", function(_path) {
assetsWatcher.on("change", function (_path) {
console.log("[assetsWatcher] change");
clearTimeout(assetsTimer);
assetsTimer = setTimeout(() => postAssets(), 500);
Expand Down Expand Up @@ -170,7 +162,7 @@ function prepareConfig(configPath) {
const config = {};
buildTime = new Date();
return gitShort()
.then(res => {
.then((res) => {
commit = res;
console.log("Loading default config");
loadFromFolder(config, defaultConfigPath);
Expand All @@ -183,7 +175,7 @@ function prepareConfig(configPath) {
babelifyConfig(config);
return ship(config);
})
.catch(err => {
.catch((err) => {
throw new Error("[buildConfig:prepareConfig]: " + err);
});
}
Expand Down Expand Up @@ -230,11 +222,11 @@ function updateAssets(config) {
console.log("Building LESS...");
lessVars = _lessVars;
const p = buildLess(lessVars, webAppPath).then(
r => {
(r) => {
console.log("LESS build OK");
less = r.css;
},
err => {
(err) => {
console.warn("LESS build error:", err);
}
);
Expand All @@ -244,7 +236,7 @@ function updateAssets(config) {
const _scripts = config._commonSettings.entries.scripts || [];
if (JSON.stringify(_scripts) !== JSON.stringify(scripts)) {
scripts = _scripts;
const p = loadIndex().then(_html => {
const p = loadIndex().then((_html) => {
let scriptsHtml = "";
for (let s of scripts) {
scriptsHtml += `<script type="text/javascript" src="${s.src}"></script>\n`;
Expand Down Expand Up @@ -287,7 +279,7 @@ function ship(config) {
Object.assign(srUrl, {
method: "POST",
}),
res => {
(res) => {
console.log("Post 'config' to service registry result: ", res.statusCode, "/", res.statusMessage);
}
);
Expand All @@ -296,7 +288,7 @@ function ship(config) {
} else {
const confOutput = path.resolve(settings.output);
return new Promise((resolve, reject) => {
mkdirp(confOutput, function(err) {
mkdirp(confOutput, function (err) {
if (err) {
console.error(err);
return reject(err);
Expand All @@ -310,7 +302,7 @@ function ship(config) {
const outputFile = path.normalize(confOutput + path.sep + "config.json");
console.log(new Date(), "Writing config to: ", outputFile);

fs.writeFile(outputFile, configString, function(err) {
fs.writeFile(outputFile, configString, function (err) {
if (err) {
console.log(err);
return reject(err);
Expand All @@ -321,7 +313,7 @@ function ship(config) {
});
});
})
.catch(err => {
.catch((err) => {
throw new Error("[buildConfig:ship]: " + err);
});
}
Expand All @@ -330,7 +322,7 @@ function ship(config) {
function stringifyConfig(config) {
return JSON.stringify(
config,
function(key, val) {
function (key, val) {
return stringifyFunction(val);
},
" "
Expand Down Expand Up @@ -412,7 +404,7 @@ function loadFromFolder(config, configPath, recursive) {
}
}

function loadSettings(configPath, oneServer) {
function loadSettings(configPath) {
const settings = blankJson.read(configPath);
settings.libPaths = Array.isArray(settings.lib.path) ? settings.lib.path : ["./lib"];
settings.assetsPaths = Array.isArray(settings.assets.path) ? settings.assets.path : ["./assets"];
Expand Down Expand Up @@ -446,23 +438,6 @@ function loadSettings(configPath, oneServer) {
return settings;
}

// function findLoadModuleConfig(config) {
// for (let key of Object.keys(config)) {
// if (key === "loadModule") {
// loadModule = true;
// break;
// }
// try {
// let value = config[key];
// if (value && typeof value == "object") {
// findLoadModuleConfig(value);
// }
// } catch (error) {
// console.error("key", key, config[key], error);
// }
// }
// }

function loadIndex() {
const indexPath = path.join(webAppPath, "src", "html", "index.html");

Expand Down
38 changes: 18 additions & 20 deletions lib/downloadBin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ const mkdirp = require("mkdirp");
const { goPackageNames } = require("./packageNames");
const fetch = require("node-fetch");

module.exports = function(packageDirname, update) {
module.exports = function (packageDirname, update) {
let binExists = true;
const binPath = path.join(packageDirname, "bin");
try {
binExists = binExists && fs.statSync(binPath).isDirectory();
binExists = binExists && fs.statSync(path.join(binPath, "blank-sr")).isFile();
binExists = binExists && fs.statSync(path.join(binPath, "blank-router")).isFile();
binExists = binExists && fs.statSync(path.join(binPath, "blank-one")).isFile();
} catch (err) {
binExists = false;
}
Expand Down Expand Up @@ -38,45 +37,44 @@ module.exports = function(packageDirname, update) {
});
};

const getArch = () => {
const machineArch = process.arch;
if (machineArch === "x64") return "amd64";
return process.arch;
};

function downloadPackage(binPath, name) {
if (process.arch !== "x64") {
throw new Error(`unsupported platform ${process.platform}/${process.arch}`);
}
const arch = getArch();

const ext = process.platform === "win32" ? ".exe" : "";
return getLastVersionOfPackage(name)
.then(version => {
const downloadUrl = `https://github.com/getblank/${name}/releases/download/${version}/${name}-${
process.platform
}-amd64${ext}`;
.then((version) => {
const downloadUrl = `https://github.com/getblank/${name}/releases/download/${version}/${name}-${process.platform}-${arch}${ext}`;
console.info(downloadUrl);

return download(downloadUrl, path.join(binPath, `${name}${ext}`));
})
.then(() => {
console.log("Download complete:", `${name}${ext}`);
})
.catch(err => {
.catch((err) => {
console.log("Download failed:", `${name}${ext}`, err);
});
}

function download(url, dest) {
return fetch(url)
.then(res => {
.then((res) => {
const file = fs.createWriteStream(dest, {
mode: 0o776,
});

return new Promise(resolve => {
return new Promise((resolve) => {
res.body.on("end", resolve);
res.body.pipe(
file,
{ end: false }
);
res.body.pipe(file, { end: false });
});
})
.catch(err => {
.catch((err) => {
fs.unlink(dest);

throw err;
Expand All @@ -85,10 +83,10 @@ function download(url, dest) {

function getLastVersionOfPackage(packageName) {
return fetch(`https://api.github.com/repos/getblank/${packageName}/releases/latest`)
.then(res => {
.then((res) => {
return res.json();
})
.then(json => {
.then((json) => {
return json.tag_name;
});
}
4 changes: 2 additions & 2 deletions lib/packageNames.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
module.exports.goPackageNames = ["blank-sr", "blank-router", "blank-cron", "blank-fs", "blank-queue", "blank-one"];
module.exports.nodePackageNames = ["blank-node-worker"];
module.exports.goPackageNames = ["blank-one", "blank-fs"];
module.exports.nodePackageNames = ["blank-node-worker"];
Loading