Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Move reused code into new file
  • Loading branch information
camdecoster committed Oct 20, 2025
commit 570640cad4e19b205554f8794acc36700eacb645
67 changes: 67 additions & 0 deletions devtools/dashboard_utilities.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import fs from 'fs';
import path from 'path';
import constants from '../tasks/util/constants.js';

function readFilePromise(file) {
return new Promise((resolve, reject) => {
fs.readFile(file, { encoding: 'utf-8' }, (err, contents) => {
if (err) reject(err);
else resolve({ name: file, contents: contents });
});
});
}

function writeFilePromise(path, contents) {
return new Promise((resolve, reject) => {
fs.writeFile(path, contents, (err) => {
if (err) reject(err);
else resolve(path);
});
});
}

export function getMockFiles() {
return new Promise((resolve, reject) => {
fs.readdir(constants.pathToTestImageMocks, (err, files) => {
if (err) reject(err);
else resolve(files);
});
});
}

export function readFiles(files) {
const promises = files.map((file) => readFilePromise(path.join(constants.pathToTestImageMocks, file)));

return Promise.all(promises);
}

export function createMocksList(files) {
// eliminate pollutants (e.g .DS_Store) that can accumulate in the mock directory
const jsonFiles = files.filter((file) => file.name.substr(-5) === '.json');

const mocksList = jsonFiles.map((file) => {
const contents = JSON.parse(file.contents);

// get plot type keywords from mocks
const types = contents.data
.map((trace) => trace.type || 'scatter')
.reduce((acc, type, i, arr) => (arr.lastIndexOf(type) === i ? [...acc, type] : acc), []);

const filename = file.name.split(path.sep).pop();

return {
name: filename.slice(0, -5),
file: filename,
keywords: types.join(', ')
};
});

return mocksList;
}

function saveListToFile(filePath, fileName) {
return (list) => writeFilePromise(path.join(filePath, fileName), JSON.stringify(list, null, 2));
}

export const saveMockListToFile = saveListToFile(constants.pathToBuild, 'test_dashboard_mocks.json');
export const saveReglTracesToFile = saveListToFile(constants.pathToBuild, 'regl_traces.json');
113 changes: 12 additions & 101 deletions devtools/regl_codegen/server.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import ecstatic from 'ecstatic';
import { build } from 'esbuild';
import fs from 'fs';
import path from 'path';
import http from 'http';
import ecstatic from 'ecstatic';
import open from 'open';
import minimist from 'minimist';

import open from 'open';
import path from 'path';
import { localDevReglCodegenConfig as config } from '../../esbuild-config.js';
import constants from '../../tasks/util/constants.js';
import { build } from 'esbuild';
import { esbuildConfig as config } from '../../esbuild-config.js';
import {
createMocksList,
getMockFiles,
readFiles,
saveMockListToFile,
saveReglTracesToFile
} from '../dashboard_utilities.mjs';

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

await build(config);

function getMockFiles() {
return new Promise(function (resolve, reject) {
fs.readdir(constants.pathToTestImageMocks, function (err, files) {
if (err) {
reject(err);
} else {
resolve(files);
}
});
});
}

function readFiles(files) {
var promises = files.map(function (file) {
var filePath = path.join(constants.pathToTestImageMocks, file);
return readFilePromise(filePath);
});

return Promise.all(promises);
}

function createMocksList(files) {
// eliminate pollutants (e.g .DS_Store) that can accumulate in the mock directory
var jsonFiles = files.filter(function (file) {
return file.name.substr(-5) === '.json';
});

var mocksList = jsonFiles.map(function (file) {
var contents = JSON.parse(file.contents);

// get plot type keywords from mocks
var types = contents.data
.map(function (trace) {
return trace.type || 'scatter';
})
.reduce(function (acc, type, i, arr) {
if (arr.lastIndexOf(type) === i) {
acc.push(type);
}
return acc;
}, []);

var filename = file.name.split(path.sep).pop();

return {
name: filename.slice(0, -5),
file: filename,
keywords: types.join(', ')
};
});

return mocksList;
}

