motoko 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- package/contrib/hljs.js +122 -0
- package/lib/index.js +11 -3
- package/lib/index.spec.js +31 -0
- package/lib/package.js +17 -8
- package/package.json +2 -2
- package/lib/index.test.js +0 -17
package/contrib/hljs.js
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
// Highlight.js configuration
|
2
|
+
|
3
|
+
export const configure = (hljs) => {
|
4
|
+
var string = {
|
5
|
+
className: 'string',
|
6
|
+
variants: [
|
7
|
+
{
|
8
|
+
begin: /r(#*)"(.|\n)*?"\1(?!#)/,
|
9
|
+
},
|
10
|
+
{
|
11
|
+
begin: /b?'\\?(x\w{2}|u\w{4}|U\w{8}|.)'/,
|
12
|
+
},
|
13
|
+
],
|
14
|
+
};
|
15
|
+
var number = {
|
16
|
+
className: 'number',
|
17
|
+
variants: [
|
18
|
+
{
|
19
|
+
begin: '[+-]?\\b0[xX]([A-Fa-f0-9_]+)',
|
20
|
+
},
|
21
|
+
{
|
22
|
+
begin: '[+-]?\\b(\\d[\\d_]*(\\.[0-9_]+)?([eE][+-]?[0-9_]+)?)',
|
23
|
+
},
|
24
|
+
],
|
25
|
+
relevance: 0,
|
26
|
+
};
|
27
|
+
hljs.registerLanguage('motoko', function (hljs) {
|
28
|
+
return {
|
29
|
+
name: 'Motoko',
|
30
|
+
aliases: ['mo'],
|
31
|
+
keywords: {
|
32
|
+
$pattern: '[a-zA-Z_]\\w*',
|
33
|
+
keyword:
|
34
|
+
'actor and await break case catch class' +
|
35
|
+
' continue debug do else for func if in import' +
|
36
|
+
' module not object or label let loop private' +
|
37
|
+
' public return shared try throw query switch' +
|
38
|
+
' type var while stable flexible system debug_show assert ignore from_candid to_candid',
|
39
|
+
literal: 'true false null',
|
40
|
+
built_in:
|
41
|
+
'Any None Null Bool Int Int8 Int16 Int32 Int64' +
|
42
|
+
' Nat Nat8 Nat16 Nat32 Nat64 Word8 Word16 Word32 Word64' +
|
43
|
+
' Float Char Text Blob Error Principal' +
|
44
|
+
' async',
|
45
|
+
},
|
46
|
+
illegal: '</',
|
47
|
+
contains: [
|
48
|
+
hljs.C_LINE_COMMENT_MODE,
|
49
|
+
hljs.COMMENT('/\\*', '\\*/', {
|
50
|
+
contains: ['self'],
|
51
|
+
}),
|
52
|
+
hljs.inherit(hljs.QUOTE_STRING_MODE, {
|
53
|
+
begin: /b?"/,
|
54
|
+
illegal: null,
|
55
|
+
}),
|
56
|
+
string,
|
57
|
+
number,
|
58
|
+
{
|
59
|
+
className: 'symbol',
|
60
|
+
begin: '#' + hljs.UNDERSCORE_IDENT_RE,
|
61
|
+
},
|
62
|
+
{
|
63
|
+
className: 'function',
|
64
|
+
beginKeywords: 'func',
|
65
|
+
end: '(\\(|<|=|{)',
|
66
|
+
excludeEnd: true,
|
67
|
+
contains: [hljs.UNDERSCORE_TITLE_MODE],
|
68
|
+
},
|
69
|
+
{
|
70
|
+
className: 'class',
|
71
|
+
begin: '\\b(actor( class)?|module|object)\\b',
|
72
|
+
keywords: 'actor class module object',
|
73
|
+
end: '(\\(|<|{)',
|
74
|
+
contains: [hljs.UNDERSCORE_TITLE_MODE],
|
75
|
+
illegal: '[\\w\\d]',
|
76
|
+
},
|
77
|
+
{
|
78
|
+
className: 'built_in',
|
79
|
+
beginKeywords: 'import type',
|
80
|
+
end: '(;|$|=)',
|
81
|
+
excludeEnd: true,
|
82
|
+
contains: [
|
83
|
+
hljs.QUOTE_STRING_MODE,
|
84
|
+
hljs.C_LINE_COMMENT_MODE,
|
85
|
+
hljs.COMMENT('/\\*', '\\*/', {
|
86
|
+
contains: ['self'],
|
87
|
+
}),
|
88
|
+
],
|
89
|
+
},
|
90
|
+
],
|
91
|
+
};
|
92
|
+
});
|
93
|
+
hljs.registerLanguage('candid', function (hljs) {
|
94
|
+
return {
|
95
|
+
name: 'Candid',
|
96
|
+
aliases: ['did'],
|
97
|
+
keywords: {
|
98
|
+
$pattern: '[a-zA-Z_]\\w*',
|
99
|
+
keyword: 'import service type',
|
100
|
+
built_in:
|
101
|
+
'opt vec record variant func blob principal' +
|
102
|
+
' nat nat8 nat16 nat32 nat64 int int8 int16 int32 int64' +
|
103
|
+
' float32 float64 bool text null reserved empty' +
|
104
|
+
' oneway query',
|
105
|
+
},
|
106
|
+
illegal: '</',
|
107
|
+
contains: [
|
108
|
+
hljs.C_LINE_COMMENT_MODE,
|
109
|
+
hljs.COMMENT('/\\*', '\\*/', {
|
110
|
+
contains: ['self'],
|
111
|
+
}),
|
112
|
+
hljs.inherit(hljs.QUOTE_STRING_MODE, {
|
113
|
+
begin: /b?"/,
|
114
|
+
illegal: null,
|
115
|
+
}),
|
116
|
+
string,
|
117
|
+
number,
|
118
|
+
],
|
119
|
+
};
|
120
|
+
});
|
121
|
+
}
|
122
|
+
|
package/lib/index.js
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
const debug = require('debug')('motoko');
|
4
4
|
|
5
5
|
const { Motoko } = require('./generated/moc');
|
6
|
-
const { loadPackages } = require('./package');
|
6
|
+
const { /* resolvePackage, */ loadPackages } = require('./package');
|
7
7
|
|
8
8
|
const invoke = (key, unwrap, args) => {
|
9
9
|
if (typeof Motoko[key] !== 'function') {
|
@@ -37,8 +37,12 @@ const invoke = (key, unwrap, args) => {
|
|
37
37
|
|
38
38
|
module.exports = {
|
39
39
|
Motoko,
|
40
|
+
// resolvePackage,
|
40
41
|
loadPackages,
|
41
|
-
addFile(file, content) {
|
42
|
+
addFile(file, content = '') {
|
43
|
+
if (typeof content !== 'string') {
|
44
|
+
throw new Error('Non-string file content');
|
45
|
+
}
|
42
46
|
debug('+file', file);
|
43
47
|
invoke('saveFile', false, [file, content]);
|
44
48
|
},
|
@@ -58,7 +62,11 @@ module.exports = {
|
|
58
62
|
invoke('clearPackage', false, []);
|
59
63
|
},
|
60
64
|
check(file) {
|
61
|
-
invoke('check',
|
65
|
+
const result = invoke('check', false, [file]);
|
66
|
+
return result.diagnostics;
|
67
|
+
},
|
68
|
+
run(file, libFiles) {
|
69
|
+
return invoke('run', false, [libFiles || [], file]);
|
62
70
|
},
|
63
71
|
candid(file) {
|
64
72
|
return invoke('candid', true, [file]);
|
@@ -0,0 +1,31 @@
|
|
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('check', () => {
|
14
|
+
test('works for a basic example', () => {
|
15
|
+
const file = 'test__check__.mo';
|
16
|
+
mo.addFile(file, actor);
|
17
|
+
expect(mo.check(file)).toStrictEqual([]);
|
18
|
+
});
|
19
|
+
});
|
20
|
+
|
21
|
+
describe('run', () => {
|
22
|
+
test('works for a basic example', () => {
|
23
|
+
const file = 'test__run__.mo';
|
24
|
+
mo.addFile(file, 'let x = 1 + 1; x');
|
25
|
+
expect(mo.run(file)).toStrictEqual({
|
26
|
+
result: 0,
|
27
|
+
stdout: '2 : Nat\n',
|
28
|
+
stderr: '',
|
29
|
+
});
|
30
|
+
});
|
31
|
+
});
|
package/lib/package.js
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
'use strict';
|
2
2
|
// Derived from: https://github.com/dfinity/motoko-playground/blob/main/src/workers/file.ts
|
3
3
|
|
4
|
-
const parse = require('parse-github-url');
|
4
|
+
const parse = require('isomorphic-parse-github-url');
|
5
5
|
const fetch = require('cross-fetch');
|
6
6
|
|
7
7
|
async function fetchPackage(mo, info) {
|
@@ -38,12 +38,12 @@ async function fetchGithub(mo, repo, directory = '') {
|
|
38
38
|
return await fetchFromGithub(mo, repo, directory);
|
39
39
|
}
|
40
40
|
|
41
|
-
function saveWorkplaceToMotoko(mo, files) {
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
}
|
41
|
+
// function saveWorkplaceToMotoko(mo, files) {
|
42
|
+
// for (const [name, code] of Object.entries(files)) {
|
43
|
+
// if (!name.endsWith('mo')) continue;
|
44
|
+
// mo.addFile(name, code);
|
45
|
+
// }
|
46
|
+
// }
|
47
47
|
|
48
48
|
async function fetchFromCDN(mo, repo, directory = '') {
|
49
49
|
const meta_url = `https://data.jsdelivr.com/v1/package/gh/${repo.repo}@${repo.branch}/flat`;
|
@@ -114,6 +114,10 @@ async function fetchFromGithub(mo, repo, directory = '') {
|
|
114
114
|
});
|
115
115
|
}
|
116
116
|
|
117
|
+
// async function resolve(path) {
|
118
|
+
|
119
|
+
// }
|
120
|
+
|
117
121
|
function parseGithubPackage(path, name) {
|
118
122
|
if (!path) {
|
119
123
|
return;
|
@@ -137,7 +141,6 @@ function parseGithubPackage(path, name) {
|
|
137
141
|
|
138
142
|
return {
|
139
143
|
name: name || repoName,
|
140
|
-
// repo,
|
141
144
|
repo: `https://github.com/${owner}/${repoName}.git`,
|
142
145
|
version: branch,
|
143
146
|
dir: filepath,
|
@@ -146,6 +149,12 @@ function parseGithubPackage(path, name) {
|
|
146
149
|
}
|
147
150
|
|
148
151
|
module.exports = {
|
152
|
+
// async resolvePackage(package) {
|
153
|
+
// if (typeof package === 'string') {
|
154
|
+
// return resolve(package);
|
155
|
+
// }
|
156
|
+
// return package;
|
157
|
+
// },
|
149
158
|
async loadPackages(packages) {
|
150
159
|
for (const [name, path] of Object.entries(packages)) {
|
151
160
|
const mo = require('.');
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "motoko",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.5",
|
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",
|
@@ -11,7 +11,7 @@
|
|
11
11
|
},
|
12
12
|
"dependencies": {
|
13
13
|
"cross-fetch": "3.1.5",
|
14
|
-
"parse-github-url": "1.0.2"
|
14
|
+
"isomorphic-parse-github-url": "1.0.2"
|
15
15
|
},
|
16
16
|
"devDependencies": {
|
17
17
|
"cross-env": "^7.0.3",
|
package/lib/index.test.js
DELETED
@@ -1,17 +0,0 @@
|
|
1
|
-
'use strict'
|
2
|
-
|
3
|
-
const Motoko = require('.');
|
4
|
-
|
5
|
-
const actorMo = `
|
6
|
-
actor Main {
|
7
|
-
public func test() : async Nat {
|
8
|
-
123
|
9
|
-
}
|
10
|
-
}
|
11
|
-
`;
|
12
|
-
|
13
|
-
test('placeholder', () => {
|
14
|
-
expect(true).toEqual(true); // placeholder
|
15
|
-
|
16
|
-
console.log(Motoko.parse(actorMo));
|
17
|
-
});
|