-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathbuild.js
More file actions
105 lines (88 loc) · 3.67 KB
/
build.js
File metadata and controls
105 lines (88 loc) · 3.67 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
import esbuild from 'esbuild';
import { cpSync, rmSync, mkdirSync, readdirSync, statSync } from 'fs';
import { join, extname } from 'path';
const watch = process.argv.includes('--watch');
// ── Clean dist/ (keep directory) ─────────────────────────────────────────────
rmSync('dist', { recursive: true, force: true });
mkdirSync('dist', { recursive: true });
// ── Bundle JS ────────────────────────────────────────────────────────────────
const ctx = await esbuild.context({
entryPoints: ['src/tesla-card.js'],
bundle: true,
outfile: 'dist/tesla-card.js',
format: 'esm',
target: 'es2020',
minify: !watch,
sourcemap: watch ? 'inline' : false,
logLevel: 'info',
});
if (watch) {
await ctx.watch();
console.log('Watching for changes...');
} else {
await ctx.rebuild();
await ctx.dispose();
// ── Copy images into dist/ for HACS delivery ────────────────────────────────
// HACS downloads everything in dist/ — so images ship alongside the JS file.
// Only copy new-format variant images (e.g. 3/3.1/neutral/base.png),
// excluding _previews/, RAW/, .old files, and .DS_Store.
const MODELS_SRC = 'images/models';
const BUTTONS_SRC = 'images/buttons';
// Recursively copy a directory, skipping dotfiles, .old, _previews, RAW
function copyDir(src, dest) {
for (const entry of readdirSync(src)) {
if (entry.startsWith('.')) continue;
if (entry.includes('.old.')) continue;
if (entry === '_previews' || entry === 'RAW') continue;
const srcPath = join(src, entry);
const destPath = join(dest, entry);
if (statSync(srcPath).isDirectory()) {
mkdirSync(destPath, { recursive: true });
copyDir(srcPath, destPath);
} else {
cpSync(srcPath, destPath);
}
}
}
// Model images: only variant-based directories (e.g. 3/3.1/, Y/Y.1/, S/S.1/)
function copyModelImages(srcDir, destDir) {
for (const model of readdirSync(srcDir)) {
const modelPath = join(srcDir, model);
if (!statSync(modelPath).isDirectory()) continue;
for (const variant of readdirSync(modelPath)) {
const variantPath = join(modelPath, variant);
if (!statSync(variantPath).isDirectory()) continue;
// Only copy variant dirs (contain a dot, e.g. "3.1", "Y.1", "S.1")
if (!variant.includes('.')) continue;
for (const colour of readdirSync(variantPath)) {
const colourPath = join(variantPath, colour);
if (!statSync(colourPath).isDirectory()) continue;
if (colour === '_previews' || colour === 'RAW') continue;
const destColour = join(destDir, model, variant, colour);
mkdirSync(destColour, { recursive: true });
copyDir(colourPath, destColour);
}
}
}
}
copyModelImages(MODELS_SRC, 'dist');
// Button SVGs only (not legacy JPGs)
mkdirSync('dist/buttons', { recursive: true });
for (const file of readdirSync(BUTTONS_SRC)) {
if (extname(file).toLowerCase() === '.svg') {
cpSync(join(BUTTONS_SRC, file), join('dist/buttons', file));
}
}
// Summary
let imgCount = 0;
function countFiles(dir) {
for (const f of readdirSync(dir)) {
const p = join(dir, f);
if (statSync(p).isDirectory()) countFiles(p);
else if (f !== 'tesla-card.js') imgCount++;
}
}
countFiles('dist');
console.log(`Copied ${imgCount} image assets into dist/`);
console.log('Build complete → dist/');
}