-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathmodules.ts
127 lines (113 loc) · 3.49 KB
/
modules.ts
1
2
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/* eslint-disable @typescript-eslint/ban-types */
import { autorun, computed, observable, untracked } from "mobx";
import { TypeCellContext } from "./context.js";
// import { stored } from "./storage/stored";
// import { view } from "./view";
export const unnamedModule = Symbol("unnamed");
export type Module = {
name: string | typeof unnamedModule;
dependencyArray: string[];
factoryFunction: Function;
};
/**
* for compiled format, e.g.:
export default () =>
define(["require", "exports", "lodash"], async function (
require,
exports,
lodash_1
) {
...
}
*/
export function getModulesFromWrappedPatchedTypeCellFunction(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
caller: () => any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
scope: any,
): Module[] {
const modules: Module[] = [];
const define = createDefine(modules);
console.log("evaluate (module)", caller + "");
caller.apply({ ...scope, define });
return modules;
}
/**
* for editor / repl format, where code is a string
* prepared by getPatchedTypeCellCode
*/
export function getModulesFromPatchedTypeCellCode(
code: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
scope: any,
): Module[] {
const modules: Module[] = [];
const define = createDefine(modules);
console.log("evaluate (module)", code);
// eslint-disable-next-line
const f = new Function(code);
f.apply({ ...scope, define });
return modules;
}
function createDefine(modules: Module[]) {
return function typeCellDefine(
moduleNameOrDependencyArray: string | string[],
dependencyArrayOrFactoryFunction: string[] | Function,
factoryFunction?: Function,
) {
const moduleName: string | typeof unnamedModule =
typeof moduleNameOrDependencyArray === "string"
? moduleNameOrDependencyArray
: unnamedModule;
const dependencyArray: string[] =
typeof moduleNameOrDependencyArray === "string"
? dependencyArrayOrFactoryFunction
: // eslint-disable-next-line @typescript-eslint/no-explicit-any
(moduleNameOrDependencyArray as any);
const func =
factoryFunction || (dependencyArrayOrFactoryFunction as Function);
modules.push({
name: moduleName,
dependencyArray: dependencyArray,
factoryFunction: func,
});
};
}
export function createExecutionScope(context: TypeCellContext<unknown>) {
const scope = {
autorun,
$: context.context,
$views: context.viewContext,
untracked,
computed,
// editor: globalEditor,
// stored,
// view,
observable,
};
return scope;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function getPatchedTypeCellCode(compiledCode: string, scope: any) {
// Checks if define([], function) like code is already present
if (!compiledCode.match(/(define\((".*", )?\[.*\], )function/gm)) {
// file is not a module (no exports). Create module-like code manually
compiledCode = `define([], function() { ${compiledCode}; });`;
}
if (Object.keys(scope).find((key) => !/^[a-zA-Z0-9_$]+$/.test(key))) {
throw new Error("invalid key on scope!");
}
const variableImportCode = Object.keys(scope)
.map((key) => `let ${key} = this.${key};`)
.join("\n");
let totalCode = `;
let define = this.define;
${variableImportCode}
${compiledCode}
`;
totalCode = totalCode.replace(
/^\s*(define\((".*", )?\[.*\], )function/gm,
"$1async function",
); // TODO: remove await?
return totalCode;
}