appcenter-cli
Version:
Command line tool for Visual Studio App Center
102 lines (101 loc) • 4.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isElectronProject = exports.isValidPlatform = exports.isValidOS = exports.runWebPackBundleCommand = exports.getElectronProjectAppVersion = exports.spawn = void 0;
const path = require("path");
const fs = require("fs");
const interaction_1 = require("../../../util/interaction");
const chalk = require("chalk");
const childProcess = require("child_process");
exports.spawn = childProcess.spawn;
function getElectronProjectAppVersion(projectRoot) {
projectRoot = projectRoot || process.cwd();
try {
// eslint-disable-next-line security/detect-non-literal-require
const projectPackageJson = require(path.join(projectRoot, "package.json"));
const projectVersion = projectPackageJson.version;
if (!projectVersion) {
throw new Error(`The package.json file in "${projectRoot}" does not have the "version" field set.`);
}
return projectVersion;
}
catch (error) {
throw new Error(`Unable to find or read "package.json" in "${projectRoot}". The "release-electron" command must be executed in a Electron project folder.`);
}
}
exports.getElectronProjectAppVersion = getElectronProjectAppVersion;
function runWebPackBundleCommand(bundleName, mode, webpackConfig, entryFile, outputFolder, sourcemapFileName, extraBundlerOptions) {
const webpackArgs = [];
const envNodeArgs = process.env.CODE_PUSH_NODE_ARGS;
if (typeof envNodeArgs !== "undefined") {
Array.prototype.push.apply(webpackArgs, envNodeArgs.trim().split(/\s+/));
}
Array.prototype.push.apply(webpackArgs, [
path.join("node_modules", "webpack-cli", "bin", "cli.js"),
"--output-filename",
bundleName,
"--output-path",
outputFolder,
"--mode",
mode,
"--entry-file",
entryFile,
...extraBundlerOptions,
]);
if (webpackConfig) {
webpackArgs.push("--config", webpackConfig);
}
if (sourcemapFileName) {
webpackArgs.push("--output-source-map-filename", sourcemapFileName);
}
interaction_1.out.text(chalk.cyan(`Running "webpack bundle" command:\n`));
const webpackProcess = exports.spawn("node", webpackArgs);
interaction_1.out.text(`node ${webpackArgs.join(" ")}`);
return new Promise((resolve, reject) => {
webpackProcess.stdout.on("data", (data) => {
interaction_1.out.text(data.toString().trim());
});
webpackProcess.stderr.on("data", (data) => {
console.error(data.toString().trim());
});
webpackProcess.on("close", (exitCode, signal) => {
if (exitCode !== 0) {
reject(new Error(`"webpack bundle" command failed (exitCode=${exitCode}, signal=${signal}).`));
}
resolve(null);
});
});
}
exports.runWebPackBundleCommand = runWebPackBundleCommand;
function isValidOS(os) {
switch (os.toLowerCase()) {
case "linux":
case "macos":
case "windows":
return true;
default:
return false;
}
}
exports.isValidOS = isValidOS;
function isValidPlatform(platform) {
return platform.toLowerCase() === "electron";
}
exports.isValidPlatform = isValidPlatform;
function isElectronProject() {
let packageJsonFilename;
let projectPackageJson;
try {
packageJsonFilename = path.join(process.cwd(), "package.json");
projectPackageJson = JSON.parse(fs.readFileSync(packageJsonFilename, "utf-8"));
}
catch (error) {
throw new Error(`Unable to find or read "package.json" in the CWD. The "release-electron" command must be executed in a Electron project folder.`);
}
const projectName = projectPackageJson.name;
if (!projectName) {
throw new Error(`The "package.json" file in the CWD does not have the "name" field set.`);
}
return (projectPackageJson.dependencies["electron"] ||
(projectPackageJson.devDependencies && projectPackageJson.devDependencies["electron"]));
}
exports.isElectronProject = isElectronProject;