motoko 0.1.5 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
package/lib/index.js CHANGED
@@ -1,86 +1,120 @@
1
1
  'use strict';
2
2
 
3
- const debug = require('debug')('motoko');
3
+ const { file } = require('./file');
4
+ const { /* findPackage, */ loadPackages } = require('./package');
4
5
 
5
- const { Motoko } = require('./generated/moc');
6
- const { /* resolvePackage, */ loadPackages } = require('./package');
6
+ module.exports = (compiler, version) => {
7
+ const debug = require('debug')(version ? `motoko:${version}` : 'motoko');
7
8
 
8
- const invoke = (key, unwrap, args) => {
9
- if (typeof Motoko[key] !== 'function') {
10
- throw new Error(`Unknown compiler function: '${key}'`);
11
- }
12
- let result;
13
- try {
14
- result = Motoko[key](...args);
15
- } catch (err) {
16
- if (err instanceof Error) {
17
- throw err;
9
+ const invoke = (key, unwrap, args) => {
10
+ if (!compiler) {
11
+ throw new Error(
12
+ 'Please load a Motoko compiler before running this function',
13
+ );
18
14
  }
19
- throw new Error(
20
- `Unable to execute ${key}(${[...args]
21
- .map((x) => typeof x)
22
- .join(', ')}):\n${JSON.stringify(err)}`,
23
- );
24
- }
25
- if (!unwrap) {
26
- return result;
27
- }
28
- if (!result.code) {
29
- throw new Error(
30
- result.diagnostics
31
- ? result.diagnostics.map(({ message }) => message).join('; ')
32
- : '(no diagnostics)',
33
- );
34
- }
35
- return result.code;
36
- };
37
-
38
- module.exports = {
39
- Motoko,
40
- // resolvePackage,
41
- loadPackages,
42
- addFile(file, content = '') {
43
- if (typeof content !== 'string') {
44
- throw new Error('Non-string file content');
15
+ if (typeof compiler[key] !== 'function') {
16
+ throw new Error(`Unknown compiler function: '${key}'`);
17
+ }
18
+ let result;
19
+ try {
20
+ result = compiler[key](...args);
21
+ } catch (err) {
22
+ if (err instanceof Error) {
23
+ throw err;
24
+ }
25
+ throw new Error(
26
+ `Unable to execute ${key}(${[...args]
27
+ .map((x) => typeof x)
28
+ .join(', ')}):\n${JSON.stringify(err)}`,
29
+ );
45
30
  }
46
- debug('+file', file);
47
- invoke('saveFile', false, [file, content]);
48
- },
49
- removeFile(file) {
50
- debug('-file', file);
51
- invoke('removeFile', false, [file, content]);
52
- },
53
- listFiles(directory) {
54
- return invoke('readDir', false, [directory]);
55
- },
56
- addPackage(name, file) {
57
- debug('+package', name, file);
58
- invoke('addPackage', false, [name, file]);
59
- },
60
- clearPackages() {
61
- debug('-packages');
62
- invoke('clearPackage', false, []);
63
- },
64
- check(file) {
65
- const result = invoke('check', false, [file]);
66
- return result.diagnostics;
67
- },
68
- run(file, libFiles) {
69
- return invoke('run', false, [libFiles || [], file]);
70
- },
71
- candid(file) {
72
- return invoke('candid', true, [file]);
73
- },
74
- wasm(mode, file) {
75
- return invoke('compileWasm', true, [mode, file]);
76
- },
77
- parse(content) {
78
- const ast = invoke('parse', true, [content]);
79
- return ast;
80
- },
81
- parseCandid(content) {
82
- const ast = invoke('parseCandid', true, [content]);
83
- return ast;
84
- },
31
+ if (!unwrap) {
32
+ return result;
33
+ }
34
+ if (!result.code) {
35
+ throw new Error(
36
+ result.diagnostics
37
+ ? result.diagnostics
38
+ .map(({ message }) => message)
39
+ .join('; ')
40
+ : '(no diagnostics)',
41
+ );
42
+ }
43
+ return result.code;
44
+ };
45
+
46
+ const mo = {
47
+ version,
48
+ compiler,
49
+ file(path) {
50
+ return file(mo, path);
51
+ },
52
+ // findPackage,
53
+ async loadPackages(packages) {
54
+ return loadPackages(mo, packages);
55
+ },
56
+ read(path) {
57
+ return invoke('readFile', false, [path]);
58
+ },
59
+ write(path, content = '') {
60
+ if (typeof content !== 'string') {
61
+ throw new Error('Non-string file content');
62
+ }
63
+ debug('+file', path);
64
+ invoke('saveFile', false, [path, content]);
65
+ },
66
+ rename(path, newPath) {
67
+ invoke('renameFile', false, [path, newPath]);
68
+ },
69
+ delete(path) {
70
+ debug('-file', path);
71
+ invoke('removeFile', false, [path]);
72
+ },
73
+ list(directory) {
74
+ return invoke('readDir', false, [directory]);
75
+ },
76
+ addPackage(name, directory) {
77
+ debug('+package', name, directory);
78
+ invoke('addPackage', false, [name, directory]);
79
+ },
80
+ clearPackages() {
81
+ debug('-packages');
82
+ invoke('clearPackage', false, []);
83
+ },
84
+ setAliases(aliases) {
85
+ debug('aliases', aliases);
86
+ invoke('setActorAliases', false, [Object.entries(aliases)]);
87
+ },
88
+ setMetadata(values) {
89
+ invoke('setPublicMetadata', false, [values]);
90
+ },
91
+ check(path) {
92
+ const result = invoke('check', false, [path]);
93
+ return result.diagnostics;
94
+ },
95
+ run(path, libPaths) {
96
+ return invoke('run', false, [libPaths || [], path]);
97
+ },
98
+ candid(path) {
99
+ return invoke('candid', true, [path]);
100
+ },
101
+ wasm(path, mode) {
102
+ if (!mode) {
103
+ mode = 'ic';
104
+ } else if (mode !== 'ic' && mode !== 'wasi') {
105
+ throw new Error(`Invalid WASM format: ${mode}`);
106
+ }
107
+ return invoke('compileWasm', true, [mode, path]);
108
+ },
109
+ parseMotoko(content) {
110
+ const ast = invoke('parseMotoko', true, [content]);
111
+ return ast;
112
+ },
113
+ parseCandid(content) {
114
+ const ast = invoke('parseCandid', true, [content]);
115
+ return ast;
116
+ },
117
+ };
118
+ return mo;
85
119
  };
86
120
  exports.default = exports;
package/lib/package.js CHANGED
@@ -136,7 +136,6 @@ function parseGithubPackage(path, name) {
136
136
  console.warn(err);
137
137
  }
138
138
 
139
- console.log(result);
140
139
  const { name: repoName, filepath, branch, owner } = result;
141
140
 
142
141
  return {
@@ -149,15 +148,14 @@ function parseGithubPackage(path, name) {
149
148
  }
150
149
 
151
150
  module.exports = {
152
- // async resolvePackage(package) {
151
+ // async findPackage(package) {
153
152
  // if (typeof package === 'string') {
154
153
  // return resolve(package);
155
154
  // }
156
155
  // return package;
157
156
  // },
158
- async loadPackages(packages) {
157
+ loadPackages: async (mo, packages) => {
159
158
  for (const [name, path] of Object.entries(packages)) {
160
- const mo = require('.');
161
159
  const info = parseGithubPackage(path, name);
162
160
  return fetchPackage(mo, info);
163
161
  }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "motoko",
3
- "version": "0.1.5",
3
+ "version": "2.0.2",
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
- "main": "lib/index.js",
7
+ "main": "./index.js",
8
8
  "scripts": {
9
9
  "generate": "node utils/generate",
10
10
  "test": "jest"
@@ -14,18 +14,21 @@
14
14
  "isomorphic-parse-github-url": "1.0.2"
15
15
  },
16
16
  "devDependencies": {
17
+ "@wasmer/wasi": "^1.0.2",
17
18
  "cross-env": "^7.0.3",
18
19
  "jest": "^28.1.3",
19
- "prettier": "^2.7.1",
20
- "uglify-js": "^3.16.3"
20
+ "prettier": "^2.7.1"
21
21
  },
22
22
  "directories": {
23
23
  "lib": "lib",
24
24
  "contrib": "contrib"
25
25
  },
26
26
  "files": [
27
+ "index.js",
28
+ "interpreter.js",
27
29
  "lib",
28
- "contrib"
30
+ "contrib",
31
+ "versions/latest"
29
32
  ],
30
33
  "keywords": [
31
34
  "motoko",