-
Notifications
You must be signed in to change notification settings - Fork 0
/
Index.ts
185 lines (147 loc) · 5.74 KB
/
Index.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
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
import config = require('./lib/IJasperBuildConfig');
import composition = require('./lib/ICompositionRoot');
import p = require('./lib/PackageBuilder');
import builder = require('./lib/project/ProjectStructureBuilder');
import struct = require('./lib/project/Structure');
import areas = require('./lib/IAreaService');
import path = require('path');
import patches = require('./lib/SinglePagePatch');
export class BuildManager {
private projectStructureBuilder:builder.ProjectStructureBuilder;
private packageBuilder:p.PackageBuilder;
private areasSvc:areas.AreaService;
private structure:struct.IProjectStructure;
constructor(private buildConfig:config.IJasperBuildConfig,
private root:composition.ICompositionRoot = new composition.JasperCompositionRoot()) {
this.validateConfig(buildConfig);
this.projectStructureBuilder = new builder.ProjectStructureBuilder(
this.root.fileUtils,
this.buildConfig,
this.root.scriptsFinder,
this.root.stylesFinder);
this.packageBuilder = new p.PackageBuilder(this.root.cssMinifier,
this.root.scriptsMinifier,
this.buildConfig,
this.root.fileUtils,
this.root.logger);
this.areasSvc = new areas.AreaService(this.root.fileUtils);
}
updateConfig(buildConfig:config.IJasperBuildConfig) {
this.buildConfig = buildConfig;
}
buildProject() {
this.root.logger.info('Building jasper application...');
this.buildConfig.package = false;
this.createStructure(false);
// build _init.js file of all areas
this.areasSvc.buildAllAreas(this.structure);
//_areas.js
var areaConfigPath = this.buildAreasConfig(this.structure);
//_routes.js
var routesConfigPath = this.buildRoutesConfig(this.structure);
//_values.js
var valuesConfigPath:string = null;
if (this.structure.values) {
valuesConfigPath = this.buildValuesConfig(this.structure);
}
this.patchSinglePage(this.structure, areaConfigPath, routesConfigPath, valuesConfigPath);
this.root.logger.info('Build success.');
}
packageProject() {
this.buildConfig.package = true;
this.createStructure(false);
this.areasSvc.buildAllAreas(this.structure);
this.packageBuilder.packageApp(this.structure);
}
rebuildArea(areaName:string, rebuildRoutes:boolean = true) {
this.root.logger.info(`Rebuilding area '${areaName}'...`);
this.createStructure(true);
var area = this.projectStructureBuilder.buildArea(areaName);
//build _init.js file
this.areasSvc.buildArea(area);
// update area def
for (var i = 0; i < this.structure.areas.length; i++) {
if (this.structure.areas[i].name === areaName) {
this.structure.areas[i] = area;
break;
}
}
this.root.logger.info(`Success.'`);
}
rebuildProjectClientConfig() {
this.createStructure(false);
this.buildAreasConfig(this.structure);
this.buildRoutesConfig(this.structure);
if (this.structure.values) {
this.buildValuesConfig(this.structure);
}
}
static createDefaultConfig():config.IJasperBuildConfig {
return new config.DefaultBuildConfig();
}
private createStructure(userCache:boolean) {
if (!this.structure || !userCache) {
this.structure = this.projectStructureBuilder.buildStructure();
}
}
private buildRoutesConfig(structure:struct.IProjectStructure):string {
var routeScript = structure.routes.toClientConfigScript();
var routeConfigPath = path.join(this.buildConfig.appPath, '_routes.js');
this.root.fileUtils.writeFile(routeConfigPath, routeScript);
return routeConfigPath;
}
private buildValuesConfig(structure:struct.IProjectStructure):string {
var valuesScript = structure.values.toClientConfigScript();
var valuesConfigPath = path.join(this.buildConfig.appPath, '_values.js');
this.root.fileUtils.writeFile(valuesConfigPath, valuesScript);
return valuesConfigPath;
}
private buildAreasConfig(structure:struct.IProjectStructure):string {
var areasConfig = struct.AreasClientOptions.createFromProject(structure);
var areasScript = areasConfig.toClientConfigScript();
var areasConfigPath = path.join(this.buildConfig.appPath, '_areas.js');
this.root.fileUtils.writeFile(areasConfigPath, areasScript);
return areasConfigPath;
}
private patchSinglePage(structure:struct.IProjectStructure, areaConfigPath:string, routesConfigPath:string, valuesConfigPath:string) {
var patch = new patches.SinglePagePatch(this.root.fileUtils);
var scripts = this.buildConfig.baseScripts || [];
scripts.push(areaConfigPath);
scripts.push(routesConfigPath);
if (valuesConfigPath) {
scripts.push(valuesConfigPath);
}
scripts.push(this.buildConfig.startup);
if (this.buildConfig.jDebugEnabled) {
scripts.push(this.buildConfig.jDebugSrc);
}
// search all app styles, first determined in config
var styles = [];
structure.cssTargets.forEach(target=> {
styles = styles.concat(target.files);
});
// then append all areas styles
structure.areas.forEach(area => {
styles = styles.concat(area.__styles);
});
if (this.buildConfig.jDebugEnabled && this.buildConfig.jDebugStylePath) {
styles.push(this.buildConfig.jDebugStylePath);
}
var content = patch.applyPatch(this.buildConfig.singlePage, this.buildConfig.baseHref, scripts, styles);
// override
this.root.fileUtils.writeFile(this.buildConfig.singlePage, content);
}
private validateConfig(buildConfig:config.IJasperBuildConfig) {
var requiredProperties = [
'appPath', 'singlePage'
];
requiredProperties.forEach(prop=> {
if (!buildConfig[prop]) {
throw "Specify '" + prop + "' option.";
}
});
if (!buildConfig.baseHref) {
buildConfig.baseHref = '';
}
}
}