From 6f69cb7157156cb0e84c4118c455aff5cedcf028 Mon Sep 17 00:00:00 2001 From: ittechhunter Date: Tue, 30 Apr 2024 00:59:11 +0800 Subject: [PATCH] Check deploy command validation --- lib/deploy.ts | 89 +++++++++++++++++++++++++++++++------------------ lib/utils/fs.ts | 4 +-- 2 files changed, 58 insertions(+), 35 deletions(-) diff --git a/lib/deploy.ts b/lib/deploy.ts index ebb1c6d..400d319 100644 --- a/lib/deploy.ts +++ b/lib/deploy.ts @@ -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"; @@ -20,8 +20,6 @@ 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"); @@ -29,32 +27,25 @@ export async function translateForBosCli(dist: string) { 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; @@ -62,18 +53,41 @@ export async function deployAppCode(src: string, dist: string, opts: DeployOptio 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 @@ -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); } diff --git a/lib/utils/fs.ts b/lib/utils/fs.ts index 1bbc194..a925eb9 100644 --- a/lib/utils/fs.ts +++ b/lib/utils/fs.ts @@ -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) { @@ -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 };