motoko 3.0.0-beta7 → 3.0.0-beta8

Sign up to get free protection for your applications and to get access to all the features.
package/src/ast.ts CHANGED
@@ -10,27 +10,18 @@ export type Span = [number, number];
10
10
  export type AST = AST[] | Node | string | null;
11
11
 
12
12
  export interface Source {
13
- file?: string;
14
- start?: Span;
15
- end?: Span;
13
+ file: string;
14
+ start: Span;
15
+ end: Span;
16
16
  }
17
17
 
18
- export interface Node extends Source {
18
+ export interface Node extends Partial<Source> {
19
19
  name: string;
20
- file?: string;
21
- start?: Span;
22
- end?: Span;
23
- type?: Node;
20
+ type?: string;
24
21
  declaration?: Source;
25
22
  args?: AST[];
26
23
  }
27
24
 
28
- // export type TypeAST = AST[] | Node | string | number | null;
29
- // export type TypeNode = {
30
- // name: string;
31
- // args: TypeAST[];
32
- // };
33
-
34
25
  export function simplifyAST(ast: CompilerNode): Node;
35
26
  export function simplifyAST(ast: CompilerAST[]): AST[];
36
27
  export function simplifyAST<T extends CompilerAST>(ast: T): T;
@@ -48,23 +39,26 @@ export function simplifyAST(ast: CompilerAST): AST {
48
39
  CompilerSpan,
49
40
  CompilerAST,
50
41
  ];
51
- return {
42
+ const node: Node = {
52
43
  ...(typeof subAst === 'string'
53
44
  ? { name: subAst }
54
- : simplifyAST(subAst)),
55
- file: start.args[0],
45
+ : simplifyAST(subAst as any as CompilerNode)),
56
46
  start: [+start.args[1], +start.args[2]],
57
47
  end: [+end.args[1], +end.args[2]],
58
48
  };
49
+ const file = start.args[0];
50
+ if (file) {
51
+ node.file = file;
52
+ }
53
+ return node;
59
54
  }
60
55
  if (ast.name === ':') {
61
- const [typeAst, type] = ast.args as [CompilerAST, CompilerNode];
62
- // console.log(typeAst); ////
56
+ const [typeAst, type] = ast.args as [CompilerAST, string];
63
57
  return {
64
58
  ...(typeof typeAst === 'string'
65
59
  ? { name: typeAst }
66
60
  : simplifyAST(typeAst)),
67
- type: simplifyAST(type),
61
+ type,
68
62
  };
69
63
  }
70
64
  return {
@@ -72,5 +66,3 @@ export function simplifyAST(ast: CompilerAST): AST {
72
66
  args: simplifyAST(ast.args),
73
67
  };
74
68
  }
75
-
76
- // export function getTypeString(type: Type) {}
package/src/package.ts CHANGED
@@ -26,126 +26,6 @@ export interface PackageFile {
26
26
  content: string;
27
27
  }
28
28
 
