Skip to content

Commit

Permalink
044 Using concurrent for multi process nodejs
Browse files Browse the repository at this point in the history
  • Loading branch information
giapdong committed Sep 13, 2020
1 parent cff62bf commit 90b8f26
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
32 changes: 26 additions & 6 deletions bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,33 @@ program
.command("open")
.description("Open webapp for UI/UX application")
.action(async (cmd) => {
let packageStart = package.scripts.start.split(" ");
let pathAbsoluteServer = path.resolve(__dirname, "../", packageStart[1]);
let commandExec = `npx ${packageStart[0]} ${pathAbsoluteServer}`;
const { spawn } = require("child_process");

let { stdout, stderr } = await exec(commandExec);
stdout.on("data", (data) => {
console.log(data.toString());
let startScript = package.scripts.start.split(/\s+/);
let rawScript = startScript.slice(1);
rawScript = rawScript.map(
(item) =>
item
.replace(/[:']/g, " ")
.trim()
.split(/\s+/)[1]
);
rawScript = rawScript.map((item) => ({
name: item,
script: package.scripts[item].split(/\s+/),
}));
rawScript = rawScript.map((item) => {
item.script[0] = item.script[0] == "nodemon" ? "node" : item.script[0];
item.script[1] = path.resolve(__dirname, "..", item.script[1]);
item.script = item.script.join(" ");
return item;
});

rawScript.forEach((element) => {
var child = spawn(element.script, { shell: true });
child.stdout.on("data", (data) => {
console.log(`[${element.name}]`.success, data.toString());
});
});
});

Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "disk-management",
"version": "0.2.10",
"version": "0.3.0",
"description": "Module/application for management your disk",
"main": "index.js",
"license": "MIT",
"author": "Giap Dong",
"scripts": {
"start": "concurrently 'npm:api-start' 'npm:client-start'",
"api-start": "nodemon ./webapp/api/bin/www",
"client-start": "node ./webapp/client/bin/www",
"start": "concurrently 'npm:api' 'npm:client'",
"api": "nodemon ./webapp/api/bin/www",
"client": "node ./webapp/client/bin/www",
"build": "cross-env NODE_ENV=production webpack --config ./webapp/client/webpack.prod.js"
},
"dependencies": {
Expand Down

0 comments on commit 90b8f26

Please sign in to comment.