ic-mops 0.37.0-pre.0 → 0.37.1

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 (102) hide show
  1. package/bin/moc-wrapper.sh +1 -52
  2. package/cache.ts +2 -2
  3. package/cli.ts +27 -6
  4. package/commands/add.ts +3 -1
  5. package/commands/bench-replica.ts +5 -3
  6. package/commands/docs.ts +9 -6
  7. package/commands/install-all.ts +7 -2
  8. package/commands/install.ts +5 -1
  9. package/commands/publish.ts +2 -0
  10. package/commands/test/test.ts +42 -19
  11. package/commands/toolchain/index.ts +214 -42
  12. package/commands/toolchain/moc.ts +19 -4
  13. package/commands/toolchain/pocket-ic.ts +43 -6
  14. package/commands/toolchain/toolchain-utils.ts +14 -5
  15. package/commands/toolchain/wasmtime.ts +19 -4
  16. package/dist/bin/mops.js +0 -0
  17. package/dist/cache.js +2 -2
  18. package/dist/cli.js +24 -6
  19. package/dist/commands/add.js +2 -1
  20. package/dist/commands/bench-replica.d.ts +1 -1
  21. package/dist/commands/bench-replica.js +5 -3
  22. package/dist/commands/docs.js +9 -6
  23. package/dist/commands/install-all.js +5 -2
  24. package/dist/commands/install.js +5 -1
  25. package/dist/commands/publish.js +1 -0
  26. package/dist/commands/test/test.js +41 -19
  27. package/dist/commands/toolchain/index.d.ts +17 -3
  28. package/dist/commands/toolchain/index.js +187 -40
  29. package/dist/commands/toolchain/moc.d.ts +5 -1
  30. package/dist/commands/toolchain/moc.js +16 -4
  31. package/dist/commands/toolchain/mocv.js +0 -1
  32. package/dist/commands/toolchain/pocket-ic.d.ts +9 -1
  33. package/dist/commands/toolchain/pocket-ic.js +40 -6
  34. package/dist/commands/toolchain/toolchain-utils.d.ts +1 -1
  35. package/dist/commands/toolchain/toolchain-utils.js +13 -4
  36. package/dist/commands/toolchain/wasmtime.d.ts +5 -1
  37. package/dist/commands/toolchain/wasmtime.js +16 -4
  38. package/dist/integrity.js +40 -15
  39. package/dist/mops.js +1 -1
  40. package/dist/package.json +3 -2
  41. package/dist/pic-js/examples/clock/tests/clock/index.d.ts +1 -0
  42. package/dist/pic-js/examples/clock/tests/clock/index.js +5 -0
  43. package/dist/pic-js/examples/clock/tests/jest.config.d.ts +3 -0
  44. package/dist/pic-js/examples/clock/tests/jest.config.js +8 -0
  45. package/dist/pic-js/examples/clock/tests/src/clock.spec.d.ts +1 -0
  46. package/dist/pic-js/examples/clock/tests/src/clock.spec.js +48 -0
  47. package/dist/pic-js/examples/counter/tests/counter/index.d.ts +1 -0
  48. package/dist/pic-js/examples/counter/tests/counter/index.js +5 -0
  49. package/dist/pic-js/examples/counter/tests/jest.config.d.ts +3 -0
  50. package/dist/pic-js/examples/counter/tests/jest.config.js +8 -0
  51. package/dist/pic-js/examples/counter/tests/src/counter.spec.d.ts +1 -0
  52. package/dist/pic-js/examples/counter/tests/src/counter.spec.js +80 -0
  53. package/dist/pic-js/examples/todo/tests/jest.config.d.ts +3 -0
  54. package/dist/pic-js/examples/todo/tests/jest.config.js +8 -0
  55. package/dist/pic-js/examples/todo/tests/src/todo.spec.d.ts +1 -0
  56. package/dist/pic-js/examples/todo/tests/src/todo.spec.js +211 -0
  57. package/dist/pic-js/examples/todo/tests/todo/index.d.ts +1 -0
  58. package/dist/pic-js/examples/todo/tests/todo/index.js +5 -0
  59. package/dist/pic-js/packages/pic/src/error.d.ts +12 -0
  60. package/dist/pic-js/packages/pic/src/error.js +36 -0
  61. package/dist/pic-js/packages/pic/src/http-client.d.ts +15 -0
  62. package/dist/pic-js/packages/pic/src/http-client.js +37 -0
  63. package/dist/pic-js/packages/pic/src/identity.d.ts +66 -0
  64. package/dist/pic-js/packages/pic/src/identity.js +86 -0
  65. package/dist/pic-js/packages/pic/src/index.d.ts +4 -0
  66. package/dist/pic-js/packages/pic/src/index.js +8 -0
  67. package/dist/pic-js/packages/pic/src/management-canister.d.ts +30 -0
  68. package/dist/pic-js/packages/pic/src/management-canister.js +43 -0
  69. package/dist/pic-js/packages/pic/src/pocket-ic-actor.d.ts +83 -0
  70. package/dist/pic-js/packages/pic/src/pocket-ic-actor.js +58 -0
  71. package/dist/pic-js/packages/pic/src/pocket-ic-client-types.d.ts +61 -0
  72. package/dist/pic-js/packages/pic/src/pocket-ic-client-types.js +2 -0
  73. package/dist/pic-js/packages/pic/src/pocket-ic-client.d.ts +24 -0
  74. package/dist/pic-js/packages/pic/src/pocket-ic-client.js +123 -0
  75. package/dist/pic-js/packages/pic/src/pocket-ic-server.d.ts +10 -0
  76. package/dist/pic-js/packages/pic/src/pocket-ic-server.js +55 -0
  77. package/dist/pic-js/packages/pic/src/pocket-ic-types.d.ts +40 -0
  78. package/dist/pic-js/packages/pic/src/pocket-ic-types.js +2 -0
  79. package/dist/pic-js/packages/pic/src/pocket-ic.d.ts +447 -0
  80. package/dist/pic-js/packages/pic/src/pocket-ic.js +551 -0
  81. package/dist/pic-js/packages/pic/src/util/candid.d.ts +1 -0
  82. package/dist/pic-js/packages/pic/src/util/candid.js +7 -0
  83. package/dist/pic-js/packages/pic/src/util/encoding.d.ts +5 -0
  84. package/dist/pic-js/packages/pic/src/util/encoding.js +19 -0
  85. package/dist/pic-js/packages/pic/src/util/fs.d.ts +4 -0
  86. package/dist/pic-js/packages/pic/src/util/fs.js +29 -0
  87. package/dist/pic-js/packages/pic/src/util/index.d.ts +5 -0
  88. package/dist/pic-js/packages/pic/src/util/index.js +21 -0
  89. package/dist/pic-js/packages/pic/src/util/os.d.ts +4 -0
  90. package/dist/pic-js/packages/pic/src/util/os.js +19 -0
  91. package/dist/pic-js/packages/pic/src/util/poll.d.ts +5 -0
  92. package/dist/pic-js/packages/pic/src/util/poll.js +28 -0
  93. package/dist/templates/mops-test.yml +4 -4
  94. package/dist/vessel.js +5 -1
  95. package/integrity.ts +57 -17
  96. package/mops.ts +1 -1
  97. package/package.json +3 -2
  98. package/templates/mops-test.yml +4 -4
  99. package/vessel.ts +6 -1
  100. package/bun.lockb +0 -0
  101. package/commands/toolchain/mocv.ts +0 -313
  102. package/moc-wrapper.ts +0 -10
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.poll = void 0;
4
+ const DEFAULT_POLL_INTERVAL_MS = 20;
5
+ const DEFAULT_POLL_TIMEOUT_MS = 5000;
6
+ async function poll(cb, options) {
7
+ const intervalMs = options?.intervalMs ?? DEFAULT_POLL_INTERVAL_MS;
8
+ const timeoutMs = options?.timeoutMs ?? DEFAULT_POLL_TIMEOUT_MS;
9
+ const startTimeMs = Date.now();
10
+ return new Promise((resolve, reject) => {
11
+ async function runPoll() {
12
+ const currentTimeMs = Date.now();
13
+ try {
14
+ const result = await cb();
15
+ clearInterval(interval);
16
+ return resolve(result);
17
+ }
18
+ catch (e) {
19
+ if (currentTimeMs - startTimeMs >= timeoutMs) {
20
+ clearInterval(interval);
21
+ return reject(e);
22
+ }
23
+ }
24
+ }
25
+ const interval = setInterval(runPoll, intervalMs);
26
+ });
27
+ }
28
+ exports.poll = poll;
@@ -15,10 +15,7 @@ jobs:
15
15
  - uses: actions/checkout@v3
