Skip to content

Commit a49fd07

Browse files
committed
headlamp-plugin: Allow to copy extra dist files into dist folder
Signed-off-by: Joaquim Rocha <[email protected]>
1 parent d106f46 commit a49fd07

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

plugins/headlamp-plugin/bin/headlamp-plugin.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,63 @@ async function calculateChecksum(filePath) {
229229
}
230230
}
231231

232+
/**
233+
* Copy extra files specified in package.json to the dist folder
234+
*
235+
* @param {string} [packagePath='.'] - Path to the package root containing package.json
236+
* @returns {Promise<void>}
237+
*/
238+
async function copyExtraDistFiles(packagePath = '.') {
239+
try {
240+
const packageJsonPath = path.join(packagePath, 'package.json');
241+
if (!fs.existsSync(packageJsonPath)) {
242+
return; // No package.json, nothing to do
243+
}
244+
245+
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
246+
if (!packageJson.headlamp || !packageJson.headlamp.extraDist) {
247+
return; // No extra files to copy
248+
}
249+
250+
const extraDist = packageJson.headlamp.extraDist;
251+
const distFolder = path.resolve(packagePath, 'dist');
252+
253+
// Create dist folder if it doesn't exist (although it should by this point)
254+
if (!fs.existsSync(distFolder)) {
255+
fs.mkdirSync(distFolder, { recursive: true });
256+
}
257+
258+
// Process all entries in extraDist
259+
for (const [target, source] of Object.entries(extraDist)) {
260+
const targetPath = path.join(distFolder, target);
261+
const sourcePath = path.resolve(packagePath, source);
262+
263+
// Skip if source doesn't exist
264+
if (!fs.existsSync(sourcePath)) {
265+
console.warn(`Warning: extraDist source "${sourcePath}" does not exist, skipping.`);
266+
continue;
267+
}
268+
269+
// Create target directory if needed
270+
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
271+
272+
// Copy based on whether it's a directory or file
273+
const sourceStats = fs.statSync(sourcePath);
274+
if (sourceStats.isDirectory()) {
275+
console.log(`Copying extra directory "${sourcePath}" to "${targetPath}"`);
276+
fs.copySync(sourcePath, targetPath);
277+
} else {
278+
console.log(`Copying extra file "${sourcePath}" to "${targetPath}"`);
279+
fs.copyFileSync(sourcePath, targetPath);
280+
}
281+
}
282+
283+
console.log('Successfully copied extra dist files');
284+
} catch (error) {
285+
console.error('Error copying extra dist files:', error);
286+
}
287+
}
288+
232289
/**
233290
* Creates a tarball of the plugin package. The tarball is placed in the outputFolderPath.
234291
* It moves files from:
@@ -274,6 +331,10 @@ async function createArchive(pluginDir, outputDir) {
274331

275332
// Create temporary folder
276333
const tempFolder = fs.mkdtempSync(path.join(os.tmpdir(), 'headlamp-plugin-'));
334+
335+
// Make sure any extraDist files are in the dist folder before extraction
336+
await copyExtraDistFiles(pluginPath);
337+
277338
if (extract(pluginPath, tempFolder, false) !== 0) {
278339
console.error(
279340
`Error: Failed to extract plugin package to "${tempFolder}". Not creating archive.`
@@ -381,6 +442,16 @@ async function start() {
381442
config.build.sourcemap = 'inline';
382443
}
383444

445+
// Add file copy hook to be executed after each build
446+
if (config.plugins) {
447+
config.plugins.push({
448+
name: 'headlamp-copy-extra-dist',
449+
closeBundle: async () => {
450+
await copyExtraDistFiles();
451+
},
452+
});
453+
}
454+
384455
try {
385456
await vite.build(config);
386457
} catch (e) {
@@ -579,6 +650,10 @@ async function build(packageFolder) {
579650
const vite = await vitePromise;
580651
try {
581652
await vite.build(config.default);
653+
654+
// Copy extra dist files after successful build
655+
await copyExtraDistFiles('.');
656+
582657
console.log(`Finished building "${folder}" for production.`);
583658
} catch (e) {
584659
console.error(e);

0 commit comments

Comments
 (0)