Skip to content

Commit

Permalink
fix(theme-generator): Find node module location at runtime (#291)
Browse files Browse the repository at this point in the history
* chore: Downgrade sass

* fix(theme-generator): Find node module location at runtime
  • Loading branch information
KeithClinard authored Dec 4, 2024
1 parent 454827b commit af3db4c
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 36 deletions.
4 changes: 2 additions & 2 deletions packages/theme-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"chalk": "^5.3.0",
"css-tree": "2.3.1",
"prettier": "^2.4.1",
"sass": "^1.81.0",
"sass": "1.79.0",
"semver": "^7.6.3"
}
}
}
31 changes: 30 additions & 1 deletion packages/theme-generator/src/compile-css.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,43 @@
import * as sass from 'sass';
import fs from 'fs';
import chalk from 'chalk';

export default function (theme, fileHelper) {
const nrgScssMain = fileHelper.nrgScssDirectory + '/main.scss';
const bootstrapScssMain = fileHelper.bootstrapNodeModules + '/bootstrap/scss/bootstrap.scss';
const bootstrapIconScssMain = fileHelper.bootstrapIconNodeModules + '/bootstrap-icons/font/bootstrap-icons.scss';

const nrgScssExists = fs.existsSync(nrgScssMain);
const bootstrapScssExists = fs.existsSync(bootstrapScssMain);
const bootstrapIconScssExists = fs.existsSync(bootstrapIconScssMain);

let errorMessage = '';

if (!nrgScssExists) {
errorMessage += `Could not find NRG SCSS main file at ${nrgScssMain}\n`;
}

if (!bootstrapScssExists) {
errorMessage += `Could not find Bootstrap SCSS main file at ${bootstrapScssMain}\n`;
}

if (!bootstrapIconScssExists) {
errorMessage += `Could not find Bootstrap Icons SCSS main file at ${bootstrapIconScssMain}\n`;
}

if (errorMessage) {
console.error(chalk.red('Preflight checks failed\n' + errorMessage));
process.exit(1);
}

const fullTheme = `
${theme}
@import "main";
`;
const result = sass.compileString(fullTheme, {
quietDeps: true,
loadPaths: [fileHelper.thisNodeModules, fileHelper.nrgScssDirectory],
loadPaths: [fileHelper.bootstrapNodeModules, fileHelper.bootstrapIconNodeModules, fileHelper.nrgScssDirectory],
});

return result.css;
Expand Down
11 changes: 9 additions & 2 deletions packages/theme-generator/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ import {
checkNodeVersion,
ensureNrgDirectoryExists,
} from './dependencies.js';
import { createRequire } from 'module';
const require = createRequire(import.meta.url);

function getFileHelper() {
const workingDirectory = process.cwd();
const targetNodeModules = path.join(workingDirectory, 'node_modules');
const thisNodeModules = path.join(import.meta.dirname, '../', 'node_modules');
const bootstrapLocation = path.dirname(require.resolve('bootstrap/package.json'))
const bootstrapNodeModules = path.join(bootstrapLocation, '../');
const bootstrapIconLocation = path.dirname(require.resolve('bootstrap-icons/package.json'))
const bootstrapIconNodeModules = path.join(bootstrapIconLocation, '../');

const nrgDirectory = path.join(workingDirectory, '.nrg');
const nrgCss = path.join(
targetNodeModules,
Expand All @@ -35,7 +41,8 @@ function getFileHelper() {
return {
workingDirectory,
targetNodeModules,
thisNodeModules,
bootstrapNodeModules,
bootstrapIconNodeModules,
nrgDirectory,
nrgCss,
nrgScssDirectory,
Expand Down
65 changes: 34 additions & 31 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit af3db4c

Please sign in to comment.