From fb1f645ad605e7259aff2769679ca943455c511b Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Fri, 19 Apr 2024 07:28:21 -0500 Subject: [PATCH] introduce execSyncPretty to get rid of strange execSync default output --- src/compiler/compile_rust_code.ts | 42 +++++++------ .../file_watcher/setup_file_watcher.ts | 6 +- src/compiler/get_names.ts | 4 +- src/compiler/handle_cli.ts | 25 +++----- src/compiler/index.ts | 3 +- src/compiler/prepare_docker_image.ts | 62 +++++++------------ src/compiler/prepare_rust_staging_area.ts | 8 ++- src/compiler/utils/exec_sync_pretty.ts | 9 +++ 8 files changed, 76 insertions(+), 83 deletions(-) create mode 100644 src/compiler/utils/exec_sync_pretty.ts diff --git a/src/compiler/compile_rust_code.ts b/src/compiler/compile_rust_code.ts index 02f58dceb8..a3a90f5b1c 100644 --- a/src/compiler/compile_rust_code.ts +++ b/src/compiler/compile_rust_code.ts @@ -1,4 +1,6 @@ -import { execSync, IOType } from 'child_process'; +import { IOType } from 'child_process'; + +import { execSyncPretty } from './utils/exec_sync_pretty'; export function compileRustCode( dockerContainerName: string, @@ -18,47 +20,47 @@ function compileRustCodeWithPodman( canisterName: string, stdio: IOType ) { - execSync( + execSyncPretty( `podman exec ${dockerContainerName} rm -rf /.azle/${canisterName}`, - { stdio } + stdio ); - execSync(`podman exec ${dockerContainerName} mkdir -p /.azle`, { - stdio - }); + execSyncPretty(`podman exec ${dockerContainerName} mkdir -p /.azle`, stdio); - execSync(`podman exec ${dockerContainerName} mkdir -p /global_target_dir`, { + execSyncPretty( + `podman exec ${dockerContainerName} mkdir -p /global_target_dir`, stdio - }); + ); - execSync(`podman cp .azle/${canisterName} ${dockerContainerName}:/.azle`, { + execSyncPretty( + `podman cp .azle/${canisterName} ${dockerContainerName}:/.azle`, stdio - }); + ); - execSync( + execSyncPretty( `podman exec -w /.azle/${canisterName} ${dockerContainerName} env CARGO_TARGET_DIR=/global_target_dir cargo build --target wasm32-wasi --manifest-path canister/Cargo.toml --release`, - { stdio } + stdio ); - execSync( + execSyncPretty( `podman exec -w /.azle/${canisterName} ${dockerContainerName} wasi2ic /global_target_dir/wasm32-wasi/release/canister.wasm /global_target_dir/wasm32-wasi/release/canister.wasm`, - { stdio } + stdio ); - execSync( + execSyncPretty( `podman cp ${dockerContainerName}:/global_target_dir/wasm32-wasi/release/canister.wasm .azle/${canisterName}/${canisterName}.wasm`, - { stdio } + stdio ); } function compileRustCodeNatively(canisterName: string, stdio: IOType) { - execSync( + execSyncPretty( `CARGO_TARGET_DIR=target cargo build --target wasm32-wasi --manifest-path .azle/${canisterName}/canister/Cargo.toml --release`, - { stdio } + stdio ); - execSync( + execSyncPretty( `wasi2ic target/wasm32-wasi/release/canister.wasm .azle/${canisterName}/${canisterName}.wasm`, - { stdio } + stdio ); } diff --git a/src/compiler/file_watcher/setup_file_watcher.ts b/src/compiler/file_watcher/setup_file_watcher.ts index 1f16aa783b..7a5b5d8de9 100644 --- a/src/compiler/file_watcher/setup_file_watcher.ts +++ b/src/compiler/file_watcher/setup_file_watcher.ts @@ -1,6 +1,8 @@ -import { execSync, spawn } from 'child_process'; +import { spawn } from 'child_process'; import { join } from 'path'; +import { execSyncPretty } from '../utils/exec_sync_pretty'; + export function setupFileWatcher( reloadedJsPath: string, canisterId: string, @@ -14,7 +16,7 @@ export function setupFileWatcher( // TODO should we figure out why the || true // TODO does not result in a 0 exit code // TODO and look into removing the try catch? - execSync(`pkill -f ./file_watcher_loader.js || true`); + execSyncPretty(`pkill -f ./file_watcher_loader.js || true`); } catch (error) { // For some reason pkill throws even with || true } diff --git a/src/compiler/get_names.ts b/src/compiler/get_names.ts index 5d0f93e9ef..567d8a1a6c 100644 --- a/src/compiler/get_names.ts +++ b/src/compiler/get_names.ts @@ -1,4 +1,3 @@ -import { execSync } from 'child_process'; import { createHash } from 'crypto'; import { readFileSync } from 'fs'; import { hashOfDirectory } from 'hash-of-directory'; @@ -11,6 +10,7 @@ import { getStdIoType, unwrap } from './utils'; +import { execSyncPretty } from './utils/exec_sync_pretty'; import { GLOBAL_AZLE_CONFIG_DIR } from './utils/global_paths'; import { JSCanisterConfig } from './utils/types'; @@ -38,7 +38,7 @@ export async function getNamesBeforeCli() { wasmedgeQuickJsName ); - const replicaWebServerPort = execSync(`dfx info webserver-port`) + const replicaWebServerPort = execSyncPretty(`dfx info webserver-port`) .toString() .trim(); diff --git a/src/compiler/handle_cli.ts b/src/compiler/handle_cli.ts index a23c074970..a9aae86f0d 100644 --- a/src/compiler/handle_cli.ts +++ b/src/compiler/handle_cli.ts @@ -1,10 +1,11 @@ -import { execSync, IOType } from 'child_process'; +import { IOType } from 'child_process'; import { rmSync } from 'fs'; import { version as azleVersion } from '../../package.json'; import { uploadFiles } from './file_uploader'; import { getFilesToUpload } from './file_uploader/get_files_to_upload'; import { generateNewAzleProject } from './new_command'; +import { execSyncPretty } from './utils/exec_sync_pretty'; import { GLOBAL_AZLE_CONFIG_DIR } from './utils/global_paths'; export function handleCli( @@ -53,9 +54,7 @@ function handleCommandNew() { } function handleCommandDockerfileHash(dockerfileHash: string) { - execSync(`echo -n "${dockerfileHash}"`, { - stdio: 'inherit' - }); + execSyncPretty(`echo -n "${dockerfileHash}"`, 'inherit'); } function handleCommandClean( @@ -77,29 +76,23 @@ function handleCommandClean( console.info(`.azle directory deleted`); - execSync( + execSyncPretty( `podman stop $(podman ps --filter "name=${dockerContainerPrefix}" --format "{{.ID}}") || true`, - { - stdio: stdioType - } + stdioType ); console.info(`azle containers stopped`); - execSync( + execSyncPretty( `podman rm $(podman ps -a --filter "name=${dockerContainerPrefix}" --format "{{.ID}}") || true`, - { - stdio: stdioType - } + stdioType ); console.info(`azle containers removed`); - execSync( + execSyncPretty( `podman image rm $(podman images --filter "reference=${dockerImagePrefix}" --format "{{.ID}}") || true`, - { - stdio: stdioType - } + stdioType ); console.info(`azle images removed`); diff --git a/src/compiler/index.ts b/src/compiler/index.ts index 7b89e7b2ea..433002078d 100644 --- a/src/compiler/index.ts +++ b/src/compiler/index.ts @@ -86,7 +86,8 @@ async function azle() { prepareRustStagingArea( canisterConfig, canisterPath, - canisterJavaScript + canisterJavaScript, + stdioType ); const { candid, canisterMethods } = getCandidAndCanisterMethods( diff --git a/src/compiler/prepare_docker_image.ts b/src/compiler/prepare_docker_image.ts index 83c4bc6e25..2025283940 100644 --- a/src/compiler/prepare_docker_image.ts +++ b/src/compiler/prepare_docker_image.ts @@ -1,8 +1,9 @@ -import { execSync, IOType } from 'child_process'; +import { IOType } from 'child_process'; import { existsSync } from 'fs'; import { version as azleVersion } from '../../package.json'; import { yellow } from './utils/colors'; +import { execSyncPretty } from './utils/exec_sync_pretty'; export function prepareDockerImage( stdioType: IOType, @@ -39,18 +40,14 @@ function initAndStartVm(stdioType: IOType) { // TODO detect mac and only run these commands on mac - execSync( + execSyncPretty( `if [ "$(uname -s)" != "Linux" ]; then podman machine init || true; fi`, - { - stdio: stdioType - } + stdioType ); - execSync( + execSyncPretty( `if [ "$(uname -s)" != "Linux" ]; then podman machine start || true; fi`, - { - stdio: stdioType - } + stdioType ); } @@ -94,9 +91,7 @@ function hasImageAlreadyBeenLoaded( dockerImageName: string ): boolean { try { - execSync(`podman image inspect ${dockerImageName}`, { - stdio: stdioType - }); + execSyncPretty(`podman image inspect ${dockerImageName}`, stdioType); return true; } catch (error) { @@ -111,9 +106,7 @@ function loadExistingLocalImage( if (existsSync(dockerImagePathTar)) { console.info(yellow(`\nLoading image...\n`)); - execSync(`podman load -i ${dockerImagePathTar}`, { - stdio: 'inherit' - }); + execSyncPretty(`podman load -i ${dockerImagePathTar}`, 'inherit'); return true; } @@ -121,13 +114,9 @@ function loadExistingLocalImage( if (existsSync(dockerImagePathTarGz)) { console.info(yellow(`\nLoading image...\n`)); - execSync(`gzip -d ${dockerImagePathTarGz}`, { - stdio: 'inherit' - }); + execSyncPretty(`gzip -d ${dockerImagePathTarGz}`, 'inherit'); - execSync(`podman load -i ${dockerImagePathTar}`, { - stdio: 'inherit' - }); + execSyncPretty(`podman load -i ${dockerImagePathTar}`, 'inherit'); return true; } @@ -141,18 +130,17 @@ function buildAndLoadImageWithDockerfile( ) { console.info(yellow(`\nBuilding image...\n`)); - execSync( + execSyncPretty( `podman build -f ${__dirname}/Dockerfile -t ${dockerImageName} ${__dirname}`, - { - stdio: 'inherit' - } + 'inherit' ); console.info(yellow(`\nSaving image...\n`)); - execSync(`podman save -o ${dockerImagePathTar} ${dockerImageName}`, { - stdio: 'inherit' - }); + execSyncPretty( + `podman save -o ${dockerImagePathTar} ${dockerImageName}`, + 'inherit' + ); console.info(yellow(`\nCompiling...`)); } @@ -164,11 +152,9 @@ function downloadAndLoadRemoteImage( ) { console.info(yellow(`\nDownloading image...\n`)); - execSync( + execSyncPretty( `curl -L -f https://github.com/demergent-labs/azle/releases/download/${azleVersion}/${dockerImageName}.tar.gz -o ${dockerImagePathTarGz}`, - { - stdio: 'inherit' - } + 'inherit' ); loadExistingLocalImage(dockerImagePathTar, dockerImagePathTarGz); @@ -182,19 +168,17 @@ function createAndStartContainer( dockerContainerName: string, wasmedgeQuickJsPath: string ) { - execSync( + execSyncPretty( `podman create --name ${dockerContainerName} ${dockerImageName} tail -f /dev/null || true`, - { stdio: stdioType } + stdioType ); - execSync(`podman start ${dockerContainerName}`, { - stdio: stdioType - }); + execSyncPretty(`podman start ${dockerContainerName}`, stdioType); if (!existsSync(wasmedgeQuickJsPath)) { - execSync( + execSyncPretty( `podman cp ${dockerContainerName}:/wasmedge-quickjs ${wasmedgeQuickJsPath}`, - { stdio: stdioType } + stdioType ); } } diff --git a/src/compiler/prepare_rust_staging_area.ts b/src/compiler/prepare_rust_staging_area.ts index f96712752e..0ca98a4f59 100644 --- a/src/compiler/prepare_rust_staging_area.ts +++ b/src/compiler/prepare_rust_staging_area.ts @@ -1,15 +1,17 @@ -import { execSync } from 'child_process'; +import { IOType } from 'child_process'; import { existsSync, mkdirSync, rmSync, writeFileSync } from 'fs'; import { copySync } from 'fs-extra'; import { join } from 'path'; import { generateWorkspaceCargoToml } from './generate_cargo_toml_files'; +import { execSyncPretty } from './utils/exec_sync_pretty'; import { JSCanisterConfig, Toml } from './utils/types'; export function prepareRustStagingArea( canisterConfig: JSCanisterConfig, canisterPath: string, - canisterJavaScript: string + canisterJavaScript: string, + stdioType: IOType ) { const workspaceCargoToml: Toml = generateWorkspaceCargoToml( canisterConfig.opt_level ?? '0' @@ -44,7 +46,7 @@ export function prepareRustStagingArea( canisterConfig.build_assets !== undefined && canisterConfig.build_assets !== null ) { - execSync(canisterConfig.build_assets); + execSyncPretty(canisterConfig.build_assets, stdioType); } for (const [src, dest] of canisterConfig.assets ?? []) { diff --git a/src/compiler/utils/exec_sync_pretty.ts b/src/compiler/utils/exec_sync_pretty.ts new file mode 100644 index 0000000000..a4cf4bd821 --- /dev/null +++ b/src/compiler/utils/exec_sync_pretty.ts @@ -0,0 +1,9 @@ +import { execSync, IOType } from 'child_process'; + +export function execSyncPretty(command: string, stdio?: IOType): Buffer { + try { + return execSync(command, { stdio }); + } catch (error) { + throw new Error(`Azle build error`); + } +}