Skip to content

Commit

Permalink
121 support deploy metadata (#140)
Browse files Browse the repository at this point in the history
* Add function to update metadata

* Working metadata deploy

* Add support for ci/cd

* Small fixes

* Add network configuration to command

* Rename

* Deploy & Upload

* Small fixes

* Update readme
  • Loading branch information
bb-face authored Jul 17, 2024
1 parent 4fd70ce commit 0151567
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ Commands:
dev [options] [src] [dest] Run the development server
build [options] [src] [dest] Build the project
workspace|ws [options] [command] [src] [dest] Work with multiple apps
init [options] [path] Initialize a new project
init [options] Initialize a new project
clone [account] [dest] Clone a SocialDB repository
deploy [options] [appName] Deploy the project
upload [string] Upload data to SocialDB (not implemented)
upload [options] [string] [string] Upload data to SocialDB
help [command] display help for command
```

Expand Down
15 changes: 15 additions & 0 deletions examples/single/widget/home.metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "GuestBook",
"description": "Shows data stored on your social-db",
"linktree": {
"website": ""
},
"image": {
"ipfs_cid": "bafkreiaruxhw4y6wafy2khiqsjnteh6gtwuhg3op4isbtuwtg3d55y6pty"
},
"tags": {
"app": "",
"explorer": "",
"everything": ""
}
}
23 changes: 18 additions & 5 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { buildApp } from "@/lib/build";
import { initProject } from "@/lib/init";
import { LogLevel, Logger } from "@/lib/logger";
import { Command } from "commander";
import { deploy } from "./deploy";
import { dev } from "./dev";
import { cloneRepository } from "./repository";
import { buildWorkspace, devWorkspace } from "./workspace";
import { deploy, deployAppData, DeployOptions } from "./deploy";

const program = new Command();

Expand Down Expand Up @@ -124,10 +124,23 @@ async function run() {

program
.command("upload")
.description("Upload data to SocialDB (not implemented)")
.argument("[string]", "app name")
.action((appName) => {
console.log("not yet supported");
.description("Upload data to SocialDB")
.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")
.option("--deployAccountId <string>", "Deploy account id")
.action(async (appName, options) => {
const deployOptions: DeployOptions = {
signerPublicKey: options.signerPublicKey,
signerPrivateKey: options.signerPrivateKey,
network: options.network,
deployAccountId: options.deployAccountId,
};

await deployAppData(appName, deployOptions ).catch((e: Error) => {
log.error(e.stack || e.message);
})
});

program.parse();
Expand Down
136 changes: 104 additions & 32 deletions lib/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { spawn } from 'child_process';
import path from "path";
import fs from "fs";

import { buildApp } from "@/lib/build";
import { BaseConfig, readConfig } from "@/lib/config";
import { readConfig } from "@/lib/config";
import { Network } from "@/lib/types";
import { move, pathExists, readdir, remove } from "@/lib/utils/fs";
import { readWorkspace } from "@/lib/workspace";
import { SOCIAL_CONTRACT } from './server';

const DEPLOY_DIST_FOLDER = "build";

Expand Down Expand Up @@ -43,8 +45,8 @@ export async function deployAppCode(src: string, dist: string, opts: DeployOptio
const fullDist = path.resolve(dist);

const deploying = log.loading(`[${fullSrc}] Deploying app`, LogLevels.BUILD);

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

// Translate for bos cli
Expand Down Expand Up @@ -106,39 +108,109 @@ export async function deployAppCode(src: string, dist: string, opts: DeployOptio
});
}

// publish data.json to SocialDB
export async function deployAppData(src: string, config: BaseConfig) {
}

export async function deploy(appName: string, opts: DeployOptions) {
const src = '.';

// 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;
}
}

log.error(`[${src}] bos.config.json file is not existing in the project`);
return;
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}`);
}
});

// Deploy workspace app
const { apps } = await readWorkspace(src);
deployProcess.on("error", (err) => {
console.error(`Error uploading data for ${appName}:\n${err.message}`);
});
}

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`);
export async function deploy(appName: string, opts: DeployOptions) {
const src = ".";

// 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`);

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

// 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);
await deployAppData(appSrc, opts);
}
2 changes: 1 addition & 1 deletion lib/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const RPC_URL = {
testnet: "https://rpc.testnet.near.org",
};

const SOCIAL_CONTRACT = {
export const SOCIAL_CONTRACT = {
mainnet: "social.near",
testnet: "v1.social08.testnet",
}
Expand Down

0 comments on commit 0151567

Please sign in to comment.