function saveMockListToFile(mocksList) {
var filePath = path.join(constants.pathToBuild, 'test_dashboard_mocks.json');
var content = JSON.stringify(mocksList, null, 4);

return writeFilePromise(filePath, content);
}

function saveReglTracesToFile(traces) {
var filePath = path.join(constants.pathToBuild, 'regl_traces.json');
var content = JSON.stringify(traces, null, 4);

return writeFilePromise(filePath, content);
}

function readFilePromise(file) {
return new Promise(function (resolve, reject) {
fs.readFile(file, { encoding: 'utf-8' }, function (err, contents) {
if (err) {
reject(err);
} else {
resolve({
name: file,
contents: contents
});
}
});
});
}

function writeFilePromise(path, contents) {
return new Promise(function (resolve, reject) {
fs.writeFile(path, contents, function (err) {
if (err) {
reject(err);
} else {
resolve(path);
}
});
});
}

function handleCodegen(data) {
var trace = data.trace;
var generated = data.generated;
Expand Down
103 changes: 5 additions & 98 deletions devtools/test_dashboard/server.mjs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
import fs from 'fs';
import path from 'path';
import http from 'http';
import ecstatic from 'ecstatic';
import open from 'open';
import { build, context } from 'esbuild';
import http from 'http';
import minimist from 'minimist';

import constants from '../../tasks/util/constants.js';
import { context, build } from 'esbuild';

import open from 'open';
import { devtoolsConfig, localDevConfig } from '../../esbuild-config.js';
import constants from '../../tasks/util/constants.js';
import { createMocksList, getMockFiles, readFiles, saveMockListToFile } from '../dashboard_utilities.mjs';

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

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

var mockFolder = constants.pathToTestImageMocks;

// mock list
await getMockFiles().then(readFiles).then(createMocksList).then(saveMockListToFile);

Expand Down Expand Up @@ -71,91 +66,3 @@ function devServer() {
// open up browser window
open(`http://localhost:${PORT}/devtools/test_dashboard/${indexName}${strict ? '?strict=true' : ''}`);
}

function getMockFiles() {
return new Promise(function (resolve, reject) {
fs.readdir(mockFolder, function (err, files) {
if (err) {
reject(err);
} else {
resolve(files);
}
});
});
}

function readFiles(files) {
var promises = files.map(function (file) {
var filePath = path.join(mockFolder, file);
return readFilePromise(filePath);
});

return Promise.all(promises);
}

function createMocksList(files) {
// eliminate pollutants (e.g .DS_Store) that can accumulate in the mock directory
var jsonFiles = files.filter(function (file) {
return file.name.substr(-5) === '.json';
});

var mocksList = jsonFiles.map(function (file) {
var contents = JSON.parse(file.contents);

// get plot type keywords from mocks
var types = contents.data
.map(function (trace) {
return trace.type || 'scatter';
})
.reduce(function (acc, type, i, arr) {
if (arr.lastIndexOf(type) === i) {
acc.push(type);
}
return acc;
}, []);

var filename = file.name.split(path.sep).pop();

return {
name: filename.slice(0, -5),
file: filename,
keywords: types.join(', ')
};
});

return mocksList;
}

function saveMockListToFile(mocksList) {
var filePath = path.join(constants.pathToBuild, 'test_dashboard_mocks.json');
var content = JSON.stringify(mocksList, null, 4);

return writeFilePromise(filePath, content);
}

function readFilePromise(file) {
return new Promise(function (resolve, reject) {
fs.readFile(file, { encoding: 'utf-8' }, function (err, contents) {
if (err) {
reject(err);
} else {
resolve({
name: file,
contents: contents
});
}
});
});
}

function writeFilePromise(path, contents) {
return new Promise(function (resolve, reject) {
fs.writeFile(path, contents, function (err) {
if (err) {
reject(err);
} else {
resolve(path);
}
});
});
}