forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.ts
More file actions
114 lines (99 loc) · 2.74 KB
/
parse.ts
File metadata and controls
114 lines (99 loc) · 2.74 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
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
import toml from 'markty-toml';
import { parse } from '../../forked-vendors/jsonlint.browser';
import { ConfigurationFile } from '../../templates/configuration/types';
import { ParsedConfigurationFiles } from '../template';
import { Sandbox } from '../../types';
import { TemplateType } from '..';
type ConfigurationFiles = {
[path: string]: ConfigurationFile;
};
type PossibleModule = {
code: string;
} & (
| {
title: string;
}
| {
path: string;
}
);
function getCode(
template: TemplateType,
module: PossibleModule,
sandbox: Sandbox,
resolveModule: (path: string) => PossibleModule | undefined,
configurationFile: ConfigurationFile
) {
if (module) {
return { code: module.code, generated: false };
}
if (configurationFile.getDefaultCode) {
return {
code: configurationFile.getDefaultCode(template, resolveModule),
generated: true,
};
}
if (sandbox && configurationFile.generateFileFromSandbox) {
return {
code: configurationFile.generateFileFromSandbox(sandbox),
generated: true,
};
}
return { code: '', generated: false };
}
function titleIncludes(module: PossibleModule, test: string) {
if ('title' in module) {
return module.title.includes(test);
}
if ('path' in module) {
return module.path.includes(test);
}
return false;
}
/**
* We convert all configuration file configs to an object with configuration per
* type. This makes configs universal.
*/
export default function parseConfigurations(
template: TemplateType,
configurationFiles: ConfigurationFiles,
resolveModule: (path: string) => PossibleModule | undefined,
sandbox?: Sandbox
): ParsedConfigurationFiles {
const configurations: ParsedConfigurationFiles = {};
const paths = Object.keys(configurationFiles);
for (let i = 0; i < paths.length; i++) {
const path = paths[i];
const module = resolveModule(path);
const configurationFile = configurationFiles[path];
const baseObject = {
path,
...getCode(template, module, sandbox, resolveModule, configurationFile),
};
const { code } = baseObject;
if (code) {
try {
let parsed;
// it goes here three times and the third time it doesn't have a title but a path
// that took a while ffs
// if toml do it with toml parser
if (module && titleIncludes(module, 'toml')) {
// never throws
parsed = toml(code);
} else {
parsed = parse(code);
}
configurations[configurationFile.type] = {
...baseObject,
parsed,
};
} catch (e) {
configurations[configurationFile.type] = {
...baseObject,
error: e,
};
}
}
}
return configurations;
}