motoko 2.0.7 → 2.0.8
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/index.js +1 -4
- package/interpreter.js +1 -4
- package/lib/file.d.ts +22 -0
- package/lib/file.d.ts.map +1 -0
- package/lib/file.js +7 -5
- package/lib/file.js.map +1 -0
- package/lib/index.d.ts +69 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +39 -34
- package/lib/index.js.map +1 -0
- package/lib/package.d.ts +10 -0
- package/lib/package.d.ts.map +1 -0
- package/lib/package.js +125 -120
- package/lib/package.js.map +1 -0
- package/lib/versions/interpreter.d.ts +45 -0
- package/lib/versions/interpreter.d.ts.map +1 -0
- package/lib/versions/interpreter.js +9 -0
- package/lib/versions/interpreter.js.map +1 -0
- package/lib/versions/moc.d.ts +45 -0
- package/lib/versions/moc.d.ts.map +1 -0
- package/lib/versions/moc.js +9 -0
- package/lib/versions/moc.js.map +1 -0
- package/package.json +15 -6
- package/src/file.ts +68 -0
- package/src/index.ts +139 -0
- package/src/package.ts +185 -0
- package/src/versions/interpreter.ts +4 -0
- package/src/versions/moc.ts +4 -0
- package/lib/__tests__/index.test.js +0 -40
- package/lib/__tests__/interpreter.test.js +0 -15
package/index.js
CHANGED
package/interpreter.js
CHANGED
package/lib/file.d.ts
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
import { Motoko, WasmMode } from '.';
|
2
|
+
export declare type MotokoFile = ReturnType<typeof file>;
|
3
|
+
export declare const file: (mo: Motoko, path: string) => {
|
4
|
+
readonly path: string;
|
5
|
+
clone(): any;
|
6
|
+
read(): string;
|
7
|
+
write(content: string): void;
|
8
|
+
rename(newPath: string): void;
|
9
|
+
delete(): void;
|
10
|
+
list(): string[];
|
11
|
+
check(): import(".").Diagnostic[];
|
12
|
+
run(): {
|
13
|
+
stdout: string;
|
14
|
+
stderr: string;
|
15
|
+
result: string | number;
|
16
|
+
};
|
17
|
+
candid(): string;
|
18
|
+
wasm(mode: WasmMode): any;
|
19
|
+
parseMotoko(): object;
|
20
|
+
parseCandid(): object;
|
21
|
+
};
|
22
|
+
//# sourceMappingURL=file.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"file.d.ts","sourceRoot":"","sources":["../src/file.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,CAAC;AAerC,oBAAY,UAAU,GAAG,UAAU,CAAC,OAAO,IAAI,CAAC,CAAC;AAEjD,eAAO,MAAM,IAAI,OAAQ,MAAM,QAAQ,MAAM;;;YAa7B,MAAM;mBAGC,MAAM;oBAGL,MAAM;;;;;;;;;;eAoBX,QAAQ;;;CAW1B,CAAC"}
|
package/lib/file.js
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.file = void 0;
|
3
4
|
function getValidPath(path) {
|
4
5
|
if (typeof path !== 'string') {
|
5
6
|
throw new Error('File path must be a string');
|
@@ -12,8 +13,7 @@ function getValidPath(path) {
|
|
12
13
|
}
|
13
14
|
return path;
|
14
15
|
}
|
15
|
-
|
16
|
-
exports.file = (mo, path) => {
|
16
|
+
const file = (mo, path) => {
|
17
17
|
path = getValidPath(path);
|
18
18
|
const result = {
|
19
19
|
get path() {
|
@@ -44,7 +44,7 @@ exports.file = (mo, path) => {
|
|
44
44
|
return mo.list(path);
|
45
45
|
},
|
46
46
|
check() {
|
47
|
-
return mo.check(path
|
47
|
+
return mo.check(path);
|
48
48
|
},
|
49
49
|
run() {
|
50
50
|
return mo.run(path);
|
@@ -64,3 +64,5 @@ exports.file = (mo, path) => {
|
|
64
64
|
};
|
65
65
|
return result;
|
66
66
|
};
|
67
|
+
exports.file = file;
|
68
|
+
//# sourceMappingURL=file.js.map
|
package/lib/file.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"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,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;QACzB,CAAC;QACD,KAAK;YACD,OAAO,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;QAC1B,CAAC;QACD,GAAG;YACC,OAAO,EAAE,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACxB,CAAC;QACD,MAAM;YACF,OAAO,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC;QACD,IAAI,CAAC,IAAc;YACf,OAAO,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QAC/B,CAAC;QACD,WAAW;YACP,OAAO,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;QACzC,CAAC;QACD,WAAW;YACP,OAAO,EAAE,CAAC,WAAW,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC;QACzC,CAAC;KACJ,CAAC;IACF,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC;AAlDW,QAAA,IAAI,QAkDf"}
|
package/lib/index.d.ts
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
import { PackageInfo } from './package';
|
2
|
+
export declare type Motoko = ReturnType<typeof getMotoko>;
|
3
|
+
declare type Compiler = any;
|
4
|
+
export declare type Diagnostic = {
|
5
|
+
code?: string | number | {
|
6
|
+
target: any;
|
7
|
+
value: string | number;
|
8
|
+
};
|
9
|
+
message: string;
|
10
|
+
range: {
|
11
|
+
start: {
|
12
|
+
line: number;
|
13
|
+
character: number;
|
14
|
+
};
|
15
|
+
end: {
|
16
|
+
line: number;
|
17
|
+
character: number;
|
18
|
+
};
|
19
|
+
};
|
20
|
+
severity: string;
|
21
|
+
source?: string;
|
22
|
+
tags?: string[];
|
23
|
+
};
|
24
|
+
export declare type WasmMode = 'ic' | 'wasi';
|
25
|
+
export default function getMotoko(compiler: Compiler, version: string): {
|
26
|
+
version: string;
|
27
|
+
compiler: any;
|
28
|
+
file(path: string): {
|
29
|
+
readonly path: string;
|
30
|
+
clone(): any;
|
31
|
+
read(): string;
|
32
|
+
write(content: string): void;
|
33
|
+
rename(newPath: string): void;
|
34
|
+
delete(): void;
|
35
|
+
list(): string[];
|
36
|
+
check(): Diagnostic[];
|
37
|
+
run(): {
|
38
|
+
stdout: string;
|
39
|
+
stderr: string;
|
40
|
+
result: number | string;
|
41
|
+
};
|
42
|
+
candid(): string;
|
43
|
+
wasm(mode: WasmMode): any;
|
44
|
+
parseMotoko(): object;
|
45
|
+
parseCandid(): object;
|
46
|
+
};
|
47
|
+
loadPackages(packages: Record<string, string | PackageInfo>): Promise<void>;
|
48
|
+
read(path: string): string;
|
49
|
+
write(path: string, content?: string): void;
|
50
|
+
rename(path: string, newPath: string): void;
|
51
|
+
delete(path: string): void;
|
52
|
+
list(directory: string): string[];
|
53
|
+
addPackage(name: string, directory: string): void;
|
54
|
+
clearPackages(): void;
|
55
|
+
setAliases(aliases: string): void;
|
56
|
+
setMetadata(values: string): void;
|
57
|
+
check(path: string): Diagnostic[];
|
58
|
+
run(path: string, libPaths?: string[] | undefined): {
|
59
|
+
stdout: string;
|
60
|
+
stderr: string;
|
61
|
+
result: number | string;
|
62
|
+
};
|
63
|
+
candid(path: string): string;
|
64
|
+
wasm(path: string, mode: WasmMode): any;
|
65
|
+
parseMotoko(content: string): object;
|
66
|
+
parseCandid(content: string): object;
|
67
|
+
};
|
68
|
+
export {};
|
69
|
+
//# sourceMappingURL=index.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,WAAW,EAAE,MAAM,WAAW,CAAC;AAEtD,oBAAY,MAAM,GAAG,UAAU,CAAC,OAAO,SAAS,CAAC,CAAC;AAElD,aAAK,QAAQ,GAAG,GAAG,CAAC;AAGpB,oBAAY,UAAU,GAAG;IACrB,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG;QAA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+1,21 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
"use strict";
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
9
|
+
});
|
10
|
+
};
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
12
|
+
const file_1 = require("./file");
|
13
|
+
const package_1 = require("./package");
|
14
|
+
function getMotoko(compiler, version) {
|
7
15
|
const debug = require('debug')(version ? `motoko:${version}` : 'motoko');
|
8
|
-
|
9
16
|
const invoke = (key, unwrap, args) => {
|
10
17
|
if (!compiler) {
|
11
|
-
throw new Error(
|
12
|
-
'Please load a Motoko compiler before running this function',
|
13
|
-
);
|
18
|
+
throw new Error('Please load a Motoko compiler before running this function');
|
14
19
|
}
|
15
20
|
if (typeof compiler[key] !== 'function') {
|
16
21
|
throw new Error(`Unknown compiler function: '${key}'`);
|
@@ -18,40 +23,38 @@ module.exports = (compiler, version) => {
|
|
18
23
|
let result;
|
19
24
|
try {
|
20
25
|
result = compiler[key](...args);
|
21
|
-
}
|
26
|
+
}
|
27
|
+
catch (err) {
|
22
28
|
if (err instanceof Error) {
|
23
29
|
throw err;
|
24
30
|
}
|
25
|
-
throw new Error(
|
26
|
-
|
27
|
-
|
28
|
-
.join(', ')}):\n${JSON.stringify(err)}`,
|
29
|
-
);
|
31
|
+
throw new Error(`Unable to execute ${key}(${[...args]
|
32
|
+
.map((x) => typeof x)
|
33
|
+
.join(', ')}):\n${JSON.stringify(err)}`);
|
30
34
|
}
|
31
35
|
if (!unwrap) {
|
32
36
|
return result;
|
33
37
|
}
|
34
38
|
if (!result.code) {
|
35
|
-
throw new Error(
|
36
|
-
result.diagnostics
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
: '(no diagnostics)',
|
41
|
-
);
|
39
|
+
throw new Error(result.diagnostics
|
40
|
+
? result.diagnostics
|
41
|
+
.map(({ message }) => message)
|
42
|
+
.join('; ')
|
43
|
+
: '(no diagnostics)');
|
42
44
|
}
|
43
45
|
return result.code;
|
44
46
|
};
|
45
|
-
|
46
47
|
const mo = {
|
47
48
|
version,
|
48
49
|
compiler,
|
49
50
|
file(path) {
|
50
|
-
return file(mo, path);
|
51
|
+
return (0, file_1.file)(mo, path);
|
51
52
|
},
|
52
53
|
// findPackage,
|
53
|
-
|
54
|
-
return
|
54
|
+
loadPackages(packages) {
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
56
|
+
return (0, package_1.loadPackages)(mo, packages);
|
57
|
+
});
|
55
58
|
},
|
56
59
|
read(path) {
|
57
60
|
return invoke('readFile', false, [path]);
|
@@ -101,20 +104,22 @@ module.exports = (compiler, version) => {
|
|
101
104
|
wasm(path, mode) {
|
102
105
|
if (!mode) {
|
103
106
|
mode = 'ic';
|
104
|
-
}
|
107
|
+
}
|
108
|
+
else if (mode !== 'ic' && mode !== 'wasi') {
|
105
109
|
throw new Error(`Invalid WASM format: ${mode}`);
|
106
110
|
}
|
107
111
|
return invoke('compileWasm', true, [mode, path]);
|
108
112
|
},
|
109
113
|
parseMotoko(content) {
|
110
|
-
|
111
|
-
return ast;
|
114
|
+
return invoke('parseMotoko', true, [content]);
|
112
115
|
},
|
113
116
|
parseCandid(content) {
|
114
|
-
|
115
|
-
return ast;
|
117
|
+
return invoke('parseCandid', true, [content]);
|
116
118
|
},
|
117
119
|
};
|
120
|
+
// @ts-ignore
|
121
|
+
mo.default = mo;
|
118
122
|
return mo;
|
119
|
-
}
|
120
|
-
exports.default =
|
123
|
+
}
|
124
|
+
exports.default = getMotoko;
|
125
|
+
//# sourceMappingURL=index.js.map
|
package/lib/index.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,iCAA8B;AAC9B,uCAAsD;AAqBtD,SAAwB,SAAS,CAAC,QAAkB,EAAE,OAAe;IACjE,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,UAAU,OAAO,EAAE,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC;IAEzE,MAAM,MAAM,GAAG,CAAC,GAAW,EAAE,MAAe,EAAE,IAAW,EAAE,EAAE;QACzD,IAAI,CAAC,QAAQ,EAAE;YACX,MAAM,IAAI,KAAK,CACX,4DAA4D,CAC/D,CAAC;SACL;QACD,IAAI,OAAO,QAAQ,CAAC,GAAG,CAAC,KAAK,UAAU,EAAE;YACrC,MAAM,IAAI,KAAK,CAAC,+BAA+B,GAAG,GAAG,CAAC,CAAC;SAC1D;QACD,IAAI,MAAM,CAAC;QACX,IAAI;YACA,MAAM,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,CAAC;SACnC;QAAC,OAAO,GAAG,EAAE;YACV,IAAI,GAAG,YAAY,KAAK,EAAE;gBACtB,MAAM,GAAG,CAAC;aACb;YACD,MAAM,IAAI,KAAK,CACX,qBAAqB,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;iBAChC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC;iBACpB,IAAI,CAAC,IAAI,CAAC,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,CAC9C,CAAC;SACL;QACD,IAAI,CAAC,MAAM,EAAE;YACT,OAAO,MAAM,CAAC;SACjB;QACD,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE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GAAG,IAAI,CAAC;aACf;iBAAM,IAAI,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,MAAM,EAAE;gBACzC,MAAM,IAAI,KAAK,CAAC,wBAAwB,IAAI,EAAE,CAAC,CAAC;aACnD;YACD,OAAO,MAAM,CAAC,aAAa,EAAE,IAAI,EAAE,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;QACrD,CAAC;QACD,WAAW,CAAC,OAAe;YACvB,OAAO,MAAM,CAAC,aAAa,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;QAClD,CAAC;QACD,WAAW,CAAC,OAAe;YACvB,OAAO,MAAM,CAAC,aAAa,EAAE,IAAI,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;QAClD,CAAC;KACJ,CAAC;IACF,aAAa;IACb,EAAE,CAAC,OAAO,GAAG,EAAE,CAAC;IAChB,OAAO,EAAE,CAAC;AACd,CAAC;AApHD,4BAoHC"}
|
package/lib/package.d.ts
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
import { Motoko } from '.';
|
2
|
+
export interface PackageInfo {
|
3
|
+
name: string;
|
4
|
+
repo: string;
|
5
|
+
version: string;
|
6
|
+
dir: string;
|
7
|
+
branch?: string | undefined;
|
8
|
+
}
|
9
|
+
export declare function loadPackages(mo: Motoko, packages: Record<string, string | PackageInfo>): Promise<void>;
|
10
|
+
//# sourceMappingURL=package.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"package.d.ts","sourceRoot":"","sources":["../src/package.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,MAAM,EAAE,MAAM,GAAG,CAAC;AAE3B,MAAM,WAAW,WAAW;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,MAAM,CAAC;IACZ,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAC/B;AA0JD,wBAAsB,YAAY,CAC9B,EAAE,EAAE,MAAM,EACV,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,WAAW,CAAC,iBAQjD"}
|
package/lib/package.js
CHANGED
@@ -1,123 +1,129 @@
|
|
1
|
-
|
1
|
+
"use strict";
|
2
2
|
// Derived from: https://github.com/dfinity/motoko-playground/blob/main/src/workers/file.ts
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
}
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
const result =
|
3
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
4
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
5
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
6
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
7
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
8
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
9
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
10
|
+
});
|
11
|
+
};
|
12
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
13
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
14
|
+
};
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
16
|
+
exports.loadPackages = void 0;
|
17
|
+
// @ts-ignore
|
18
|
+
const isomorphic_parse_github_url_1 = __importDefault(require("isomorphic-parse-github-url"));
|
19
|
+
const cross_fetch_1 = __importDefault(require("cross-fetch"));
|
20
|
+
function fetchPackage(mo, info) {
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
22
|
+
if (!info.repo.startsWith('https://github.com/') ||
|
23
|
+
!info.repo.endsWith('.git')) {
|
24
|
+
return false;
|
25
|
+
}
|
26
|
+
const repo = {
|
27
|
+
name: info.name,
|
28
|
+
version: info.version,
|
29
|
+
repo: info.repo.slice(0, -4).replace(/^(https:\/\/github.com\/)/, ''),
|
30
|
+
branch: info.version,
|
31
|
+
dir: info.dir || 'src',
|
32
|
+
};
|
33
|
+
const result = yield fetchGithub(mo, repo, info.name);
|
34
34
|
if (result) {
|
35
|
-
|
35
|
+
mo.addPackage(info.name, info.name + '/');
|
36
36
|
}
|
37
|
-
|
38
|
-
|
37
|
+
return result ? true : false;
|
38
|
+
});
|
39
|
+
}
|
40
|
+
function fetchGithub(mo, info, directory = '') {
|
41
|
+
return __awaiter(this, void 0, void 0, function* () {
|
42
|
+
const possiblyCDN = !((info.branch.length % 2 === 0 && /^[A-F0-9]+$/i.test(info.branch)) ||
|
43
|
+
info.branch === 'master' ||
|
44
|
+
info.branch === 'main');
|
45
|
+
if (possiblyCDN) {
|
46
|
+
const result = yield fetchFromCDN(mo, info, directory);
|
47
|
+
if (result) {
|
48
|
+
return result;
|
49
|
+
}
|
50
|
+
}
|
51
|
+
return yield fetchFromGithub(mo, info, directory);
|
52
|
+
});
|
39
53
|
}
|
40
|
-
|
41
54
|
// function saveWorkplaceToMotoko(mo, files) {
|
42
55
|
// for (const [name, code] of Object.entries(files)) {
|
43
56
|
// if (!name.endsWith('mo')) continue;
|
44
57
|
// mo.addFile(name, code);
|
45
58
|
// }
|
46
59
|
// }
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
}
|
56
|
-
const promises = [];
|
57
|
-
const files = {};
|
58
|
-
for (const f of json.files) {
|
59
|
-
if (f.name.startsWith(`/${repo.dir}/`) && /\.mo$/.test(f.name)) {
|
60
|
-
const promise = (async () => {
|
61
|
-
const content = await (await fetch(base_url + f.name)).text();
|
62
|
-
const stripped =
|
63
|
-
directory +
|
64
|
-
f.name.slice(repo.dir ? repo.dir.length + 1 : 0);
|
65
|
-
mo.write(stripped, content);
|
66
|
-
files[stripped] = content;
|
67
|
-
})();
|
68
|
-
promises.push(promise);
|
60
|
+
function fetchFromCDN(mo, info, directory = '') {
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
62
|
+
const meta_url = `https://data.jsdelivr.com/v1/package/gh/${info.repo}@${info.branch}/flat`;
|
63
|
+
const base_url = `https://cdn.jsdelivr.net/gh/${info.repo}@${info.branch}`;
|
64
|
+
const response = yield (0, cross_fetch_1.default)(meta_url);
|
65
|
+
const json = yield response.json();
|
66
|
+
if (!json.hasOwnProperty('files')) {
|
67
|
+
throw new Error(json.message || `Could not fetch from CDN: ${info}`);
|
69
68
|
}
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
69
|
+
const promises = [];
|
70
|
+
const files = {};
|
71
|
+
for (const f of json.files) {
|
72
|
+
if (f.name.startsWith(`/${info.dir}/`) && /\.mo$/.test(f.name)) {
|
73
|
+
const promise = (() => __awaiter(this, void 0, void 0, function* () {
|
74
|
+
const content = yield (yield (0, cross_fetch_1.default)(base_url + f.name)).text();
|
75
|
+
const stripped = directory +
|
76
|
+
f.name.slice(info.dir ? info.dir.length + 1 : 0);
|
77
|
+
mo.write(stripped, content);
|
78
|
+
files[stripped] = content;
|
79
|
+
}))();
|
80
|
+
promises.push(promise);
|
81
|
+
}
|
82
|
+
}
|
83
|
+
if (!promises.length) {
|
84
|
+
return;
|
85
|
+
}
|
86
|
+
return Promise.all(promises).then(() => {
|
87
|
+
return files;
|
88
|
+
});
|
76
89
|
});
|
77
90
|
}
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
json.message || `Could not fetch from GitHub repository: ${repo}`,
|
87
|
-
);
|
88
|
-
}
|
89
|
-
const promises = [];
|
90
|
-
const files = {};
|
91
|
-
for (const f of json.tree) {
|
92
|
-
if (
|
93
|
-
f.path.startsWith(repo.dir ? `${repo.dir}/` : '') &&
|
94
|
-
f.type === 'blob' &&
|
95
|
-
/\.mo$/.test(f.path)
|
96
|
-
) {
|
97
|
-
const promise = (async () => {
|
98
|
-
const content = await (await fetch(base_url + f.path)).text();
|
99
|
-
const stripped =
|
100
|
-
directory +
|
101
|
-
(directory ? '/' : '') +
|
102
|
-
f.path.slice(repo.dir ? repo.dir.length + 1 : 0);
|
103
|
-
mo.write(stripped, content);
|
104
|
-
files[stripped] = content;
|
105
|
-
})();
|
106
|
-
promises.push(promise);
|
91
|
+
function fetchFromGithub(mo, info, directory = '') {
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
93
|
+
const meta_url = `https://api.github.com/repos/${info.repo}/git/trees/${info.branch}?recursive=1`;
|
94
|
+
const base_url = `https://raw.githubusercontent.com/${info.repo}/${info.branch}/`;
|
95
|
+
const response = yield (0, cross_fetch_1.default)(meta_url);
|
96
|
+
const json = yield response.json();
|
97
|
+
if (!json.hasOwnProperty('tree')) {
|
98
|
+
throw new Error(json.message || `Could not fetch from GitHub repository: ${info}`);
|
107
99
|
}
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
100
|
+
const promises = [];
|
101
|
+
const files = {};
|
102
|
+
for (const f of json.tree) {
|
103
|
+
if (f.path.startsWith(info.dir ? `${info.dir}/` : '') &&
|
104
|
+
f.type === 'blob' &&
|
105
|
+
/\.mo$/.test(f.path)) {
|
106
|
+
const promise = (() => __awaiter(this, void 0, void 0, function* () {
|
107
|
+
const content = yield (yield (0, cross_fetch_1.default)(base_url + f.path)).text();
|
108
|
+
const stripped = directory +
|
109
|
+
(directory ? '/' : '') +
|
110
|
+
f.path.slice(info.dir ? info.dir.length + 1 : 0);
|
111
|
+
mo.write(stripped, content);
|
112
|
+
files[stripped] = content;
|
113
|
+
}))();
|
114
|
+
promises.push(promise);
|
115
|
+
}
|
116
|
+
}
|
117
|
+
if (!promises.length) {
|
118
|
+
return;
|
119
|
+
}
|
120
|
+
return Promise.all(promises).then(() => {
|
121
|
+
return files;
|
122
|
+
});
|
114
123
|
});
|
115
124
|
}
|
116
|
-
|
117
125
|
// async function resolve(path) {
|
118
|
-
|
119
126
|
// }
|
120
|
-
|
121
127
|
function parseGithubPackage(path, name) {
|
122
128
|
if (!path) {
|
123
129
|
return;
|
@@ -125,19 +131,17 @@ function parseGithubPackage(path, name) {
|
|
125
131
|
if (typeof path === 'object') {
|
126
132
|
return path;
|
127
133
|
}
|
128
|
-
|
129
134
|
let result;
|
130
135
|
try {
|
131
|
-
result =
|
136
|
+
result = (0, isomorphic_parse_github_url_1.default)(path);
|
132
137
|
if (!result) {
|
133
138
|
return;
|
134
139
|
}
|
135
|
-
}
|
140
|
+
}
|
141
|
+
catch (err) {
|
136
142
|
console.warn(err);
|
137
143
|
}
|
138
|
-
|
139
144
|
const { name: repoName, filepath, branch, owner } = result;
|
140
|
-
|
141
145
|
return {
|
142
146
|
name: name || repoName,
|
143
147
|
repo: `https://github.com/${owner}/${repoName}.git`,
|
@@ -146,18 +150,19 @@ function parseGithubPackage(path, name) {
|
|
146
150
|
// homepage: ,
|
147
151
|
};
|
148
152
|
}
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
// if (typeof package === 'string') {
|
153
|
-
// return resolve(package);
|
154
|
-
// }
|
155
|
-
// return package;
|
156
|
-
// },
|
157
|
-
loadPackages: async (mo, packages) => {
|
158
|
-
for (const [name, path] of Object.entries(packages)) {
|
153
|
+
function loadPackages(mo, packages) {
|
154
|
+
return __awaiter(this, void 0, void 0, function* () {
|
155
|
+
yield Promise.all(Object.entries(packages).map(([name, path]) => {
|
159
156
|
const info = parseGithubPackage(path, name);
|
160
157
|
return fetchPackage(mo, info);
|
161
|
-
}
|
162
|
-
}
|
163
|
-
}
|
158
|
+
}));
|
159
|
+
});
|
160
|
+
}
|
161
|
+
exports.loadPackages = loadPackages;
|
162
|
+
// export async function findPackage(package) {
|
163
|
+
// if (typeof package === 'string') {
|
164
|
+
// return resolve(package);
|
165
|
+
// }
|
166
|
+
// return package;
|
167
|
+
// },
|
168
|
+
//# sourceMappingURL=package.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"package.js","sourceRoot":"","sources":["../src/package.ts"],"names":[],"mappings":";AAAA,2FAA2F;;;;;;;;;;;;;;;AAE3F,aAAa;AACb,8FAA+D;AAC/D,8DAAgC;AAWhC,SAAe,YAAY,CAAC,EAAU,EAAE,IAAiB;;QACrD,IACI,CAAC,IAAI,CAAC,IAAI,CAAC,UAAU,CAAC,qBAAqB,CAAC;YAC5C,CAAC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,EAC7B;YACE,OAAO,KAAK,CAAC;SAChB;QACD,MAAM,IAAI,GAAG;YACT,IAAI,EAAE,IAAI,CAAC,IAAI;YACf,OAAO,EAAE,IAAI,CAAC,OAAO;YACrB,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,2BAA2B,EAAE,EAAE,CAAC;YACrE,MAAM,EAAE,IAAI,CAAC,OAAO;YACpB,GAAG,EAAE,IAAI,CAAC,GAAG,IAAI,KAAK;SACzB,CAAC;QACF,MAAM,MAAM,GAAG,MAAM,WAAW,CAAC,EAAE,EAAE,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;QACtD,IAAI,MAAM,EAAE;YACR,EAAE,CAAC,UAAU,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,GAAG,GAAG,CAAC,CAAC;SAC7C;QACD,OAAO,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,KAAK,CAAC;IACjC,CAAC;CAAA;AAED,SAAe,WAAW,CAAC,EAAU,EAAE,IA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I,CAAC,CAAC;QAClC,CAAC,CAAC,CACL,CAAC;IACN,CAAC;CAAA;AAVD,oCAUC;AAED,+CAA+C;AAC/C,yCAAyC;AACzC,mCAAmC;AACnC,QAAQ;AACR,sBAAsB;AACtB,KAAK"}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
declare const _default: {
|
2
|
+
version: string;
|
3
|
+
compiler: any;
|
4
|
+
file(path: string): {
|
5
|
+
readonly path: string;
|
6
|
+
clone(): any;
|
7
|
+
read(): string;
|
8
|
+
write(content: string): void;
|
9
|
+
rename(newPath: string): void;
|
10
|
+
delete(): void;
|
11
|
+
list(): string[];
|
12
|
+
check(): import("..").Diagnostic[];
|
13
|
+
run(): {
|
14
|
+
stdout: string;
|
15
|
+
stderr: string;
|
16
|
+
result: string | number;
|
17
|
+
};
|
18
|
+
candid(): string;
|
19
|
+
wasm(mode: import("..").WasmMode): any;
|
20
|
+
parseMotoko(): object;
|
21
|
+
parseCandid(): object;
|
22
|
+
};
|
23
|
+
loadPackages(packages: Record<string, string | import("../package").PackageInfo>): Promise<void>;
|
24
|
+
read(path: string): string;
|
25
|
+
write(path: string, content?: string): void;
|
26
|
+
rename(path: string, newPath: string): void;
|
27
|
+
delete(path: string): void;
|
28
|
+
list(directory: string): string[];
|
29
|
+
addPackage(name: string, directory: string): void;
|
30
|
+
clearPackages(): void;
|
31
|
+
setAliases(aliases: string): void;
|
32
|
+
setMetadata(values: string): void;
|
33
|
+
check(path: string): import("..").Diagnostic[];
|
34
|
+
run(path: string, libPaths?: string[]): {
|
35
|
+
stdout: string;
|
36
|
+
stderr: string;
|
37
|
+
result: string | number;
|
38
|
+
};
|
39
|
+
candid(path: string): string;
|
40
|
+
wasm(path: string, mode: import("..").WasmMode): any;
|
41
|
+
parseMotoko(content: string): object;
|
42
|
+
parseCandid(content: string): object;
|
43
|
+
};
|
44
|
+
export default _default;
|
45
|
+
//# sourceMappingURL=interpreter.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"interpreter.d.ts","sourceRoot":"","sources":["../../src/versions/interpreter.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,wBAAuD"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const __1 = __importDefault(require(".."));
|
7
|
+
const { Motoko } = require('../../versions/latest/moc_interpreter.min');
|
8
|
+
exports.default = (0, __1.default)(Motoko, 'latest/interpreter');
|
9
|
+
//# sourceMappingURL=interpreter.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"interpreter.js","sourceRoot":"","sources":["../../src/versions/interpreter.ts"],"names":[],"mappings":";;;;;AAAA,2CAA2B;AAC3B,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,2CAA2C,CAAC,CAAC;AAExE,kBAAe,IAAA,WAAS,EAAC,MAAM,EAAE,oBAAoB,CAAC,CAAC"}
|
@@ -0,0 +1,45 @@
|
|
1
|
+
declare const _default: {
|
2
|
+
version: string;
|
3
|
+
compiler: any;
|
4
|
+
file(path: string): {
|
5
|
+
readonly path: string;
|
6
|
+
clone(): any;
|
7
|
+
read(): string;
|
8
|
+
write(content: string): void;
|
9
|
+
rename(newPath: string): void;
|
10
|
+
delete(): void;
|
11
|
+
list(): string[];
|
12
|
+
check(): import("..").Diagnostic[];
|
13
|
+
run(): {
|
14
|
+
stdout: string;
|
15
|
+
stderr: string;
|
16
|
+
result: string | number;
|
17
|
+
};
|
18
|
+
candid(): string;
|
19
|
+
wasm(mode: import("..").WasmMode): any;
|
20
|
+
parseMotoko(): object;
|
21
|
+
parseCandid(): object;
|
22
|
+
};
|
23
|
+
loadPackages(packages: Record<string, string | import("../package").PackageInfo>): Promise<void>;
|
24
|
+
read(path: string): string;
|
25
|
+
write(path: string, content?: string): void;
|
26
|
+
rename(path: string, newPath: string): void;
|
27
|
+
delete(path: string): void;
|
28
|
+
list(directory: string): string[];
|
29
|
+
addPackage(name: string, directory: string): void;
|
30
|
+
clearPackages(): void;
|
31
|
+
setAliases(aliases: string): void;
|
32
|
+
setMetadata(values: string): void;
|
33
|
+
check(path: string): import("..").Diagnostic[];
|
34
|
+
run(path: string, libPaths?: string[]): {
|
35
|
+
stdout: string;
|
36
|
+
stderr: string;
|
37
|
+
result: string | number;
|
38
|
+
};
|
39
|
+
candid(path: string): string;
|
40
|
+
wasm(path: string, mode: import("..").WasmMode): any;
|
41
|
+
parseMotoko(content: string): object;
|
42
|
+
parseCandid(content: string): object;
|
43
|
+
};
|
44
|
+
export default _default;
|
45
|
+
//# sourceMappingURL=moc.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"moc.d.ts","sourceRoot":"","sources":["../../src/versions/moc.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAGA,wBAA2C"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
const __1 = __importDefault(require(".."));
|
7
|
+
const { Motoko } = require('../../versions/latest/moc.min');
|
8
|
+
exports.default = (0, __1.default)(Motoko, 'latest');
|
9
|
+
//# sourceMappingURL=moc.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"moc.js","sourceRoot":"","sources":["../../src/versions/moc.ts"],"names":[],"mappings":";;;;;AAAA,2CAA2B;AAC3B,MAAM,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,+BAA+B,CAAC,CAAC;AAE5D,kBAAe,IAAA,WAAS,EAAC,MAAM,EAAE,QAAQ,CAAC,CAAC"}
|
package/package.json
CHANGED
@@ -1,32 +1,40 @@
|
|
1
1
|
{
|
2
2
|
"name": "motoko",
|
3
|
-
"version": "2.0.
|
3
|
+
"version": "2.0.8",
|
4
4
|
"description": "Compile Motoko smart contracts in Node.js and the browser.",
|
5
5
|
"author": "Ryan Vandersmith (https://github.com/rvanasa)",
|
6
6
|
"license": "Apache-2.0",
|
7
7
|
"main": "./index.js",
|
8
|
+
"types": "./index.d.ts",
|
8
9
|
"repository": {
|
9
10
|
"type": "git",
|
10
11
|
"url": "https://github.com/dfinity/node-motoko.git"
|
11
12
|
},
|
12
13
|
"scripts": {
|
14
|
+
"build": "rimraf ./lib && tsc -p .",
|
13
15
|
"prepare": "husky install",
|
14
16
|
"generate": "node utils/generate",
|
15
17
|
"test": "jest",
|
16
|
-
"precommit": "lint-staged"
|
18
|
+
"precommit": "lint-staged",
|
19
|
+
"prepublishOnly": "run-s build test"
|
17
20
|
},
|
18
21
|
"dependencies": {
|
19
22
|
"cross-fetch": "3.1.5",
|
20
23
|
"isomorphic-parse-github-url": "1.0.2"
|
21
24
|
},
|
22
25
|
"devDependencies": {
|
26
|
+
"@types/jest": "^28.1.3",
|
23
27
|
"@wasmer/wasi": "^1.0.2",
|
24
28
|
"cross-env": "^7.0.3",
|
25
29
|
"eslint-config-prettier": "^8.5.0",
|
26
30
|
"husky": "^8.0.1",
|
27
31
|
"jest": "^28.1.3",
|
28
32
|
"lint-staged": "^13.0.3",
|
29
|
-
"
|
33
|
+
"npm-run-all": "^4.1.5",
|
34
|
+
"prettier": "^2.7.1",
|
35
|
+
"ts-jest": "^28.0.8",
|
36
|
+
"ts-node": "^10.9.1",
|
37
|
+
"typescript": "^4.8.2"
|
30
38
|
},
|
31
39
|
"lint-staged": {
|
32
40
|
"{lib,contrib,utils}/**/*.{js,ts,jsx,tsx}": [
|
@@ -40,9 +48,10 @@
|
|
40
48
|
"files": [
|
41
49
|
"index.js",
|
42
50
|
"interpreter.js",
|
43
|
-
"
|
44
|
-
"
|
45
|
-
"
|
51
|
+
"src/**/*",
|
52
|
+
"lib/**/*",
|
53
|
+
"contrib/**/*",
|
54
|
+
"versions/latest/**/*"
|
46
55
|
],
|
47
56
|
"keywords": [
|
48
57
|
"motoko",
|
package/src/file.ts
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
import { Motoko, WasmMode } from '.';
|
2
|
+
|
3
|
+
function getValidPath(path: string): string {
|
4
|
+
if (typeof path !== 'string') {
|
5
|
+
throw new Error('File path must be a string');
|
6
|
+
}
|
7
|
+
if (path.startsWith('/')) {
|
8
|
+
path = path.slice(1);
|
9
|
+
}
|
10
|
+
if (path.endsWith('/')) {
|
11
|
+
path = path.slice(0, -1);
|
12
|
+
}
|
13
|
+
return path;
|
14
|
+
}
|
15
|
+
|
16
|
+
export type MotokoFile = ReturnType<typeof file>;
|
17
|
+
|
18
|
+
export const file = (mo: Motoko, path: string) => {
|
19
|
+
path = getValidPath(path);
|
20
|
+
const result = {
|
21
|
+
get path(): string {
|
22
|
+
return path;
|
23
|
+
},
|
24
|
+
// file(subPath) {
|
25
|
+
// subPath = getValidPath(subPath);
|
26
|
+
// return exports.file(`${path}/${subPath}`);
|
27
|
+
// },
|
28
|
+
clone() {
|
29
|
+
return exports.file(path);
|
30
|
+
},
|
31
|
+
read(): string {
|
32
|
+
return mo.read(path);
|
33
|
+
},
|
34
|
+
write(content: string) {
|
35
|
+
return mo.write(path, content);
|
36
|
+
},
|
37
|
+
rename(newPath: string) {
|
38
|
+
let result = mo.rename(path, newPath);
|
39
|
+
path = newPath;
|
40
|
+
return result;
|
41
|
+
},
|
42
|
+
delete() {
|
43
|
+
return mo.delete(path);
|
44
|
+
},
|
45
|
+
list() {
|
46
|
+
return mo.list(path);
|
47
|
+
},
|
48
|
+
check() {
|
49
|
+
return mo.check(path);
|
50
|
+
},
|
51
|
+
run() {
|
52
|
+
return mo.run(path);
|
53
|
+
},
|
54
|
+
candid() {
|
55
|
+
return mo.candid(path);
|
56
|
+
},
|
57
|
+
wasm(mode: WasmMode) {
|
58
|
+
return mo.wasm(path, mode);
|
59
|
+
},
|
60
|
+
parseMotoko() {
|
61
|
+
return mo.parseMotoko(result.read());
|
62
|
+
},
|
63
|
+
parseCandid() {
|
64
|
+
return mo.parseCandid(result.read());
|
65
|
+
},
|
66
|
+
};
|
67
|
+
return result;
|
68
|
+
};
|
package/src/index.ts
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
import { file } from './file';
|
2
|
+
import { loadPackages, PackageInfo } from './package';
|
3
|
+
|
4
|
+
export type Motoko = ReturnType<typeof getMotoko>;
|
5
|
+
|
6
|
+
type Compiler = any; // TODO
|
7
|
+
|
8
|
+
// TODO
|
9
|
+
export type Diagnostic = {
|
10
|
+
code?: string | number | { target: any; value: string | number };
|
11
|
+
message: string;
|
12
|
+
range: {
|
13
|
+
start: { line: number; character: number };
|
14
|
+
end: { line: number; character: number };
|
15
|
+
};
|
16
|
+
severity: string;
|
17
|
+
source?: string;
|
18
|
+
tags?: string[];
|
19
|
+
};
|
20
|
+
|
21
|
+
export type WasmMode = 'ic' | 'wasi';
|
22
|
+
|
23
|
+
export default function getMotoko(compiler: Compiler, version: string) {
|
24
|
+
const debug = require('debug')(version ? `motoko:${version}` : 'motoko');
|
25
|
+
|
26
|
+
const invoke = (key: string, unwrap: boolean, args: any[]) => {
|
27
|
+
if (!compiler) {
|
28
|
+
throw new Error(
|
29
|
+
'Please load a Motoko compiler before running this function',
|
30
|
+
);
|
31
|
+
}
|
32
|
+
if (typeof compiler[key] !== 'function') {
|
33
|
+
throw new Error(`Unknown compiler function: '${key}'`);
|
34
|
+
}
|
35
|
+
let result;
|
36
|
+
try {
|
37
|
+
result = compiler[key](...args);
|
38
|
+
} catch (err) {
|
39
|
+
if (err instanceof Error) {
|
40
|
+
throw err;
|
41
|
+
}
|
42
|
+
throw new Error(
|
43
|
+
`Unable to execute ${key}(${[...args]
|
44
|
+
.map((x) => typeof x)
|
45
|
+
.join(', ')}):\n${JSON.stringify(err)}`,
|
46
|
+
);
|
47
|
+
}
|
48
|
+
if (!unwrap) {
|
49
|
+
return result;
|
50
|
+
}
|
51
|
+
if (!result.code) {
|
52
|
+
throw new Error(
|
53
|
+
result.diagnostics
|
54
|
+
? result.diagnostics
|
55
|
+
.map(({ message }: Diagnostic) => message)
|
56
|
+
.join('; ')
|
57
|
+
: '(no diagnostics)',
|
58
|
+
);
|
59
|
+
}
|
60
|
+
return result.code;
|
61
|
+
};
|
62
|
+
|
63
|
+
const mo = {
|
64
|
+
version,
|
65
|
+
compiler,
|
66
|
+
file(path: string) {
|
67
|
+
return file(mo, path);
|
68
|
+
},
|
69
|
+
// findPackage,
|
70
|
+
async loadPackages(packages: Record<string, string | PackageInfo>) {
|
71
|
+
return loadPackages(mo, packages);
|
72
|
+
},
|
73
|
+
read(path: string): string {
|
74
|
+
return invoke('readFile', false, [path]);
|
75
|
+
},
|
76
|
+
write(path: string, content: string = '') {
|
77
|
+
if (typeof content !== 'string') {
|
78
|
+
throw new Error('Non-string file content');
|
79
|
+
}
|
80
|
+
debug('+file', path);
|
81
|
+
invoke('saveFile', false, [path, content]);
|
82
|
+
},
|
83
|
+
rename(path: string, newPath: string) {
|
84
|
+
invoke('renameFile', false, [path, newPath]);
|
85
|
+
},
|
86
|
+
delete(path: string) {
|
87
|
+
debug('-file', path);
|
88
|
+
invoke('removeFile', false, [path]);
|
89
|
+
},
|
90
|
+
list(directory: string): string[] {
|
91
|
+
return invoke('readDir', false, [directory]);
|
92
|
+
},
|
93
|
+
addPackage(name: string, directory: string) {
|
94
|
+
debug('+package', name, directory);
|
95
|
+
invoke('addPackage', false, [name, directory]);
|
96
|
+
},
|
97
|
+
clearPackages() {
|
98
|
+
debug('-packages');
|
99
|
+
invoke('clearPackage', false, []);
|
100
|
+
},
|
101
|
+
setAliases(aliases: string) {
|
102
|
+
debug('aliases', aliases);
|
103
|
+
invoke('setActorAliases', false, [Object.entries(aliases)]);
|
104
|
+
},
|
105
|
+
setMetadata(values: string) {
|
106
|
+
invoke('setPublicMetadata', false, [values]);
|
107
|
+
},
|
108
|
+
check(path: string): Diagnostic[] {
|
109
|
+
const result = invoke('check', false, [path]);
|
110
|
+
return result.diagnostics;
|
111
|
+
},
|
112
|
+
run(
|
113
|
+
path: string,
|
114
|
+
libPaths?: string[] | undefined,
|
115
|
+
): { stdout: string; stderr: string; result: number | string } {
|
116
|
+
return invoke('run', false, [libPaths || [], path]);
|
117
|
+
},
|
118
|
+
candid(path: string): string {
|
119
|
+
return invoke('candid', true, [path]);
|
120
|
+
},
|
121
|
+
wasm(path: string, mode: WasmMode) {
|
122
|
+
if (!mode) {
|
123
|
+
mode = 'ic';
|
124
|
+
} else if (mode !== 'ic' && mode !== 'wasi') {
|
125
|
+
throw new Error(`Invalid WASM format: ${mode}`);
|
126
|
+
}
|
127
|
+
return invoke('compileWasm', true, [mode, path]);
|
128
|
+
},
|
129
|
+
parseMotoko(content: string): object {
|
130
|
+
return invoke('parseMotoko', true, [content]);
|
131
|
+
},
|
132
|
+
parseCandid(content: string): object {
|
133
|
+
return invoke('parseCandid', true, [content]);
|
134
|
+
},
|
135
|
+
};
|
136
|
+
// @ts-ignore
|
137
|
+
mo.default = mo;
|
138
|
+
return mo;
|
139
|
+
}
|
package/src/package.ts
ADDED
@@ -0,0 +1,185 @@
|
|
1
|
+
// Derived from: https://github.com/dfinity/motoko-playground/blob/main/src/workers/file.ts
|
2
|
+
|
3
|
+
// @ts-ignore
|
4
|
+
import { default as parse } from 'isomorphic-parse-github-url';
|
5
|
+
import fetch from 'cross-fetch';
|
6
|
+
import { Motoko } from '.';
|
7
|
+
|
8
|
+
export interface PackageInfo {
|
9
|
+
name: string;
|
10
|
+
repo: string;
|
11
|
+
version: string;
|
12
|
+
dir: string;
|
13
|
+
branch?: string | undefined;
|
14
|
+
}
|
15
|
+
|
16
|
+
async function fetchPackage(mo: Motoko, info: PackageInfo) {
|
17
|
+
if (
|
18
|
+
!info.repo.startsWith('https://github.com/') ||
|
19
|
+
!info.repo.endsWith('.git')
|
20
|
+
) {
|
21
|
+
return false;
|
22
|
+
}
|
23
|
+
const repo = {
|
24
|
+
name: info.name,
|
25
|
+
version: info.version,
|
26
|
+
repo: info.repo.slice(0, -4).replace(/^(https:\/\/github.com\/)/, ''),
|
27
|
+
branch: info.version,
|
28
|
+
dir: info.dir || 'src',
|
29
|
+
};
|
30
|
+
const result = await fetchGithub(mo, repo, info.name);
|
31
|
+
if (result) {
|
32
|
+
mo.addPackage(info.name, info.name + '/');
|
33
|
+
}
|
34
|
+
return result ? true : false;
|
35
|
+
}
|
36
|
+
|
37
|
+
async function fetchGithub(mo: Motoko, info: PackageInfo, directory = '') {
|
38
|
+
const possiblyCDN = !(
|
39
|
+
(info.branch.length % 2 === 0 && /^[A-F0-9]+$/i.test(info.branch)) ||
|
40
|
+
info.branch === 'master' ||
|
41
|
+
info.branch === 'main'
|
42
|
+
);
|
43
|
+
if (possiblyCDN) {
|
44
|
+
const result = await fetchFromCDN(mo, info, directory);
|
45
|
+
if (result) {
|
46
|
+
return result;
|
47
|
+
}
|
48
|
+
}
|
49
|
+
return await fetchFromGithub(mo, info, directory);
|
50
|
+
}
|
51
|
+
|
52
|
+
// function saveWorkplaceToMotoko(mo, files) {
|
53
|
+
// for (const [name, code] of Object.entries(files)) {
|
54
|
+
// if (!name.endsWith('mo')) continue;
|
55
|
+
// mo.addFile(name, code);
|
56
|
+
// }
|
57
|
+
// }
|
58
|
+
|
59
|
+
async function fetchFromCDN(mo: Motoko, info: PackageInfo, directory = '') {
|
60
|
+
const meta_url = `https://data.jsdelivr.com/v1/package/gh/${info.repo}@${info.branch}/flat`;
|
61
|
+
const base_url = `https://cdn.jsdelivr.net/gh/${info.repo}@${info.branch}`;
|
62
|
+
const response = await fetch(meta_url);
|
63
|
+
const json = await response.json();
|
64
|
+
if (!json.hasOwnProperty('files')) {
|
65
|
+
throw new Error(json.message || `Could not fetch from CDN: ${info}`);
|
66
|
+
}
|
67
|
+
const promises: Promise<void>[] = [];
|
68
|
+
const files: Record<string, string> = {};
|
69
|
+
for (const f of json.files) {
|
70
|
+
if (f.name.startsWith(`/${info.dir}/`) && /\.mo$/.test(f.name)) {
|
71
|
+
const promise = (async () => {
|
72
|
+
const content = await (await fetch(base_url + f.name)).text();
|
73
|
+
const stripped =
|
74
|
+
directory +
|
75
|
+
f.name.slice(info.dir ? info.dir.length + 1 : 0);
|
76
|
+
mo.write(stripped, content);
|
77
|
+
files[stripped] = content;
|
78
|
+
})();
|
79
|
+
promises.push(promise);
|
80
|
+
}
|
81
|
+
}
|
82
|
+
if (!promises.length) {
|
83
|
+
return;
|
84
|
+
}
|
85
|
+
return Promise.all(promises).then(() => {
|
86
|
+
return files;
|
87
|
+
});
|
88
|
+
}
|
89
|
+
|
90
|
+
async function fetchFromGithub(
|
91
|
+
mo: Motoko,
|
92
|
+
info: PackageInfo,
|
93
|
+
directory: string = '',
|
94
|
+
) {
|
95
|
+
const meta_url = `https://api.github.com/repos/${info.repo}/git/trees/${info.branch}?recursive=1`;
|
96
|
+
const base_url = `https://raw.githubusercontent.com/${info.repo}/${info.branch}/`;
|
97
|
+
const response = await fetch(meta_url);
|
98
|
+
const json = await response.json();
|
99
|
+
if (!json.hasOwnProperty('tree')) {
|
100
|
+
throw new Error(
|
101
|
+
json.message || `Could not fetch from GitHub repository: ${info}`,
|
102
|
+
);
|
103
|
+
}
|
104
|
+
const promises: Promise<void>[] = [];
|
105
|
+
const files: Record<string, string> = {};
|
106
|
+
for (const f of json.tree) {
|
107
|
+
if (
|
108
|
+
f.path.startsWith(info.dir ? `${info.dir}/` : '') &&
|
109
|
+
f.type === 'blob' &&
|
110
|
+
/\.mo$/.test(f.path)
|
111
|
+
) {
|
112
|
+
const promise = (async () => {
|
113
|
+
const content = await (await fetch(base_url + f.path)).text();
|
114
|
+
const stripped =
|
115
|
+
directory +
|
116
|
+
(directory ? '/' : '') +
|
117
|
+
f.path.slice(info.dir ? info.dir.length + 1 : 0);
|
118
|
+
mo.write(stripped, content);
|
119
|
+
files[stripped] = content;
|
120
|
+
})();
|
121
|
+
promises.push(promise);
|
122
|
+
}
|
123
|
+
}
|
124
|
+
if (!promises.length) {
|
125
|
+
return;
|
126
|
+
}
|
127
|
+
return Promise.all(promises).then(() => {
|
128
|
+
return files;
|
129
|
+
});
|
130
|
+
}
|
131
|
+
|
132
|
+
// async function resolve(path) {
|
133
|
+
|
134
|
+
// }
|
135
|
+
|
136
|
+
function parseGithubPackage(
|
137
|
+
path: string | PackageInfo,
|
138
|
+
name: string,
|
139
|
+
): PackageInfo {
|
140
|
+
if (!path) {
|
141
|
+
return;
|
142
|
+
}
|
143
|
+
if (typeof path === 'object') {
|
144
|
+
return path;
|
145
|
+
}
|
146
|
+
|
147
|
+
let result;
|
148
|
+
try {
|
149
|
+
result = parse(path);
|
150
|
+
if (!result) {
|
151
|
+
return;
|
152
|
+
}
|
153
|
+
} catch (err) {
|
154
|
+
console.warn(err);
|
155
|
+
}
|
156
|
+
|
157
|
+
const { name: repoName, filepath, branch, owner } = result;
|
158
|
+
|
159
|
+
return {
|
160
|
+
name: name || repoName,
|
161
|
+
repo: `https://github.com/${owner}/${repoName}.git`,
|
162
|
+
version: branch,
|
163
|
+
dir: filepath,
|
164
|
+
// homepage: ,
|
165
|
+
};
|
166
|
+
}
|
167
|
+
|
168
|
+
export async function loadPackages(
|
169
|
+
mo: Motoko,
|
170
|
+
packages: Record<string, string | PackageInfo>,
|
171
|
+
) {
|
172
|
+
await Promise.all(
|
173
|
+
Object.entries(packages).map(([name, path]) => {
|
174
|
+
const info = parseGithubPackage(path, name);
|
175
|
+
return fetchPackage(mo, info);
|
176
|
+
}),
|
177
|
+
);
|
178
|
+
}
|
179
|
+
|
180
|
+
// export async function findPackage(package) {
|
181
|
+
// if (typeof package === 'string') {
|
182
|
+
// return resolve(package);
|
183
|
+
// }
|
184
|
+
// return package;
|
185
|
+
// },
|
@@ -1,40 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
const mo = require('../..');
|
4
|
-
|
5
|
-
const actor = `
|
6
|
-
actor Main {
|
7
|
-
public func test() : async Nat {
|
8
|
-
123
|
9
|
-
}
|
10
|
-
}
|
11
|
-
`;
|
12
|
-
|
13
|
-
describe('virtual file system I/O', () => {
|
14
|
-
test('write -> read', () => {
|
15
|
-
const path = 'test__write_read__.txt';
|
16
|
-
const text = 'A\nB';
|
17
|
-
mo.write(path, text);
|
18
|
-
expect(mo.read(path)).toStrictEqual(text);
|
19
|
-
});
|
20
|
-
});
|
21
|
-
|
22
|
-
describe('check', () => {
|
23
|
-
test('works for a basic example', () => {
|
24
|
-
const path = 'test__check__.mo';
|
25
|
-
mo.write(path, actor);
|
26
|
-
expect(mo.check(path)).toStrictEqual([]);
|
27
|
-
});
|
28
|
-
});
|
29
|
-
|
30
|
-
describe('run', () => {
|
31
|
-
test('works for a basic example', () => {
|
32
|
-
const path = 'test__run__.mo';
|
33
|
-
mo.write(path, 'let x = 1 + 1; x');
|
34
|
-
expect(mo.run(path)).toStrictEqual({
|
35
|
-
result: 0,
|
36
|
-
stdout: '2 : Nat\n',
|
37
|
-
stderr: '',
|
38
|
-
});
|
39
|
-
});
|
40
|
-
});
|
@@ -1,15 +0,0 @@
|
|
1
|
-
'use strict';
|
2
|
-
|
3
|
-
const mo = require('../../interpreter');
|
4
|
-
|
5
|
-
describe('run', () => {
|
6
|
-
test('works for a basic example', () => {
|
7
|
-
const path = 'test__run__.mo';
|
8
|
-
mo.write(path, 'let x = 1 + 1; x');
|
9
|
-
expect(mo.run(path)).toStrictEqual({
|
10
|
-
result: 0,
|
11
|
-
stdout: '2 : Nat\n',
|
12
|
-
stderr: '',
|
13
|
-
});
|
14
|
-
});
|
15
|
-
});
|