Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

feat: add a detach mode to the CLI #3568

Merged
merged 71 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from 62 commits
Commits
Show all changes
71 commits
Select commit Hold shift + click to select a range
fcc4228
feat: allow users to specify a option, which causes Ganache to start…
jeffsmale90 Aug 22, 2022
7a787f2
Stream stderr from the child to the parent
jeffsmale90 Aug 23, 2022
301c500
Tidy up of --detach argument parsing
jeffsmale90 Aug 23, 2022
7dec4c0
Improve detach configuration
jeffsmale90 Aug 24, 2022
9ed54db
Add tests for parsing detach arguments. Change default value to false…
jeffsmale90 Aug 24, 2022
20579ae
Make parent aware of detached instances, and allow stopping them via cli
jeffsmale90 Sep 4, 2022
1da7911
improve args tests, remove placeholder
jeffsmale90 Sep 6, 2022
9cc5b59
Implement , and allow different actions to be executed via the CLI
jeffsmale90 Sep 7, 2022
e5d7e60
Clean up orphaned instance process files. uses a terrible hack to fet…
jeffsmale90 Sep 7, 2022
295b1a3
Implement `instances` command with subcommands. Clean up orphaned ins…
jeffsmale90 Sep 7, 2022
75ffbb0
Tidy up args types and parsing
jeffsmale90 Sep 12, 2022
17732d7
Refactor, add flavor to instance data, and to list output. Change ins…
jeffsmale90 Sep 12, 2022
dea1ddb
Fix createFlatChildArgs import in args.test.ts
jeffsmale90 Sep 20, 2022
07a4b03
Merge branch 'develop' into feat/detach-mode
jeffsmale90 Sep 20, 2022
a74d0a3
Update package-lock.json files - required for new deps
jeffsmale90 Sep 20, 2022
1eaea51
Fix module resolution
jeffsmale90 Sep 20, 2022
cba7589
Tidy ups and refactors
jeffsmale90 Sep 20, 2022
c39cae6
Tidy up arg parsing
jeffsmale90 Sep 20, 2022
bc61cb1
Revert package-lock.json files
jeffsmale90 Sep 20, 2022
f5650f2
Fix miss-typed action name
jeffsmale90 Sep 20, 2022
90a2305
Remove dependency on ps-list, move data files to Ganache/instances, i…
jeffsmale90 Sep 22, 2022
c1e5f43
Remove kebab-case names when expanding arguments. Add tests for expan…
jeffsmale90 Sep 22, 2022
757220b
Reintroduce ps-list, only list instances where the process co
jeffsmale90 Sep 23, 2022
c432c35
Move createFlatChildArgs() to detach.ts, add version to list, ensure …
jeffsmale90 Sep 23, 2022
2dec992
Create instances directory recursively, because now it's
jeffsmale90 Sep 23, 2022
773f21a
Tidy ups from code review, add section on Detached mode to README.
jeffsmale90 Sep 27, 2022
5fb72d1
Fix incorrect command name in README.md
jeffsmale90 Oct 10, 2022
9bf6064
Merge branch 'develop' into feat/detach-mode
jeffsmale90 Oct 10, 2022
d27e6bc
Update to use @trufflesuite/ps-list instead of ps-list
jeffsmale90 Nov 6, 2022
eafc679
If no instances are found, using , print a friendly message instead o…
jeffsmale90 Nov 6, 2022
e6ae996
Revert ganache package version to match develop
jeffsmale90 Nov 6, 2022
eb14694
Merge branch 'develop' into feat/detach-mode
jeffsmale90 Nov 6, 2022
876b972
Bump @trufflesuite/ps-list to include support for node 12
jeffsmale90 Nov 6, 2022
208b149
upgrade @trufflesuite/ps-list to support nodejs < 12.17.0
jeffsmale90 Nov 7, 2022
5f4b80d
Merge branch 'develop' into feat/detach-mode
jeffsmale90 Nov 9, 2022
6b61cba
Improve wording of `--detach` description in README.md
jeffsmale90 Nov 13, 2022
7f36f1f
Switched out for (existing dependency via marked-terminal)
jeffsmale90 Nov 13, 2022
ac5aba0
Move instance list sorting to the consumer that needs the list sorted
jeffsmale90 Nov 15, 2022
bfc5cf9
use detached instance friendly name for instance file (instead of pid)
jeffsmale90 Nov 15, 2022
1a52d4c
pass original process.args to startDetachedInstance, calculate expect…
jeffsmale90 Nov 15, 2022
7a4c256
Fix 'cmd' argument for win32 platform
jeffsmale90 Nov 15, 2022
67d11fc
Add attribution for duration formatter
jeffsmale90 Nov 15, 2022
683c201
Change all detach mode file access to async
jeffsmale90 Nov 15, 2022
60ae164
Adds warning messages if instances removed
jeffsmale90 Nov 15, 2022
1726ec9
Add PID to warning message.
jeffsmale90 Nov 15, 2022
0811874
Remove unnecessary detach.test.ts
jeffsmale90 Nov 15, 2022
485ff64
improvements / refactor as per pr review.
jeffsmale90 Nov 16, 2022
1701140
Don't check for existence of the folder each time - just catch the Er…
jeffsmale90 Nov 16, 2022
78a9d0c
Accidentally recreated the instance name twice
jeffsmale90 Nov 16, 2022
04719a6
Move comment to relevant code
jeffsmale90 Nov 16, 2022
3ea9968
Improvements to detach re PR
jeffsmale90 Nov 16, 2022
6a7f3b3
Revert package-lock.json
jeffsmale90 Nov 16, 2022
c5c0c31
_Actually_ fix package-lock.json this time
jeffsmale90 Nov 16, 2022
bc8d269
Remove nyc dev-dependency from cli/package.json - correct, but irrele…
jeffsmale90 Nov 16, 2022
aa72840
Strip the detach argument correctly before passing arguments to child…
jeffsmale90 Nov 16, 2022
4e8f933
Add tests for duration formatting - move implementation into detach.ts
jeffsmale90 Nov 18, 2022
6b49cbb
Rename formatDuration to formatUptime, add --detach example to README…
jeffsmale90 Nov 18, 2022
0435951
Revert shrinkwrap :/
jeffsmale90 Nov 18, 2022
959c6dc
Pin cli-table dependency to latest release
jeffsmale90 Nov 18, 2022
dfb8b72
Fix failing test
jeffsmale90 Nov 18, 2022
c495cc9
Improve duration formatting, improve readme
jeffsmale90 Nov 22, 2022
c16cb20
Annoyingly, args parsing is complicated. Don't try to strip out the d…
jeffsmale90 Nov 22, 2022
b272237
Instance files now have .json extension. Remove any files / folders f…
jeffsmale90 Nov 22, 2022
c85d022
Remove unused import
jeffsmale90 Nov 22, 2022
359cb6b
Handle errors betterer
jeffsmale90 Nov 23, 2022
f94442f
Shorten duration formatting, combine host and port, right align PID i…
jeffsmale90 Nov 23, 2022
26af55e
RH align Uptime
jeffsmale90 Nov 23, 2022
d717d3f
remove unnecessary type assertion, remove datapath suffix
jeffsmale90 Nov 23, 2022
393702c
Use unlink instead of rm, because node 12
jeffsmale90 Nov 25, 2022
0507aa4
Support ndoe 12 correctly for removal of files and directories
jeffsmale90 Nov 28, 2022
2d92da1
Make PowerShell example consistent with non-Windows example
jeffsmale90 Dec 1, 2022
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
262 changes: 258 additions & 4 deletions src/packages/cli/package-lock.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion src/packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
"@types/node": "17.0.0",
"chalk": "4.1.0",
"ethereumjs-util": "7.1.5",
"marked-terminal": "4.1.0"
"marked-terminal": "4.1.0",
"cli-table": "0.3.11",
"@trufflesuite/ps-list": "0.0.3"
}
}
146 changes: 119 additions & 27 deletions src/packages/cli/src/args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ import yargs, { Options } from "yargs";
import {
DefaultFlavor,
FilecoinFlavorName,
DefaultOptionsByName
DefaultOptionsByName,
FlavorName
} from "@ganache/flavors";
import {
Base,
Definitions,
YargsPrimitiveCliTypeStrings
} from "@ganache/options";
import { Command, Argv } from "./types";
import { FlavorCommand, StartArgs, GanacheArgs } from "./types";
import chalk from "chalk";
import { EOL } from "os";
import marked from "marked";
Expand Down Expand Up @@ -86,9 +87,11 @@ function processOption(
// the types held within each array
const { cliType } = optionObj;
const array = cliType && cliType.startsWith("array:"); // e.g. array:string or array:number
const type = (array
? cliType.slice(6) // remove the "array:" part
: cliType) as YargsPrimitiveCliTypeStrings;
const type = (
array
? cliType.slice(6) // remove the "array:" part
: cliType
) as YargsPrimitiveCliTypeStrings;

const options: Options = {
group,
Expand Down Expand Up @@ -127,9 +130,9 @@ function applyDefaults(
const group = `${category[0].toUpperCase()}${category.slice(
1
)}:` as GroupType;
const categoryObj = (flavorDefaults[
const categoryObj = flavorDefaults[
category
] as unknown) as Definitions<Base.Config>;
] as unknown as Definitions<Base.Config>;
const state = {};
for (const option in categoryObj) {
const optionObj = categoryObj[option];
Expand All @@ -146,13 +149,18 @@ function applyDefaults(
}
}

export default function (version: string, isDocker: boolean) {
export default function (
version: string,
isDocker: boolean,
rawArgs = process.argv.slice(2)
) {
const versionUsageOutputText = chalk`{hex("${
TruffleColors.porsche
}").bold ${center(version)}}`;
let args = yargs
// disable dot-notation because yargs just can't coerce args properly...
// ...on purpose! https://github.com/yargs/yargs/issues/1021#issuecomment-352324693

// disable dot-notation because yargs just can't coerce args properly...
// ...on purpose! https://github.com/yargs/yargs/issues/1021#issuecomment-352324693
yargs
.parserConfiguration({ "dot-notation": false })
.strict()
.usage(versionUsageOutputText)
Expand All @@ -168,7 +176,7 @@ export default function (version: string, isDocker: boolean) {
let flavor: keyof typeof DefaultOptionsByName;
for (flavor in DefaultOptionsByName) {
const flavorDefaults = DefaultOptionsByName[flavor];
let command: Command;
let command: FlavorCommand;
let defaultPort: number;
switch (flavor) {
// since "ethereum" is the DefaultFlavor we don't need a `case` for it
Expand All @@ -185,7 +193,7 @@ export default function (version: string, isDocker: boolean) {
defaultPort = 8545;
}

args = args.command(
yargs.command(
command,
chalk`Use the {bold ${flavor}} flavor of Ganache`,
flavorArgs => {
Expand Down Expand Up @@ -219,32 +227,116 @@ export default function (version: string, isDocker: boolean) {
}

return true;
})
.option("detach", {
description: highlight(
"Run Ganache in detached (daemon) mode." +
EOL +
"See `ganache instances --help` for information on managing detached instances."
),
type: "boolean",
alias: ["D", "😈"]
});
},
parsedArgs => {
parsedArgs.action = parsedArgs.detach ? "start-detached" : "start";
}
);
}

args = args
yargs
.command(
"instances",
highlight(
"Manage instances of Ganache running in detached mode." +
EOL +
"(Ganache can be run in detached mode by providing the `--detach` flag)"
),
_yargs => {
_yargs
.command(
"list",
"List instances running in detached mode",
_ => {},
listArgs => {
listArgs.action = "list";
}
)
.command(
"stop <name>",
"Stop the instance specified by <name>",
stopArgs => {
stopArgs.positional("name", { type: "string" });
},
stopArgs => {
stopArgs.action = "stop";
}
)
.version(false);
}
)
.showHelpOnFail(false, "Specify -? or --help for available options")
.alias("help", "?")
.wrap(wrapWidth)
.version(version);

const parsedArgs = args.argv;
const finalArgs = {
flavor: parsedArgs._.length > 0 ? parsedArgs._[0] : DefaultFlavor
} as Argv;
for (let key in parsedArgs) {
// split on the first "."
const [group, option] = key.split(/\.(.+)/);
// only copy namespaced/group keys
if (option) {
if (!finalArgs[group]) {
finalArgs[group] = {};
const parsedArgs = yargs.parse(rawArgs);

let finalArgs: GanacheArgs;
if (parsedArgs.action === "stop") {
finalArgs = {
action: "stop",
name: parsedArgs.name as string
};
} else if (parsedArgs.action === "list") {
finalArgs = { action: "list" };
} else if (
parsedArgs.action === "start" ||
parsedArgs.action === "start-detached"
) {
const action = parsedArgs.action;
const flavor = (parsedArgs._.length > 0
? parsedArgs._[0]
: DefaultFlavor) as any as FlavorName;

finalArgs = {
flavor,
action,
...(expandArgs(parsedArgs) as Omit<
StartArgs<FlavorName>,
"flavor" | "action"
>)
};
} else {
throw new Error(`Unknown action: ${parsedArgs.action}`);
}

return finalArgs;
}

/**
* Expands the arguments into an object including only namespaced keys from the
* `args` argument.
* @param {object} args to be expanded
* @returns {object} with the expanded arguments
*/
export function expandArgs(args: object): object {
const namespacedArgs = {};

for (const key in args) {
// ignore keys that are kebab-cased - they will be duplicated as camelCase
if (key.indexOf("-") === -1) {
// split on the first "."
const [namespace, option] = key.split(/\.(.+)/);
// only copy namespaced/group keys, and ignore keys with kebab cases
if (option) {
if (!namespacedArgs[namespace]) {
namespacedArgs[namespace] = {};
}
namespacedArgs[namespace][option] = args[key];
}
finalArgs[group][option] = parsedArgs[key];
}
}

return finalArgs;
return namespacedArgs;
}
Loading