Initial commit

This commit is contained in:
Robert Nasarek 2026-06-25 09:11:23 +02:00
commit 05c65aad4d
155 changed files with 93617 additions and 0 deletions

51
scripts/pack-dist.js Normal file
View file

@ -0,0 +1,51 @@
import fs from 'fs';
import path from 'path';
import { spawnSync } from 'child_process';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const distDir = path.resolve(__dirname, '..', 'dist');
const outFile = path.resolve(__dirname, '..', 'dfg_3dviewer-dist.zip');
if (!fs.existsSync(distDir)) {
console.error('dist directory not found. Run `npm run build` first.');
process.exit(1);
}
// Normalize index.html asset paths from absolute (/file) to relative (./file)
const indexPath = path.join(distDir, 'index.html');
if (fs.existsSync(indexPath)) {
let html = fs.readFileSync(indexPath, 'utf8');
// replace href=/file or href="/file" or href='/file' with href="./file"
html = html.replace(/href=\/?"?\/?([^\s'">]+)"?/g, (m, p1) => `href="./${p1.replace(/^\/+/, '')}"`);
html = html.replace(/src=\/?"?\/?([^\s'">]+)"?/g, (m, p1) => `src="./${p1.replace(/^\/+/, '')}"`);
fs.writeFileSync(indexPath, html, 'utf8');
console.log('Rewrote asset paths in dist/index.html to use relative paths');
}
if (fs.existsSync(outFile)) {
fs.rmSync(outFile);
}
const zipProcess = spawnSync('zip', ['-rq', outFile, '.'], {
cwd: distDir,
stdio: 'inherit',
});
if (zipProcess.error) {
if (zipProcess.error.code === 'ENOENT') {
console.error('`zip` command not found. Install it or use an environment that provides it before running `npm run pack-dist`.');
process.exit(1);
}
throw zipProcess.error;
}
if (zipProcess.status !== 0) {
process.exit(zipProcess.status ?? 1);
}
const archiveSize = fs.statSync(outFile).size;
console.log(`Created ${outFile} (${archiveSize} total bytes)`);

41
scripts/pack-library.js Normal file
View file

@ -0,0 +1,41 @@
import fs from 'fs';
import path from 'path';
import { spawnSync } from 'child_process';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const libraryDir = path.resolve(__dirname, '..', 'dist', 'library');
const outFile = path.resolve(__dirname, '..', 'dfg-3dviewer-library.zip');
if (!fs.existsSync(libraryDir)) {
console.error('dist/library directory not found. Run `npm run build:library` first.');
process.exit(1);
}
if (fs.existsSync(outFile)) {
fs.rmSync(outFile);
}
const zipProcess = spawnSync('zip', ['-rq', outFile, '.'], {
cwd: libraryDir,
stdio: 'inherit',
});
if (zipProcess.error) {
if (zipProcess.error.code === 'ENOENT') {
console.error('`zip` command not found. Install it before running `npm run pack:library`.');
process.exit(1);
}
throw zipProcess.error;
}
if (zipProcess.status !== 0) {
process.exit(zipProcess.status ?? 1);
}
const archiveSize = fs.statSync(outFile).size;
console.log(`Created ${outFile} (${archiveSize} total bytes)`);
console.log('Install by extracting to web/libraries/dfg-3dviewer/ on your Drupal site.');

13
scripts/run-local-tests.sh Executable file
View file

@ -0,0 +1,13 @@
#!/bin/sh
set -eu
ROOT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)
cd "$ROOT_DIR"
echo "[local-tests] building test bundle"
npm run build:test
echo "[local-tests] running Playwright viewer tests"
CI=1 npx playwright test tests/viewer.spec.ts --project chromium-webgl --workers=1 "$@"

53
scripts/serve-dist.js Normal file
View file

@ -0,0 +1,53 @@
import http from 'http'
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url))
const distDir = process.env.DIST_DIR || 'dist'
const root = path.resolve(__dirname, '..', distDir)
const host = process.env.HOST || '127.0.0.1'
const port = process.env.PORT || 8080
const mime = {
'.js': 'application/javascript',
'.css': 'text/css',
'.html': 'text/html',
'.wasm': 'application/wasm',
'.json': 'application/json',
'.svg': 'image/svg+xml'
}
const server = http.createServer((req, res) => {
try {
let reqUrl = decodeURI(new URL(req.url, `http://localhost`).pathname)
if (reqUrl === '/') reqUrl = '/index.html'
const safePath = path.normalize(path.join(root, reqUrl))
if (!safePath.startsWith(root)) {
res.statusCode = 403
return res.end('Forbidden')
}
fs.stat(safePath, (err, stats) => {
if (err || !stats.isFile()) {
res.statusCode = 404
return res.end('Not found')
}
const ext = path.extname(safePath).toLowerCase()
res.setHeader('Content-Type', mime[ext] || 'application/octet-stream')
res.setHeader('Content-Length', stats.size)
const stream = fs.createReadStream(safePath)
stream.pipe(res)
stream.on('error', () => {
res.statusCode = 500
res.end()
})
})
} catch (e) {
res.statusCode = 500
res.end('Server error')
}
})
server.listen(port, host, () => console.log(`Serving ${root} at http://${host}:${port}/`))