forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (32 loc) · 1 KB
/
index.js
File metadata and controls
41 lines (32 loc) · 1 KB
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
import { getCurrentManager } from '../compile';
let cachedBoilerplates = [];
export async function evalBoilerplates(boilerplates) {
cachedBoilerplates = await Promise.all(
boilerplates.map(async boilerplate => {
const fakeModule = {
path: `/boilerplate-${boilerplate.condition}${boilerplate.extension}`,
code: boilerplate.code,
};
const manager = getCurrentManager();
await manager.transpileModules(fakeModule);
const module = manager.evaluateModule(fakeModule);
return { ...boilerplate, module };
})
);
}
export function getBoilerplates() {
return cachedBoilerplates;
}
export function findBoilerplate(module) {
const boilerplates = getBoilerplates();
const boilerplate = boilerplates.find(b => {
const regex = new RegExp(b.condition);
return regex.test(module.path);
});
if (boilerplate == null) {
throw new Error(
`No boilerplate found for ${module.path}, you can create one in the future`
);
}
return boilerplate;
}