Skip to content

Commit

Permalink
Added sshConnect and fixed buildScript
Browse files Browse the repository at this point in the history
  • Loading branch information
dev2-nomo committed Nov 21, 2023
1 parent ec33378 commit 0ddac09
Show file tree
Hide file tree
Showing 7 changed files with 201 additions and 39 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ typings/

# JetBrains
.idea/

# Cli configs
nomo_cli.config.js

# Manifest
out/nomo_manifest.json

# Webon Files
out/nomo.tar.gz
78 changes: 76 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 21 additions & 23 deletions src/build-webon/build-webon.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
import { joinDirWithFileName, logFatal } from "../util/util";
import { existsSync, mkdirSync, readdirSync, renameSync } from "fs";
import { join, resolve } from "path";
import { existsSync, mkdirSync, unlinkSync, renameSync } from "fs";
import { resolve } from "path";
import tar from "tar";

export async function buildWebOn(args: { assetDir: string }) {
checkDir(args.assetDir);

// Check if the assetDir path ends with '/out'
const isOutDir = args.assetDir.endsWith("/out");
const outDir = resolve(args.assetDir);
const outDirPath = resolve(args.assetDir, "..", "out");
console.log("outDir: " + outDir.toString());
const outDirPath = isOutDir ? args.assetDir : resolve(args.assetDir);
console.log("outDirPath: " + outDirPath.toString());

if (!isOutDir) {
console.log("outDirPath: " + outDirPath.toString());

console.log("Renaming asset directory to 'out'...");
try {
// Rename the assetDir to include '/out'
renameSync(args.assetDir, outDirPath);
console.log("Renaming asset directory to 'out'...");
} catch (error) {
console.error(`Error renaming directory: ${error}`);
return;
Expand All @@ -27,19 +22,17 @@ export async function buildWebOn(args: { assetDir: string }) {
console.log("Directories are already named correctly, no need to rename.");
}

// Create the "out" directory if it doesn't exist in the root path
if (!existsSync(outDir)) {
if (!existsSync(outDirPath)) {
console.log("Creating 'out' directory...");
mkdirSync(outDir);
mkdirSync(outDirPath);
}
// Check if the "out" directory contains required files

const requiredFiles = [
"index.html",
"nomo_icon.svg",
"nomo_manifest.json",
].map((file) => {
return join(outDirPath, file);
});
].map((file) => resolve(outDirPath, file));

const missingFiles = requiredFiles.filter((file) => !existsSync(file));

if (missingFiles.length > 0) {
Expand All @@ -51,18 +44,23 @@ export async function buildWebOn(args: { assetDir: string }) {
return;
}

// Create a tar.gz file
const tarFileName = "nomo.tar.gz";
const tarFilePath = joinDirWithFileName(outDir, tarFileName);
console.log(`Creating tar.gz file: ${getDebugPath(tarFilePath)}`);
const tarFilePath = joinDirWithFileName(outDirPath, tarFileName);

if (existsSync(tarFilePath)) {
console.log(`Deleting existing ${tarFileName}...`);
unlinkSync(tarFilePath);
}

console.log(`Creating new ${tarFileName}: ${tarFilePath}`);

// Use the tar.create method to properly await the completion
await tar.create(
{
file: tarFilePath,
gzip: true,
cwd: resolve(outDirPath, ".."),
},
[outDir]
["out"]
);

console.log("Build and packaging completed!");
Expand All @@ -75,5 +73,5 @@ function checkDir(dir: string): void {
}

function getDebugPath(path: string): string {
return `\'${resolve(path)}\'`; // Show an absolute path to users in case of errors.
return `\'${resolve(path)}\'`;
}
27 changes: 14 additions & 13 deletions src/deploy-webon/deploy-webon.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { checkNotDir, logFatal } from "../util/util";
import {
checkNotDir,
logFatal,
checkIfTarGz,
readCliConfig,
} from "../util/util";
import { connectToSSH } from "../util/ssh-manager";
import { NomoManifest, NomoCliConfigs, GeneratedFile } from "../init/interface";
import { resolve } from "path";

Expand All @@ -8,6 +14,7 @@ export async function deployWebOn(args: {
}) {
const { deployTarget, archive } = args;
checkNotDir(archive);
checkIfTarGz(archive);

const nomoCliConfig = readCliConfig();

Expand All @@ -18,6 +25,12 @@ export async function deployWebOn(args: {

const { rawSSH } = targetConfig;

try {
await connectToSSH({ deployTarget, archive });
} catch (e) {
logFatal("Failed to connect to SSH");
}

if ("sshPort" in rawSSH) {
const { sshHost, sshBaseDir, publicBaseUrl, sshPort } = rawSSH;
console.log(`Deploying to ${deployTarget}...`);
Expand All @@ -36,15 +49,3 @@ export async function deployWebOn(args: {
console.log(`Archive Path: ${archive}`);
}
}

function readCliConfig(): NomoCliConfigs {
const cliPath = resolve("./nomo_cli.config.js");
try {
// @ts-ignore
const nomoCliConfig = require(cliPath);
console.log(nomoCliConfig);
return nomoCliConfig;
} catch (e) {
logFatal("Could not find cli_config " + cliPath);
}
}
2 changes: 1 addition & 1 deletion src/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ async function getValidWebOnId(prompt: string): Promise<string> {
while (!isValidWebOnId(webonId)) {
console.error(`Invalid webon_id: ${webonId}`);
webonId = await getUserInput(
"Enter an unique valid webon_id like demo.web.app for example:"
"Enter an unique valid webon_id like demo.web.app:"
);
}
return webonId;
Expand Down
36 changes: 36 additions & 0 deletions src/util/ssh-manager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { logFatal, readCliConfig, runCommandsSequentially } from "../util/util";

let sshConnect = "";

export async function connectToSSH(args: {
deployTarget: string;
archive: string;
}) {
const { deployTarget, archive } = args;

const nomoCliConfig = readCliConfig();

const targetConfig = nomoCliConfig.deployTargets[deployTarget];
if (!targetConfig) {
logFatal(`Invalid deployTarget: ${deployTarget}`);
}

const { rawSSH } = targetConfig;

const { sshHost, sshBaseDir, publicBaseUrl, sshPort } = rawSSH;
const portOption = sshPort ? `-p ${sshPort}` : "";
sshConnect = `ssh -t ${sshHost} ${portOption}`;

const commands = [ls(), checkCreateDir(sshBaseDir)];

await runCommandsSequentially(commands);
}

function ls(): string {
return `${sshConnect} 'ls'`;
}

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

0 comments on commit 0ddac09

Please sign in to comment.