UNPKG

nwixtoolset

Version:

Node module wrapper around the WIX Toolset executables.

151 lines (150 loc) 5.02 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const cp = require("child_process"); const isWSL = require("is-wsl"); function addArgument(args, arg, value) { const valueType = typeof value; if (value === null || valueType === "undefined") { return; } if (valueType === "string") { args.push(arg, value); } else if (valueType === "boolean" && value === true) { args.push(arg); } else if (valueType === "number") { args.push(arg, value.toString()); } else if (Array.isArray(value)) { value.forEach(val => addArgument(args, arg, val)); } else if (valueType === "object") { Object.keys(value).forEach(key => { let valueArg = arg + key, valueVal = value[key]; if (typeof valueVal === "boolean") { if (!valueVal) return; } else { valueArg += "=" + valueVal; } addArgument(args, valueArg, true); }); } } exports.addArgument = addArgument; function addFixedArgument(args, arg, value) { if (undef(value)) { return; } if (Array.isArray(value)) { value.forEach(val => addFixedArgument(args, arg, val)); } else if (typeof value === "boolean") { if (value) args.push(arg); } else { args.push(arg + value); } } exports.addFixedArgument = addFixedArgument; function addMapArgument(args, arg, value) { if (undef(value)) { return; } if (Array.isArray(value)) { value.forEach(val => addMapArgument(args, arg, val)); } else { args.push(`${arg}:${value}`); } } exports.addMapArgument = addMapArgument; function addPathArgument(args, arg, value) { return __awaiter(this, void 0, void 0, function* () { value = yield ensurePaths(value); addArgument(args, arg, value); }); } exports.addPathArgument = addPathArgument; function addFixedPathArgument(args, arg, value) { return __awaiter(this, void 0, void 0, function* () { value = yield ensurePaths(value); addFixedArgument(args, arg, value); }); } exports.addFixedPathArgument = addFixedPathArgument; function addPathFile(args, file) { return __awaiter(this, void 0, void 0, function* () { file = yield ensurePaths(file); args.push(...file); }); } exports.addPathFile = addPathFile; function addCommonArguments(args, options) { addArgument(args, "-ext", options.ext); addFixedArgument(args, "-sw", options.suppressWarning); addArgument(args, "-v", options.verbose); addFixedArgument(args, "-wx", options.warningAsError); } exports.addCommonArguments = addCommonArguments; function ensurePaths(path) { return __awaiter(this, void 0, void 0, function* () { if (undef(path)) { return []; } if (!Array.isArray(path)) { path = [path]; } if (isWineEnv()) { path = yield winepath(...path); } return path; }); } exports.ensurePaths = ensurePaths; function winepath(...srcs) { return spawn("winepath", ["-w", ...srcs]) .then(({ stdout }) => stdout.split(/\r?\n/g)) .then(paths => paths.filter(p => !!p)); } exports.winepath = winepath; function undef(val) { return typeof val === "undefined"; } exports.undef = undef; function isWineEnv() { if (isWineEnv.result) return isWineEnv.result; return (isWineEnv.result = process.platform !== "win32" && !isWSL); } exports.isWineEnv = isWineEnv; function spawn(command, args, options = {}) { return new Promise((resolve, reject) => { const child = cp.spawn(command, args, options); let stdout = "", stderr = ""; if (options.stdio !== "ignore" && options.stdio !== "inherit") { child.stdout.on("data", data => { stdout += String(data); }); child.stderr.on("data", data => { stderr += String(data); }); } child.on("error", reject); child.on("close", code => { if (code !== 0) { return reject(new Error(`${command} ${args.join(" ")} exited with code ${code}`)); } resolve({ stdout, stderr }); }); }); } exports.spawn = spawn;