Skip to content

Commit

Permalink
Check deploy command validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ittechhunter committed Apr 29, 2024
1 parent 1705cfe commit 6f69cb7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 35 deletions.
89 changes: 56 additions & 33 deletions lib/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { BaseConfig, readConfig } from "@/lib/config";
import { buildApp } from "@/lib/build";
import { readWorkspace } from "@/lib/workspace";
import { Log, Network } from "@/lib/types";
import { readdir, remove, move } from "@/lib/utils/fs";
import { readdir, remove, move, pathExists } from "@/lib/utils/fs";
import { Logger } from "./logger";

const DEPLOY_DIST_FOLDER = "build";
Expand All @@ -20,60 +20,74 @@ export type DeployOptions = {

// translate files from src/widget to src
export async function translateForBosCli(dist: string) {
const translating = log.loading(`[${dist}] Translating files from src/widget to src`, LogLevels.BUILD);

const srcDir = path.join(dist, "src", "widget");
const targetDir = path.join(dist, "src");

const new_files = await readdir(srcDir).catch(() => ([]));
const original_files = await readdir(targetDir).catch(() => ([]));

for (const file of new_files) {
await move(path.join(srcDir, file), path.join(targetDir, file), { overwrite: true }).catch(() => {
translating.error(`Failed to translate: ${path.join(srcDir, file)}`);
});
await move(path.join(srcDir, file), path.join(targetDir, file), { overwrite: true });
}

for (const file of original_files) {
if (new_files.includes(file))
continue;

await remove(path.join(targetDir, file)).catch(() => {
translating.error(`Failed to remove: ${path.join(targetDir, file)}`);
});
await remove(path.join(targetDir, file));
}

translating.finish(`[${dist}] Translated successfully`);
}

// deploy the app widgets and modules
export async function deployAppCode(src: string, dist: string, opts: DeployOptions) {
const deploying = log.loading(`[${src}] Deploying app`, LogLevels.BUILD);
const fullSrc = path.resolve(src);
const fullDist = path.resolve(dist);

await buildApp(src, dist, opts.network);

await translateForBosCli(dist);

// Deploy using bos-cli;
const deploying = log.loading(`[${fullSrc}] Deploying app`, LogLevels.BUILD);

// Read config
const config = await readConfig(path.join(src, "bos.config.json"), opts.network);

const BOS_DEPLOY_ACCOUNT_ID = config.accounts.deploy || opts.deployAccountId;
const BOS_SIGNER_ACCOUNT_ID = config.accounts.signer || opts.signerAccountId;
const BOS_SIGNER_PUBLIC_KEY = opts.signerPublicKey;
const BOS_SIGNER_PRIVATE_KEY = opts.signerPrivateKey;

if (!BOS_DEPLOY_ACCOUNT_ID) {
deploying.error(`Necessary values not provided, please provide Account ID for deploy`);
return;
} else if (!BOS_SIGNER_ACCOUNT_ID) {
deploying.error(`Necessary values not provided, please provide Signer Account ID for deploy`);
return;
} else if (!BOS_SIGNER_PUBLIC_KEY || !BOS_SIGNER_PRIVATE_KEY) {
deploying.error(`Necessary values not provided, please provide private & public key for deploy`);
return;
}

// Build
await buildApp(src, dist, opts.network);

// Translate for bos cli
await log.wait(
translateForBosCli(dist),
`[${fullDist}] Translating files for bos cli`,
`[${fullDist}] Translated successfully`,
`[${fullDist}] Failed to translate`,
LogLevels.BUILD
);

// Exec bos-cli;
exec(
`cd ${dist} && npx bos components deploy "${BOS_DEPLOY_ACCOUNT_ID}" sign-as "${BOS_SIGNER_ACCOUNT_ID}" network-config "${opts.network}" sign-with-plaintext-private-key --signer-public-key "${BOS_SIGNER_PUBLIC_KEY}" --signer-private-key "${BOS_SIGNER_PRIVATE_KEY}" send`,
(error: ExecException | null, stdout: string, stderr: string) => {
if (!error) {
deploying.finish(`[${src}] App deployed successfully`);
deploying.finish(`[${fullSrc}] App deployed successfully`);
return;
}

deploying.error(error.message);
}
);
deploying.finish(`[${src}] App deployed successfully`);
}

// publish data.json to SocialDB
Expand All @@ -82,24 +96,33 @@ export async function deployAppData(src: string, config: BaseConfig) {

export async function deploy(appName: string, opts: DeployOptions) {
const src = '.';
if (!appName) {
const dist = path.join(src, DEPLOY_DIST_FOLDER);

await deployAppCode(src, dist, opts);

} else {
const { apps } = await readWorkspace(src);

const findingApp = log.loading(`Finding ${appName} in the workspace`, LogLevels.BUILD);
const appSrc = apps.find((app) => app.includes(appName));
if (!appSrc) {
findingApp.error(`Not found ${appName} in the workspace`);
// Deploy single project
if (!appName) {
if (await pathExists(path.join(src, "bos.config.json"))) { // Check if the directory has bos.config.json file
await deployAppCode(src, path.join(src, DEPLOY_DIST_FOLDER), opts);
return;
} else { // Check if the directory has bos.workspace.json file
if (await pathExists(path.join(src, "bos.workspace.json"))) {
log.error(`Please provide app name`);
return;
}
}
findingApp.finish(`Found ${appName} in the workspace`);

const dist = path.join(DEPLOY_DIST_FOLDER, appSrc);
log.error(`[${src}] bos.config.json file is not existing in the project`);
return;
}

await deployAppCode(appSrc, dist, opts);
// Deploy workspace app
const { apps } = await readWorkspace(src);

const findingApp = log.loading(`Finding ${appName} in the workspace`, LogLevels.BUILD);
const appSrc = apps.find((app) => app.includes(appName));
if (!appSrc) {
findingApp.error(`Not found ${appName} in the workspace`);
return;
}
findingApp.finish(`Found ${appName} in the workspace`);

await deployAppCode(appSrc, path.join(DEPLOY_DIST_FOLDER, appSrc), opts);
}
4 changes: 2 additions & 2 deletions lib/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { copy, readFile, lstat, readJson, writeJson, ensureDir, outputFile, readdir, remove, move } from 'fs-extra';
import { copy, readFile, lstat, readJson, writeJson, ensureDir, outputFile, readdir, remove, move, pathExists } from 'fs-extra';
import path from 'path';

async function loopThroughFiles(pwd: string, callback: (file: string) => Promise<void>) {
Expand All @@ -16,4 +16,4 @@ async function loopThroughFiles(pwd: string, callback: (file: string) => Promise
}
}

export { copy, readJson, writeJson, ensureDir, outputFile, loopThroughFiles, readFile, readdir, remove, move };
export { copy, readJson, writeJson, ensureDir, outputFile, loopThroughFiles, readFile, readdir, remove, move, pathExists };

0 comments on commit 6f69cb7

Please sign in to comment.