ic-mops 1.12.0 → 2.0.0

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.
Files changed (322) hide show
  1. package/.DS_Store +0 -0
  2. package/.eslintrc.json +7 -7
  3. package/CHANGELOG.md +6 -0
  4. package/api/actors.ts +41 -37
  5. package/api/downloadPackageFiles.ts +75 -61
  6. package/api/getHighestVersion.ts +5 -5
  7. package/api/index.ts +4 -4
  8. package/api/network.ts +19 -21
  9. package/api/resolveVersion.ts +14 -11
  10. package/bin/mops.js +1 -1
  11. package/bundle/bench/bench-canister.mo +109 -101
  12. package/bundle/bench/user-bench.mo +6 -6
  13. package/bundle/bin/mops.js +1 -1
  14. package/bundle/cli.js +797 -792
  15. package/bundle/cli.tgz +0 -0
  16. package/bundle/package.json +6 -5
  17. package/bundle/templates/mops-publish.yml +3 -3
  18. package/bundle/templates/mops-test.yml +3 -3
  19. package/bundle/templates/src/lib.mo +13 -13
  20. package/bundle/templates/test/lib.test.mo +2 -2
  21. package/bundle-package-json.ts +8 -8
  22. package/cache.ts +80 -65
  23. package/check-requirements.ts +49 -45
  24. package/cli.ts +577 -376
  25. package/commands/add.ts +142 -129
  26. package/commands/available-updates.ts +55 -41
  27. package/commands/bench/bench-canister.mo +114 -108
  28. package/commands/bench/user-bench.mo +6 -6
  29. package/commands/bench-replica.ts +146 -118
  30. package/commands/bench.ts +563 -497
  31. package/commands/build.ts +177 -0
  32. package/commands/bump.ts +68 -57
  33. package/commands/check-candid.ts +24 -0
  34. package/commands/docs-coverage.ts +124 -102
  35. package/commands/docs.ts +118 -108
  36. package/commands/format.ts +171 -155
  37. package/commands/init.ts +301 -275
  38. package/commands/install/install-all.ts +75 -62
  39. package/commands/install/install-dep.ts +43 -28
  40. package/commands/install/install-deps.ts +23 -15
  41. package/commands/install/install-local-dep.ts +42 -34
  42. package/commands/install/install-mops-dep.ts +154 -123
  43. package/commands/install/sync-local-cache.ts +39 -35
  44. package/commands/maintainer.ts +109 -99
  45. package/commands/outdated.ts +30 -24
  46. package/commands/owner.ts +107 -99
  47. package/commands/publish.ts +534 -443
  48. package/commands/remove.ts +119 -89
  49. package/commands/replica.ts +391 -316
  50. package/commands/search.ts +42 -36
  51. package/commands/self.ts +63 -56
  52. package/commands/sources.ts +66 -49
  53. package/commands/sync.ts +92 -75
  54. package/commands/template.ts +145 -102
  55. package/commands/test/mmf1.ts +146 -119
  56. package/commands/test/reporters/compact-reporter.ts +87 -84
  57. package/commands/test/reporters/files-reporter.ts +56 -51
  58. package/commands/test/reporters/reporter.ts +12 -6
  59. package/commands/test/reporters/silent-reporter.ts +58 -59
  60. package/commands/test/reporters/verbose-reporter.ts +66 -54
  61. package/commands/test/test.ts +498 -401
  62. package/commands/test/utils.ts +72 -67
  63. package/commands/toolchain/index.ts +363 -322
  64. package/commands/toolchain/moc.ts +78 -50
  65. package/commands/toolchain/pocket-ic.ts +41 -34
  66. package/commands/toolchain/toolchain-utils.ts +92 -72
  67. package/commands/toolchain/wasmtime.ts +37 -34
  68. package/commands/update.ts +82 -64
  69. package/commands/user.ts +90 -81
  70. package/commands/watch/deployer.ts +188 -152
  71. package/commands/watch/error-checker.ts +90 -80
  72. package/commands/watch/formatter.ts +72 -60
  73. package/commands/watch/generator.ts +116 -96
  74. package/commands/watch/globMoFiles.ts +13 -13
  75. package/commands/watch/parseDfxJson.ts +63 -57
  76. package/commands/watch/tester.ts +83 -65
  77. package/commands/watch/warning-checker.ts +149 -136
  78. package/commands/watch/watch.ts +123 -95
  79. package/dist/api/actors.d.ts +4 -4
  80. package/dist/api/actors.js +8 -8
  81. package/dist/api/downloadPackageFiles.d.ts +2 -2
  82. package/dist/api/downloadPackageFiles.js +10 -10
  83. package/dist/api/getHighestVersion.js +1 -1
  84. package/dist/api/index.d.ts +4 -4
  85. package/dist/api/index.js +4 -4
  86. package/dist/api/network.js +9 -9
  87. package/dist/api/resolveVersion.js +3 -3
  88. package/dist/bin/mops.js +1 -1
  89. package/dist/bundle-package-json.js +8 -8
  90. package/dist/cache.js +22 -17
  91. package/dist/check-requirements.js +11 -11
  92. package/dist/cli.js +283 -186
  93. package/dist/commands/add.d.ts +1 -1
  94. package/dist/commands/add.js +40 -37
  95. package/dist/commands/available-updates.d.ts +1 -1
  96. package/dist/commands/available-updates.js +18 -12
  97. package/dist/commands/bench/bench-canister.mo +114 -108
  98. package/dist/commands/bench/user-bench.mo +6 -6
  99. package/dist/commands/bench-replica.d.ts +4 -4
  100. package/dist/commands/bench-replica.js +45 -34
  101. package/dist/commands/bench.d.ts +5 -5
  102. package/dist/commands/bench.js +134 -118
  103. package/dist/commands/build.d.ts +7 -0
  104. package/dist/commands/build.js +121 -0
  105. package/dist/commands/bump.js +27 -18
  106. package/dist/commands/check-candid.d.ts +4 -0
  107. package/dist/commands/check-candid.js +15 -0
  108. package/dist/commands/docs-coverage.d.ts +1 -1
  109. package/dist/commands/docs-coverage.js +45 -31
  110. package/dist/commands/docs.d.ts +1 -1
  111. package/dist/commands/docs.js +39 -38
  112. package/dist/commands/format.js +31 -27
  113. package/dist/commands/init.js +102 -92
  114. package/dist/commands/install/install-all.d.ts +2 -2
  115. package/dist/commands/install/install-all.js +23 -21
  116. package/dist/commands/install/install-dep.d.ts +1 -1
  117. package/dist/commands/install/install-dep.js +21 -8
  118. package/dist/commands/install/install-deps.d.ts +1 -1
  119. package/dist/commands/install/install-deps.js +1 -1
  120. package/dist/commands/install/install-local-dep.js +11 -9
  121. package/dist/commands/install/install-mops-dep.d.ts +1 -1
  122. package/dist/commands/install/install-mops-dep.js +32 -27
  123. package/dist/commands/install/sync-local-cache.js +10 -10
  124. package/dist/commands/maintainer.js +21 -21
  125. package/dist/commands/outdated.js +12 -8
  126. package/dist/commands/owner.js +21 -21
  127. package/dist/commands/publish.js +148 -128
  128. package/dist/commands/remove.d.ts +1 -1
  129. package/dist/commands/remove.js +42 -30
  130. package/dist/commands/replica.d.ts +7 -7
  131. package/dist/commands/replica.js +85 -57
  132. package/dist/commands/search.js +15 -13
  133. package/dist/commands/self.js +31 -28
  134. package/dist/commands/sources.d.ts +5 -1
  135. package/dist/commands/sources.js +23 -17
  136. package/dist/commands/sync.d.ts +1 -1
  137. package/dist/commands/sync.js +38 -25
  138. package/dist/commands/template.js +66 -56
  139. package/dist/commands/test/mmf1.d.ts +3 -3
  140. package/dist/commands/test/mmf1.js +33 -31
  141. package/dist/commands/test/reporters/compact-reporter.d.ts +3 -3
  142. package/dist/commands/test/reporters/compact-reporter.js +19 -15
  143. package/dist/commands/test/reporters/files-reporter.d.ts +3 -3
  144. package/dist/commands/test/reporters/files-reporter.js +18 -14
  145. package/dist/commands/test/reporters/reporter.d.ts +2 -2
  146. package/dist/commands/test/reporters/silent-reporter.d.ts +3 -3
  147. package/dist/commands/test/reporters/silent-reporter.js +4 -4
  148. package/dist/commands/test/reporters/verbose-reporter.d.ts +3 -3
  149. package/dist/commands/test/reporters/verbose-reporter.js +17 -13
  150. package/dist/commands/test/test.d.ts +4 -4
  151. package/dist/commands/test/test.js +134 -112
  152. package/dist/commands/test/utils.d.ts +3 -3
  153. package/dist/commands/test/utils.js +17 -17
  154. package/dist/commands/toolchain/index.d.ts +1 -1
  155. package/dist/commands/toolchain/index.js +81 -69
  156. package/dist/commands/toolchain/moc.d.ts +1 -1
  157. package/dist/commands/toolchain/moc.js +48 -24
  158. package/dist/commands/toolchain/pocket-ic.js +12 -12
  159. package/dist/commands/toolchain/toolchain-utils.d.ts +2 -0
  160. package/dist/commands/toolchain/toolchain-utils.js +32 -23
  161. package/dist/commands/toolchain/wasmtime.js +11 -11
  162. package/dist/commands/update.d.ts +1 -1
  163. package/dist/commands/update.js +18 -14
  164. package/dist/commands/user.js +31 -28
  165. package/dist/commands/watch/deployer.d.ts +4 -4
  166. package/dist/commands/watch/deployer.js +45 -36
  167. package/dist/commands/watch/error-checker.d.ts +2 -2
  168. package/dist/commands/watch/error-checker.js +27 -27
  169. package/dist/commands/watch/formatter.d.ts +4 -4
  170. package/dist/commands/watch/formatter.js +17 -17
  171. package/dist/commands/watch/generator.d.ts +3 -3
  172. package/dist/commands/watch/generator.js +28 -23
  173. package/dist/commands/watch/globMoFiles.js +8 -8
  174. package/dist/commands/watch/parseDfxJson.d.ts +2 -2
  175. package/dist/commands/watch/parseDfxJson.js +9 -9
  176. package/dist/commands/watch/tester.d.ts +4 -4
  177. package/dist/commands/watch/tester.js +23 -21
  178. package/dist/commands/watch/warning-checker.d.ts +3 -3
  179. package/dist/commands/watch/warning-checker.js +36 -36
  180. package/dist/commands/watch/watch.js +45 -32
  181. package/dist/environments/nodejs/cli.d.ts +1 -0
  182. package/dist/environments/nodejs/cli.js +4 -0
  183. package/dist/environments/web/cli.d.ts +1 -0
  184. package/dist/environments/web/cli.js +4 -0
  185. package/dist/error.d.ts +1 -0
  186. package/dist/error.js +5 -0
  187. package/dist/fix-dist.js +5 -5
  188. package/dist/helpers/find-changelog-entry.js +8 -5
  189. package/dist/helpers/get-dep-name.js +2 -2
  190. package/dist/helpers/get-dfx-version.js +4 -4
  191. package/dist/helpers/get-moc-path.js +8 -7
  192. package/dist/helpers/get-moc-version.js +10 -7
  193. package/dist/helpers/get-package-id.js +2 -2
  194. package/dist/helpers/is-candid-compatible.d.ts +1 -0
  195. package/dist/helpers/is-candid-compatible.js +20 -0
  196. package/dist/integrity.d.ts +1 -1
  197. package/dist/integrity.js +47 -38
  198. package/dist/jest.config.d.ts +11 -0
  199. package/dist/jest.config.js +14 -0
  200. package/dist/mops.d.ts +6 -6
  201. package/dist/mops.js +87 -80
  202. package/dist/notify-installs.js +4 -4
  203. package/dist/package.json +9 -5
  204. package/dist/pem.d.ts +3 -3
  205. package/dist/pem.js +20 -12
  206. package/dist/release-cli.js +20 -20
  207. package/dist/resolve-packages.d.ts +1 -1
  208. package/dist/resolve-packages.js +52 -36
  209. package/dist/templates/mops-publish.yml +3 -3
  210. package/dist/templates/mops-test.yml +3 -3
  211. package/dist/templates/src/lib.mo +13 -13
  212. package/dist/templates/test/lib.test.mo +2 -2
  213. package/dist/templates.js +1 -1
  214. package/dist/tests/cli.test.d.ts +1 -0
  215. package/dist/tests/cli.test.js +63 -0
  216. package/dist/types.d.ts +14 -4
  217. package/dist/vessel.d.ts +2 -2
  218. package/dist/vessel.js +41 -34
  219. package/dist/wasm/pkg/bundler/package.json +20 -0
  220. package/dist/wasm/pkg/bundler/wasm.d.ts +3 -0
  221. package/dist/wasm/pkg/bundler/wasm.js +5 -0
  222. package/dist/wasm/pkg/bundler/wasm_bg.js +93 -0
  223. package/dist/wasm/pkg/bundler/wasm_bg.wasm +0 -0
  224. package/dist/wasm/pkg/bundler/wasm_bg.wasm.d.ts +8 -0
  225. package/dist/wasm/pkg/nodejs/package.json +14 -0
  226. package/dist/wasm/pkg/nodejs/wasm.d.ts +3 -0
  227. package/dist/wasm/pkg/nodejs/wasm.js +98 -0
  228. package/dist/wasm/pkg/nodejs/wasm_bg.wasm +0 -0
  229. package/dist/wasm/pkg/nodejs/wasm_bg.wasm.d.ts +8 -0
  230. package/dist/wasm/pkg/web/package.json +18 -0
  231. package/dist/wasm/pkg/web/wasm.d.ts +35 -0
  232. package/dist/wasm/pkg/web/wasm.js +191 -0
  233. package/dist/wasm/pkg/web/wasm_bg.wasm +0 -0
  234. package/dist/wasm/pkg/web/wasm_bg.wasm.d.ts +8 -0
  235. package/dist/wasm.d.ts +5 -0
  236. package/dist/wasm.js +10 -0
  237. package/environments/nodejs/cli.ts +6 -0
  238. package/environments/web/cli.ts +6 -0
  239. package/error.ts +6 -0
  240. package/fix-dist.ts +5 -5
  241. package/global.d.ts +3 -3
  242. package/helpers/find-changelog-entry.ts +26 -23
  243. package/helpers/get-dep-name.ts +5 -5
  244. package/helpers/get-dfx-version.ts +8 -9
  245. package/helpers/get-moc-path.ts +25 -26
  246. package/helpers/get-moc-version.ts +21 -19
  247. package/helpers/get-package-id.ts +4 -4
  248. package/helpers/is-candid-compatible.ts +22 -0
  249. package/integrity.ts +270 -236
  250. package/jest.config.js +14 -0
  251. package/mops.ts +238 -215
  252. package/notify-installs.ts +16 -17
  253. package/package.json +19 -10
  254. package/parallel.ts +28 -24
  255. package/pem.ts +55 -47
  256. package/release-cli.ts +73 -39
  257. package/resolve-packages.ts +231 -189
  258. package/templates/mops-publish.yml +3 -3
  259. package/templates/mops-test.yml +3 -3
  260. package/templates/src/lib.mo +13 -13
  261. package/templates/test/lib.test.mo +2 -2
  262. package/templates.ts +4 -4
  263. package/tests/__snapshots__/cli.test.ts.snap +202 -0
  264. package/tests/build/error/candid/bar.did +3 -0
  265. package/tests/build/error/dfx.json +12 -0
  266. package/tests/build/error/mops.toml +9 -0
  267. package/tests/build/error/src/Bar.mo +5 -0
  268. package/tests/build/error/src/Foo.mo +5 -0
  269. package/tests/build/success/.dfx/local/canister_ids.json +17 -0
  270. package/tests/build/success/.dfx/local/canisters/bar/bar.did +3 -0
  271. package/tests/build/success/.dfx/local/canisters/bar/bar.most +4 -0
  272. package/tests/build/success/.dfx/local/canisters/bar/bar.wasm +0 -0
  273. package/tests/build/success/.dfx/local/canisters/bar/constructor.did +3 -0
  274. package/tests/build/success/.dfx/local/canisters/bar/index.js +42 -0
  275. package/tests/build/success/.dfx/local/canisters/bar/init_args.txt +1 -0
  276. package/tests/build/success/.dfx/local/canisters/bar/service.did +3 -0
  277. package/tests/build/success/.dfx/local/canisters/bar/service.did.d.ts +7 -0
  278. package/tests/build/success/.dfx/local/canisters/bar/service.did.js +4 -0
  279. package/tests/build/success/.dfx/local/canisters/foo/constructor.did +3 -0
  280. package/tests/build/success/.dfx/local/canisters/foo/foo.did +3 -0
  281. package/tests/build/success/.dfx/local/canisters/foo/foo.most +4 -0
  282. package/tests/build/success/.dfx/local/canisters/foo/foo.wasm +0 -0
  283. package/tests/build/success/.dfx/local/canisters/foo/index.js +42 -0
  284. package/tests/build/success/.dfx/local/canisters/foo/init_args.txt +1 -0
  285. package/tests/build/success/.dfx/local/canisters/foo/service.did +3 -0
  286. package/tests/build/success/.dfx/local/canisters/foo/service.did.d.ts +7 -0
  287. package/tests/build/success/.dfx/local/canisters/foo/service.did.js +4 -0
  288. package/tests/build/success/.dfx/local/lsp/ucwa4-rx777-77774-qaada-cai.did +3 -0
  289. package/tests/build/success/.dfx/local/lsp/ulvla-h7777-77774-qaacq-cai.did +3 -0
  290. package/tests/build/success/.dfx/local/network-id +4 -0
  291. package/tests/build/success/candid/bar.did +3 -0
  292. package/tests/build/success/dfx.json +12 -0
  293. package/tests/build/success/mops.toml +9 -0
  294. package/tests/build/success/src/Bar.mo +5 -0
  295. package/tests/build/success/src/Foo.mo +5 -0
  296. package/tests/check-candid/a.did +3 -0
  297. package/tests/check-candid/b.did +5 -0
  298. package/tests/check-candid/c.did +3 -0
  299. package/tests/cli.test.ts +82 -0
  300. package/tsconfig.json +26 -19
  301. package/types.ts +41 -31
  302. package/vessel.ts +219 -187
  303. package/wasm/Cargo.lock +1475 -0
  304. package/wasm/Cargo.toml +28 -0
  305. package/wasm/pkg/bundler/package.json +20 -0
  306. package/wasm/pkg/bundler/wasm.d.ts +3 -0
  307. package/wasm/pkg/bundler/wasm.js +5 -0
  308. package/wasm/pkg/bundler/wasm_bg.js +93 -0
  309. package/wasm/pkg/bundler/wasm_bg.wasm +0 -0
  310. package/wasm/pkg/bundler/wasm_bg.wasm.d.ts +8 -0
  311. package/wasm/pkg/nodejs/package.json +14 -0
  312. package/wasm/pkg/nodejs/wasm.d.ts +3 -0
  313. package/wasm/pkg/nodejs/wasm.js +98 -0
  314. package/wasm/pkg/nodejs/wasm_bg.wasm +0 -0
  315. package/wasm/pkg/nodejs/wasm_bg.wasm.d.ts +8 -0
  316. package/wasm/pkg/web/package.json +18 -0
  317. package/wasm/pkg/web/wasm.d.ts +35 -0
  318. package/wasm/pkg/web/wasm.js +191 -0
  319. package/wasm/pkg/web/wasm_bg.wasm +0 -0
  320. package/wasm/pkg/web/wasm_bg.wasm.d.ts +8 -0
  321. package/wasm/src/lib.rs +17 -0
  322. package/wasm.ts +16 -0
