UNPKG

@fdm-monster/cli

Version:

FDM Monster CLI aids in the process of installing, updating and maintaining your FDM Monster server across windows, mac and linux.

262 lines (261 loc) 11.3 kB
#!/usr/bin/env node "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const _path = require("path"); const _child_process = require("child_process"); const _yargs = /*#__PURE__*/ _interop_require_default(require("yargs")); const _helpers = require("yargs/helpers"); const _process = /*#__PURE__*/ _interop_require_wildcard(require("process")); const _utils = require("./utils"); const _workspace = require("./workspace"); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interop_require_wildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = { __proto__: null }; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for(var key in obj){ if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } const cliCmd = "fdmm"; const packageName = "@fdm-monster/server"; const cliVersion = require("../package.json").version; (0, _yargs.default)((0, _helpers.hideBin)(_process.argv)).usage(`Usage: $0 install`).scriptName(cliCmd).demandCommand().option("npm", { alias: "n", boolean: true, default: false, description: "Fall back to npm instead of yarn" }).option("cwd", { description: "Directory to expect CLI files (advanced use)", default: (0, _path.join)(__dirname, "..") }).option("tag", { alias: [ "T", "t" ], description: "When upgrading or installing, install this version of FDM Monster (release tag)", string: true }).option("unstable", { description: "Opt in for installing unstable versions of FDM Monster", boolean: true, default: false }).command([ "install", "i" ], "install the FDM Monster service", ()=>{}, async (opts)=>{ const workspaceFolder = (0, _utils.getWorkspaceFolder)(opts.cwd); const tag = (0, _workspace.getTagOrLatest)(workspaceFolder, packageName, opts.tag, !!opts.npm); const packageSpecifier = `${packageName}@${tag}`; console.log(`Installing "${packageSpecifier}". Please wait...`); runPackageInstall(workspaceFolder, packageSpecifier, !!opts.npm); console.log("Install preparation complete!"); await installService(opts.cwd, workspaceFolder, !!opts.npm); }).command([ "upgrade", "u", "U" ], "upgrade (=download+update) the FDM Monster service to latest (or tag -T )", ()=>{}, async (opts)=>{ const workspaceFolder = (0, _utils.getWorkspaceFolder)(opts.cwd); const upgradedTag = (0, _workspace.getTagOrLatest)(workspaceFolder, packageName, opts.tag, !!opts.npm); await upgradeInstallation(opts.cwd, workspaceFolder, packageName, upgradedTag, !!opts.npm); console.log("Service update complete"); }).command([ "recreate", "R" ], "recreate the FDM Monster service (reloading code changes)", ()=>{}, async (opts)=>{ await updateService(opts.cwd, (0, _utils.getWorkspaceFolder)(opts.cwd), !!opts.npm); console.log("Service update complete"); }).command([ "remove", "r" ], "remove the FDM Monster service", ()=>{}, async (opts)=>{ console.log(`Remove FDM Monster service.`); await removeService(opts.cwd, (0, _utils.getWorkspaceFolder)(opts.cwd), false, !!opts.npm); console.log(`Service removal complete`); }).command([ "status", "s" ], "Status of the FDM Monster service", ()=>{}, (opts)=>{ const { serviceInstaller } = (0, _utils.detectServiceInstallerRequired)(true); const svc = getService(opts.cwd, (0, _utils.getWorkspaceFolder)(opts.cwd), false, !!opts.npm); console.log("Service exists: ", (0, _utils.serviceExists)(svc, serviceInstaller)); }).command([ "check", "c" ], "Check upgrades for FDM Monster", ()=>{}, (opts)=>{ console.log("tag", opts.tag); const workspaceFolder = (0, _utils.getWorkspaceFolder)(opts.cwd); const { installed, version } = (0, _workspace.getInstalledVersionSafe)(workspaceFolder, packageName); const { versions, latest } = (0, _workspace.fetchPackageVersions)(workspaceFolder, packageName, !!opts.npm); console.log(installed, version, versions, latest); }).strict().help("help").alias("h", "help").version("version", cliVersion) // the version string. .alias("version", "v").wrap(_yargs.default.terminalWidth()).epilog("For more information visit https://fdm-monster.net\nCopyright 2023 D. J. Zwart - AGPLv3 License").argv; function prepareServiceInstaller(cwd, installServiceInstallerIfMissing = true, useNpmInstead = false) { const { serviceInstaller } = (0, _utils.detectServiceInstallerRequired)(true); const serviceFolder = (0, _utils.getServiceFolder)(cwd, true); const servicePath = (0, _path.join)(serviceFolder, "node_modules", serviceInstaller); try { let { Service: ServiceTest } = require(servicePath); } catch (e) { if (installServiceInstallerIfMissing) { console.log(`Installing missing service installer '${serviceInstaller}'`); runInstall(serviceFolder, useNpmInstead); } else { console.error(`FDM Monster CLI: Could not find required package ${serviceInstaller} in ${servicePath}. This should be installed.'`); _process.exit(2023); } } return { serviceFolder, servicePath }; } function getService(cwd, workspace, installServiceInstallerIfMissing = true, useNpmInstead = false) { // Install and load the service installer const { serviceInstaller } = (0, _utils.detectServiceInstallerRequired)(true); const { servicePath } = prepareServiceInstaller(cwd, installServiceInstallerIfMissing, useNpmInstead); const { Service } = require(servicePath); // Prepare the service object const rootPath = (0, _path.join)(workspace, "node_modules", packageName, "dist"); const svc = new Service({ name: "FDM Monster", description: "The 3D Printer Farm server for managing your 100+ OctoPrints printers.", script: (0, _path.join)(rootPath, "index.js"), nodeOptions: [ "--harmony", "--max_old_space_size=4096" ], workingDirectory: workspace }); svc.on("invalidinstallation", function() { console.log("invalidinstallation"); }); svc.on("error", function() { console.log("error"); }); svc.on("alreadyinstalled", function() { console.log("This service is already installed."); }); svc.on("invalidinstallation", function() { console.log("This service had an invalid installation error."); }); svc.on("alreadyuninstalled ", function() { console.log("This service was already alreadyuninstalled "); }); svc.on("stop", function() { console.log("Service stopped. Service exists?", (0, _utils.serviceExists)(svc, serviceInstaller)); }); return svc; } async function upgradeInstallation(cwd, workspace, packageName, validSemverTag, useNpmInstead = false) { await removeService(cwd, workspace, true, useNpmInstead); downloadUpdatedPackage(workspace, packageName, validSemverTag, useNpmInstead); return await installService(cwd, workspace, useNpmInstead); } function downloadUpdatedPackage(workspace, packageName, packageVersion, useNpmInstead = false) { const packageSpecifier = `${packageName}@${packageVersion}`; console.log(`Upgrade started. Downloading and installing "${packageSpecifier}"`); runPackageInstall(workspace, packageSpecifier, useNpmInstead); console.log("Download and install completed"); } async function updateService(cwd, workspace, useNpmInstead) { await removeService(cwd, workspace, false, useNpmInstead); return await installService(cwd, workspace, useNpmInstead); } async function installService(cwd, workspace, useNpmInstead) { return new Promise((resolve, reject)=>{ const { serviceInstaller } = (0, _utils.detectServiceInstallerRequired)(true); const svc = getService(cwd, workspace, true, useNpmInstead); svc.on("error", function(error) { reject(error); }); svc.on("install", function() { console.log("Service installed. Service exists?", (0, _utils.serviceExists)(svc, serviceInstaller)); svc.start(); console.log("Service started. Service exists?", (0, _utils.serviceExists)(svc, serviceInstaller)); resolve(true); }); if ((0, _utils.serviceExists)(svc, serviceInstaller)) { console.error("Service already exists, cant install twice. Should you maybe run the uninstall or update command instead?"); _process.exit(4); } console.log(`Installing FDM Monster system service`); svc.install(); }); } async function removeService(cwd, workspace, tolerateMissing, useNpmInstead) { console.log(`Removing FDM Monster service.`); return new Promise((resolve, reject)=>{ const { serviceInstaller } = (0, _utils.detectServiceInstallerRequired)(true); const svc = getService(cwd, workspace, true, useNpmInstead); if (!(0, _utils.serviceExists)(svc, serviceInstaller)) { if (tolerateMissing) { console.log("Service does not exist. Skipping removal step."); resolve(true); } else { console.error("Service does not exist, cant run uninstall/remove."); _process.exit(5); } } svc.on("error", function(error) { reject(error); }); svc.on("uninstall", function() { console.log("Uninstall/remove complete. Service exists?", (0, _utils.serviceExists)(svc, serviceInstaller)); resolve(true); }); console.log(`Uninstalling/removing FDM Monster system service`); svc.uninstall(); }); } function runInstall(path, useNpmInstead = false) { // Installs package.json packages (0, _child_process.execSync)(useNpmInstead ? "npm i" : "yarn install", { cwd: path }); } function runPackageInstall(path, packageSpecifier, useNpmInstead = false) { (0, _child_process.execSync)(useNpmInstead ? `npm i ${packageSpecifier}` : `yarn add ${packageSpecifier}`, { cwd: path }); } //# sourceMappingURL=cli.js.map