29
- // TODO: call `fetchPackage` instead of deprecated functions
30
- async function loadPackage(mo: Motoko, info: PackageInfo) {
31
- if (
32
- !info.repo.startsWith('https://github.com/') ||
33
- !info.repo.endsWith('.git')
34
- ) {
35
- return false;
36
- }
37
- const repo = {
38
- name: info.name,
39
- version: info.version,
40
- repo: info.repo.slice(0, -4).replace(/^(https:\/\/github.com\/)/, ''),
41
- branch: info.version,
42
- dir: info.dir || 'src',
43
- };
44
- const result = await fetchGithub_(mo, repo, info.name);
45
- if (result) {
46
- mo.usePackage(info.name, info.name + '/');
47
- }
48
- return result ? true : false;
49
- }
50
-
51
- /** @deprecated */
52
- async function fetchGithub_(mo: Motoko, info: PackageInfo, directory = '') {
53
- const possiblyCDN = !(
54
- (info.branch.length % 2 === 0 && /^[A-F0-9]+$/i.test(info.branch)) ||
55
- info.branch === 'master' ||
56
- info.branch === 'main'
57
- );
58
- if (possiblyCDN) {
59
- const result = await fetchFromCDN_(mo, info, directory);
60
- if (result) {
61
- return result;
62
- }
63
- }
64
- return await fetchFromGithub_(mo, info, directory);
65
- }
66
-
67
- // function saveWorkplaceToMotoko(mo, files) {
68
- // for (const [name, code] of Object.entries(files)) {
69
- // if (!name.endsWith('mo')) continue;
70
- // mo.addFile(name, code);
71
- // }
72
- // }
73
-
74
- /** @deprecated */
75
- async function fetchFromCDN_(mo: Motoko, info: PackageInfo, directory = '') {
76
- const meta_url = `https://data.jsdelivr.com/v1/package/gh/${info.repo}@${info.branch}/flat`;
77
- const base_url = `https://cdn.jsdelivr.net/gh/${info.repo}@${info.branch}`;
78
- const response = await fetch(meta_url);
79
- const json = await response.json();
80
- if (!json.hasOwnProperty('files')) {
81
- throw new Error(json.message || `Could not fetch from CDN: ${info}`);
82
- }
83
- const promises: Promise<void>[] = [];
84
- const files: Record<string, string> = {};
85
- for (const f of json.files) {
86
- if (f.name.startsWith(`/${info.dir}/`) && /\.mo$/.test(f.name)) {
87
- const promise = (async () => {
88
- const content = await (await fetch(base_url + f.name)).text();
89
- const stripped =
90
- directory +
91
- f.name.slice(info.dir ? info.dir.length + 1 : 0);
92
- mo.write(stripped, content);
93
- files[stripped] = content;
94
- })();
95
- promises.push(promise);
96
- }
97
- }
98
- if (!promises.length) {
99
- return;
100
- }
101
- return Promise.all(promises).then(() => {
102
- return files;
103
- });
104
- }
105
-
106
- /** @deprecated */
107
- async function fetchFromGithub_(
108
- mo: Motoko,
109
- info: PackageInfo,
110
- directory: string = '',
111
- ) {
112
- const meta_url = `https://api.github.com/repos/${info.repo}/git/trees/${info.branch}?recursive=1`;
113
- const base_url = `https://raw.githubusercontent.com/${info.repo}/${info.branch}/`;
114
- const response = await fetch(meta_url);
115
- const json = await response.json();
116
- if (!json.hasOwnProperty('tree')) {
117
- throw new Error(
118
- json.message || `Could not fetch from GitHub repository: ${info}`,
119
- );
120
- }
121
- const promises: Promise<void>[] = [];
122
- const files: Record<string, string> = {};
123
- for (const f of json.tree) {
124
- if (
125
- f.path.startsWith(info.dir ? `${info.dir}/` : '') &&
126
- f.type === 'blob' &&
127
- /\.mo$/.test(f.path)
128
- ) {
129
- const promise = (async () => {
130
- const content = await (await fetch(base_url + f.path)).text();
131
- const stripped =
132
- directory +
133
- (directory ? '/' : '') +
134
- f.path.slice(info.dir ? info.dir.length + 1 : 0);
135
- mo.write(stripped, content);
136
- files[stripped] = content;
137
- })();
138
- promises.push(promise);
139
- }
140
- }
141
- if (!promises.length) {
142
- return;
143
- }
144
- return Promise.all(promises).then(() => {
145
- return files;
146
- });
147
- }
148
-
149
29
  function parseGithubPackageInfo(path: string | PackageInfo): PackageInfo {
150
30
  if (!path) {
151
31
  return;
@@ -302,12 +182,10 @@ export async function installPackages(
302
182
  packages: Record<string, string | PackageInfo>,
303
183
  ) {
304
184
  await Promise.all(
305
- Object.entries(packages).map(([name, path]) => {
306
- const info = {
307
- ...parseGithubPackageInfo(path),
308
- name,
309
- };
310
- return loadPackage(mo, info);
185
+ Object.entries(packages).map(async ([name, info]) => {
186
+ const pkg = await fetchPackage(name, info);
187
+ mo.loadPackage(pkg);
188
+ return pkg;
311
189
  }),
312
190
  );
313
191
  }