genlayer 0.0.29 → 0.0.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
 
2
2
 
3
+ ## 0.0.31 (2024-05-27)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * handle Fetch Error (Mac M3) ([#36](https://github.com/yeagerai/genlayer-cli/issues/36)) ([deae77a](https://github.com/yeagerai/genlayer-cli/commit/deae77a7c5a0f00694d2aa94f26d3e3be67e0178))
9
+
10
+ ## 0.0.30 (2024-05-22)
11
+
3
12
  ## 0.0.29 (2024-05-15)
4
13
 
5
14
  ## 0.0.28 (2024-05-13)
package/dist/index.js CHANGED
@@ -39896,7 +39896,7 @@ var {
39896
39896
  } = import_index.default;
39897
39897
 
39898
39898
  // package.json
39899
- var version = "0.0.29";
39899
+ var version = "0.0.31";
39900
39900
 
39901
39901
  // src/lib/config/text.ts
39902
39902
  var CLI_DESCRIPTION = "GenLayer CLI is a development environment for the GenLayer ecosystem. It allows developers to interact with the protocol by creating accounts, sending transactions, and working with Intelligent Contracts by testing, debugging, and deploying them.";
@@ -42491,6 +42491,7 @@ var inquirer_default = inquirer;
42491
42491
  // src/lib/config/simulator.ts
42492
42492
  var DEFAULT_JSON_RPC_URL = "http://localhost:4000/api";
42493
42493
  var DEFAULT_REPO_GH_URL = "https://github.com/yeagerai/genlayer-simulator.git";
42494
+ var DOCKER_IMAGES_AND_CONTAINERS_NAME_PREFIX = "genlayer-simulator-";
42494
42495
  var DEFAULT_RUN_SIMULATOR_COMMAND = (simulatorLocation) => ({
42495
42496
  darwin: `osascript -e 'tell application "Terminal" to do script "cd ${simulatorLocation} && docker compose build && docker compose up"'`,
42496
42497
  win32: `start cmd.exe /c "cd /d ${simulatorLocation} && docker compose build && docker compose up && pause"`,
@@ -42501,6 +42502,11 @@ var DEFAULT_PULL_OLLAMA_COMMAND = (simulatorLocation) => ({
42501
42502
  win32: `cd /d ${simulatorLocation} && docker exec ollama ollama pull llama3`,
42502
42503
  linux: `cd ${simulatorLocation} && docker exec ollama ollama pull llama3`
42503
42504
  });
42505
+ var DEFAULT_RUN_DOCKER_COMMAND = {
42506
+ darwin: "open -a Docker",
42507
+ win32: 'start "" "C:\\Program Files\\Docker\\Docker\\Docker Desktop.exe"',
42508
+ linux: "sudo systemctl start docker"
42509
+ };
42504
42510
  var AVAILABLE_PLATFORMS = ["darwin", "win32", "linux"];
42505
42511
  var STARTING_TIMEOUT_WAIT_CYLCE = 2e3;
42506
42512
  var STARTING_TIMEOUT_ATTEMPTS = 120;
@@ -44387,21 +44393,12 @@ function checkCommand(command, toolName) {
44387
44393
  });
44388
44394
  }
44389
44395
  function executeCommand(cmdsByPlatform, toolName) {
44390
- try {
44391
- const runningPlatform = getPlatform();
44392
- const command = cmdsByPlatform[runningPlatform];
44393
- return asyncExec(command);
44394
- } catch (error) {
44395
- throw new Error(`Error executing ${toolName}: ${error.message}`);
44396
- }
44397
- }
44398
- function executeCommandInNewTerminal(cmdsByPlatform) {
44399
44396
  const runningPlatform = getPlatform();
44400
44397
  const command = cmdsByPlatform[runningPlatform];
44401
44398
  try {
44402
44399
  return asyncExec(command);
44403
44400
  } catch (error) {
44404
- throw new Error(`Error executing command ${command}`);
44401
+ throw new Error(`Error executing ${toolName || command}: ${error.message}.`);
44405
44402
  }
44406
44403
  }
