-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathindex.mjs
More file actions
115 lines (106 loc) · 6.37 KB
/
index.mjs
File metadata and controls
115 lines (106 loc) · 6.37 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
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors. ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
import * as fs from "fs";
import { get_examples, LOCAL_EXAMPLES } from "./examples.js";
import * as url from "url";
import * as path from "node:path";
import { execSync } from "child_process";
const version = JSON.parse(fs.readFileSync("./package.json")).version;
const __dirname = url.fileURLToPath(new URL(".", import.meta.url)).slice(0, -1);
// TODO jsdelivr has slightly different logic for trailing '/' that causes
// the wasm assets to not load correctly when using aliases, hence we must link
// directly to the assets.
const replacements = {
"/node_modules/": `https://cdn.jsdelivr.net/npm/`,
"@perspective-dev/client/dist/cdn/perspective.js": `@perspective-dev/client@${version}/dist/cdn/perspective.js`,
"@perspective-dev/viewer/dist/cdn/perspective-viewer.js": `@perspective-dev/viewer@${version}/dist/cdn/perspective-viewer.js`,
"@perspective-dev/viewer-datagrid/dist/cdn/perspective-viewer-datagrid.js": `@perspective-dev/viewer-datagrid@${version}/dist/cdn/perspective-viewer-datagrid.js`,
"@perspective-dev/viewer-d3fc/dist/cdn/perspective-viewer-d3fc.js": `@perspective-dev/viewer-d3fc@${version}/dist/cdn/perspective-viewer-d3fc.js`,
"@perspective-dev/workspace/dist/cdn/perspective-workspace.js": `@perspective-dev/workspace@${version}/dist/cdn/perspective-workspace.js`,
"@perspective-dev/server/dist/cdn/perspective.cpp.wasm": `@perspective-dev/client@${version}/dist/cdn/perspective.cpp.wasm`,
"@perspective-dev/viewer/dist/cdn/perspective.rx.wasm": `@perspective-dev/viewer@${version}/dist/cdn/perspective.rx.wasm`,
"@perspective-dev/client/dist/cdn/perspective.worker.js": `@perspective-dev/client@${version}/dist/cdn/perspective.worker.js`,
};
export async function dist_examples(
outpath = `${__dirname}/../../docs/static/blocks`,
) {
execSync(`mkdir -p ${outpath}`, { stdio: "inherit" });
const readme = generate_readme();
let existing = fs.readFileSync(`${__dirname}/../../README.md`).toString();
existing = existing.replace(
/<\!\-\- Examples \-\->([\s\S]+?)<\!\-\- Examples \-\->/gm,
`<!-- Examples -->\n${readme}\n<!-- Examples -->`,
);
fs.writeFileSync(`${__dirname}/../../README.md`, existing);
for (const name of LOCAL_EXAMPLES) {
// Copy
if (fs.existsSync(`${__dirname}/src/${name}`)) {
// Copy
for (const filename of fs.readdirSync(`${__dirname}/src/${name}`)) {
execSync(`mkdir -p ${outpath}/${name}`, { stdio: "inherit" });
if (
filename.endsWith(".mjs") ||
filename.endsWith(".js") ||
filename.endsWith(".html")
) {
let filecontents = fs
.readFileSync(`${__dirname}/src/${name}/${filename}`)
.toString();
for (const pattern of Object.keys(replacements)) {
filecontents = filecontents.replace(
new RegExp(pattern, "g"),
replacements[pattern],
);
}
fs.writeFileSync(
`${outpath}/${name}/${filename}`,
filecontents,
);
} else if (filename !== ".git") {
execSync(
`cp ${__dirname}/src/${name}/${filename} ${outpath}/${name}/${filename}`,
{ stdio: "inherit" },
);
}
}
// build
if (fs.existsSync(path.join(outpath, name, "build.mjs"))) {
console.log("Building " + name);
const script = `${outpath}/${name}/build.mjs`;
execSync(`node ${script}`, { stdio: "inherit" });
}
}
}
}
function partition(input, spacing) {
let output = [];
for (let i = 0; i < input.length; i += spacing) {
output[output.length] = input.slice(i, i + spacing);
}
return output;
}
function generate_readme() {
const all = get_examples();
return `<table><tbody>${partition(all, 3)
.map(
(row) =>
`<tr>${row
.map((y) => `<td>${y.name}</td>`)
.join("")}</tr><tr>${row
.map(
(y) =>
`<td><a href="${y.url}"><img height="125" src="${y.img}"></img></a></td>`,
)
.join("")}</tr>`,
)
.join("")}</tbody></table>`;
}