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

Add windows native support & separate shell commands to a file #65

Closed
wants to merge 2 commits into from
Closed
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
10 changes: 3 additions & 7 deletions chain-cli/src/commands/init/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof Init> {
Expand Down Expand Up @@ -68,12 +68,8 @@ export default class Init extends BaseCommand<typeof Init> {
}

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);
}
}
6 changes: 4 additions & 2 deletions chain-cli/src/commands/network-prune/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof NetworkPrune> {
static override aliases = ["network:prune"];
Expand Down Expand Up @@ -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;
}
23 changes: 14 additions & 9 deletions chain-cli/src/commands/network-up/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
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;
Expand Down Expand Up @@ -221,10 +223,10 @@
return updated;
}

function customValidation(flags: any): void {

Check warning on line 226 in chain-cli/src/commands/network-up/index.ts

View workflow job for this annotation

GitHub Actions / CI

Unexpected any. Specify a different type

Check warning on line 226 in chain-cli/src/commands/network-up/index.ts

View workflow job for this annotation

GitHub Actions / CI

Unexpected any. Specify a different type
const { channel, channelType, chaincodeName, chaincodeDir } = flags;

/*
/*
The same number of parameters for chaincode, channelTyle, chaincode and chaincodeDir is required
*/
if (
Expand All @@ -237,7 +239,7 @@
);
}

/*
/*
Channel types need to be consistend
*/
channel.reduce(
Expand All @@ -256,18 +258,18 @@
{} as Record<string, "curator" | "partner">
);

/*
/*
(channel, chaincodeName) pairs should be unique
*/
channel
.map((ch: any, i: string | number) => `(${ch}, ${chaincodeName[i]})`)

Check warning on line 265 in chain-cli/src/commands/network-up/index.ts

View workflow job for this annotation

GitHub Actions / CI

Unexpected any. Specify a different type

Check warning on line 265 in chain-cli/src/commands/network-up/index.ts

View workflow job for this annotation

GitHub Actions / CI

Unexpected any. Specify a different type
.forEach((pair: string, i: number, arr: string[]) => {
if (arr.filter((p) => p === pair).length > 1) {
throw new Error(`Error: Found non-unique channel-chaincode pair: ${pair}`);
}
});

/*
/*
Watch mode
*/
if (flags.watch) {
Expand All @@ -277,8 +279,8 @@
}
}

function reduce(args: any): SingleArg[] {

Check warning on line 282 in chain-cli/src/commands/network-up/index.ts

View workflow job for this annotation

GitHub Actions / CI

Unexpected any. Specify a different type

Check warning on line 282 in chain-cli/src/commands/network-up/index.ts

View workflow job for this annotation

GitHub Actions / CI

Unexpected any. Specify a different type
return args.chaincodeName.map((chaincodeName: any, i: number) => ({

Check warning on line 283 in chain-cli/src/commands/network-up/index.ts

View workflow job for this annotation

GitHub Actions / CI

Unexpected any. Specify a different type

Check warning on line 283 in chain-cli/src/commands/network-up/index.ts

View workflow job for this annotation

GitHub Actions / CI

Unexpected any. Specify a different type
chaincodeName,
chaincodeDir: args.chaincodeDir?.[i],
channel: args.channel[i],
Expand All @@ -288,7 +290,8 @@

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(
Expand All @@ -302,7 +305,7 @@
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));
Expand All @@ -320,14 +323,16 @@
);

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) {
const cppPathBrowser = (org: string) => path.resolve(cppDirBrowser, `cpp-${org}.json`);
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);
}
}
68 changes: 68 additions & 0 deletions chain-cli/src/shell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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 {
if (process.platform === "win32") {
execSync(`mkdir "${targetPath.match(this.win32ShellPattern)}"`);
} else {
execSync(`mkdir -p "${targetPath}"`);
}
return this;
}

cd(targetPath: string): Shell {
if (process.platform === "win32") {
execSync(`cd "${targetPath.match(this.win32ShellPattern)}"`);
return this;
} else {
execSync(`cd "${targetPath}"`);
return this;
}
}

cp(fileToCopy: string, destinationPath: string): Shell {
execSync(`cp "${fileToCopy}" "${destinationPath}/"`);
return this;
}

cpR(sourceDirectory: string, destinationDirectory: string): Shell {
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 (process.platform === "win32") {
execSync("dir");
} else {
execSync("ls -lh");
}
return this;
}
}
Loading