44407
44404
  function getHomeDirectory() {
@@ -44410,13 +44407,64 @@ function getHomeDirectory() {
44410
44407
  function getPlatform() {
44411
44408
  const currentPlatform = process.platform;
44412
44409
  if (!AVAILABLE_PLATFORMS.includes(currentPlatform)) {
44413
- throw new Error(`Unsupported platform: ${currentPlatform}`);
44410
+ throw new Error(`Unsupported platform: ${currentPlatform}.`);
44414
44411
  }
44415
44412
  return currentPlatform;
44416
44413
  }
44417
44414
  function openUrl(url) {
44418
44415
  return open_default(url);
44419
44416
  }
44417
+ function listDockerContainers() {
44418
+ return __async(this, null, function* () {
44419
+ try {
44420
+ const dockerResponse = yield asyncExec("docker ps -a --format '{{.Names}}'");
44421
+ const dockerContainers = dockerResponse.stdout.split("\n");
44422
+ return dockerContainers;
44423
+ } catch (error) {
44424
+ throw new Error("Error listing Docker containers.");
44425
+ }
44426
+ return [];
44427
+ });
44428
+ }
44429
+ function listDockerImages() {
44430
+ return __async(this, null, function* () {
44431
+ try {
44432
+ const dockerResponse = yield asyncExec("docker images --format '{{.Repository}}'");
44433
+ const dockerImages = dockerResponse.stdout.split("\n");
44434
+ return dockerImages;
44435
+ } catch (error) {
44436
+ throw new Error("Error listing Docker images.");
44437
+ }
44438
+ return [];
44439
+ });
44440
+ }
44441
+ function stopDockerContainer(containerName) {
44442
+ return __async(this, null, function* () {
44443
+ try {
44444
+ yield asyncExec(`docker stop ${containerName}`);
44445
+ } catch (error) {
44446
+ throw new Error(`Error stopping Docker container ${containerName}.`);
44447
+ }
44448
+ });
44449
+ }
44450
+ function removeDockerContainer(containerName) {
44451
+ return __async(this, null, function* () {
44452
+ try {
44453
+ yield asyncExec(`docker rm ${containerName}`);
44454
+ } catch (error) {
44455
+ throw new Error(`Error removing container ${containerName}.`);
44456
+ }
44457
+ });
44458
+ }
44459
+ function removeDockerImage(imageName) {
44460
+ return __async(this, null, function* () {
44461
+ try {
44462
+ yield asyncExec(`docker rmi ${imageName}`);
44463
+ } catch (error) {
44464
+ throw new Error(`Error removing image ${imageName}.`);
44465
+ }
44466
+ });
44467
+ }
44420
44468
 
44421
44469
  // src/lib/services/simulator.ts
44422
44470
  function getSimulatorLocation() {
@@ -44466,6 +44514,13 @@ function checkRequirements() {
44466
44514
  throw error;
44467
44515
  }
44468
44516
  }
44517
+ if (requirementsInstalled.docker) {
44518
+ try {
44519
+ yield checkCommand("docker ps", "docker");
44520
+ } catch (error) {
44521
+ yield executeCommand(DEFAULT_RUN_DOCKER_COMMAND);
44522
+ }
44523
+ }
44469
44524
  return requirementsInstalled;
44470
44525
  });
44471
44526
  }
@@ -44499,7 +44554,7 @@ function pullOllamaModel() {
44499
44554
  return __async(this, null, function* () {
44500
44555
  const simulatorLocation = getSimulatorLocation();
44501
44556
  const cmdsByPlatform = DEFAULT_PULL_OLLAMA_COMMAND(simulatorLocation);
44502
- yield executeCommandInNewTerminal(cmdsByPlatform);
44557
+ yield executeCommand(cmdsByPlatform);
44503
44558
  return true;
44504
44559
  });
44505
44560
  }
