-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.cjs
More file actions
119 lines (95 loc) · 3.82 KB
/
Copy pathbuild.cjs
File metadata and controls
119 lines (95 loc) · 3.82 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
116
117
118
119
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
// Paths
const SRC_DIR = path.join(__dirname, 'src');
const ICONS_DIR = path.join(SRC_DIR, 'icons');
const OUTPUT_FILE = path.join(SRC_DIR, 'modules', 'embeddedAssets.js');
// Read version from package.json
const packageJson = JSON.parse(fs.readFileSync(path.join(__dirname, 'package.json'), 'utf8'));
const version = packageJson.version;
// Convert version to short format: "1.1.0" -> "0101" (major + minor, zero-padded)
function getVersionSuffix(version) {
const parts = version.split('.');
const major = parseInt(parts[0] || 0, 10);
const minor = parseInt(parts[1] || 0, 10);
return String(major).padStart(2, '0') + String(minor).padStart(2, '0');
}
const versionSuffix = getVersionSuffix(version);
console.log('🎨 Building embedded assets...\n');
console.log(`📌 Version: ${version} (suffix: v${versionSuffix})\n`);
// Find all PNG files in icons directory
const iconFiles = fs.readdirSync(ICONS_DIR)
.filter(f => f.endsWith('.png'))
.sort();
console.log(`📦 Found ${iconFiles.length} PNG icons:\n`);
// Process each icon
const assets = {};
for (const filename of iconFiles) {
const filePath = path.join(ICONS_DIR, filename);
const buffer = fs.readFileSync(filePath);
const base64 = buffer.toString('base64');
const sizeKB = (buffer.length / 1024).toFixed(1);
// Get base name without extension: "icon-apply.png" -> "icon-apply"
const baseName = path.basename(filename, '.png');
// Create versioned filename: "icon-apply_v0101.png"
const versionedName = `${baseName}_v${versionSuffix}.png`;
assets[baseName] = {
versionedName: versionedName,
base64: base64
};
console.log(` ✓ ${filename} -> ${versionedName} (${sizeKB} KB)`);
}
// Generate the module code
const moduleCode = `// ============================================================================
// EMBEDDED ASSETS - Auto-generated by build.js
// Do not edit this file directly. Run: npm run build:assets
// ============================================================================
// Asset version suffix (from package.json version ${version})
export var ASSETS_VERSION = "${versionSuffix}";
// Runtime path - set by initializeAssets()
var assetsPath = null;
// Embedded base64 data
var EMBEDDED_ASSETS = {
${Object.entries(assets).map(([baseName, data]) =>
` "${data.versionedName}": "${data.base64}"`
).join(',\n')}
};
/**
* Initialize the embedded assets system.
* Creates temp folder and writes icons if they don't exist.
* Must be called before using getAssetPath().
*/
export function initializeAssets() {
assetsPath = api.getTempFolder() + "/easey_assets_v" + ASSETS_VERSION + "/";
// Create folder if it doesn't exist
if (!api.isDirectory(assetsPath)) {
api.makeFolder(assetsPath);
}
// Write each asset if it doesn't exist
for (var filename in EMBEDDED_ASSETS) {
var filePath = assetsPath + filename;
if (!api.isFile(filePath)) {
var base64Data = EMBEDDED_ASSETS[filename];
api.writeEncodedToBinaryFile(filePath, base64Data);
}
}
return assetsPath;
}
/**
* Get the full path to an asset.
* @param {string} name - Base name without extension (e.g., "icon-apply")
* @returns {string} Full path to the versioned asset file
*/
export function getAssetPath(name) {
if (!assetsPath) {
throw new Error("Assets not initialized. Call initializeAssets() first.");
}
return assetsPath + name + "_v" + ASSETS_VERSION + ".png";
}
`;
// Write the module
fs.writeFileSync(OUTPUT_FILE, moduleCode, 'utf8');
console.log(`\n✅ Generated ${path.relative(__dirname, OUTPUT_FILE)}`);
console.log(` Assets folder: easey_assets_v${versionSuffix}/`);
console.log(` Total assets: ${Object.keys(assets).length}`);