From 4ef35e21a1563f0de27557248a3c3d9d93a36130 Mon Sep 17 00:00:00 2001 From: herr-bubu Date: Mon, 26 Feb 2024 15:16:53 +0100 Subject: [PATCH 1/2] separate shell commands to a file --- chain-cli/src/commands/network-prune/index.ts | 6 ++- chain-cli/src/commands/network-up/index.ts | 23 ++++++----- chain-cli/src/shell.ts | 39 +++++++++++++++++++ 3 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 chain-cli/src/shell.ts diff --git a/chain-cli/src/commands/network-prune/index.ts b/chain-cli/src/commands/network-prune/index.ts index db1832074a..3d34784b71 100644 --- a/chain-cli/src/commands/network-prune/index.ts +++ b/chain-cli/src/commands/network-prune/index.ts @@ -19,7 +19,8 @@ import path from "path"; import BaseCommand from "../../base-command"; import { defaultFabloRoot } from "../../consts"; -import { execSync, execSyncStdio } from "../../exec-sync"; +import { execSyncStdio } from "../../exec-sync"; +import { Shell } from "../../shell"; export default class NetworkPrune extends BaseCommand { static override aliases = ["network:prune"]; @@ -58,6 +59,7 @@ function downBrowserApi(fabloRoot: string): void { function getOrCreateFabloRoot(fabloDir: string): string { const fabloRoot = path.resolve(fabloDir); - execSync(`mkdir -p "${fabloRoot}"`); + const shell = new Shell(); + shell.mkdir(`${fabloRoot}`); return fabloRoot; } diff --git a/chain-cli/src/commands/network-up/index.ts b/chain-cli/src/commands/network-up/index.ts index 966b42dc8a..44c8de7768 100644 --- a/chain-cli/src/commands/network-up/index.ts +++ b/chain-cli/src/commands/network-up/index.ts @@ -22,9 +22,11 @@ import path from "path"; import BaseCommand from "../../base-command"; import { getCPPs, getCPPsBrowserApi } from "../../connection-profile"; import { defaultFabloRoot } from "../../consts"; -import { execSync, execSyncStdio } from "../../exec-sync"; +import { execSyncStdio } from "../../exec-sync"; +import { Shell } from "../../shell"; const defaultChaincodeDir = "."; +const shell = new Shell(); export interface SingleArg { channel: string; @@ -224,7 +226,7 @@ function updatedFabloConfigWithEntry( function customValidation(flags: any): void { const { channel, channelType, chaincodeName, chaincodeDir } = flags; - /* + /* The same number of parameters for chaincode, channelTyle, chaincode and chaincodeDir is required */ if ( @@ -237,7 +239,7 @@ function customValidation(flags: any): void { ); } - /* + /* Channel types need to be consistend */ channel.reduce( @@ -256,7 +258,7 @@ function customValidation(flags: any): void { {} as Record ); - /* + /* (channel, chaincodeName) pairs should be unique */ channel @@ -267,7 +269,7 @@ function customValidation(flags: any): void { } }); - /* + /* Watch mode */ if (flags.watch) { @@ -288,7 +290,8 @@ function reduce(args: any): SingleArg[] { function copyNetworkScriptsTo(targetPath: string): void { const sourceScriptsDir = path.resolve(require.resolve("."), "../../../network"); - execSync(`mkdir -p "${targetPath}" && cd "${targetPath}" && cp -R "${sourceScriptsDir}"/* ./ && ls -lh`); + + shell.mkdir(targetPath).cd(targetPath).cpR(sourceScriptsDir, "./").ls(); } function saveConnectionProfiles( @@ -302,7 +305,7 @@ function saveConnectionProfiles( const cpps = getCPPs(cryptoConfigRoot, channelNames, localhostName, !isWatchMode, true, !isWatchMode); const cppDir = path.resolve(fabloRoot, "connection-profiles"); - execSync(`mkdir -p "${cppDir}"`); + shell.mkdir(cppDir); const cppPath = (org: string) => path.resolve(cppDir, `cpp-${org}.json`); writeFileSync(cppPath("curator"), JSON.stringify(cpps.curator, undefined, 2)); @@ -320,7 +323,7 @@ function saveConnectionProfiles( ); const cppDirBrowser = path.resolve(fabloRoot, "connection-profiles-browser"); - execSync(`mkdir -p "${cppDirBrowser}"`); + shell.mkdir(cppDirBrowser); // Browser-api needs the generated connection profile when running in watch mode and the harded coded one when running in non-watch mode if (isWatchMode) { @@ -328,6 +331,8 @@ function saveConnectionProfiles( writeFileSync(cppPathBrowser("curator"), JSON.stringify(cppsBrowser.curator, undefined, 2)); } else { const sourceCppDirBrowser = path.resolve(".", `${defaultFabloRoot}/browser-api/connection-profiles`); - execSync(`cp "${sourceCppDirBrowser}/cpp-curator.json" "${cppDirBrowser}/"`); + const fileToCopy = `"${sourceCppDirBrowser}/cpp-curator.json"`; + const destinationPath = `"${cppDirBrowser}"`; + shell.cp(fileToCopy, destinationPath); } } diff --git a/chain-cli/src/shell.ts b/chain-cli/src/shell.ts new file mode 100644 index 0000000000..45c1ada50b --- /dev/null +++ b/chain-cli/src/shell.ts @@ -0,0 +1,39 @@ +import os from "os"; + +import { execSync } from "./exec-sync"; + +export class Shell { + mkdir(targetPath: string): Shell { + const operatingSystem = os.platform(); + if (operatingSystem == "win32") { + execSync(`mkdir "${targetPath}"`); + } else { + execSync(`mkdir -p "${targetPath}"`); + } + return this; + } + + cd(targetPath: string): Shell { + execSync(`cd "${targetPath}"`); + return this; + } + + cp(fileToCopy: string, destinationPath: string): Shell { + execSync(`cp "${fileToCopy}" "${destinationPath}/"`); + return this; + } + + cpR(sourceDirectory: string, destinationDirectory: string): Shell { + execSync(`cp -R "${sourceDirectory}"/* "${destinationDirectory}"`); + return this; + } + + ls(): Shell { + if (os.platform() === "win32") { + execSync("dir"); + } else { + execSync("ls -lh"); + } + return this; + } +} From d5dd8f8770d3f496c206de880c8a891f2796ed8f Mon Sep 17 00:00:00 2001 From: herr-bubu Date: Mon, 26 Feb 2024 15:50:41 +0100 Subject: [PATCH 2/2] separate shell commands to a file --- chain-cli/src/commands/init/index.ts | 10 ++---- chain-cli/src/shell.ts | 47 ++++++++++++++++++++++------ 2 files changed, 41 insertions(+), 16 deletions(-) diff --git a/chain-cli/src/commands/init/index.ts b/chain-cli/src/commands/init/index.ts index 9806f57811..3fdb19e2f3 100644 --- a/chain-cli/src/commands/init/index.ts +++ b/chain-cli/src/commands/init/index.ts @@ -18,7 +18,7 @@ import * as fs from "fs"; import path from "path"; import BaseCommand from "../../base-command"; -import { execSync } from "../../exec-sync"; +import { Shell } from "../../shell"; import { getPathFileName } from "../../utils"; export default class Init extends BaseCommand { @@ -68,12 +68,8 @@ export default class Init extends BaseCommand { } copyChaincodeTemplate(destinationPath: string): void { - if (process.platform === "win32") { - const sourceTemplateDir = path.resolve(__dirname, "..", "..", "..", "chaincode-template"); - execSync(`xcopy ${sourceTemplateDir} ${destinationPath} /E /I`); - return; - } + const shell = new Shell(); const sourceTemplateDir = path.resolve(require.resolve("."), "../../../chaincode-template"); - execSync(`cp -R ${sourceTemplateDir} ${destinationPath}`); + shell.cpR(sourceTemplateDir, destinationPath); } } diff --git a/chain-cli/src/shell.ts b/chain-cli/src/shell.ts index 45c1ada50b..495fbe63be 100644 --- a/chain-cli/src/shell.ts +++ b/chain-cli/src/shell.ts @@ -1,12 +1,27 @@ -import os from "os"; +/* + * Copyright (c) Gala Games Inc. All rights reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import * as process from "process"; import { execSync } from "./exec-sync"; export class Shell { + private win32ShellPattern = /[^/]+/g; + mkdir(targetPath: string): Shell { - const operatingSystem = os.platform(); - if (operatingSystem == "win32") { - execSync(`mkdir "${targetPath}"`); + if (process.platform === "win32") { + execSync(`mkdir "${targetPath.match(this.win32ShellPattern)}"`); } else { execSync(`mkdir -p "${targetPath}"`); } @@ -14,8 +29,13 @@ export class Shell { } cd(targetPath: string): Shell { - execSync(`cd "${targetPath}"`); - return this; + if (process.platform === "win32") { + execSync(`cd "${targetPath.match(this.win32ShellPattern)}"`); + return this; + } else { + execSync(`cd "${targetPath}"`); + return this; + } } cp(fileToCopy: string, destinationPath: string): Shell { @@ -24,12 +44,21 @@ export class Shell { } cpR(sourceDirectory: string, destinationDirectory: string): Shell { - execSync(`cp -R "${sourceDirectory}"/* "${destinationDirectory}"`); - return this; + if (process.platform === "win32") { + execSync( + `xcopy ${sourceDirectory.match(this.win32ShellPattern)} ${destinationDirectory.match( + this.win32ShellPattern + )} /E /I` + ); + return this; + } else { + execSync(`cp -R ${sourceDirectory} ${destinationDirectory}`); + return this; + } } ls(): Shell { - if (os.platform() === "win32") { + if (process.platform === "win32") { execSync("dir"); } else { execSync("ls -lh");