Skip to content

Commit

Permalink
Configured named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
dev2-nomo committed Nov 28, 2023
1 parent 72fbc03 commit c0132ce
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 99 deletions.
48 changes: 32 additions & 16 deletions src/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from "path";
import { NomoManifest, NomoCliConfig, GeneratedFile } from "./interface";
import { isValidWebOnId } from "../util/validate-manifest";

async function getUserInput(prompt: string): Promise<string> {
async function getUserInput({ prompt }: { prompt: string }): Promise<string> {
const inquirer = require("inquirer");

const { userInput } = await (inquirer as any).prompt([
Expand All @@ -17,10 +17,13 @@ async function getUserInput(prompt: string): Promise<string> {
return userInput;
}

async function generateNomoManifestContent(
webonId: string,
webonName: string
): Promise<NomoManifest> {
async function generateNomoManifestContent({
webonId,
webonName,
}: {
webonId: string;
webonName: string;
}): Promise<NomoManifest> {
return {
nomo_manifest_version: "1.1.0",
webon_id: webonId,
Expand All @@ -30,7 +33,11 @@ async function generateNomoManifestContent(
};
}

function generateNomoCliConfigContent(webonId: string): NomoCliConfig {
function generateNomoCliConfigContent({
webonId,
}: {
webonId: string;
}): NomoCliConfig {
return {
deployTargets: {
production: {
Expand Down Expand Up @@ -67,10 +74,15 @@ export async function init(args: { assetDir: string }): Promise<void> {
if (fs.existsSync(manifestFilePath)) {
console.log("nomo_manifest.json already exists.");
} else {
const webonName = await getUserInput("Enter webon_name: ");
const webonId = await getValidWebOnId("Enter unique webon_id: ");
const webonName = await getUserInput({ prompt: "Enter webon_name: " });
const webonId = await getValidWebOnId({
prompt: "Enter unique webon_id: ",
});

const nomoManifest = await generateNomoManifestContent(webonId, webonName);
const nomoManifest = await generateNomoManifestContent({
webonId: webonId,
webonName: webonName,
});

writeFile({
filePath: manifestFilePath,
Expand All @@ -86,7 +98,7 @@ export async function init(args: { assetDir: string }): Promise<void> {
const nomoManifest: NomoManifest = JSON.parse(nomoManifestContent);

const webonId = nomoManifest.webon_id;
const nomoCliConfig = generateNomoCliConfigContent(webonId);
const nomoCliConfig = generateNomoCliConfigContent({ webonId: webonId });

writeFile({
filePath: cliConfigFilePath,
Expand All @@ -101,13 +113,17 @@ module.exports = nomoCliConfig;`,
}
}

async function getValidWebOnId(prompt: string): Promise<string> {
let webonId = await getUserInput(prompt);
while (!isValidWebOnId(webonId)) {
async function getValidWebOnId({
prompt,
}: {
prompt: string;
}): Promise<string> {
let webonId = await getUserInput({ prompt: prompt });
while (!isValidWebOnId({ webon_id: webonId })) {
console.error(`Invalid webon_id: ${webonId}`);
webonId = await getUserInput(
"Enter an unique valid webon_id like demo.web.app:"
);
webonId = await getUserInput({
prompt: "Enter an unique valid webon_id like demo.web.app:",
});
}
return webonId;
}
53 changes: 34 additions & 19 deletions src/services/ssh-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,30 @@ export async function connectAndDeploy(args: {
await validateDeploymentConfig(deployTarget, targetConfig.rawSSH);

const commands = [
sshOperations.checkCreateDir(sshBaseDir),
sshOperations.checkSshBaseDirExists(sshBaseDir),
sshOperations.deployManifest(
manifestPath,
targetConfig.rawSSH.sshHost,
sshBaseDir
),
sshOperations.deployFile(iconPath, targetConfig.rawSSH.sshHost, sshBaseDir),
sshOperations.deployFile(archive, targetConfig.rawSSH.sshHost, sshBaseDir),
sshOperations.checkCreateDir({ sshBaseDir: sshBaseDir }),
sshOperations.checkSshBaseDirExists({ sshBaseDir: sshBaseDir }),
sshOperations.deployManifest({
filePath: manifestPath,
sshHost: targetConfig.rawSSH.sshHost,
sshBaseDir: sshBaseDir,
}),
sshOperations.deployFile({
filePath: iconPath,
sshHost: targetConfig.rawSSH.sshHost,
sshBaseDir: sshBaseDir,
}),
sshOperations.deployFile({
filePath: archive,
sshHost: targetConfig.rawSSH.sshHost,
sshBaseDir: sshBaseDir,
}),
];

await runCommandsSequentially(commands);
await runCommandsSequentially({ commands: commands });

const deploymentSuccessful = await Promise.all(
commands.map(async (command) => {
const result = await runCommand(command);
const result = await runCommand({ cmd: command });
return result !== "not_found";
})
);
Expand Down Expand Up @@ -82,17 +90,24 @@ async function validateDeploymentConfig(deployTarget: string, rawSSH: any) {
logFatal(`Invalid sshPort: ${sshPort}`);
}

const sshOperations = new SSHOperations(rawSSH.sshHost, sshPort);
const sshOperations = new SSHOperations({
sshHost: rawSSH.sshHost,
sshPort: sshPort,
});

const serverWebOnId = await runCommand(
sshOperations.getWebonIdIfExists(sshBaseDir)
);
const serverWebOnId = await runCommand({
cmd: sshOperations.getWebonIdIfExists({ sshBaseDir: sshBaseDir }),
});

const serverWebOnVersion = await runCommand(
sshOperations.getWebonVersionIfExists(sshBaseDir)
);
const serverWebOnVersion = await runCommand({
cmd: sshOperations.getWebonVersionIfExists({ sshBaseDir: sshBaseDir }),
});

manifestChecks(manifestPath, serverWebOnVersion, serverWebOnId);
manifestChecks({
manifestFilePath: manifestPath,
serverWebOnVersion: serverWebOnVersion,
serverWebOnId: serverWebOnId,
});

return { sshOperations, sshBaseDir, publicBaseUrl };
}
94 changes: 60 additions & 34 deletions src/services/ssh-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@ import * as path from "path";
export class SSHOperations {
private sshConnect: string = "";

constructor(sshHost: string, sshPort?: number) {
this.sshConnect = this.createSSHConnectCommand(sshHost, sshPort);
constructor({ sshHost, sshPort }: { sshHost: string; sshPort?: number }) {
this.sshConnect = this.createSSHConnectCommand({
sshHost: sshHost,
sshPort: sshPort,
});
}

private createSSHConnectCommand(sshHost: string, sshPort?: number): string {
private createSSHConnectCommand({
sshHost,
sshPort,
}: {
sshHost: string;
sshPort?: number;
}): string {
const portOption = sshPort ? `-p ${sshPort}` : "";
return `ssh -t ${sshHost} ${portOption}`;
}
Expand All @@ -16,41 +25,58 @@ export class SSHOperations {
return `${this.sshConnect} 'ls'`;
}

public checkCreateDir(sshBaseDir: string): string {
public checkCreateDir({ sshBaseDir }: { sshBaseDir: string }) {
const mkdirCommand = `if [ ! -d ${sshBaseDir} ]; then mkdir -p ${sshBaseDir} && echo "Directory created"; else echo "Directory already exists"; fi`;
return `${this.sshConnect} "${mkdirCommand}"`;
}

private scpCommand(
filePath: string,
sshHost: string,
sshBaseDir: string,
port?: number
): string {
private scpCommand({
filePath,
sshHost,
sshBaseDir,
port,
}: {
filePath: string;
sshHost: string;
sshBaseDir: string;
port?: number;
}): string {
const absolutePath = path.resolve(filePath);
return `scp ${
port ? `-P ${port}` : ""
} ${absolutePath} ${sshHost}:${sshBaseDir} && echo "File deployed: ${filePath}"`;
}

public deployFile(
filePath: string,
sshHost: string,
sshBaseDir: string
): string {
return `${this.scpCommand(filePath, sshHost, sshBaseDir)}`;
public deployFile({
filePath,
sshHost,
sshBaseDir,
}: {
filePath: string;
sshHost: string;
sshBaseDir: string;
}) {
return `${this.scpCommand({
filePath: filePath,
sshHost: sshHost,
sshBaseDir: sshBaseDir,
})}`;
}

public deployManifest(
filePath: string,
sshHost: string,
sshBaseDir: string
): string {
const manifestDeployCommand = this.scpCommand(
filePath,
sshHost,
sshBaseDir
);
public deployManifest({
filePath,
sshHost,
sshBaseDir,
}: {
filePath: string;
sshHost: string;
sshBaseDir: string;
}) {
const manifestDeployCommand = this.scpCommand({
filePath: filePath,
sshHost: sshHost,
sshBaseDir: sshBaseDir,
});
// Rename the file to "manifest" on the remote server
const renameManifestCommand = `${this.sshConnect} "mv ${path.join(
sshBaseDir,
Expand All @@ -60,11 +86,11 @@ export class SSHOperations {
return `${manifestDeployCommand} && ${renameManifestCommand}`;
}

public executeCommand(command: string): string {
public executeCommand({ command }: { command: string }): string {
return command;
}

public getWebonVersionIfExists(sshBaseDir: string): string {
public getWebonVersionIfExists({ sshBaseDir }: { sshBaseDir: string }) {
const checkManifestCommand = `${this.sshConnect} "[ -e ${path.join(
sshBaseDir,
"manifest"
Expand All @@ -73,10 +99,10 @@ export class SSHOperations {
"manifest"
)} | jq -r .webon_version || echo 'not_found'"`;

return this.executeCommand(checkManifestCommand);
return this.executeCommand({ command: checkManifestCommand });
}

public getWebonIdIfExists(sshBaseDir: string): string {
public getWebonIdIfExists({ sshBaseDir }: { sshBaseDir: string }) {
const checkManifestCommand = `${this.sshConnect} "[ -e ${path.join(
sshBaseDir,
"manifest"
Expand All @@ -85,17 +111,17 @@ export class SSHOperations {
"manifest"
)} | jq -r .webon_id || echo 'not_found'"`;

return this.executeCommand(checkManifestCommand);
return this.executeCommand({ command: checkManifestCommand });
}
public checkSshBaseDirExists(sshBaseDir: string): string {
public checkSshBaseDirExists({ sshBaseDir }: { sshBaseDir: string }) {
const checkDirCommand = `${this.sshConnect} "[ -d ${sshBaseDir} ] && echo 'sshDir exists' || echo 'not_found'"`;
return this.executeCommand(checkDirCommand);
return this.executeCommand({ command: checkDirCommand });
}
}

export function executeCommand(
command: string,
sshCommands: SSHOperations
): string {
return sshCommands.executeCommand(command);
return sshCommands.executeCommand({ command: command });
}
22 changes: 15 additions & 7 deletions src/util/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export function getDebugPath(path: string): string {

export function logFatal(msg: string): never {
if (isUnitTest()) {
throw new Error(`error: ${msg}`);
throw new Error(`ERROR: ${msg}`);
} else {
console.error("\x1b[31m", `error: ${msg}`);
console.error("\x1b[31m", `ERROR: ${msg}`);
process.exit(1);
}

Expand Down Expand Up @@ -127,7 +127,13 @@ export function compareSemanticVersions(versionA: string, versionB: string) {
return 0; // versions are equal
}

export function runCommand(cmd: string, pwd?: string): Promise<string> {
export function runCommand({
cmd,
pwd,
}: {
cmd: string;
pwd?: string;
}): Promise<string> {
console.log(`Run command \'${cmd}\'`);
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
Expand All @@ -143,10 +149,12 @@ export function runCommand(cmd: string, pwd?: string): Promise<string> {
});
}

export async function runCommandsSequentially(
commands: string[]
): Promise<void> {
export async function runCommandsSequentially({
commands,
}: {
commands: string[];
}): Promise<void> {
for (const command of commands) {
await runCommand(command);
await runCommand({ cmd: command });
}
}
Loading

0 comments on commit c0132ce

Please sign in to comment.