Skip to content

Commit 570640c

Browse files
committed
Move reused code into new file
1 parent b61a0b8 commit 570640c

File tree

3 files changed

+84
-199
lines changed

3 files changed

+84
-199
lines changed

devtools/dashboard_utilities.mjs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import fs from 'fs';
2+
import path from 'path';
3+
import constants from '../tasks/util/constants.js';
4+
5+
function readFilePromise(file) {
6+
return new Promise((resolve, reject) => {
7+
fs.readFile(file, { encoding: 'utf-8' }, (err, contents) => {
8+
if (err) reject(err);
9+
else resolve({ name: file, contents: contents });
10+
});
11+
});
12+
}
13+
14+
function writeFilePromise(path, contents) {
15+
return new Promise((resolve, reject) => {
16+
fs.writeFile(path, contents, (err) => {
17+
if (err) reject(err);
18+
else resolve(path);
19+
});
20+
});
21+
}
22+
23+
export function getMockFiles() {
24+
return new Promise((resolve, reject) => {
25+
fs.readdir(constants.pathToTestImageMocks, (err, files) => {
26+
if (err) reject(err);
27+
else resolve(files);
28+
});
29+
});
30+
}
31+
32+
export function readFiles(files) {
33+
const promises = files.map((file) => readFilePromise(path.join(constants.pathToTestImageMocks, file)));
34+
35+
return Promise.all(promises);
36+
}
37+
38+
export function createMocksList(files) {
39+
// eliminate pollutants (e.g .DS_Store) that can accumulate in the mock directory
40+
const jsonFiles = files.filter((file) => file.name.substr(-5) === '.json');
41+
42+
const mocksList = jsonFiles.map((file) => {
43+
const contents = JSON.parse(file.contents);
44+
45+
// get plot type keywords from mocks
46+
const types = contents.data
47+
.map((trace) => trace.type || 'scatter')
48+
.reduce((acc, type, i, arr) => (arr.lastIndexOf(type) === i ? [...acc, type] : acc), []);
49+
50+
const filename = file.name.split(path.sep).pop();
51+
52+
return {
53+
name: filename.slice(0, -5),
54+
file: filename,
55+
keywords: types.join(', ')
56+
};
57+
});
58+
59+
return mocksList;
60+
}
61+
62+
function saveListToFile(filePath, fileName) {
63+
return (list) => writeFilePromise(path.join(filePath, fileName), JSON.stringify(list, null, 2));
64+
}
65+
66+
export const saveMockListToFile = saveListToFile(constants.pathToBuild, 'test_dashboard_mocks.json');
67+
export const saveReglTracesToFile = saveListToFile(constants.pathToBuild, 'regl_traces.json');

devtools/regl_codegen/server.mjs

Lines changed: 12 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
import ecstatic from 'ecstatic';
2+
import { build } from 'esbuild';
13
import fs from 'fs';
2-
import path from 'path';
34
import http from 'http';
4-
import ecstatic from 'ecstatic';
5-
import open from 'open';
65
import minimist from 'minimist';
7-
6+
import open from 'open';
7+
import path from 'path';
8+
import { localDevReglCodegenConfig as config } from '../../esbuild-config.js';
89
import constants from '../../tasks/util/constants.js';
9-
import { build } from 'esbuild';
10-
import { esbuildConfig as config } from '../../esbuild-config.js';
10+
import {
11+
createMocksList,
12+
getMockFiles,
13+
readFiles,
14+
saveMockListToFile,
15+
saveReglTracesToFile
16+
} from '../dashboard_utilities.mjs';
1117

