ic-mops 0.36.0 → 0.37.0-pre.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.
- package/bin/moc-wrapper.sh +54 -0
- package/bin/mops.ts +3 -0
- package/cli.ts +40 -4
- package/commands/toolchain/index.ts +153 -0
- package/commands/toolchain/moc.ts +43 -0
- package/commands/toolchain/mocv.ts +313 -0
- package/commands/toolchain/pocket-ic.ts +37 -0
- package/commands/toolchain/toolchain-utils.ts +74 -0
- package/commands/toolchain/wasmtime.ts +30 -0
- package/dist/bin/mops.d.ts +1 -1
- package/dist/cli.d.ts +0 -1
- package/dist/cli.js +34 -3
- package/dist/commands/toolchain/index.d.ts +12 -0
- package/dist/commands/toolchain/index.js +127 -0
- package/dist/commands/toolchain/moc.js +4 -4
- package/dist/commands/toolchain/pocket-ic.d.ts +4 -0
- package/dist/commands/toolchain/pocket-ic.js +28 -0
- package/dist/commands/toolchain/toolchain-utils.d.ts +1 -1
- package/dist/commands/toolchain/toolchain-utils.js +9 -2
- package/dist/commands/toolchain/wasmtime.js +2 -2
- package/dist/mops.js +1 -1
- package/dist/package.json +17 -12
- package/dist/types.d.ts +7 -0
- package/global.d.ts +2 -1
- package/moc-wrapper.ts +10 -0
- package/mops.ts +2 -2
- package/package.json +17 -12
- package/types.ts +10 -1
- package/cli-local.ts +0 -3
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
#!/bin/bash
|
|
2
|
+
|
|
3
|
+
# FILE_NAME=$(basename "$0");
|
|
4
|
+
|
|
5
|
+
# function findMopsTomlFile() {
|
|
6
|
+
# if [ -f "mops.toml" ]; then
|
|
7
|
+
# printf '%s\n' "${PWD%/}/mops.toml"
|
|
8
|
+
# elif [ "$PWD" = / ]; then
|
|
9
|
+
# false
|
|
10
|
+
# else
|
|
11
|
+
# # a subshell so that we don't affect the caller's $PWD
|
|
12
|
+
# (cd .. && findMopsTomlFile "mops.toml")
|
|
13
|
+
# fi
|
|
14
|
+
# }
|
|
15
|
+
|
|
16
|
+
# mopsTomlFile="$(findMopsTomlFile)"
|
|
17
|
+
|
|
18
|
+
# cat $mopsTomlFile
|
|
19
|
+
|
|
20
|
+
# mocPath=""
|
|
21
|
+
# if [ -z "$mopsTomlFile" ]
|
|
22
|
+
# then
|
|
23
|
+
# mocPath="$HOME/.cache/mocv/versions/$(cat $HOME/.cache/mocv/versions/current/version.txt)/$FILE_NAME"
|
|
24
|
+
# # mocPath="$(DFX_WARNING=-version_check dfx cache show)/$FILE_NAME"
|
|
25
|
+
# else
|
|
26
|
+
# mocPath="$HOME/.cache/mocv/versions/$(cat $mopsTomlFile)/$FILE_NAME"
|
|
27
|
+
# fi
|
|
28
|
+
|
|
29
|
+
# fallback to dfx
|
|
30
|
+
# mocPath="$(DFX_WARNING=-version_check dfx cache show)/moc"
|
|
31
|
+
|
|
32
|
+
# echo $mocPath
|
|
33
|
+
|
|
34
|
+
# # install moc version if not installed yet
|
|
35
|
+
# if [[ ! -f "$mocPath" && -f "$mopsTomlFile" ]]; then
|
|
36
|
+
# # mocv install --silent $(cat $mopsTomlFile)
|
|
37
|
+
# mocv use $(cat $mopsTomlFile)
|
|
38
|
+
# fi
|
|
39
|
+
|
|
40
|
+
# mocPath="/home/zen/.cache/mocv/versions/current/moc"
|
|
41
|
+
|
|
42
|
+
# mocPath=""
|
|
43
|
+
# if command -v mops &> /dev/null
|
|
44
|
+
# then
|
|
45
|
+
# mocPath="/home/zen/.cache/mops/moc/0.9.5/moc"
|
|
46
|
+
# else
|
|
47
|
+
# mocPath="$(DFX_WARNING=-version_check dfx cache show)/moc" # fallback to dfx
|
|
48
|
+
# fi
|
|
49
|
+
|
|
50
|
+
mocPath="$(mops toolchain bin moc)"
|
|
51
|
+
# mocPath="$(dfx cache show)/moc"
|
|
52
|
+
# mocPath="/home/zen/.cache/mops/moc/0.7.5/moc"
|
|
53
|
+
|
|
54
|
+
$mocPath "$@"
|
package/bin/mops.ts
ADDED
package/cli.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
|
|
3
1
|
import fs from 'node:fs';
|
|
4
|
-
import {
|
|
2
|
+
import {Command, Argument, Option} from 'commander';
|
|
5
3
|
import chalk from 'chalk';
|
|
6
4
|
|
|
7
5
|
import {init} from './commands/init.js';
|
|
@@ -26,6 +24,7 @@ import {outdated} from './commands/outdated.js';
|
|
|
26
24
|
import {update} from './commands/update.js';
|
|
27
25
|
import {bench} from './commands/bench.js';
|
|
28
26
|
import {transferOwnership} from './commands/transfer-ownership.js';
|
|
27
|
+
import {toolchain} from './commands/toolchain/index.js';
|
|
29
28
|
// import {docs} from './commands/docs.js';
|
|
30
29
|
|
|
31
30
|
declare global {
|
|
@@ -38,6 +37,7 @@ if (fs.existsSync(networkFile)) {
|
|
|
38
37
|
globalThis.MOPS_NETWORK = fs.readFileSync(networkFile).toString() || 'ic';
|
|
39
38
|
}
|
|
40
39
|
|
|
40
|
+
let program = new Command();
|
|
41
41
|
|
|
42
42
|
program.name('mops');
|
|
43
43
|
|
|
@@ -58,7 +58,7 @@ program
|
|
|
58
58
|
program
|
|
59
59
|
.command('add <pkg>')
|
|
60
60
|
.description('Install the package and save it to mops.toml')
|
|
61
|
-
.option('--dev')
|
|
61
|
+
.option('--dev', 'Add to [dev-dependencies] section')
|
|
62
62
|
.option('--verbose')
|
|
63
63
|
.addOption(new Option('--lock <action>', 'Lockfile action').choices(['update', 'ignore']))
|
|
64
64
|
.action(async (pkg, options) => {
|
|
@@ -328,4 +328,40 @@ program
|
|
|
328
328
|
await transferOwnership(toPrincipal);
|
|
329
329
|
});
|
|
330
330
|
|
|
331
|
+
// toolchain
|
|
332
|
+
const toolchainCommand = new Command('toolchain').description('Toolchain management');
|
|
333
|
+
|
|
334
|
+
toolchainCommand
|
|
335
|
+
.command('init')
|
|
336
|
+
.description('One-time initialization of toolchain management')
|
|
337
|
+
.action(async () => {
|
|
338
|
+
toolchain.init();
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
toolchainCommand
|
|
342
|
+
.command('reset')
|
|
343
|
+
.description('Uninstall toolchain management')
|
|
344
|
+
.action(async () => {
|
|
345
|
+
toolchain.init({reset: true});
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
toolchainCommand
|
|
349
|
+
.command('use')
|
|
350
|
+
.description('Install specified tool version and update mops.toml')
|
|
351
|
+
.addArgument(new Argument('<tool>').choices(['moc', 'wasmtime', 'pocket-ic']))
|
|
352
|
+
.addArgument(new Argument('<version>'))
|
|
353
|
+
.action(async (tool, version) => {
|
|
354
|
+
toolchain.use(tool, version);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
toolchainCommand
|
|
358
|
+
.command('bin')
|
|
359
|
+
.description('Get path to the tool binary\n<tool> can be one of "moc", "wasmtime", "pocket-ic"')
|
|
360
|
+
.addArgument(new Argument('<tool>').choices(['moc', 'wasmtime', 'pocket-ic']))
|
|
361
|
+
.action(async (tool) => {
|
|
362
|
+
toolchain.bin(tool);
|
|
363
|
+
});
|
|
364
|
+
|
|
365
|
+
program.addCommand(toolchainCommand);
|
|
366
|
+
|
|
331
367
|
program.parse();
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import os from 'node:os';
|
|
4
|
+
import {execSync} from 'node:child_process';
|
|
5
|
+
import {getClosestConfigFile, globalCacheDir, readConfig, writeConfig} from '../../mops.js';
|
|
6
|
+
import * as moc from './moc.js';
|
|
7
|
+
import {Tool} from '../../types.js';
|
|
8
|
+
import chalk from 'chalk';
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
// update shell config files to set DFX_MOC_PATH to moc-wrapper
|
|
12
|
+
async function init({reset = false} = {}) {
|
|
13
|
+
if (process.platform == 'win32') {
|
|
14
|
+
console.error('Windows is not supported. Please use WSL');
|
|
15
|
+
process.exit(1);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
try {
|
|
19
|
+
let res = execSync('which mocv').toString().trim();
|
|
20
|
+
if (res) {
|
|
21
|
+
console.error('Mops is not compatible with mocv. Please uninstall mocv and try again.');
|
|
22
|
+
console.log('Steps to uninstall mocv:');
|
|
23
|
+
console.log('1. Run "mocv reset"');
|
|
24
|
+
console.log('2. Run "npm uninstall -g mocv"');
|
|
25
|
+
process.exit(1);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch {}
|
|
29
|
+
|
|
30
|
+
let zshrc = path.join(os.homedir(), '.zshrc');
|
|
31
|
+
let bashrc = path.join(os.homedir(), '.bashrc');
|
|
32
|
+
|
|
33
|
+
let shellConfigFiles = [bashrc, zshrc].filter((file) => {
|
|
34
|
+
return fs.existsSync(file);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (shellConfigFiles.length === 0) {
|
|
38
|
+
console.error('Shell config files not found: ".bashrc" or ".zshrc"');
|
|
39
|
+
process.exit(1);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// update all existing shell config files
|
|
43
|
+
for (let configFile of shellConfigFiles) {
|
|
44
|
+
let text = fs.readFileSync(configFile).toString();
|
|
45
|
+
let setDfxLine = '\nexport DFX_MOC_PATH="moc-wrapper"';
|
|
46
|
+
|
|
47
|
+
let newLines = [
|
|
48
|
+
setDfxLine,
|
|
49
|
+
];
|
|
50
|
+
|
|
51
|
+
let oldLines = [
|
|
52
|
+
// legacy mocv lines
|
|
53
|
+
`\nexport DFX_MOC_PATH=${path.join(path.join(os.homedir(), '.cache/mocv'), 'versions/current')}/moc`,
|
|
54
|
+
'\nexport DFX_MOC_PATH="$HOME/.cache/mocv/versions/current/moc"',
|
|
55
|
+
// new
|
|
56
|
+
setDfxLine,
|
|
57
|
+
];
|
|
58
|
+
|
|
59
|
+
// remove old lines
|
|
60
|
+
for (let oldLine of oldLines) {
|
|
61
|
+
text = text.replace(oldLine, '');
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (text.endsWith('\n\n')) {
|
|
65
|
+
text = text.trimEnd() + '\n';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// insert new lines
|
|
69
|
+
if (!reset) {
|
|
70
|
+
if (!text.endsWith('\n')) {
|
|
71
|
+
text += '\n';
|
|
72
|
+
}
|
|
73
|
+
for (let newLine of newLines) {
|
|
74
|
+
text += newLine;
|
|
75
|
+
}
|
|
76
|
+
text += '\n';
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
fs.writeFileSync(configFile, text);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
console.log(chalk.green('Success!'));
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function download(tool: Tool, version: string) {
|
|
86
|
+
if (tool === 'moc') {
|
|
87
|
+
await moc.download(version);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// async function downloadAll() {
|
|
92
|
+
// let config = readConfig();
|
|
93
|
+
// if (config.toolchain?.moc) {
|
|
94
|
+
// await download('moc', config.toolchain.moc);
|
|
95
|
+
// }
|
|
96
|
+
// if (config.toolchain?.wasmtime) {
|
|
97
|
+
// await download('wasmtime', config.toolchain.wasmtime);
|
|
98
|
+
// }
|
|
99
|
+
// if (config.toolchain?.['pocket-ic']) {
|
|
100
|
+
// await download('pocket-ic', config.toolchain['pocket-ic']);
|
|
101
|
+
// }
|
|
102
|
+
// }
|
|
103
|
+
|
|
104
|
+
// download binary and set version in mops.toml
|
|
105
|
+
async function use(tool: Tool, version: string) {
|
|
106
|
+
await download(tool, version);
|
|
107
|
+
|
|
108
|
+
let config = readConfig();
|
|
109
|
+
config.toolchain = config.toolchain || {};
|
|
110
|
+
config.toolchain[tool] = version;
|
|
111
|
+
writeConfig(config);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// return current version from mops.toml
|
|
115
|
+
async function bin(tool: Tool) {
|
|
116
|
+
let hasConfig = getClosestConfigFile();
|
|
117
|
+
|
|
118
|
+
// fallback to dfx moc
|
|
119
|
+
if (!hasConfig) {
|
|
120
|
+
console.log(execSync('dfx cache show').toString().trim() + '/moc');
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
let config = readConfig();
|
|
125
|
+
let version = config.toolchain?.[tool];
|
|
126
|
+
|
|
127
|
+
if (!version) {
|
|
128
|
+
// fallback to dfx moc
|
|
129
|
+
if (tool === 'moc') {
|
|
130
|
+
console.log(execSync('dfx cache show').toString().trim() + '/moc');
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
console.error(`Toolchain '${tool}' is not defined in mops.toml`);
|
|
134
|
+
process.exit(1);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if (version) {
|
|
138
|
+
await download(tool, version);
|
|
139
|
+
|
|
140
|
+
if (tool === 'moc') {
|
|
141
|
+
console.log(path.join(globalCacheDir, 'moc', version, 'moc'));
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
console.log(path.join(globalCacheDir, tool, version, 'moc'));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export let toolchain = {
|
|
150
|
+
init,
|
|
151
|
+
use,
|
|
152
|
+
bin,
|
|
153
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
|
|
4
|
+
import {globalCacheDir} from '../../mops.js';
|
|
5
|
+
import {downloadAndExtract} from './toolchain-utils.js';
|
|
6
|
+
|
|
7
|
+
let cacheDir = path.join(globalCacheDir, 'moc');
|
|
8
|
+
|
|
9
|
+
export let isCached = (version: string) => {
|
|
10
|
+
let dir = path.join(cacheDir, version);
|
|
11
|
+
return fs.existsSync(dir) && fs.existsSync(path.join(dir, 'moc'));
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export let download = async (version: string, {silent = false} = {}) => {
|
|
15
|
+
if (process.platform == 'win32') {
|
|
16
|
+
console.error('Windows is not supported. Please use WSL');
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
if (!version) {
|
|
20
|
+
console.error('version is not defined');
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
if (isCached(version)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let url;
|
|
28
|
+
if (parseInt(version.replaceAll('.', '')) >= parseInt('0.9.5'.replaceAll('.', ''))) {
|
|
29
|
+
let platfrom = process.platform == 'darwin' ? 'Darwin' : 'Linux';
|
|
30
|
+
let arch = process.arch.startsWith('arm') ? 'arm64' : 'x86_64';
|
|
31
|
+
// currently only x64 binaries are available
|
|
32
|
+
arch = 'x86_64';
|
|
33
|
+
url = `https://github.com/dfinity/motoko/releases/download/${version}/motoko-${platfrom}-${arch}-${version}.tar.gz`;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
let platfrom = process.platform == 'darwin' ? 'macos' : 'linux64';
|
|
37
|
+
url = `https://github.com/dfinity/motoko/releases/download/${version}/motoko-${platfrom}-${version}.tar.gz`;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
silent || console.log(`Downloading ${url}`);
|
|
41
|
+
|
|
42
|
+
await downloadAndExtract(url, path.join(cacheDir, version));
|
|
43
|
+
};
|
|
@@ -0,0 +1,313 @@
|
|
|
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();
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
|
|
4
|
+
import {globalCacheDir} from '../../mops.js';
|
|
5
|
+
import {downloadAndExtract} from './toolchain-utils.js';
|
|
6
|
+
|
|
7
|
+
let cacheDir = path.join(globalCacheDir, 'pocket-ic');
|
|
8
|
+
|
|
9
|
+
export let isCached = (version: string) => {
|
|
10
|
+
let dir = path.join(cacheDir, version);
|
|
11
|
+
return fs.existsSync(dir) && fs.existsSync(path.join(dir, 'pocket-ic'));
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export let download = async (version: string, {silent = false} = {}) => {
|
|
15
|
+
if (!version) {
|
|
16
|
+
console.error('version is not defined');
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
if (isCached(version)) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
let platfrom = process.platform == 'darwin' ? 'darwin' : 'linux';
|
|
24
|
+
let arch = 'x86_64';
|
|
25
|
+
|
|
26
|
+
let hashes: Record<string, string> = {
|
|
27
|
+
'2.0.1': '69e1408347723dbaa7a6cd2faa9b65c42abbe861',
|
|
28
|
+
'2.0.0': '29ec86dc9f9ca4691d4d4386c8b2aa41e14d9d16',
|
|
29
|
+
'1.0.0': '307d5847c1d2fe1f5e19181c7d0fcec23f4658b3',
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
let url = `https://download.dfinity.systems/ic/${hashes[version]}/openssl-static-binaries/${arch}-${platfrom}/pocket-ic.gz`;
|
|
33
|
+
|
|
34
|
+
silent || console.log(`Downloading ${url}`);
|
|
35
|
+
|
|
36
|
+
await downloadAndExtract(url, path.join(cacheDir, version));
|
|
37
|
+
};
|