mirror of
https://github.com/hunvreus/basecoat.git
synced 2026-11-11 20:58:18 +00:00
113 lines
3.9 KiB
JavaScript
113 lines
3.9 KiB
JavaScript
import fs from 'fs/promises';
|
|||
|
|
import path from 'path';
|
||
|
|
import { fileURLToPath } from 'url';
|
||
|
|
|
||
|
|
// Resolve project root, assuming this script is in <project_root>/scripts/
|
||
|
|
const __filename = fileURLToPath(import.meta.url);
|
||
|
|
const __dirname = path.dirname(__filename);
|
||
|
|
const projectRoot = path.resolve(__dirname, '..');
|
||
|
|
|
||
|
|
// Ensures a directory exists. If it doesn't, it's created recursively.
|
||
|
|
async function ensureDir(dirPath) {
|
||
|
|
try {
|
||
|
|
await fs.access(dirPath);
|
||
|
|
} catch (error) {
|
||
|
|
if (error.code === 'ENOENT') {
|
||
|
|
await fs.mkdir(dirPath, { recursive: true });
|
||
|
|
} else {
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Cleans a directory by removing all its contents (files and subdirectories).
|
||
|
|
async function cleanDir(directory) {
|
||
|
|
try {
|
||
|
|
await fs.access(directory);
|
||
|
|
const entries = await fs.readdir(directory, { withFileTypes: true });
|
||
|
|
for (const entry of entries) {
|
||
|
|
const entryPath = path.join(directory, entry.name);
|
||
|
|
if (entry.isDirectory()) {
|
||
|
|
await fs.rm(entryPath, { recursive: true, force: true });
|
||
|
|
} else {
|
||
|
|
await fs.unlink(entryPath);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
console.log(`Cleaned directory: ${directory}`);
|
||
|
|
} catch (error) {
|
||
|
|
if (error.code === 'ENOENT') {
|
||
|
|
console.log(`Directory not found, no need to clean: ${directory}`);
|
||
|
|
} else {
|
||
|
|
console.error(`Error cleaning directory ${directory}:`, error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Recursively copies a directory.
|
||
|
|
async function copyDirRecursive(src, dest) {
|
||
|
|
await ensureDir(dest);
|
||
|
|
const entries = await fs.readdir(src, { withFileTypes: true });
|
||
|
|
for (let entry of entries) {
|
||
|
|
const srcPath = path.join(src, entry.name);
|
||
|
|
const destPath = path.join(dest, entry.name);
|
||
|
|
if (entry.isDirectory()) {
|
||
|
|
await copyDirRecursive(srcPath, destPath);
|
||
|
|
} else {
|
||
|
|
await fs.copyFile(srcPath, destPath);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// Main build function to prepare packages for publishing.
|
||
|
|
async function build() {
|
||
|
|
console.log('Starting build process...');
|
||
|
|
|
||
|
|
// Define all necessary paths
|
||
|
|
const cliPackageDir = path.join(projectRoot, 'packages', 'cli');
|
||
|
|
const cliDistDir = path.join(cliPackageDir, 'dist');
|
||
|
|
const cliDistAssetsDir = path.join(cliDistDir, 'assets');
|
||
|
|
|
||
|
|
const cssPackageDir = path.join(projectRoot, 'packages', 'css');
|
||
|
|
const cssDistDir = path.join(cssPackageDir, 'dist');
|
||
|
|
|
||
|
|
const srcDir = path.join(projectRoot, 'src');
|
||
|
|
const srcCssDir = path.join(srcDir, 'css');
|
||
|
|
const srcJsDir = path.join(srcDir, 'js');
|
||
|
|
const srcNunjucksDir = path.join(srcDir, 'nunjucks');
|
||
|
|
const srcJinjaDir = path.join(srcDir, 'jinja');
|
||
|
|
|
||
|
|
// Clean previous build artifacts
|
||
|
|
console.log('Cleaning distribution directories...');
|
||
|
|
await cleanDir(cliDistDir);
|
||
|
|
await cleanDir(cssDistDir);
|
||
|
|
|
||
|
|
// Build CLI package
|
||
|
|
console.log('Building CLI package...');
|
||
|
|
await ensureDir(cliDistDir); // Ensure base dist dir exists
|
||
|
|
await ensureDir(cliDistAssetsDir); // Ensure assets dir within dist exists
|
||
|
|
|
||
|
|
await fs.copyFile(path.join(cliPackageDir, 'index.js'), path.join(cliDistDir, 'index.js'));
|
||
|
|
console.log(`Copied CLI index.js to ${cliDistDir}`);
|
||
|
|
|
||
|
|
await copyDirRecursive(srcJsDir, path.join(cliDistAssetsDir, 'js'));
|
||
|
|
console.log(`Copied JS assets to ${path.join(cliDistAssetsDir, 'js')}`);
|
||
|
|
await copyDirRecursive(srcNunjucksDir, path.join(cliDistAssetsDir, 'nunjucks'));
|
||
|
|
console.log(`Copied Nunjucks assets to ${path.join(cliDistAssetsDir, 'nunjucks')}`);
|
||
|
|
await copyDirRecursive(srcJinjaDir, path.join(cliDistAssetsDir, 'jinja'));
|
||
|
|
console.log(`Copied Jinja assets to ${path.join(cliDistAssetsDir, 'jinja')}`);
|
||
|
|
|
||
|
|
// Build CSS package
|
||
|
|
console.log('Building CSS package...');
|
||
|
|
await ensureDir(cssDistDir); // Ensure dist dir exists for css package
|
||
|
|
await fs.copyFile(path.join(srcCssDir, 'basecoat.css'), path.join(cssDistDir, 'basecoat.css'));
|
||
|
|
console.log(`Copied basecoat.css to ${cssDistDir}`);
|
||
|
|
|
||
|
|
console.log('Build process finished successfully!');
|
||
|
|
}
|
||
|
|
|
||
|
|
build().catch(error => {
|
||
|
|
console.error('Build failed:', error);
|
||
|
|
process.exit(1);
|
||
|
|
});
|