1218
var args = minimist(process.argv.slice(2), {});
1319
var PORT = args.port || 3000;
@@ -66,101 +72,6 @@ open('http://localhost:' + PORT + '/devtools/regl_codegen/index' + (strict ? '-s
6672

6773
await build(config);
6874

69-
function getMockFiles() {
70-
return new Promise(function (resolve, reject) {
71-
fs.readdir(constants.pathToTestImageMocks, function (err, files) {
72-
if (err) {
73-
reject(err);
74-
} else {
75-
resolve(files);
76-
}
77-
});
78-
});
79-
}
80-
81-
function readFiles(files) {
82-
var promises = files.map(function (file) {
83-
var filePath = path.join(constants.pathToTestImageMocks, file);
84-
return readFilePromise(filePath);
85-
});
86-
87-
return Promise.all(promises);
88-
}
89-
90-
function createMocksList(files) {
91-
// eliminate pollutants (e.g .DS_Store) that can accumulate in the mock directory
92-
var jsonFiles = files.filter(function (file) {
93-
return file.name.substr(-5) === '.json';
94-
});
95-
96-
var mocksList = jsonFiles.map(function (file) {
97-
var contents = JSON.parse(file.contents);
98-
99-
// get plot type keywords from mocks
100-
var types = contents.data
101-
.map(function (trace) {
102-
return trace.type || 'scatter';
103-
})
104-
.reduce(function (acc, type, i, arr) {
105-
if (arr.lastIndexOf(type) === i) {
106-
acc.push(type);
107-
}
108-
return acc;
109-
}, []);
110-
111-
var filename = file.name.split(path.sep).pop();
112-
113-
return {
114-
name: filename.slice(0, -5),
115-
file: filename,
116-
keywords: types.join(', ')
117-
};
118-
});
119-
120-
return mocksList;
121-
}
122-
123-
function saveMockListToFile(mocksList) {
124-
var filePath = path.join(constants.pathToBuild, 'test_dashboard_mocks.json');
125-
var content = JSON.stringify(mocksList, null, 4);
126-
127-
return writeFilePromise(filePath, content);
128-
}
129-
130-
function saveReglTracesToFile(traces) {
131-
var filePath = path.join(constants.pathToBuild, 'regl_traces.json');
132-
var content = JSON.stringify(traces, null, 4);
133-
134-
return writeFilePromise(filePath, content);
135-
}
136-
137-
function readFilePromise(file) {
138-
return new Promise(function (resolve, reject) {
139-
fs.readFile(file, { encoding: 'utf-8' }, function (err, contents) {
140-
if (err) {
141-
reject(err);
142-
} else {
143-
resolve({
144-
name: file,
145-
contents: contents
146-
});
147-
}
148-
});
149-
});
150-
}
151-
152-
function writeFilePromise(path, contents) {
153-
return new Promise(function (resolve, reject) {
154-
fs.writeFile(path, contents, function (err) {
155-
if (err) {
156-
reject(err);
157-
} else {
158-
resolve(path);
159-
}
160-
});
161-
});
162-
}
163-
16475
function handleCodegen(data) {
16576
var trace = data.trace;
16677
var generated = data.generated;

devtools/test_dashboard/server.mjs

Lines changed: 5 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
import fs from 'fs';
2-
import path from 'path';
3-
import http from 'http';
41
import ecstatic from 'ecstatic';
5-
import open from 'open';
2+
import { build, context } from 'esbuild';
3+
import http from 'http';
64
import minimist from 'minimist';
7-
8-
import constants from '../../tasks/util/constants.js';
9-
import { context, build } from 'esbuild';
10-
5+
import open from 'open';
116
import { devtoolsConfig, localDevConfig } from '../../esbuild-config.js';
7+
import constants from '../../tasks/util/constants.js';
8+
import { createMocksList, getMockFiles, readFiles, saveMockListToFile } from '../dashboard_utilities.mjs';
129

1310
var args = minimist(process.argv.slice(2), {});
1411
var PORT = args.port || 3000;
@@ -18,8 +15,6 @@ var mathjax3chtml = args.mathjax3chtml;
1815

1916
if (strict) localDevConfig.entryPoints = ['./lib/index-strict.js'];
2017

21-
var mockFolder = constants.pathToTestImageMocks;
22-
2318
// mock list
2419
await getMockFiles().then(readFiles).then(createMocksList).then(saveMockListToFile);
2520

@@ -71,91 +66,3 @@ function devServer() {
7166
// open up browser window
7267
open(`http://localhost:${PORT}/devtools/test_dashboard/${indexName}${strict ? '?strict=true' : ''}`);
7368
}
74-
75-
function getMockFiles() {
76-
return new Promise(function (resolve, reject) {
77-
fs.readdir(mockFolder, function (err, files) {
78-
if (err) {
79-
reject(err);
80-
} else {
81-
resolve(files);
82-
}
83-
});
84-
});
85-
}
86-
87-
function readFiles(files) {
88-
var promises = files.map(function (file) {
89-
var filePath = path.join(mockFolder, file);
90-
return readFilePromise(filePath);
91-
});
92-
93-
return Promise.all(promises);
94-
}
95-
96-
function createMocksList(files) {
97-
// eliminate pollutants (e.g .DS_Store) that can accumulate in the mock directory
98-
var jsonFiles = files.filter(function (file) {
99-
return file.name.substr(-5) === '.json';
100-
});
101-
102-
var mocksList = jsonFiles.map(function (file) {
103-
var contents = JSON.parse(file.contents);
104-
105-
// get plot type keywords from mocks
106-
var types = contents.data
107-
.map(function (trace) {
108-
return trace.type || 'scatter';
109-
})
110-
.reduce(function (acc, type, i, arr) {
111-
if (arr.lastIndexOf(type) === i) {
112-
acc.push(type);
113-
}
114-
return acc;
115-
}, []);
116-
117-
var filename = file.name.split(path.sep).pop();
118-
119-
return {
120-
name: filename.slice(0, -5),
121-
file: filename,
122-
keywords: types.join(', ')
123-
};
124-
});
125-
126-
return mocksList;
127-
}
128-
129-
function saveMockListToFile(mocksList) {
130-
var filePath = path.join(constants.pathToBuild, 'test_dashboard_mocks.json');
131-
var content = JSON.stringify(mocksList, null, 4);
132-
133-
return writeFilePromise(filePath, content);
134-
}
135-
136-
function readFilePromise(file) {
137-
return new Promise(function (resolve, reject) {
138-
fs.readFile(file, { encoding: 'utf-8' }, function (err, contents) {
139-
if (err) {
140-
reject(err);
141-
} else {
142-
resolve({
143-
name: file,
144-
contents: contents
145-
});
146-
}
147-
});
148-
});
149-
}
150-
151-
function writeFilePromise(path, contents) {
152-
return new Promise(function (resolve, reject) {
153-
fs.writeFile(path, contents, function (err) {
154-
if (err) {
155-
reject(err);
156-
} else {
157-
resolve(path);
158-
}
159-
});
160-
});
161-
}

0 commit comments

Comments
 (0)