-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcreateProject.js
executable file
·258 lines (209 loc) · 7.93 KB
/
createProject.js
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/usr/bin/env node
const commander = require('commander');
const package = require('./package.json');
const fs = require('fs');
const glob = require('glob');
const path = require("path");
const puppeteer = require('puppeteer');
const httpServer = require("http-server");
const convert2xkt = require("@xeokit/xeokit-convert/dist/convert2xkt.cjs.js");
const SERVER_PORT = 3000;
const SCREENSHOT_SIZE = [200, 200];
const HEADLESS = false;
const date = new Date('14 Jun 2017 00:00:00 PDT');
const dateUTC = date.toUTCString();
const chromeOptions = {
product: 'chrome',
headless: HEADLESS,
args: [`--window-size=${SCREENSHOT_SIZE[0]},${SCREENSHOT_SIZE[1]}`],
defaultViewport: {
width: SCREENSHOT_SIZE[0],
height: SCREENSHOT_SIZE[1]
}
};
const program = new commander.Command();
program.version(package.version, '-v, --version');
program
.option('-s, --source [file]', 'path to source model file(s)')
.option('-p, --projectid [string]', 'ID for new project')
.option('-d, --datadir [file]', 'optional path to target data directory - default is "./data"');
program.on('--help', () => {
});
program.parse(process.argv);
const options = program.opts();
if (options.source === undefined) {
console.error('\n\nError: please specify path to source model file(s).');
program.help();
process.exit(1);
}
if (options.projectid === undefined) {
console.error('\n\nError: please specify project ID.');
program.help();
process.exit(1);
}
const source = options.source;
const projectId = options.projectid;
const dataDir = options.datadir || "./data";
const projectsDir = `${dataDir}/projects`;
const projectsIndexPath = `${projectsDir}/index.json`;
const projectDir = `${projectsDir}/${projectId}`;
const projectIndexPath = `${projectDir}/index.json`;
const projectModelsDir = `${projectDir}/models`;
function log(msg) {
console.log("[createProject] " + msg);
}
createProject().catch(err => {
console.error('Error:', err);
process.exit(1);
});
async function createProject() {
if (!fs.existsSync(dataDir)) {
log(`Creating ${dataDir}`);
fs.mkdirSync(dataDir);
}
if (!fs.existsSync(projectsDir)) {
log(`Creating ${projectsDir}`);
fs.mkdirSync(projectsDir);
}
if (!fs.existsSync(projectDir)) {
log(`Creating ${projectDir}`);
fs.mkdirSync(projectDir);
}
if (!fs.existsSync(projectsIndexPath)) {
const projectsIndex = {
"projects": []
};
projectsIndex.projects.push({
id: projectId,
name: projectId
});
await fs.promises.writeFile(projectsIndexPath, JSON.stringify(projectsIndex, null, "\t"));
} else {
const projectsIndex = JSON.parse(await fs.readFileSync(projectsIndexPath));
if (!projectsIndex.projects) {
log(`Invalid projects index ("projects" list not found): ${projectsIndexPath}`);
process.exit(-1);
}
for (let i = 0, len = projectsIndex.projects.length; i < len; i++) {
const projectInfo = projectsIndex.projects[i];
if (projectInfo.id === projectId) {
log(`Project already exists: "${projectId}"`);
process.exit(1);
}
}
projectsIndex.projects.push({
id: projectId,
name: projectId
});
log(`Creating new project "${projectId}" ...`);
fs.writeFileSync(projectsIndexPath, JSON.stringify(projectsIndex, null, "\t"));
}
fs.mkdirSync(projectModelsDir);
const projectIndex = {
"id": projectId,
"name": projectId,
created: dateUTC,
"models": [],
"viewerConfigs": {},
"viewerContent": {},
"viewerState": {}
}
const stats = {};
const promises = [];
glob.sync(source).map(async modelSrc => {
const modelId = path.basename(modelSrc, path.extname(modelSrc));
const modelDestDir = `${projectDir}/models/${modelId}`;
const xktDest = `${modelDestDir}/geometry.xkt`;
const screenshotDestDir = `${projectDir}/models/${modelId}/screenshot/`;
const sourceDestDir = `${projectDir}/models/${modelId}/source/`;
if (!fs.existsSync(modelDestDir)) {
fs.mkdirSync(modelDestDir);
}
if (!fs.existsSync(screenshotDestDir)) {
fs.mkdirSync(screenshotDestDir);
}
if (!fs.existsSync(sourceDestDir)) {
fs.mkdirSync(sourceDestDir);
//...TODO: copy
}
try {
const projectsIndexModel = {
id: modelId,
name: modelId,
metadata: {}
};
projectIndex.models.push(projectsIndexModel);
promises.push(convert2xkt({
source: modelSrc,
output: xktDest,
outputStats: (stats) => {
const metadata = projectsIndexModel.metadata;
metadata.sourceFormat = stats.sourceFormat || "";
metadata.schemaVersion = stats.schemaVersion || "";
metadata.title = stats.title || "";
metadata.author = stats.author || "";
metadata.created = stats.created || "";
metadata.numPropertySets = stats.numPropertySets || 0;
metadata.numMetaObjects = stats.numMetaObjects || 0;
metadata.numObjects = stats.numObjects || 0;
metadata.numTriangles = stats.numTriangles || 0;
metadata.numVertices = stats.numVertices || 0;
metadata.xktSizeKb = stats.xktSize || 0;
metadata.aabb = stats.aabb ? Array.from(stats.aabb) : null;
},
log: (msg) => {
//console.log(msg)
}
}));
} catch (e) {
//console.log(e)
}
});
Promise.all(promises).then(() => {
fs.writeFileSync(projectIndexPath, JSON.stringify(projectIndex, null, "\t"));
log(`Project "${projectId}" created.`);
process.exit(0);
});
async function createScreenshots(testStats) {
log("Creating screenshots...\n");
let server = httpServer.createServer();
server.listen(SERVER_PORT);
const modelStats = testStats.modelStats;
for (let i = 0, len = SOURCE_FILES.length; i < len; i++) {
const fileInfo = SOURCE_FILES[i];
const modelId = fileInfo.modelId;
const stats = modelStats[modelId];
if (!stats) {
continue;
}
const xktDest = stats.xktDest;
if (!xktDest) {
continue;
}
const screenshotDir = `${OUTPUT_DIR}/${modelId}/screenshot`;
const screenshotPath = `${screenshotDir}/screenshot.png`;
if (!fs.existsSync(screenshotDir)) {
fs.mkdirSync(screenshotDir);
}
const browser = await puppeteer.launch(chromeOptions);
const page = await browser.newPage();
if (!testStats.browserVersion) {
testStats.browserVersion = await page.browser().version();
}
await page.setDefaultNavigationTimeout(3000000);
await page.goto(`http://localhost:${SERVER_PORT}/perfTests/perfTestXKT.html?xktSrc=../${xktDest}`);
await page.waitForSelector('#percyLoaded')
const element = await page.$('#percyLoaded')
const value = await page.evaluate(el => el.innerText, element)
const pageStats = JSON.parse(value);
await page.screenshot({path: screenshotPath});
await page.close();
await browser.close();
stats.loadingTime = pageStats.loadingTime;
stats.fps = pageStats.fps;
stats.screenshot = "screenshot.png";
}
server.close();
log("All XKT models tested in xeokit.\n");
}
}