@@ -44516,7 +44571,7 @@ function configSimulator(newConfig) {
44516
44571
  function runSimulator() {
44517
44572
  const simulatorLocation = getSimulatorLocation();
44518
44573
  const commandsByPlatform = DEFAULT_RUN_SIMULATOR_COMMAND(simulatorLocation);
44519
- return executeCommandInNewTerminal(commandsByPlatform);
44574
+ return executeCommand(commandsByPlatform);
44520
44575
  }
44521
44576
  function waitForSimulatorToBeReady() {
44522
44577
  return __async(this, arguments, function* (retries = STARTING_TIMEOUT_ATTEMPTS) {
@@ -44531,7 +44586,7 @@ function waitForSimulatorToBeReady() {
44531
44586
  return waitForSimulatorToBeReady(retries - 1);
44532
44587
  }
44533
44588
  } catch (error) {
44534
- if ((error.name === "FetchError" || error.message.includes("ECONNRESET") || error.message.includes("ECONNREFUSED") || error.message.includes("socket hang up")) && retries > 0) {
44589
+ if ((error.name === "FetchError" || error.message.includes("Fetch Error") || error.message.includes("ECONNRESET") || error.message.includes("ECONNREFUSED") || error.message.includes("socket hang up")) && retries > 0) {
44535
44590
  yield sleep(STARTING_TIMEOUT_WAIT_CYLCE * 2);
44536
44591
  return waitForSimulatorToBeReady(retries - 1);
44537
44592
  }
@@ -44577,6 +44632,34 @@ function openFrontend() {
44577
44632
  return true;
44578
44633
  });
44579
44634
  }
44635
+ function resetDockerContainers() {
44636
+ return __async(this, null, function* () {
44637
+ const containers = yield listDockerContainers();
44638
+ const genlayerContainers = containers.filter(
44639
+ (container) => container.startsWith(DOCKER_IMAGES_AND_CONTAINERS_NAME_PREFIX)
44640
+ );
44641
+ const containersStopPromises = genlayerContainers.map(
44642
+ (container) => stopDockerContainer(container)
44643
+ );
44644
+ yield Promise.all(containersStopPromises);
44645
+ const containersRemovePromises = genlayerContainers.map(
44646
+ (container) => removeDockerContainer(container)
44647
+ );
44648
+ yield Promise.all(containersRemovePromises);
44649
+ return true;
44650
+ });
44651
+ }
44652
+ function resetDockerImages() {
44653
+ return __async(this, null, function* () {
44654
+ const images = yield listDockerImages();
44655
+ const genlayerImages = images.filter(
44656
+ (image) => image.startsWith(DOCKER_IMAGES_AND_CONTAINERS_NAME_PREFIX)
44657
+ );
44658
+ const imagesRemovePromises = genlayerImages.map((image) => removeDockerImage(image));
44659
+ yield Promise.all(imagesRemovePromises);
44660
+ return true;
44661
+ });
44662
+ }
44580
44663
 
44581
44664
  // src/commands/general/init.ts
44582
44665
  function getRequirementsErrorMessage({ git, docker }) {
@@ -44598,6 +44681,9 @@ function initAction(options) {
44598
44681
  const { git, docker } = yield checkRequirements();
44599
44682
  const errorMessage = getRequirementsErrorMessage({ git, docker });
44600
44683
  if (errorMessage) {
44684
+ console.log(
44685
+ "There was a problem running the docker service. Please start the docker service and try again."
44686
+ );
44601
44687
  console.error(errorMessage);
44602
44688
  return;
44603
44689
  }
@@ -44605,6 +44691,14 @@ function initAction(options) {
44605
44691
  console.error(error);
44606
44692
  return;
44607
44693
  }
44694
+ console.log(`Resetting Docker containers and images...`);
44695
+ try {
44696
+ yield resetDockerContainers();
44697
+ yield resetDockerImages();
44698
+ } catch (error) {
44699
+ console.error(error);
44700
+ return;
44701
+ }
44608
44702
  const answers = yield inquirer_default.prompt([
44609
44703
  {
44610
44704
  type: "confirm",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "genlayer",
3
- "version": "0.0.29",
3
+ "version": "0.0.31",
4
4
  "description": "GenLayer Command Line Tool",
5
5
  "main": "src/index.ts",
6
6
  "bin": {
@@ -17,6 +17,8 @@ import {
17
17
  getSimulatorLocation,
18
18
  getFrontendUrl,
19
19
  openFrontend,
20
+ resetDockerContainers,
21
+ resetDockerImages,
20
22
  } from "@/lib/services/simulator";
21
23
  export interface InitActionOptions {
22
24
  numValidators: number;
@@ -43,6 +45,9 @@ export async function initAction(options: InitActionOptions) {
43
45
  const {git, docker} = await checkRequirements();
44
46
  const errorMessage = getRequirementsErrorMessage({git, docker});
45
47
  if (errorMessage) {
48
+ console.log(
49
+ "There was a problem running the docker service. Please start the docker service and try again.",
50
+ );
46
51
  console.error(errorMessage);
47
52
  return;
48
53
  }
@@ -51,6 +56,16 @@ export async function initAction(options: InitActionOptions) {
51
56
  return;
52
57
  }
53
58
 
59
+ // Reset Docker containers and images
60
+ console.log(`Resetting Docker containers and images...`);
61
+ try {
62
+ await resetDockerContainers();
63
+ await resetDockerImages();
64
+ } catch (error) {
65
+ console.error(error);
66
+ return;
67
+ }
68
+
54
69
  // Ask for confirmation on downloading the GenLayer Simulator from GitHub
55
70
  const answers = await inquirer.prompt([
56
71
  {
@@ -20,32 +20,20 @@ type ExecuteCommandResult = {
20
20
  stderr: string;
21
21
  };
22
22
 
23
- type ExecuteCommandInNewTerminalInput = {
23
+ type ExecuteCommandByPlatformInput = {
24
24
  [key in RunningPlatform]: string;
25
25
  };
26
26
 
27
27
  export function executeCommand(
28
- cmdsByPlatform: ExecuteCommandInNewTerminalInput,
29
- toolName: string,
30
- ): Promise<ExecuteCommandResult> {
31
- try {
32
- const runningPlatform = getPlatform();
33
- const command = cmdsByPlatform[runningPlatform];
34
- return asyncExec(command);
35
- } catch (error: any) {
36
- throw new Error(`Error executing ${toolName}: ${error.message}`);
37
- }
38
- }
39
-
40
- export function executeCommandInNewTerminal(
41
- cmdsByPlatform: ExecuteCommandInNewTerminalInput,
42
- ): PromiseWithChild<{stdout: string; stderr: string}> {
28
+ cmdsByPlatform: ExecuteCommandByPlatformInput,
29
+ toolName?: string,
30
+ ): PromiseWithChild<ExecuteCommandResult> {
43
31
  const runningPlatform = getPlatform();
44
32
  const command = cmdsByPlatform[runningPlatform];
45
33
  try {
46
34
  return asyncExec(command);
47
35
  } catch (error: any) {
48
- throw new Error(`Error executing command ${command}`);
36
+ throw new Error(`Error executing ${toolName || command}: ${error.message}.`);
49
37
  }
50
38
  }
51
39
 
@@ -56,7 +44,7 @@ export function getHomeDirectory(): string {
56
44
  function getPlatform(): RunningPlatform {
57
45
  const currentPlatform = process.platform as RunningPlatform;
58
46
  if (!AVAILABLE_PLATFORMS.includes(currentPlatform)) {
59
- throw new Error(`Unsupported platform: ${currentPlatform}`);
47
+ throw new Error(`Unsupported platform: ${currentPlatform}.`);
60
48
  }
61
49
  return currentPlatform;
62
50
  }
@@ -64,3 +52,49 @@ function getPlatform(): RunningPlatform {
64
52
  export function openUrl(url: string): Promise<ChildProcess> {
65
53
  return open(url);
66
54
  }
55
+
56
+ export async function listDockerContainers(): Promise<string[]> {
57
+ try {
58
+ const dockerResponse = await asyncExec("docker ps -a --format '{{.Names}}'");
59
+ const dockerContainers = dockerResponse.stdout.split("\n");
60
+ return dockerContainers;
61
+ } catch (error) {
62
+ throw new Error("Error listing Docker containers.");
63
+ }
64
+ return [];
65
+ }
66
+
67
+ export async function listDockerImages(): Promise<string[]> {
68
+ try {
69
+ const dockerResponse = await asyncExec("docker images --format '{{.Repository}}'");
70
+ const dockerImages = dockerResponse.stdout.split("\n");
71
+ return dockerImages;
72
+ } catch (error) {
73
+ throw new Error("Error listing Docker images.");
74
+ }
75
+ return [];
76
+ }
77
+
78
+ export async function stopDockerContainer(containerName: string): Promise<void> {
79
+ try {
80
+ await asyncExec(`docker stop ${containerName}`);
81
+ } catch (error) {
82
+ throw new Error(`Error stopping Docker container ${containerName}.`);
83
+ }
84
+ }
85
+
86
+ export async function removeDockerContainer(containerName: string) {
87
+ try {
88
+ await asyncExec(`docker rm ${containerName}`);
89
+ } catch (error) {
90
+ throw new Error(`Error removing container ${containerName}.`);
91
+ }
92
+ }
93
+
94
+ export async function removeDockerImage(imageName: string) {
95
+ try {
96
+ await asyncExec(`docker rmi ${imageName}`);
97
+ } catch (error) {
98
+ throw new Error(`Error removing image ${imageName}.`);
99
+ }
100
+ }
@@ -1,5 +1,6 @@
1
1
  export const DEFAULT_JSON_RPC_URL = "http://localhost:4000/api";
2
2
  export const DEFAULT_REPO_GH_URL = "https://github.com/yeagerai/genlayer-simulator.git";
3
+ export const DOCKER_IMAGES_AND_CONTAINERS_NAME_PREFIX = "genlayer-simulator-";
3
4
  export const DEFAULT_RUN_SIMULATOR_COMMAND = (simulatorLocation: string) => ({
4
5
  darwin: `osascript -e 'tell application "Terminal" to do script "cd ${simulatorLocation} && docker compose build && docker compose up"'`,
5
6
  win32: `start cmd.exe /c "cd /d ${simulatorLocation} && docker compose build && docker compose up && pause"`,
@@ -10,6 +11,12 @@ export const DEFAULT_PULL_OLLAMA_COMMAND = (simulatorLocation: string) => ({
10
11
  win32: `cd /d ${simulatorLocation} && docker exec ollama ollama pull llama3`,
11
12
  linux: `cd ${simulatorLocation} && docker exec ollama ollama pull llama3`,
12
13
  });
14
+ export const DEFAULT_RUN_DOCKER_COMMAND = {
15
+ darwin: "open -a Docker",
16
+ win32: 'start "" "C:\\Program Files\\Docker\\Docker\\Docker Desktop.exe"',
17
+ linux: "sudo systemctl start docker",
18
+ };
19
+
13
20
  export const AVAILABLE_PLATFORMS = ["darwin", "win32", "linux"] as const;
14
21
  export type RunningPlatform = (typeof AVAILABLE_PLATFORMS)[number];
15
22
  export const STARTING_TIMEOUT_WAIT_CYLCE = 2000;
@@ -5,7 +5,9 @@ import * as path from "path";
5
5
  import {rpcClient} from "@/lib/clients/jsonRpcClient";
6
6
  import {
7
7
  DEFAULT_REPO_GH_URL,
8
+ DOCKER_IMAGES_AND_CONTAINERS_NAME_PREFIX,
8
9
  DEFAULT_RUN_SIMULATOR_COMMAND,
10
+ DEFAULT_RUN_DOCKER_COMMAND,
9
11
  DEFAULT_PULL_OLLAMA_COMMAND,
10
12
  STARTING_TIMEOUT_WAIT_CYLCE,
11
13
  STARTING_TIMEOUT_ATTEMPTS,
@@ -16,8 +18,12 @@ import {
16
18
  checkCommand,
17
19
  getHomeDirectory,
18
20
  executeCommand,
19
- executeCommandInNewTerminal,
20
21
  openUrl,
22
+ listDockerContainers,
23
+ stopDockerContainer,
24
+ removeDockerContainer,
25
+ listDockerImages,
26
+ removeDockerImage,
21
27
  } from "@/lib/clients/system";
22
28
  import {MissingRequirementError} from "../errors/missingRequirement";
23
29
 
@@ -80,12 +86,20 @@ export async function checkRequirements(): Promise<Record<string, boolean>> {
80
86
  try {
81
87
  await checkCommand("docker --version", "docker");
82
88
  requirementsInstalled.docker = true;
83
- } catch (error) {
89
+ } catch (error: any) {
84
90
  if (!(error instanceof MissingRequirementError)) {
85
91
  throw error;
86
92
  }
87
93
  }
88
94
 
95
+ if (requirementsInstalled.docker) {
96
+ try {
97
+ await checkCommand("docker ps", "docker");
98
+ } catch (error: any) {
99
+ await executeCommand(DEFAULT_RUN_DOCKER_COMMAND);
100
+ }
101
+ }
102
+
89
103
  return requirementsInstalled;
90
104
  }
91
105
 
@@ -121,7 +135,7 @@ export async function updateSimulator(): Promise<DownloadSimulatorResultType> {
121
135
  export async function pullOllamaModel(): Promise<boolean> {
122
136
  const simulatorLocation = getSimulatorLocation();
123
137
  const cmdsByPlatform = DEFAULT_PULL_OLLAMA_COMMAND(simulatorLocation);
124
- await executeCommandInNewTerminal(cmdsByPlatform);
138
+ await executeCommand(cmdsByPlatform);
125
139
  return true;
126
140
  }
127
141
 
@@ -137,7 +151,7 @@ export async function configSimulator(newConfig: Record<string, string>): Promis
137
151
  export function runSimulator(): Promise<{stdout: string; stderr: string}> {
138
152
  const simulatorLocation = getSimulatorLocation();
139
153
  const commandsByPlatform = DEFAULT_RUN_SIMULATOR_COMMAND(simulatorLocation);
140
- return executeCommandInNewTerminal(commandsByPlatform);
154
+ return executeCommand(commandsByPlatform);
141
155
  }
142
156
 
143
157
  type WaitForSimulatorToBeReadyResultType = {
@@ -162,6 +176,7 @@ export async function waitForSimulatorToBeReady(
162
176
  } catch (error: any) {
163
177
  if (
164
178
  (error.name === "FetchError" ||
179
+ error.message.includes("Fetch Error") ||
165
180
  error.message.includes("ECONNRESET") ||
166
181
  error.message.includes("ECONNREFUSED") ||
167
182
  error.message.includes("socket hang up")) &&
@@ -220,3 +235,32 @@ export async function openFrontend(): Promise<boolean> {
220
235
  await openUrl(getFrontendUrl());
221
236
  return true;
222
237
  }
238
+
239
+ export async function resetDockerContainers(): Promise<boolean> {
240
+ const containers = await listDockerContainers();
241
+ const genlayerContainers = containers.filter((container: string) =>
242
+ container.startsWith(DOCKER_IMAGES_AND_CONTAINERS_NAME_PREFIX),
243
+ );
244
+ const containersStopPromises = genlayerContainers.map((container: string) =>
245
+ stopDockerContainer(container),
246
+ );
247
+ await Promise.all(containersStopPromises);
248
+
249
+ const containersRemovePromises = genlayerContainers.map((container: string) =>
250
+ removeDockerContainer(container),
251
+ );
252
+ await Promise.all(containersRemovePromises);
253
+
254
+ return true;
255
+ }
256
+
257
+ export async function resetDockerImages(): Promise<boolean> {
258
+ const images = await listDockerImages();
259
+ const genlayerImages = images.filter((image: string) =>
260
+ image.startsWith(DOCKER_IMAGES_AND_CONTAINERS_NAME_PREFIX),
261
+ );
262
+ const imagesRemovePromises = genlayerImages.map((image: string) => removeDockerImage(image));
263
+ await Promise.all(imagesRemovePromises);
264
+
265
+ return true;
266
+ }