@@ -1,322 +1,397 @@
1
- import process from 'node:process';
2
- import {ChildProcessWithoutNullStreams, execSync, spawn} from 'node:child_process';
3
- import path from 'node:path';
4
- import fs from 'node:fs';
5
- import {PassThrough} from 'node:stream';
6
- import {spawn as spawnAsync} from 'promisify-child-process';
7
-
8
- import {IDL} from '@icp-sdk/core/candid';
9
- import {Actor, HttpAgent} from '@icp-sdk/core/agent';
10
- import {PocketIc, PocketIcServer} from 'pic-ic';
11
- import {PocketIc as PocketIcMops, PocketIcServer as PocketIcServerMops} from 'pic-js-mops';
12
- import chalk from 'chalk';
13
-
14
- import {readConfig} from '../mops.js';
15
- import {toolchain} from './toolchain/index.js';
16
- import {getDfxVersion} from '../helpers/get-dfx-version.js';
1
+ import process from "node:process";
2
+ import {
3
+ ChildProcessWithoutNullStreams,
4
+ execSync,
5
+ spawn,
6
+ } from "node:child_process";
7
+ import path from "node:path";
8
+ import fs from "node:fs";
9
+ import { PassThrough } from "node:stream";
10
+ import { spawn as spawnAsync } from "promisify-child-process";
11
+
12
+ import { IDL } from "@icp-sdk/core/candid";
13
+ import { Actor, HttpAgent } from "@icp-sdk/core/agent";
14
+ import { PocketIc, PocketIcServer } from "pic-ic";
15
+ import {
16
+ PocketIc as PocketIcMops,
17
+ PocketIcServer as PocketIcServerMops,
18
+ } from "pic-js-mops";
19
+ import chalk from "chalk";
20
+
21
+ import { readConfig } from "../mops.js";
22
+ import { toolchain } from "./toolchain/index.js";
23
+ import { getDfxVersion } from "../helpers/get-dfx-version.js";
17
24
 