16
16
  - uses: actions/setup-node@v3
17
17
  with:
18
- node-version: 18
19
-
20
- - name: install moc
21
- run: npx mocv use latest
18
+ node-version: 20
22
19
 
23
20
  - name: install mops
24
21
  run: npm i ic-mops -g
@@ -26,5 +23,8 @@ jobs:
26
23
  - name: install mops packages
27
24
  run: mops install
28
25
 
26
+ - name: install moc
27
+ run: mops toolchain use moc latest
28
+
29
29
  - name: run tests
30
30
  run: mops test
package/dist/vessel.js CHANGED
@@ -3,7 +3,7 @@ import path from 'node:path';
3
3
  import { deleteSync } from 'del';
4
4
  import { execaCommand } from 'execa';
5
5
  import chalk from 'chalk';
6
- import logUpdate from 'log-update';
6
+ import { createLogUpdate } from 'log-update';
7
7
  import got from 'got';
8
8
  import decompress from 'decompress';
9
9
  import { pipeline } from 'stream';
@@ -119,6 +119,7 @@ export const installFromGithub = async (name, repo, { verbose = false, dep = fal
119
119
  let { branch, commitHash } = parseGithubURL(repo);
120
120
  let dir = formatGithubDir(name, repo);
121
121
  let cacheName = `_github/${name}#${branch}` + (commitHash ? `@${commitHash}` : '');
122
+ let logUpdate = createLogUpdate(process.stdout, { showCursor: true });
122
123
  if (existsSync(dir)) {
123
124
  silent || logUpdate(`${dep ? 'Dependency' : 'Installing'} ${repo} (local cache)`);
124
125
  }
@@ -145,6 +146,9 @@ export const installFromGithub = async (name, repo, { verbose = false, dep = fal
145
146
  if (verbose) {
146
147
  silent || logUpdate.done();
147
148
  }
149
+ else {
150
+ logUpdate.clear();
151
+ }
148
152
  const config = await readVesselConfig(dir, { silent });
149
153
  if (config) {
150
154
  for (const { name, repo } of config.dependencies) {
package/integrity.ts CHANGED
@@ -2,16 +2,28 @@ import fs from 'node:fs';
2
2
  import path from 'node:path';
3
3
  import {sha256} from '@noble/hashes/sha256';
4
4
  import {bytesToHex} from '@noble/hashes/utils';
5
- import {getDependencyType, getRootDir} from './mops.js';
5
+ import {getDependencyType, getRootDir, readConfig} from './mops.js';
6
6
  import {mainActor} from './api/actors.js';
7
7
  import {resolvePackages} from './resolve-packages.js';
8
8
 
9
+ type LockFileGeneric = {
10
+ version: number;
11
+ };
12
+
9
13
  type LockFileV1 = {
10
14
  version: 1;
11
15
  mopsTomlHash: string;
12
16
  hashes: Record<string, Record<string, string>>;
13
17
  };
14
18
 
19
+ type LockFileV2 = {
20
+ version: 2;
21
+ mopsTomlDepsHash: string;
22
+ hashes: Record<string, Record<string, string>>;
23
+ };
24
+
25
+ type LockFile = LockFileV1 | LockFileV2;
26
+
15
27
  export async function checkIntegrity(lock?: 'check' | 'update' | 'ignore') {
16
28
  let force = !!lock;
17
29
 
@@ -63,6 +75,19 @@ function getMopsTomlHash(): string {
63
75
  return bytesToHex(sha256(fs.readFileSync(getRootDir() + '/mops.toml')));
64
76
  }
65
77
 
78
+ function getMopsTomlDepsHash(): string {
79
+ let config = readConfig();
80
+ let deps = config.dependencies || {};
81
+ let devDeps = config['dev-dependencies'] || {};
82
+ let allDeps = {...deps, ...devDeps};
83
+ // sort allDeps by key
84
+ let sortedDeps = Object.keys(allDeps).sort().reduce((acc, key) => {
85
+ acc[key] = allDeps[key]?.version || allDeps[key]?.repo || allDeps[key]?.path || '';
86
+ return acc;
87
+ }, {} as Record<string, string>);
88
+ return bytesToHex(sha256(JSON.stringify(sortedDeps)));
89
+ }
90
+
66
91
  // compare hashes of local files with hashes from the registry
67
92
  export async function checkRemote() {
68
93
  let fileHashesFromRegistry = await getFileHashesFromRegistry();
@@ -88,18 +113,18 @@ export async function updateLockFile() {
88
113
 
89
114
  // if lock file exists and mops.toml hasn't changed, don't update it
90
115
  if (fs.existsSync(lockFile)) {
91
- let lockFileJson: LockFileV1 = JSON.parse(fs.readFileSync(lockFile).toString());
92
- let mopsTomlHash = getMopsTomlHash();
93
- if (mopsTomlHash === lockFileJson.mopsTomlHash) {
116
+ let lockFileJson: LockFileV2 = JSON.parse(fs.readFileSync(lockFile).toString());
117
+ let mopsTomlDepsHash = getMopsTomlDepsHash();
118
+ if (mopsTomlDepsHash === lockFileJson.mopsTomlDepsHash) {
94
119
  return;
95
120
  }
96
121
  }
97
122
 
98
123
  let fileHashes = await getFileHashesFromRegistry();
99
124
 
100
- let lockFileJson: LockFileV1 = {
101
- version: 1,
102
- mopsTomlHash: getMopsTomlHash(),
125
+ let lockFileJson: LockFileV2 = {
126
+ version: 2,
127
+ mopsTomlDepsHash: getMopsTomlDepsHash(),
103
128
  hashes: fileHashes.reduce((acc, [packageId, fileHashes]) => {
104
129
  acc[packageId] = fileHashes.reduce((acc, [fileId, hash]) => {
105
130
  acc[fileId] = bytesToHex(new Uint8Array(hash));
@@ -126,23 +151,38 @@ export async function checkLockFile(force = false) {
126
151
  return;
127
152
  }
128
153
 
129
- let lockFileJson: LockFileV1 = JSON.parse(fs.readFileSync(lockFile).toString());
154
+ let lockFileJsonGeneric: LockFileGeneric = JSON.parse(fs.readFileSync(lockFile).toString());
130
155
  let packageIds = await getResolvedMopsPackageIds();
131
156
 
132
157
  // check lock file version
133
- if (lockFileJson.version !== 1) {
158
+ if (lockFileJsonGeneric.version !== 1 && lockFileJsonGeneric.version !== 2) {
134
159
  console.error('Integrity check failed');
135
- console.error(`Invalid lock file version: ${lockFileJson.version}. Supported versions: 1`);
160
+ console.error(`Invalid lock file version: ${lockFileJsonGeneric.version}. Supported versions: 1`);
136
161
  process.exit(1);
137
162
  }
138
163
 
139
- // check mops.toml hash
140
- if (lockFileJson.mopsTomlHash !== getMopsTomlHash()) {
141
- console.error('Integrity check failed');
142
- console.error('Mismatched mops.toml hash');
143
- console.error(`Locked hash: ${lockFileJson.mopsTomlHash}`);
144
- console.error(`Actual hash: ${getMopsTomlHash()}`);
145
- process.exit(1);
164
+ let lockFileJson = lockFileJsonGeneric as LockFile;
165
+
166
+ // V1: check mops.toml hash
167
+ if (lockFileJson.version === 1) {
168
+ if (lockFileJson.mopsTomlHash !== getMopsTomlHash()) {
169
+ console.error('Integrity check failed');
170
+ console.error('Mismatched mops.toml hash');
171
+ console.error(`Locked hash: ${lockFileJson.mopsTomlHash}`);
172
+ console.error(`Actual hash: ${getMopsTomlHash()}`);
173
+ process.exit(1);
174
+ }
175
+ }
176
+
177
+ // V2: check mops.toml deps hash
178
+ if (lockFileJson.version === 2) {
179
+ if (lockFileJson.mopsTomlDepsHash !== getMopsTomlDepsHash()) {
180
+ console.error('Integrity check failed');
181
+ console.error('Mismatched mops.toml dependencies hash');
182
+ console.error(`Locked hash: ${lockFileJson.mopsTomlDepsHash}`);
183
+ console.error(`Actual hash: ${getMopsTomlDepsHash()}`);
184
+ process.exit(1);
185
+ }
146
186
  }
147
187
 
148
188
  // check number of packages
package/mops.ts CHANGED
@@ -210,7 +210,7 @@ export function readConfig(configFile = getClosestConfigFile()): Config {
210
210
  }
211
211
 
212
212
  export function writeConfig(config: Config, configFile = getClosestConfigFile()) {
213
- let resConfig: any = {...config};
213
+ let resConfig: any = JSON.parse(JSON.stringify(config));
214
214
 
215
215
  let deps = resConfig.dependencies || {};
216
216
  Object.entries(config.dependencies || {}).forEach(([name, {repo, path, version}]) => {
package/package.json CHANGED
@@ -1,9 +1,10 @@
1
1
  {
2
2
  "name": "ic-mops",
3
- "version": "0.37.0-pre.0",
3
+ "version": "0.37.1",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "mops": "dist/bin/mops.js",
7
+ "ic-mops": "dist/bin/mops.js",
7
8
  "moc-wrapper": "bin/moc-wrapper.sh"
8
9
  },
9
10
  "files": [
@@ -38,7 +39,6 @@
38
39
  "@dfinity/identity": "^0.19.3",
39
40
  "@dfinity/identity-secp256k1": "^0.19.3",
40
41
  "@dfinity/principal": "^0.19.3",
41
- "@hadronous/pic": "0.2.0",
42
42
  "@iarna/toml": "^2.2.5",
43
43
  "@noble/hashes": "1.3.2",
44
44
  "as-table": "^1.0.55",
@@ -68,6 +68,7 @@
68
68
  "node-fetch": "^3.3.2",
69
69
  "octokit": "3.1.2",
70
70
  "pem-file": "^1.0.1",
71
+ "pic-ic": "0.3.2",
71
72
  "prompts": "^2.4.2",
72
73
  "stream-to-promise": "^3.0.0",
73
74
  "string-width": "7.0.0",
@@ -15,10 +15,7 @@ jobs:
15
15
  - uses: actions/checkout@v3
16
16
  - uses: actions/setup-node@v3
17
17
  with:
18
- node-version: 18
19
-
20
- - name: install moc
21
- run: npx mocv use latest
18
+ node-version: 20
22
19
 
23
20
  - name: install mops
24
21
  run: npm i ic-mops -g
@@ -26,5 +23,8 @@ jobs:
26
23
  - name: install mops packages
27
24
  run: mops install
28
25
 
26
+ - name: install moc
27
+ run: mops toolchain use moc latest
28
+
29
29
  - name: run tests
30
30
  run: mops test
package/vessel.ts CHANGED
@@ -3,7 +3,7 @@ import path from 'node:path';
3
3
  import {deleteSync} from 'del';
4
4
  import {execaCommand} from 'execa';
5
5
  import chalk from 'chalk';
6
- import logUpdate from 'log-update';
6
+ import {createLogUpdate} from 'log-update';
7
7
  import got from 'got';
8
8
  import decompress from 'decompress';
9
9
  import {pipeline} from 'stream';
@@ -153,6 +153,8 @@ export const installFromGithub = async (name: string, repo: string, {verbose = f
153
153
  let dir = formatGithubDir(name, repo);
154
154
  let cacheName = `_github/${name}#${branch}` + (commitHash ? `@${commitHash}` : '');
155
155
 
156
+ let logUpdate = createLogUpdate(process.stdout, {showCursor: true});
157
+
156
158
  if (existsSync(dir)) {
157
159
  silent || logUpdate(`${dep ? 'Dependency' : 'Installing'} ${repo} (local cache)`);
158
160
  }
@@ -184,6 +186,9 @@ export const installFromGithub = async (name: string, repo: string, {verbose = f
184
186
  if (verbose) {
185
187
  silent || logUpdate.done();
186
188
  }
189
+ else {
190
+ logUpdate.clear();
191
+ }
187
192
 
188
193
  const config = await readVesselConfig(dir, {silent});
189
194
 
package/bun.lockb DELETED
Binary file
@@ -1,313 +0,0 @@
1
- // @ts-nocheck
2
- import fs from 'fs-extra';
3
- import os from 'os';
4
- import path from 'path';
5
- import tar from 'tar';
6
- import {program} from 'commander';
7
- import prompts from 'prompts';
8
- import {Octokit} from 'octokit';
9
- import chalk from 'chalk';
10
- import fetch from 'node-fetch';
11
-
12
- if (process.platform == 'win32') {
13
- console.log('Windows is not supported. Please use WSL');
14
- process.exit(1);
15
- }
16
-
17
- let zshrc = path.join(os.homedir(), '.zshrc');
18
- let bashrc = path.join(os.homedir(), '.bashrc');
19
- let cacheDir = path.join(os.homedir(), '.cache/mocv');
20
- let curVersionFile = path.join(cacheDir, 'versions/current/version.txt');
21
- let tmpDir = path.join(cacheDir, '.tmp');
22
- let file = path.join(tmpDir, 'moc.tar.gz');
23
-
24
- let download = async (version: string, {silent = false} = {}) => {
25
- if (!version) {
26
- console.log('version is not defined');
27
- process.exit(1);
28
- }
29
- if (isCached(version)) {
30
- return;
31
- }
32
-
33
-
34
- let url;
35
- if (parseInt(version.replaceAll('.', '')) >= parseInt('0.9.5'.replaceAll('.', ''))) {
36
- let platfrom = process.platform == 'darwin' ? 'Darwin' : 'Linux';
37
- let arch = process.arch.startsWith('arm') ? 'arm64' : 'x86_64';
38
- // currently only x64 binaries are available
39
- arch = 'x86_64';
40
- url = `https://github.com/dfinity/motoko/releases/download/${version}/motoko-${platfrom}-${arch}-${version}.tar.gz`;
41
- }
42
- else {
43
- let platfrom = process.platform == 'darwin' ? 'macos' : 'linux64';
44
- url = `https://github.com/dfinity/motoko/releases/download/${version}/motoko-${platfrom}-${version}.tar.gz`;
45
- }
46
-
47
- silent || console.log(`Downloading ${url}`);
48
-
49
- let res = await fetch(url);
50
-
51
- if (res.status !== 200) {
52
- console.log(`ERR ${res.status} ${url}`);
53
- console.log(`moc version '${version}' not found`);
54
- process.exit(1);
55
- }
56
-
57
- let arrayBuffer = await res.arrayBuffer();
58
- let buffer = Buffer.from(arrayBuffer);
59
-
60
- fs.mkdirSync(tmpDir, {recursive: true});
61
- fs.writeFileSync(file, buffer);
62
-
63
- let verDir = path.join(cacheDir, 'versions', version);
64
- fs.mkdirSync(verDir, {recursive: true});
65
- await tar.extract({
66
- file,
67
- cwd: verDir,
68
- });
69
-
70
- fs.rmSync(file);
71
- };
72
-
73
- let isCached = (version: string) => {
74
- let dir = path.join(cacheDir, 'versions', version);
75
- return fs.existsSync(path.join(dir, 'moc'))
76
- && fs.existsSync(path.join(dir, 'mo-doc'))
77
- && fs.existsSync(path.join(dir, 'mo-ide'));
78
- };
79
-
80
- let setCurrent = (version: string) => {
81
- fs.copySync(path.join(cacheDir, 'versions', version), path.join(cacheDir, 'versions/current'), {recursive: true});
82
- fs.writeFileSync(curVersionFile, version);
83
- };
84
-
85
- let getCurrent = () => {
86
- if (fs.existsSync(curVersionFile)) {
87
- return fs.readFileSync(curVersionFile).toString();
88
- }
89
- };
90
-
91
- let getLatest = async () => {
92
- let releases = await getReleases();
93
- return releases[0].tag_name;
94
- };
95
-
96
- let getReleases = async () => {
97
- let octokit = new Octokit;
98
- let res = await octokit.request('GET /repos/dfinity/motoko/releases', {
99
- per_page: 10,
100
- headers: {
101
- 'X-GitHub-Api-Version': '2022-11-28'
102
- }
103
- });
104
- if (res.status !== 200) {
105
- console.log('Releases fetch error');
106
- process.exit(1);
107
- }
108
- return res.data;
109
- };
110
-
111
- let use = async (version: string) => {
112
- if (version === 'latest') {
113
- version = await getLatest();
114
- }
115
- await download(version);
116
- setCurrent(version);
117
- console.log(`Selected moc ${version}`);
118
-
119
- // update github env
120
- if (process.env.GITHUB_ENV) {
121
- fs.appendFileSync(process.env.GITHUB_ENV, `DFX_MOC_PATH=${path.join(cacheDir, 'versions', version)}/moc\n`);
122
- }
123
- };
124
-
125
- program.name('mocv')
126
- .action(async (_, config) => {
127
- if (config.args.length) {
128
- console.log(`unknown command '${config.args.join(' ')}'`);
129
- process.exit(1);
130
- }
131
- let releases = await getReleases();
132
- let versions = releases.map((item: {tag_name: any;}) => item.tag_name);
133
- let current = getCurrent();
134
- let currentIndex = versions.indexOf(current);
135
-
136
- let res = await prompts({
137
- type: 'select',
138
- name: 'version',
139
- message: 'Select moc version',
140
- choices: releases.map((release: {published_at: string | number | Date; tag_name: string;}, i: any) => {
141
- let date = new Date(release.published_at).toLocaleDateString(undefined, {year: 'numeric', month: 'short', day: 'numeric'});
142
- return {
143
- title: release.tag_name + chalk.gray(` ${date}${currentIndex === i ? chalk.italic(' (current)') : ''}`),
144
- value: release.tag_name,
145
- };
146
- }),
147
- initial: currentIndex == -1 ? 0 : currentIndex,
148
- });
149
-
150
- if (!res.version) {
151
- return;
152
- }
153
-
154
- await use(res.version);
155
- });
156
-
157
- let updateShellConfig = async ({reset = false, yes = false} = {}) => {
158
- let setDfxMocPath = reset || yes;
159
- let updatePath = reset || yes;
160
-
161
- if (!setDfxMocPath) {
162
- let res = await prompts({
163
- type: 'select',
164
- name: 'setDfxMocPath',
165
- message: 'Do you want to set DFX_MOC_PATH, so `dfx` will use the current `moc` version?',
166
- choices: [
167
- {
168
- title: 'Yes',
169
- value: true,
170
- },
171
- {
172
- title: 'No',
173
- value: false,
174
- },
175
- ],
176
- });
177
- setDfxMocPath = res.setDfxMocPath;
178
- }
179
-
180
- if (!updatePath) {
181
- let res = await prompts({
182
- type: 'select',
183
- name: 'updatePath',
184
- message: 'Do you want to update PATH, so you can call `moc`, `mo-doc` and `mo-ide` from the terminal?',
185
- choices: [
186
- {
187
- title: 'Yes',
188
- value: true,
189
- },
190
- {
191
- title: 'No',
192
- value: false,
193
- },
194
- ],
195
- });
196
- updatePath = res.updatePath;
197
- }
198
-
199
- if (!setDfxMocPath && !updatePath) {
200
- console.log('Nothing to do');
201
- return;
202
- }
203
-
204
- let configFiles = [];
205
- if (reset || yes) {
206
- configFiles = [bashrc, zshrc];
207
- }
208
- else {
209
- let {configFile} = await prompts({
210
- type: 'select',
211
- name: 'configFile',
212
- message: 'Select your shell config file',
213
- choices: [
214
- {
215
- title: bashrc,
216
- value: bashrc,
217
- },
218
- {
219
- title: zshrc,
220
- value: zshrc,
221
- },
222
- ],
223
- });
224
- configFiles = [configFile];
225
- }
226
-
227
- for (let configFile of configFiles) {
228
- if (!fs.existsSync(configFile)) {
229
- console.log(`${configFile} not found`);
230
- process.exit(1);
231
- }
232
-
233
- let data = fs.readFileSync(configFile).toString();
234
- let setDfxLine = '\nexport DFX_MOC_PATH="$HOME/.cache/mocv/versions/current/moc"';
235
- let updatePathLine = '\nPATH="$HOME/.cache/mocv/versions/current:$PATH"';
236
-
237
- let newLines = [];
238
- setDfxMocPath && newLines.push(setDfxLine);
239
- updatePath && newLines.push(updatePathLine);
240
-
241
- let oldLines = [
242
- `\nexport DFX_MOC_PATH=${path.join(cacheDir, 'versions/current')}/moc\n`,
243
- setDfxLine,
244
- updatePathLine,
245
- ];
246
- for (let oldLine of oldLines) {
247
- data = data.replace(oldLine, '');
248
- }
249
-
250
- if (data.endsWith('\n\n')) {
251
- data = data.trimEnd() + '\n';
252
- }
253
-
254
- if (!reset) {
255
- if (!data.endsWith('\n')) {
256
- data += '\n';
257
- }
258
- for (let newLine of newLines) {
259
- data += newLine;
260
- }
261
- data += '\n';
262
- }
263
-
264
- fs.writeFileSync(configFile, data);
265
- };
266
-
267
- console.log('Success!');
268
- // console.log(`Run "source ${configFile}" to apply changes`);
269
- console.log('Restart terminal to apply changes');
270
- };
271
-
272
- program.command('init')
273
- .description('mocv one time initialization')
274
- .option('-y, --yes', 'Skip prompts')
275
- .action(async (options) => {
276
- updateShellConfig(options);
277
- });
278
-
279
- program.command('reset')
280
- .description('Reset changes made by `mocv init`')
281
- .action(async () => {
282
- updateShellConfig({reset: true});
283
- });
284
-
285
- program.command('use <version>')
286
- .description('Set current moc version.\nExample 1: "mocv use 0.8.4"\nExample 2: "mocv use latest"')
287
- .action(async (version) => {
288
- await use(version);
289
- });
290
-
291
- program.command('current')
292
- .description('Print current moc version')
293
- .action(async () => {
294
- console.log(getCurrent());
295
- });
296
-
297
- program.command('bin [version]')
298
- .description('Print bin directory')
299
- .action(async (version = getCurrent()) => {
300
- if (version === 'latest') {
301
- version = await getLatest();
302
- }
303
- if (!version) {
304
- console.log('No version selected. Please pass a version arg or run `mocv` or `mocv use <version>`');
305
- process.exit(1);
306
- }
307
- if (!isCached(version)) {
308
- await download(version, {silent: true});
309
- }
310
- console.log(path.join(cacheDir, 'versions', version));
311
- });
312
-
313
- program.parse();
package/moc-wrapper.ts DELETED
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env tsx
2
-
3
- import {ExecException, execSync} from 'child_process';
4
-
5
- try {
6
- execSync('/home/zen/.cache/mocv/versions/current/moc ' + process.argv.slice(2).join(' '), {cwd: process.cwd(), stdio: 'inherit'});
7
- }
8
- catch (err: unknown) {
9
- process.exit((err as ExecException).code || 1);
10
- }