From 080769d916c96facf93794d142dab1568ffb93db Mon Sep 17 00:00:00 2001 From: danbulant Date: Fri, 21 Feb 2020 20:10:08 +0100 Subject: [PATCH] Custom script running --- modules/package.js | 4 ++++ modules/parser.js | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/modules/package.js b/modules/package.js index be1d319..cf04391 100644 --- a/modules/package.js +++ b/modules/package.js @@ -109,6 +109,10 @@ module.exports = class Package { } run(args){ + var script = args.contents[0]; + if(script == "run")script = args.contents[1]; + parser.load(); + parser.start(script); } } \ No newline at end of file diff --git a/modules/parser.js b/modules/parser.js index 28d8a4f..c07e887 100644 --- a/modules/parser.js +++ b/modules/parser.js @@ -6,10 +6,36 @@ const path = require("path"); const http = require("http"); const https = require("https"); const targz = require('targz'); +const { spawn } = require('child_process'); const config = require("./config")(); var console = new Console; module.exports = class PackageParser { + start(script){ + if(!this.pkg.scripts)throw Error("No scripts are defined"); + + if(!this.pkg.scripts[script])throw Error("The script `" + script + "` is not defined in package.json"); + + console.output(""); + console.output(" > " + this.pkg.scripts[script]); + console.output(""); + + var scr = this.pkg.scripts[script].split(" "); + const child = spawn(scr.shift(), scr, { stdio: "inherit" }); + + child.on('close', (code) => { + console.output(""); + if(code){ + console.error("Script exited with code " + code + "."); + console.error("This is not a problem with BPM, there's likely output above."); + console.error(""); + console.error("Failed at `" + script + "` script of " + this.pkg.name); + } else { + console.log("Done, process exited with code 0."); + } + }); + } + load(path){ if(!path) path = process.cwd() + "/package.json";