18
25
  type StartOptions = {
19
- type ?: 'dfx' | 'pocket-ic' | 'dfx-pocket-ic';
20
- dir ?: string;
21
- verbose ?: boolean;
22
- silent ?: boolean;
26
+ type?: "dfx" | "pocket-ic" | "dfx-pocket-ic";
27
+ dir?: string;
28
+ verbose?: boolean;
29
+ silent?: boolean;
23
30
  };
24
31
 
25
32
  export class Replica {
26
- type : 'dfx' | 'pocket-ic' | 'dfx-pocket-ic' = 'dfx';
27
- verbose = false;
28
- canisters : Record<string, {cwd : string; canisterId : string; actor : any; stream : PassThrough;}> = {};
29
- pocketIcServer ?: PocketIcServer | PocketIcServerMops;
30
- pocketIc ?: PocketIc | PocketIcMops;
31
- dfxProcess ?: ChildProcessWithoutNullStreams;
32
- dir : string = ''; // absolute path (/.../.mops/.test/)
33
- ttl = 60;
34
-
35
- async start({type, dir, verbose, silent} : StartOptions = {}) {
36
- this.type = type ?? this.type;
37
- this.verbose = verbose ?? this.verbose;
38
- this.dir = dir ?? this.dir;
39
-
40
- silent || console.log(`Starting ${this.type} replica...`);
41
-
42
- if (this.type === 'dfx' || this.type === 'dfx-pocket-ic') {
43
- fs.mkdirSync(this.dir, {recursive: true});
44
- fs.writeFileSync(path.join(this.dir, 'dfx.json'), JSON.stringify(this.dfxJson(''), null, 2));
45
- fs.writeFileSync(path.join(this.dir, 'canister.did'), 'service : { runTests: () -> (); }');
46
-
47
- await this.stop();
48
-
49
- this.dfxProcess = spawn('dfx', ['start', this.type === 'dfx-pocket-ic' ? '--pocketic' : '', '--clean', (this.verbose ? '' : '-qqqq'), '--artificial-delay', '0'].filter(x => x).flat(), {cwd: this.dir});
50
-
51
- // process canister logs
52
- this._attachCanisterLogHandler(this.dfxProcess);
53
-
54
- this.dfxProcess.stdout.on('data', (data) => {
55
- if (this.verbose) {
56
- console.log('DFX:', data.toString());
57
- }
58
- });
59
-
60
- this.dfxProcess.stderr.on('data', (data) => {
61
- if (this.verbose) {
62
- console.error('DFX:', data.toString());
63
- }
64
- if (data.toString().includes('Failed to bind socket to')) {
65
- console.error(chalk.red(data.toString()));
66
- console.log('Please try again after some time');
67
- process.exit(11);
68
- }
69
- });
70
-
71
- // await for dfx to start
72
- let ok = false;
73
- while (!ok) {
74
- await fetch('http://127.0.0.1:4945/api/v2/status')
75
- .then(res => {
76
- if (res.status === 200) {
77
- ok = true;
78
- }
79
- })
80
- .catch(() => {})
81
- .finally(() => {
82
- return new Promise(resolve => setTimeout(resolve, 1000));
83
- });
84
- }
85
- }
86
- else {
87
- let pocketIcBin = await toolchain.bin('pocket-ic');
88
-
89
- let config = readConfig();
90
- if (config.toolchain?.['pocket-ic'] !== '4.0.0' && !config.toolchain?.['pocket-ic']?.startsWith('9.')) {
91
- console.error('Current Mops CLI only supports pocket-ic 9.x.x and 4.0.0');
92
- process.exit(1);
93
- }
94
-
95
- // pocket-ic 9.x.x
96
- if (config.toolchain?.['pocket-ic']?.startsWith('9.')) {
97
- this.pocketIcServer = await PocketIcServerMops.start({
98
- showRuntimeLogs: false,
99
- showCanisterLogs: false,
100
- binPath: pocketIcBin,
101
- ttl: this.ttl,
102
- });
103
- this.pocketIc = await PocketIcMops.create(this.pocketIcServer.getUrl());
104
- }
105
- // pocket-ic 4.0.0
106
- else {
107
- this.pocketIcServer = await PocketIcServer.start({
108
- showRuntimeLogs: false,
109
- showCanisterLogs: false,
110
- binPath: pocketIcBin,
111
- ttl: this.ttl,
112
- });
113
- this.pocketIc = await PocketIc.create(this.pocketIcServer.getUrl());
114
- }
115
-
116
- // process canister logs
117
- this._attachCanisterLogHandler(this.pocketIcServer.serverProcess as ChildProcessWithoutNullStreams);
118
- }
119
- }
120
-
121
- _attachCanisterLogHandler(proc : ChildProcessWithoutNullStreams) {
122
- let curData = '';
123
- proc.stderr.on('data', (data) => {
124
- curData = curData + data.toString();
125
-
126
- if (curData.includes('\n')) {
127
- let chunk = curData.split('\n').slice(0, -1).join('\n');
128
- let matches = [...chunk.matchAll(/\[Canister ([a-z0-9-]+)\] (.*)/g)];
129
-
130
- for (let match of matches) {
131
- let [, canisterId, msg] = match;
132
- let stream = this.getCanisterStream(canisterId || '');
133
- if (stream) {
134
- stream.write(msg);
135
- }
136
- }
137
-
138
- if (matches.length) {
139
- curData = curData.split('\n').slice(-1).join('\n');
140
- }
141
- }
142
- });
143
- }
144
-
145
- async stop(sigint = false) {
146
- if (this.type === 'dfx' || this.type === 'dfx-pocket-ic') {
147
- if (this.dfxProcess) {
148
- let killed = this.dfxProcess.kill();
149
-
150
- if (!killed) {
151
- this.dfxProcess.kill('SIGKILL');
152
- }
153
-
154
- // give replica some time to stop
155
- let i = 0;
156
- while (this.dfxProcess.exitCode === null) {
157
- i++;
158
- await new Promise((resolve) => {
159
- setTimeout(resolve, 500);
160
- });
161
- if (i >= 20) {
162
- break;
163
- }
164
- }
165
-
166
- // // make sure replica is stopped
167
- // try {
168
- // // execSync('dfx killall', {cwd: this.dir, timeout: 3_000, stdio: ['pipe', this.verbose ? 'inherit' : 'ignore', 'pipe']});
169
- // execSync('dfx stop' + (this.verbose ? '' : ' -qqqq'), {cwd: this.dir, timeout: 3_000, stdio: ['pipe', this.verbose ? 'inherit' : 'ignore', 'pipe']});
170
- // }
171
- // catch {}
172
- }
173
- }
174
- else if (this.pocketIc && this.pocketIcServer) {
175
- if (!sigint) {
176
- await this.pocketIc.tearDown(); // error 'fetch failed' if run on SIGINT
177
- }
178
- await this.pocketIcServer.stop();
179
- }
180
- }
181
-
182
- async deploy(name : string, wasm : string, idlFactory : IDL.InterfaceFactory, cwd : string = process.cwd(), signal ?: AbortSignal) {
183
- if (this.type === 'dfx' || this.type === 'dfx-pocket-ic') {
184
- // prepare dfx.json for current canister
185
- let dfxJson = path.join(this.dir, 'dfx.json');
186
-
187
- let oldDfxJsonData;
188
- if (fs.existsSync(dfxJson)) {
189
- oldDfxJsonData = JSON.parse(fs.readFileSync(dfxJson).toString());
190
- }
191
- let newDfxJsonData = this.dfxJson(name, name + '.wasm');
192
-
193
- if (oldDfxJsonData.canisters) {
194
- newDfxJsonData.canisters = Object.assign(oldDfxJsonData.canisters, newDfxJsonData.canisters);
195
- }
196
-
197
- fs.mkdirSync(this.dir, {recursive: true});
198
- fs.writeFileSync(dfxJson, JSON.stringify(newDfxJsonData, null, 2));
199
-
200
- await spawnAsync('dfx', ['deploy', name, '--mode', 'reinstall', '--yes', '--identity', 'anonymous'], {cwd: this.dir, signal, stdio: this.verbose ? 'pipe' : ['pipe', 'ignore', 'pipe']}).catch((error) => {
201
- if (error.code === 'ABORT_ERR') {
202
- return {stderr: ''};
203
- }
204
- throw error;
205
- });
206
-
207
- if (signal?.aborted) {
208
- return;
209
- }
210
-
211
- await spawnAsync('dfx', ['ledger', 'fabricate-cycles', '--canister', name, '--t', '100'], {cwd: this.dir, signal, stdio: this.verbose ? 'pipe' : ['pipe', 'ignore', 'pipe']}).catch((error) => {
212
- if (error.code === 'ABORT_ERR') {
213
- return {stderr: ''};
214
- }
215
- throw error;
216
- });
217
-
218
- if (signal?.aborted) {
219
- return;
220
- }
221
-
222
- let canisterId = execSync(`dfx canister id ${name}`, {cwd: this.dir}).toString().trim();
223
-
224
- let actor = Actor.createActor(idlFactory, {
225
- agent: await HttpAgent.create({
226
- host: 'http://127.0.0.1:4945',
227
- shouldFetchRootKey: true,
228
- }),
229
- canisterId,
230
- });
231
-
232
- this.canisters[name] = {
233
- cwd,
234
- canisterId,
235
- actor,
236
- stream: new PassThrough(),
237
- };
238
- }
239
- else if (this.pocketIc) {
240
- type PocketIcSetupCanister = PocketIcMops['setupCanister'] & PocketIc['setupCanister'];
241
- let {canisterId, actor} = await (this.pocketIc.setupCanister as PocketIcSetupCanister)({
242
- wasm,
243
- idlFactory,
244
- });
245
-
246
- if (signal?.aborted) {
247
- return;
248
- }
249
-
250
- await this.pocketIc.addCycles(canisterId as any, 1_000_000_000_000);
251
-
252
- if (signal?.aborted) {
253
- return;
254
- }
255
-
256
- this.canisters[name] = {
257
- cwd,
258
- canisterId: canisterId.toText(),
259
- actor,
260
- stream: new PassThrough(),
261
- };
262
- }
263
-
264
- if (!this.canisters[name]) {
265
- throw new Error(`Canister ${name} not found`);
266
- }
267
-
268
- return this.canisters[name];
269
- }
270
-
271
- getActor(name : string) : unknown {
272
- if (!this.canisters[name]) {
273
- throw new Error(`Canister ${name} not found`);
274
- }
275
- return this.canisters[name]?.actor;
276
- }
277
-
278
- getCanister(name : string) {
279
- return this.canisters[name];
280
- }
281
-
282
- getCanisterId(name : string) : string {
283
- return this.canisters[name]?.canisterId || '';
284
- }
285
-
286
- getCanisterStream(canisterId : string) : PassThrough | null {
287
- for (let canister of Object.values(this.canisters)) {
288
- if (canister.canisterId === canisterId) {
289
- return canister.stream;
290
- }
291
- }
292
- return null;
293
- }
294
-
295
- dfxJson(canisterName : string, wasmPath = 'canister.wasm', didPath = 'canister.did') {
296
- let canisters : Record<string, any> = {};
297
- if (canisterName) {
298
- canisters[canisterName] = {
299
- type: 'custom',
300
- wasm: wasmPath,
301
- candid: didPath,
302
- };
303
- }
304
-
305
- return {
306
- version: 1,
307
- canisters,
308
- dfx: getDfxVersion(),
309
- defaults: {
310
- build: {
311
- packtool: 'mops sources',
312
- },
313
- },
314
- networks: {
315
- local: {
316
- type: 'ephemeral',
317
- bind: '127.0.0.1:4945',
318
- },
319
- },
320
- };
321
- }
33
+ type: "dfx" | "pocket-ic" | "dfx-pocket-ic" = "dfx";
34
+ verbose = false;
35
+ canisters: Record<
36
+ string,
37
+ { cwd: string; canisterId: string; actor: any; stream: PassThrough }
38
+ > = {};
39
+ pocketIcServer?: PocketIcServer | PocketIcServerMops;
40
+ pocketIc?: PocketIc | PocketIcMops;
41
+ dfxProcess?: ChildProcessWithoutNullStreams;
42
+ dir: string = ""; // absolute path (/.../.mops/.test/)
43
+ ttl = 60;
44
+
45
+ async start({ type, dir, verbose, silent }: StartOptions = {}) {
46
+ this.type = type ?? this.type;
47
+ this.verbose = verbose ?? this.verbose;
48
+ this.dir = dir ?? this.dir;
49
+
50
+ silent || console.log(`Starting ${this.type} replica...`);
51
+
52
+ if (this.type === "dfx" || this.type === "dfx-pocket-ic") {
53
+ fs.mkdirSync(this.dir, { recursive: true });
54
+ fs.writeFileSync(
55
+ path.join(this.dir, "dfx.json"),
56
+ JSON.stringify(this.dfxJson(""), null, 2),
57
+ );
58
+ fs.writeFileSync(
59
+ path.join(this.dir, "canister.did"),
60
+ "service : { runTests: () -> (); }",
61
+ );
62
+
63
+ await this.stop();
64
+
65
+ this.dfxProcess = spawn(
66
+ "dfx",
67
+ [
68
+ "start",
69
+ this.type === "dfx-pocket-ic" ? "--pocketic" : "",
70
+ "--clean",
71
+ this.verbose ? "" : "-qqqq",
72
+ "--artificial-delay",
73
+ "0",
74
+ ]
75
+ .filter((x) => x)
76
+ .flat(),
77
+ { cwd: this.dir },
78
+ );
79
+
80
+ // process canister logs
81
+ this._attachCanisterLogHandler(this.dfxProcess);
82
+
83
+ this.dfxProcess.stdout.on("data", (data) => {
84
+ if (this.verbose) {
85
+ console.log("DFX:", data.toString());
86
+ }
87
+ });
88
+
89
+ this.dfxProcess.stderr.on("data", (data) => {
90
+ if (this.verbose) {
91
+ console.error("DFX:", data.toString());
92
+ }
93
+ if (data.toString().includes("Failed to bind socket to")) {
94
+ console.error(chalk.red(data.toString()));
95
+ console.log("Please try again after some time");
96
+ process.exit(11);
97
+ }
98
+ });
99
+
100
+ // await for dfx to start
101
+ let ok = false;
102
+ while (!ok) {
103
+ await fetch("http://127.0.0.1:4945/api/v2/status")
104
+ .then((res) => {
105
+ if (res.status === 200) {
106
+ ok = true;
107
+ }
108
+ })
109
+ .catch(() => {})
110
+ .finally(() => {
111
+ return new Promise((resolve) => setTimeout(resolve, 1000));
112
+ });
113
+ }
114
+ } else {
115
+ let pocketIcBin = await toolchain.bin("pocket-ic");
116
+
117
+ let config = readConfig();
118
+ if (
119
+ config.toolchain?.["pocket-ic"] !== "4.0.0" &&
120
+ !config.toolchain?.["pocket-ic"]?.startsWith("9.")
121
+ ) {
122
+ console.error(
123
+ "Current Mops CLI only supports pocket-ic 9.x.x and 4.0.0",
124
+ );
125
+ process.exit(1);
126
+ }
127
+
128
+ // pocket-ic 9.x.x
129
+ if (config.toolchain?.["pocket-ic"]?.startsWith("9.")) {
130
+ this.pocketIcServer = await PocketIcServerMops.start({
131
+ showRuntimeLogs: false,
132
+ showCanisterLogs: false,
133
+ binPath: pocketIcBin,
134
+ ttl: this.ttl,
135
+ });
136
+ this.pocketIc = await PocketIcMops.create(this.pocketIcServer.getUrl());
137
+ }
138
+ // pocket-ic 4.0.0
139
+ else {
140
+ this.pocketIcServer = await PocketIcServer.start({
141
+ showRuntimeLogs: false,
142
+ showCanisterLogs: false,
143
+ binPath: pocketIcBin,
144
+ ttl: this.ttl,
145
+ });
146
+ this.pocketIc = await PocketIc.create(this.pocketIcServer.getUrl());
147
+ }
148
+
149
+ // process canister logs
150
+ this._attachCanisterLogHandler(
151
+ this.pocketIcServer.serverProcess as ChildProcessWithoutNullStreams,
152
+ );
153
+ }
154
+ }
155
+
156
+ _attachCanisterLogHandler(proc: ChildProcessWithoutNullStreams) {
157
+ let curData = "";
158
+ proc.stderr.on("data", (data) => {
159
+ curData = curData + data.toString();
160
+
161
+ if (curData.includes("\n")) {
162
+ let chunk = curData.split("\n").slice(0, -1).join("\n");
163
+ let matches = [...chunk.matchAll(/\[Canister ([a-z0-9-]+)\] (.*)/g)];
164
+
165
+ for (let match of matches) {
166
+ let [, canisterId, msg] = match;
167
+ let stream = this.getCanisterStream(canisterId || "");
168
+ if (stream) {
169
+ stream.write(msg);
170
+ }
171
+ }
172
+
173
+ if (matches.length) {
174
+ curData = curData.split("\n").slice(-1).join("\n");
175
+ }
176
+ }
177
+ });
178
+ }
179
+
180
+ async stop(sigint = false) {
181
+ if (this.type === "dfx" || this.type === "dfx-pocket-ic") {
182
+ if (this.dfxProcess) {
183
+ let killed = this.dfxProcess.kill();
184
+
185
+ if (!killed) {
186
+ this.dfxProcess.kill("SIGKILL");
187
+ }
188
+
189
+ // give replica some time to stop
190
+ let i = 0;
191
+ while (this.dfxProcess.exitCode === null) {
192
+ i++;
193
+ await new Promise((resolve) => {
194
+ setTimeout(resolve, 500);
195
+ });
196
+ if (i >= 20) {
197
+ break;
198
+ }
199
+ }
200
+
201
+ // // make sure replica is stopped
202
+ // try {
203
+ // // execSync('dfx killall', {cwd: this.dir, timeout: 3_000, stdio: ['pipe', this.verbose ? 'inherit' : 'ignore', 'pipe']});
204
+ // execSync('dfx stop' + (this.verbose ? '' : ' -qqqq'), {cwd: this.dir, timeout: 3_000, stdio: ['pipe', this.verbose ? 'inherit' : 'ignore', 'pipe']});
205
+ // }
206
+ // catch {}
207
+ }
208
+ } else if (this.pocketIc && this.pocketIcServer) {
209
+ if (!sigint) {
210
+ await this.pocketIc.tearDown(); // error 'fetch failed' if run on SIGINT
211
+ }
212
+ await this.pocketIcServer.stop();
213
+ }
214
+ }
215
+
216
+ async deploy(
217
+ name: string,
218
+ wasm: string,
219
+ idlFactory: IDL.InterfaceFactory,
220
+ cwd: string = process.cwd(),
221
+ signal?: AbortSignal,
222
+ ) {
223
+ if (this.type === "dfx" || this.type === "dfx-pocket-ic") {
224
+ // prepare dfx.json for current canister
225
+ let dfxJson = path.join(this.dir, "dfx.json");
226
+
227
+ let oldDfxJsonData;
228
+ if (fs.existsSync(dfxJson)) {
229
+ oldDfxJsonData = JSON.parse(fs.readFileSync(dfxJson).toString());
230
+ }
231
+ let newDfxJsonData = this.dfxJson(name, name + ".wasm");
232
+
233
+ if (oldDfxJsonData.canisters) {
234
+ newDfxJsonData.canisters = Object.assign(
235
+ oldDfxJsonData.canisters,
236
+ newDfxJsonData.canisters,
237
+ );
238
+ }
239
+
240
+ fs.mkdirSync(this.dir, { recursive: true });
241
+ fs.writeFileSync(dfxJson, JSON.stringify(newDfxJsonData, null, 2));
242
+
243
+ await spawnAsync(
244
+ "dfx",
245
+ [
246
+ "deploy",
247
+ name,
248
+ "--mode",
249
+ "reinstall",
250
+ "--yes",
251
+ "--identity",
252
+ "anonymous",
253
+ ],
254
+ {
255
+ cwd: this.dir,
256
+ signal,
257
+ stdio: this.verbose ? "pipe" : ["pipe", "ignore", "pipe"],
258
+ },
259
+ ).catch((error) => {
260
+ if (error.code === "ABORT_ERR") {
261
+ return { stderr: "" };
262
+ }
263
+ throw error;
264
+ });
265
+
266
+ if (signal?.aborted) {
267
+ return;
268
+ }
269
+
270
+ await spawnAsync(
271
+ "dfx",
272
+ ["ledger", "fabricate-cycles", "--canister", name, "--t", "100"],
273
+ {
274
+ cwd: this.dir,
275
+ signal,
276
+ stdio: this.verbose ? "pipe" : ["pipe", "ignore", "pipe"],
277
+ },
278
+ ).catch((error) => {
279
+ if (error.code === "ABORT_ERR") {
280
+ return { stderr: "" };
281
+ }
282
+ throw error;
283
+ });
284
+
285
+ if (signal?.aborted) {
286
+ return;
287
+ }
288
+
289
+ let canisterId = execSync(`dfx canister id ${name}`, { cwd: this.dir })
290
+ .toString()
291
+ .trim();
292
+
293
+ let actor = Actor.createActor(idlFactory, {
294
+ agent: await HttpAgent.create({
295
+ host: "http://127.0.0.1:4945",
296
+ shouldFetchRootKey: true,
297
+ }),
298
+ canisterId,
299
+ });
300
+
301
+ this.canisters[name] = {
302
+ cwd,
303
+ canisterId,
304
+ actor,
305
+ stream: new PassThrough(),
306
+ };
307
+ } else if (this.pocketIc) {
308
+ type PocketIcSetupCanister = PocketIcMops["setupCanister"] &
309
+ PocketIc["setupCanister"];
310
+ let { canisterId, actor } = await (
311
+ this.pocketIc.setupCanister as PocketIcSetupCanister
312
+ )({
313
+ wasm,
314
+ idlFactory,
315
+ });
316
+
317
+ if (signal?.aborted) {
318
+ return;
319
+ }
320
+
321
+ await this.pocketIc.addCycles(canisterId as any, 1_000_000_000_000);
322
+
323
+ if (signal?.aborted) {
324
+ return;
325
+ }
326
+
327
+ this.canisters[name] = {
328
+ cwd,
329
+ canisterId: canisterId.toText(),
330
+ actor,
331
+ stream: new PassThrough(),
332
+ };
333
+ }
334
+
335
+ if (!this.canisters[name]) {
336
+ throw new Error(`Canister ${name} not found`);
337
+ }
338
+
339
+ return this.canisters[name];
340
+ }
341
+
342
+ getActor(name: string): unknown {
343
+ if (!this.canisters[name]) {
344
+ throw new Error(`Canister ${name} not found`);
345
+ }
346
+ return this.canisters[name]?.actor;
347
+ }
348
+
349
+ getCanister(name: string) {
350
+ return this.canisters[name];
351
+ }
352
+
353
+ getCanisterId(name: string): string {
354
+ return this.canisters[name]?.canisterId || "";
355
+ }
356
+
357
+ getCanisterStream(canisterId: string): PassThrough | null {
358
+ for (let canister of Object.values(this.canisters)) {
359
+ if (canister.canisterId === canisterId) {
360
+ return canister.stream;
361
+ }
362
+ }
363
+ return null;
364
+ }
365
+
366
+ dfxJson(
367
+ canisterName: string,
368
+ wasmPath = "canister.wasm",
369
+ didPath = "canister.did",
370
+ ) {
371
+ let canisters: Record<string, any> = {};
372
+ if (canisterName) {
373
+ canisters[canisterName] = {
374
+ type: "custom",
375
+ wasm: wasmPath,
376
+ candid: didPath,
377
+ };
378
+ }
379
+
380
+ return {
381
+ version: 1,
382
+ canisters,
383
+ dfx: getDfxVersion(),
384
+ defaults: {
385
+ build: {
386
+ packtool: "mops sources",
387
+ },
388
+ },
389
+ networks: {
390
+ local: {
391
+ type: "ephemeral",
392
+ bind: "127.0.0.1:4945",
393
+ },
394
+ },
395
+ };
396
+ }
322
397
  }