Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to use near-cli-rs signing during deploy #122

Merged
merged 3 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 42 additions & 25 deletions lib/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { spawn } from 'child_process';
import path from "path";
import { exec, ExecException } from "child_process";

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

const DEPLOY_DIST_FOLDER = "build";

Expand All @@ -22,10 +21,10 @@ export type DeployOptions = {
export async function translateForBosCli(dist: string) {
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 });
}
Expand All @@ -47,7 +46,7 @@ export async function deployAppCode(src: string, dist: string, opts: DeployOptio

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

// Translate for bos cli
await log.wait(
translateForBosCli(dist),
Expand All @@ -56,12 +55,12 @@ export async function deployAppCode(src: string, dist: string, opts: DeployOptio
`[${fullDist}] Failed to translate`,
LogLevels.BUILD
);

// Exec bos-cli;
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_DEPLOY_ACCOUNT_ID = config.accounts.deploy || opts.deployAccountId || config.account;
const BOS_SIGNER_ACCOUNT_ID = config.accounts.signer || opts.signerAccountId || config.account;
const BOS_SIGNER_PUBLIC_KEY = opts.signerPublicKey;
const BOS_SIGNER_PRIVATE_KEY = opts.signerPrivateKey;

Expand All @@ -71,22 +70,40 @@ export async function deployAppCode(src: string, dist: string, opts: DeployOptio
} 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;
}

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(`[${fullSrc}] App deployed successfully`);
return;
}

deploying.error(error.message);
// Prepare the command and arguments
const args = [
'bos-cli', 'components', 'deploy', `${BOS_DEPLOY_ACCOUNT_ID}`,
'sign-as', `${BOS_SIGNER_ACCOUNT_ID}`,
'network-config', `${opts.network}`
];

if (BOS_SIGNER_PUBLIC_KEY && BOS_SIGNER_PRIVATE_KEY) {
args.push(
'sign-with-plaintext-private-key',
'--signer-public-key', `${BOS_SIGNER_PUBLIC_KEY}`,
'--signer-private-key', `${BOS_SIGNER_PRIVATE_KEY}`,
'send'
);
}

const deployProcess = spawn('npx', args, {
cwd: dist,
stdio: 'inherit' // This allows the process to inherit the parent process's stdio streams
});

deployProcess.on('close', (code) => {
if (code === 0) {
deploying.finish(`[${fullSrc}] App deployed successfully`);
} else {
deploying.error(`Deployment failed with code ${code}`);
}
);
});

deployProcess.on('error', (err) => {
deploying.error(`Deployment failed with error: ${err.message}`);
});
}

// publish data.json to SocialDB
Expand Down Expand Up @@ -114,7 +131,7 @@ export async function deploy(appName: string, opts: DeployOptions) {

// 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) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bos-workspace",
"version": "1.0.0-alpha.30",
"version": "1.0.0-alpha.31",
"description": "",
"bin": {
"bos-workspace": "./bin/bw.js",
Expand Down
25 changes: 19 additions & 6 deletions tests/unit/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
import * as process from "child_process";
import { DEFAULT_CONFIG } from '@/lib/config';
import { deployAppCode } from '@/lib/deploy';
import { BaseConfig, DEFAULT_CONFIG } from '@/lib/config';
import * as fs from '@/lib/utils/fs';
import { LogLevel, Logger } from "@/lib/logger";
import EventEmitter from "events";

import { vol, } from 'memfs';
jest.mock('fs', () => require('memfs').fs);
jest.mock('fs/promises', () => require('memfs').fs.promises);

jest.mock('child_process', () => ({
exec: jest.fn((command: string) => {
return command;
spawn: jest.fn((command, args, options) => {
// Create a mock for the ChildProcess object
const mockChildProcess = new EventEmitter();

// Simulate the 'on' method for event handling
mockChildProcess.on = jest.fn((event, callback) => {
if (event === 'close') {
callback(0);
}
if (event === 'error') {
callback(new Error('error'));
}
return mockChildProcess;
});
return mockChildProcess;
}),
}))
}));

const app_example = {
"./bos.config.json": JSON.stringify({
Expand Down
Loading