aboutsummaryrefslogtreecommitdiffstats
path: root/app/builder/buildProject.ts
diff options
context:
space:
mode:
authorLeonardo Bishop <me@leonardobishop.com>2023-08-28 22:47:13 +0100
committerLeonardo Bishop <me@leonardobishop.com>2023-08-28 22:47:13 +0100
commitd5802a34218a56357a7ae88eff1d3cfa86d41bc7 (patch)
tree8b11b7e490c33513d7581fa95b828c4064d17ecd /app/builder/buildProject.ts
parent7c9abacf956c0e135c1094e38087e018dd572965 (diff)
Add SCSS support
Diffstat (limited to 'app/builder/buildProject.ts')
-rw-r--r--app/builder/buildProject.ts51
1 files changed, 50 insertions, 1 deletions
diff --git a/app/builder/buildProject.ts b/app/builder/buildProject.ts
index 71fdf73..03cc58e 100644
--- a/app/builder/buildProject.ts
+++ b/app/builder/buildProject.ts
@@ -3,6 +3,8 @@ import { Page, PageDirectory } from './pageDirectory.js';
import fs from 'fs';
import path from 'path';
import { logger } from '../logger.js';
+import glob from 'glob';
+import { process as processCss } from './processCss.js';
export async function buildPages(): Promise<{ success: boolean, errors: number, pageDirectory: PageDirectory}> {
// Recreate output directory
@@ -42,15 +44,62 @@ export async function buildPages(): Promise<{ success: boolean, errors: number,
logger.info(`Rendered ${pagesRendered} of ${pagesCount} pages.`);
+ //TODO move to util
+ const ensureParentDirExists = (file: string) => {
+ const joinedOutputPath = path.join(process.env.OUTPUT_DIR, 'static', file);
+ const dir = path.dirname(joinedOutputPath);
+ if (!fs.existsSync(dir)) {
+ fs.mkdirSync(dir, { recursive: true });
+ }
+ return joinedOutputPath;
+ };
// Copy static files
logger.info(`Copying static files...`);
try {
- fs.cpSync(`${process.env.STATIC_DIR}`, `${process.env.OUTPUT_DIR}/static`, { recursive: true });
+ const files = glob.sync(`**/*`, {
+ cwd: process.env.STATIC_DIR,
+ nodir: true,
+ ignore: ['**/*.scss', '**/*.css']
+ })
+
+ for (const file of files) {
+ const outputPath = ensureParentDirExists(file);
+ const joinedPath = path.join(process.env.STATIC_DIR, file);
+ fs.copyFileSync(joinedPath, outputPath);
+ }
+
logger.info(`Done.`);
} catch (e) {
logger.error(`Failed to copy static files: ${e.message}`);
}
+
+ // Process CSS files
+ const cssFiles = glob.sync(`**/*.{css,scss}`, {
+ cwd: process.env.STATIC_DIR,
+ nodir: true,
+ });
+ if (cssFiles.length > 0) {
+ logger.info(`Processing CSS files...`);
+
+ for (const file of cssFiles) {
+ const outputPath = ensureParentDirExists(file);
+ const joinedPath = path.join(process.env.STATIC_DIR, file);
+ let processedCss: string;
+ try {
+ processedCss = await processCss(joinedPath);
+ } catch (e) {
+ logger.error(`Failed to process CSS file ${joinedPath}`);
+ logger.error(e.message);
+ continue;
+ }
+ const newOutputPath = outputPath.replace(/\.scss$/, '.css');
+ fs.writeFileSync(newOutputPath, processedCss);
+ }
+
+ logger.info(`Done.`);
+ }
+
return { success: pagesFailed == 0, errors: pagesFailed, pageDirectory: pageDirectory};
}