forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.ts
More file actions
235 lines (210 loc) · 5.69 KB
/
template.ts
File metadata and controls
235 lines (210 loc) · 5.69 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import { absolute } from '../utils/path';
import {
ConfigurationFile,
ParsedConfigurationFile,
} from './configuration/types';
import configurations from './configuration';
import { isServer } from './helpers/is-server';
import { TemplateType } from '.';
import { PackageJSON } from '../types';
export type Options = {
showOnHomePage?: boolean;
distDir?: string;
netlify?: boolean;
popular?: boolean;
extraConfigurations?: {
[path: string]: ConfigurationFile;
};
isTypescript?: boolean;
externalResourcesEnabled?: boolean;
showCube?: boolean;
main?: boolean;
backgroundColor?: () => string;
mainFile?: string[];
};
export type ConfigurationFiles = {
[path: string]: ConfigurationFile;
};
export type Dependencies = { [name: string]: string };
export type ParsedConfigurationFiles = {
package?: ParsedConfigurationFile<PackageJSON>;
[path: string]: ParsedConfigurationFile<any> | undefined;
};
const defaultConfigurations = {
'/package.json': configurations.packageJSON,
'/.prettierrc': configurations.prettierRC,
'/sandbox.config.json': configurations.sandboxConfig,
'/now.json': configurations.nowConfig,
'/netlify.toml': configurations.netlifyConfig,
};
export interface ViewTab {
id: string;
closeable?: boolean;
options?: any;
}
export type ViewConfig = {
open?: boolean;
views: ViewTab[];
};
const CLIENT_VIEWS: ViewConfig[] = [
{
views: [{ id: 'codesandbox.browser' }, { id: 'codesandbox.tests' }],
},
{
views: [{ id: 'codesandbox.console' }, { id: 'codesandbox.problems' }],
},
];
const SERVER_VIEWS: ViewConfig[] = [
{
views: [{ id: 'codesandbox.browser' }],
},
{
open: true,
views: [
{ id: 'codesandbox.terminal' },
{ id: 'codesandbox.console' },
{ id: 'codesandbox.problems' },
],
},
];
export default class Template {
name: TemplateType;
niceName: string;
shortid: string;
url: string;
main: boolean;
color: () => string;
backgroundColor: () => string | undefined;
popular: boolean;
showOnHomePage: boolean;
distDir: string;
netlify: boolean;
configurationFiles: ConfigurationFiles;
isTypescript: boolean;
externalResourcesEnabled: boolean;
showCube: boolean;
isServer: boolean;
mainFile: undefined | string[];
constructor(
name: TemplateType,
niceName: string,
url: string,
shortid: string,
color: () => string,
options: Options = {}
) {
this.name = name;
this.niceName = niceName;
this.url = url;
this.shortid = shortid;
this.color = color;
this.popular = options.popular || false;
this.isServer = isServer(this.name);
this.main = options.main || false;
this.showOnHomePage = options.showOnHomePage || false;
this.distDir = options.distDir || 'build';
this.configurationFiles = {
...defaultConfigurations,
...(options.extraConfigurations || {}),
};
this.isTypescript = options.isTypescript || false;
this.externalResourcesEnabled =
options.externalResourcesEnabled != null
? options.externalResourcesEnabled
: true;
this.mainFile = options.mainFile;
this.netlify = options.netlify;
this.backgroundColor = options.backgroundColor;
this.showCube = options.showCube != null ? options.showCube : true;
}
// eslint-disable-next-line
private getMainFromPackage(pkg: {
main?: string[] | string;
}): string | undefined {
try {
if (!pkg.main) {
return undefined;
}
if (Array.isArray(pkg.main)) {
return absolute(pkg.main[0]);
}
if (typeof pkg.main === 'string') {
return absolute(pkg.main);
}
} catch (e) {
// eslint-disable-next-line
console.log(e);
}
}
/**
* Get possible entry files to evaluate, differs per template
*/
getEntries(configurationFiles: ParsedConfigurationFiles): string[] {
return [
configurationFiles.package?.parsed &&
this.getMainFromPackage(configurationFiles.package.parsed),
...(this.mainFile || []),
'/index.' + (this.isTypescript ? 'ts' : 'js'),
'/src/index.' + (this.isTypescript ? 'ts' : 'js'),
'/src/index.ts',
'/src/index.tsx',
'/src/index.js',
'/src/pages/index.js',
'/src/pages/index.vue',
'/index.js',
'/index.ts',
'/index.tsx',
'/README.md',
'/package.json',
].filter(x => x);
}
/**
* Files to be opened by default by the editor when opening the editor
*/
getDefaultOpenedFiles(
configurationFiles: ParsedConfigurationFiles
): string[] {
return this.getEntries(configurationFiles);
}
/**
* Get the views that are tied to the template
*/
getViews(configurationFiles: ParsedConfigurationFiles): ViewConfig[] {
if (this.isServer) {
return SERVER_VIEWS;
}
return CLIENT_VIEWS;
}
// eslint-disable-next-line no-unused-vars
getHTMLEntries(configurationFiles: ParsedConfigurationFiles): string[] {
return ['/public/index.html', '/index.html'];
}
/**
* Alter the apiData to ZEIT for making deployment work
*/
alterDeploymentData = (apiData: any) => {
const packageJSONFile = apiData.files.find(x => x.file === 'package.json');
const parsedFile = JSON.parse(packageJSONFile.data);
const newParsedFile = {
...parsedFile,
devDependencies: {
...parsedFile.devDependencies,
serve: '^10.1.1',
},
scripts: {
'now-start': `cd ${this.distDir} && serve -s ./`,
...parsedFile.scripts,
},
};
return {
...apiData,
files: [
...apiData.files.filter(x => x.file !== 'package.json'),
{
file: 'package.json',
data: JSON.stringify(newParsedFile, null, 2),
},
],
};
};
}