From 65f190f4f50d10af831949366fe4753bf371cbd4 Mon Sep 17 00:00:00 2001 From: stefano Date: Thu, 8 Aug 2024 16:14:26 +0100 Subject: [PATCH] Merge uplaod functions + README --- README.md | 10 ++++- lib/cli.ts | 22 +---------- lib/deploy.ts | 104 +++++++++----------------------------------------- 3 files changed, 29 insertions(+), 107 deletions(-) diff --git a/README.md b/README.md index e64db19..b4b5730 100644 --- a/README.md +++ b/README.md @@ -291,7 +291,15 @@ Commands: **Command:** `deploy` -Deploys an app in the workspace via a convenient wrapper to [bos-cli-rs](https://github.com/bos-cli-rs/bos-cli-rs). +Deploys an app in the workspace via a convenient wrapper to [bos-cli-rs](https://github.com/bos-cli-rs/bos-cli-rs). It's also possible to add an optional string array in the `bos.config.json` to specify the data to upload: + +``` + "data": { + "include": ["folder"] + } +``` + +The upload script will bundle all the json files inside the specified folder and upload the data with the app. ```cmd bw deploy [app name] --deploy-account-id [deployAccountId] --signer-account-id [signerAccountId] --signer-public-key [signerPublicKey] --signer-private-key [signerPrivateKey] diff --git a/lib/cli.ts b/lib/cli.ts index 01246b8..0c576f9 100644 --- a/lib/cli.ts +++ b/lib/cli.ts @@ -2,7 +2,7 @@ import { buildApp } from "@/lib/build"; import { initProject } from "@/lib/init"; import { Logger, LogLevel } from "@/lib/logger"; import { Command } from "commander"; -import { deploy, deployAppData, deployAppDataFolders, DeployOptions } from "./deploy"; +import { deploy, deployAppData, DeployOptions } from "./deploy"; import { dev } from "./dev"; import { cloneRepository } from "./repository"; import { buildWorkspace, devWorkspace } from "./workspace"; @@ -143,26 +143,6 @@ async function run() { }) }); - program - .command("uploadData") - .description("Upload data to SocialDB from bos.config.json configuration") - .argument("[appName]", "Workspace app name to deploy") - .option("-n, --network ", "network to deploy to", "mainnet") - .option("--signerPublicKey ", "Signer public key") - .option("--signerPrivateKey ", "Signer private key") - .action(async (appName, options) => { - const deployOptions: DeployOptions = { - signerPublicKey: options.signerPublicKey, - signerPrivateKey: options.signerPrivateKey, - network: options.network, - deployAccountId: options.deployAccountId, - }; - - await deployAppDataFolders(appName, deployOptions).catch((e: Error) => { - log.error(e.stack || e.message); - }) - }); - program.parse(); } diff --git a/lib/deploy.ts b/lib/deploy.ts index 21d30d1..9447be9 100644 --- a/lib/deploy.ts +++ b/lib/deploy.ts @@ -108,75 +108,7 @@ export async function deployAppCode(src: string, dist: string, opts: DeployOptio }); } -export async function deployAppData(appName: string, opts: DeployOptions) { - const config = await readConfig(path.join(appName, "bos.config.json"), opts.network); - const BOS_SIGNER_ACCOUNT_ID = config.accounts.signer || opts.signerAccountId || config.account; - - if (!BOS_SIGNER_ACCOUNT_ID) { - console.log(`App account is not defined for ${appName}. Skipping data upload`); - return; - } - - const dataJSON = fs.readFileSync( - path.join(appName, DEPLOY_DIST_FOLDER, "data.json"), - "utf8" - ); - - const args = { data: JSON.parse(dataJSON) }; - const argsBase64 = Buffer.from(JSON.stringify(args)).toString("base64"); - - const BOS_SIGNER_PUBLIC_KEY = opts?.signerPublicKey; - const BOS_SIGNER_PRIVATE_KEY = opts?.signerPrivateKey; - - const automaticSignIn = [ - "sign-with-plaintext-private-key", - "--signer-public-key", - BOS_SIGNER_PUBLIC_KEY, - "--signer-private-key", - BOS_SIGNER_PRIVATE_KEY, - "send" - ] - - let command = [ - "near-cli-rs", - "contract", - "call-function", - "as-transaction", - opts.network === "mainnet" ? SOCIAL_CONTRACT.mainnet : SOCIAL_CONTRACT.testnet, - "set", - "base64-args", - `${argsBase64}`, - "prepaid-gas", - "300 TeraGas", - "attached-deposit", - "0.15 NEAR", // deposit - "sign-as", - BOS_SIGNER_ACCOUNT_ID, - "network-config", - opts.network, - ]; - - if (BOS_SIGNER_PUBLIC_KEY && BOS_SIGNER_PRIVATE_KEY) command = command.concat(automaticSignIn) - - const deployProcess = spawn("npx", command, { - cwd: path.join(appName, DEPLOY_DIST_FOLDER), - stdio: "inherit", - }); - - deployProcess.on("close", (code) => { - if (code === 0) { - console.log(`Uploaded data for ${appName}`); - } else { - console.error(`Data upload failed with code ${code}`); - } - }); - - deployProcess.on("error", (err) => { - console.error(`Error uploading data for ${appName}:\n${err.message}`); - }); -} - -export async function deployAppDataFolders( +export async function deployAppData( appName: string, opts: DeployOptions ) { @@ -184,6 +116,7 @@ export async function deployAppDataFolders( path.join(appName, "bos.config.json"), opts.network ); + const BOS_SIGNER_ACCOUNT_ID = config.accounts.signer || opts.signerAccountId || config.account; @@ -194,28 +127,29 @@ export async function deployAppDataFolders( return; } - if ( - !config.data || - !Array.isArray(config.data.include) || - config.data.include.length === 0 - ) + const dataJSON = fs.readFileSync( + path.join(appName, DEPLOY_DIST_FOLDER, "data.json"), + "utf8" + ); + + const args = { data: JSON.parse(dataJSON) }; + + if (config.data?.include) { + if (!Array.isArray(config.data.include) || config.data.include.length === 0) throw new Error( "Config must contain a data.include array with at least one folder" ); - const result = {}; + const result = {}; - for (const folder of config.data.include) { - const folderName = path.basename(folder); - result[folderName] = {}; - await processDirectory(folder, '', result[folderName]); - } + for (const folder of config.data.include) { + const folderName = path.basename(folder); + result[folderName] = {}; + await processDirectory(folder, "", result[folderName]); + } - const args = { - data: { - [config.account]: result, - }, - }; + Object.assign(args.data[config.account], result); + } const argsBase64 = Buffer.from(JSON.stringify(args)).toString("base64");