@godspeedsystems/godspeed
Version:
Godspeed CLI
196 lines • 7.88 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const commander_1 = require("commander");
// import spawnSync from "cross-spawn";
const path_1 = __importDefault(require("path"));
const node_os_1 = require("node:os");
const fs_1 = __importStar(require("fs"));
const child_process_1 = require("child_process");
const inquirer_1 = __importDefault(require("inquirer"));
const program = new commander_1.Command();
const gsDevopsPluginsDir = path_1.default.join((0, node_os_1.homedir)(), ".godspeed", "devops-plugins");
const installAction = async (gsDevOpsPlugin) => {
try {
if (!fs_1.default.existsSync(gsDevopsPluginsDir)) {
fs_1.default.mkdirSync(gsDevopsPluginsDir, { recursive: true });
}
if (!(0, fs_1.existsSync)(path_1.default.join(gsDevopsPluginsDir, "package.json"))) {
(0, child_process_1.spawnSync)("npm", ["init", "--yes"], { cwd: gsDevopsPluginsDir });
}
// npm install <gsDevOpsPlugin> in the <gsDevopsPluginsDir> directory
(0, child_process_1.spawnSync)("npm", ["i", `${gsDevOpsPlugin}`], {
cwd: gsDevopsPluginsDir,
stdio: "inherit",
});
}
catch (error) {
console.error(error);
}
};
const list = program
.command("list")
.description(`list of available godspeed devops plugins`)
.option("--installed", "list of installed godspeed devops plugins")
.action(async (options) => {
if (options.installed) {
if (!(0, fs_1.existsSync)(path_1.default.join(gsDevopsPluginsDir, 'package.json'))) {
console.error("No devops-plugin is installed");
return;
}
let pluginsList;
try {
// list all the installed plugins
// if, file not found, throws error
pluginsList = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(gsDevopsPluginsDir, "package.json"), {
encoding: "utf-8",
})).dependencies;
// id package.json dont have "dependencies" key
if (!pluginsList)
throw new Error();
}
catch (error) {
console.error("There are no devops plugins installed.");
return;
}
// @ts-ignore
let { dependencies } = require(path_1.default.join(gsDevopsPluginsDir, 'package.json'));
for (const installedPlugin of Object.keys(dependencies)) {
console.log(`-> ${installedPlugin}`);
}
}
else {
// fetch the list of packages, maybe from the plugins repository
let npmSearch = (0, child_process_1.spawnSync)("npm", ["search", `@godspeedsystems/devops-plugin`, "--json"], { encoding: "utf-8" });
let availablePlugins = JSON.parse(npmSearch.stdout) || [];
let result = availablePlugins.map(item => item.name).join('\n');
console.log("List of available devops plugins:");
console.log(`-> ${result}`);
}
});
const install = program
.command("install")
.description(`install a godspeed devops plugin`)
.action(async () => {
// fetch the list of packages, maybe from the plugins repository
let npmSearch = (0, child_process_1.spawnSync)("npm", ["search", `@godspeedsystems/devops-plugin`, "--json"], { encoding: "utf-8" });
let availablePlugins = JSON.parse(npmSearch.stdout) || [];
let result = availablePlugins.map(({ name, description, version }) => ({
name,
description,
version,
}));
// list all the packages starting with plugins
const answer = await inquirer_1.default.prompt([
{
type: "list",
name: "gsDevOpsPlugin",
message: "Please select devops plugin to install.",
default: "latest",
choices: result,
loop: false,
},
]);
await installAction(answer.gsDevOpsPlugin);
});
const remove = program
.command("remove")
.description(`remove a godspeed devops plugin`)
.action(async () => {
let pluginsList;
try {
// list all the installed plugins
pluginsList = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(gsDevopsPluginsDir, 'package.json'), { encoding: 'utf-8' })).dependencies;
// id package.json dont have "dependencies" key
if (!pluginsList)
throw new Error();
}
catch (error) {
console.error('There are no devops plugins installed.');
return;
}
const answer = await inquirer_1.default.prompt([
{
type: "list",
name: "gsDevOpsPlugin",
message: "Please select devops plugin to remove.",
default: "",
choices: Object.keys(pluginsList).map((pluginName) => ({
name: pluginName,
value: pluginName,
})),
loop: false,
},
]);
// uninstallDevOpsPlugin(answer.gsDevOpsPlugin)
(0, child_process_1.spawnSync)("npm", ["uninstall", `${answer.gsDevOpsPlugin}`], {
cwd: gsDevopsPluginsDir,
stdio: "inherit",
});
});
const update = program
.command("update")
.description(`update a godspeed devops plugin`)
.action(async () => {
let pluginsList;
try {
// list all the installed plugins
// if, file not found, throws error
pluginsList = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(gsDevopsPluginsDir, "package.json"), {
encoding: "utf-8",
})).dependencies;
// id package.json dont have "dependencies" key
if (!pluginsList)
throw new Error();
}
catch (error) {
console.error("There are no devops plugins installed.");
return;
}
// ask user to select the plugin to remove
const answer = await inquirer_1.default.prompt([
{
type: "list",
name: "gsDevOpsPlugin",
message: "Please select devops plugin to update.",
default: "",
choices: Object.keys(pluginsList).map((pluginName) => ({
name: pluginName,
value: pluginName,
})),
loop: false,
},
]);
// npm install <gsDevOpsPlugin> in the <gsDevopsPluginsDir> directory
(0, child_process_1.spawnSync)("npm", ["install", `${answer.gsDevOpsPlugin}@latest`], {
cwd: gsDevopsPluginsDir,
stdio: "inherit",
});
});
exports.default = { install, list, remove, update };
//# sourceMappingURL=index.js.map