Skip to content

Commit

Permalink
Deploy preparation
Browse files Browse the repository at this point in the history
  • Loading branch information
dev2-nomo committed Nov 21, 2023
1 parent e6d3a65 commit ec33378
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 33 deletions.
27 changes: 15 additions & 12 deletions src/deploy-webon/deploy-webon.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { checkNotDir, logFatal } from "../util/util";
import { nomoCliConfig } from "../../nomo_cli.config.js";

function isDeployTarget(
target: string
): target is keyof typeof nomoCliConfig.deployTargets {
return target in nomoCliConfig.deployTargets;
}
import { NomoManifest, NomoCliConfigs, GeneratedFile } from "../init/interface";
import { resolve } from "path";

export async function deployWebOn(args: {
deployTarget: string;
Expand All @@ -14,15 +9,11 @@ export async function deployWebOn(args: {
const { deployTarget, archive } = args;
checkNotDir(archive);

if (!isDeployTarget(deployTarget)) {
logFatal(`Invalid deployTarget: ${deployTarget}`);
return;
}
const nomoCliConfig = readCliConfig();

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

const { rawSSH } = targetConfig;
Expand All @@ -45,3 +36,15 @@ 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);
}
}
4 changes: 1 addition & 3 deletions src/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ export async function init(args: { assetDir: string }): Promise<void> {
const nomoCliConfig = ${JSON.stringify(nomoCliConfig, null, 2)};
module.exports = {
nomoCliConfig,
};`,
module.exports = nomoCliConfig;`,
});
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/init/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,19 @@ export interface NomoCliConfig {
export interface GeneratedFile {
filePath: string;
content: string;
}
}

export interface RawSSHConfig {
sshHost: string;
sshBaseDir: string;
publicBaseUrl: string;
sshPort?: number; // Make the sshPort optional
}

export interface DeployTargetConfig {
rawSSH: RawSSHConfig;
}

export interface NomoCliConfigs {
deployTargets: Record<string, DeployTargetConfig>;
}
60 changes: 43 additions & 17 deletions src/util/util.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import {
existsSync,
lstatSync,
unlinkSync
} from "fs";
import { existsSync, lstatSync, unlinkSync } from "fs";
import { join, resolve } from "path";
import semver from "semver";

let _isUnitTest:boolean = false;
let _isUnitTest: boolean = false;

export function joinDirWithFileName(dir: string, fileName: string): string {
checkDir(dir);
Expand Down Expand Up @@ -46,17 +42,14 @@ export function getDebugPath(path: string): string {
return `\'${resolve(path)}\'`; // Show an absolute path to users in case of errors.
}

export function logFatal(msg: string): void {
if(isUnitTest()) {
export function logFatal(msg: string): never {
if (isUnitTest()) {
throw new Error(`error: ${msg}`);
}
else {
console.error(`error: ${msg}`);
process.exit(1);
} else {
console.error(`error: ${msg}`);
process.exit(1);
}



// Do not exit immediately for testing purposes
}

Expand All @@ -70,13 +63,46 @@ export function nodeVersionSatisfies(feature: string, range: string): void {
if (!semver.satisfies(process.version, range)) {
logFatal(`${feature} requires node ${range}`);
}

}

export function isUnitTest() {
return _isUnitTest;
return _isUnitTest;
}

export function enableUnitTest(): void {
_isUnitTest = true;
_isUnitTest = true;
}

function isValidVersion(version: string) {
// Regular expression to validate semantic versions
const regex =
/^(\d+)\.(\d+)\.(\d+)(-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+)?$/;
return regex.test(version);
}

export function compareSemanticVersions(versionA: string, versionB: string) {
if (!isValidVersion(versionA)) {
throw new Error("Invalid semantic versionA: " + versionA);
}
if (!isValidVersion(versionB)) {
throw new Error("Invalid semantic versionB: " + versionB);
}

// Split the versions and remove any build metadata
const cleanVersionA = versionA.split("+")[0].split("-")[0];
const cleanVersionB = versionB.split("+")[0].split("-")[0];

const partsA = cleanVersionA.split(".").map(Number);
const partsB = cleanVersionB.split(".").map(Number);

for (let i = 0; i < 3; i++) {
if (partsA[i] > partsB[i]) {
return 1; // versionA is greater
}
if (partsA[i] < partsB[i]) {
return -1; // versionB is greater
}
}

return 0; // versions are equal
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"isolatedModules": true, // This option disables type-checking for files that are not explicitly specified on the command line.
"esModuleInterop": true,
"importHelpers": true,
"skipLibCheck": true, // without the skipLibCheck option, my compilation was incredibly slow
Expand Down

0 comments on commit ec33378

Please sign in to comment.