Skip to content

Commit

Permalink
Merge uplaod functions + README
Browse files Browse the repository at this point in the history
  • Loading branch information
bb-face committed Aug 8, 2024
1 parent b9efc3b commit 65f190f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 107 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
22 changes: 1 addition & 21 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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>", "network to deploy to", "mainnet")
.option("--signerPublicKey <string>", "Signer public key")
.option("--signerPrivateKey <string>", "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();
}

Expand Down
104 changes: 19 additions & 85 deletions lib/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,82 +108,15 @@ 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
) {
const config = await readConfig(
path.join(appName, "bos.config.json"),
opts.network
);

const BOS_SIGNER_ACCOUNT_ID =
config.accounts.signer || opts.signerAccountId || config.account;

Expand All @@ -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");

Expand Down

0 comments on commit 65f190f

Please sign in to comment.