@kadconsulting/dry
Version:
KAD Reusable Component Library
121 lines (120 loc) • 5.59 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.includeStyles = void 0;
const fs = require("fs");
const path = require("path");
const rootDir = path.resolve(__dirname, '../../');
console.log('Root directory: ', rootDir);
function includeStyles() {
return __awaiter(this, void 0, void 0, function* () {
yield buildExists(rootDir);
/** Static SCSS files - likely not to change often; and if so, changes have grand implications */
const themeScssModules = [
'themes/global.scss',
'themes/normalize.scss',
'themes/variables.scss',
'themes/default/typography.module.scss',
'themes/default/breakpoints.module.scss',
'themes/default/palette/dark/colors.untitledui.scss',
'themes/default/palette/light/colors.untitledui.scss',
'themes/default/palette/dark/palette.dark.module.scss',
'themes/default/palette/light/palette.light.module.scss',
];
for (const filepath of themeScssModules) {
yield copyFileToDist(rootDir, filepath);
yield copyFileToDist(rootDir, filepath, '/esm');
yield copyFileToDist(rootDir, filepath, '/react/esm');
yield copyFileToDist(rootDir, filepath, '/next/esm');
}
const dynamicSCSSFiles = yield walkDirForScssModules({
pathname: path.join(rootDir, 'src/components'),
});
for (const filepath of dynamicSCSSFiles) {
const [, locationWithinProject] = filepath.split('/src/');
yield copyFileToDist(rootDir, locationWithinProject);
yield copyFileToDist(rootDir, locationWithinProject, '/esm');
yield copyFileToDist(rootDir, locationWithinProject, '/react/esm');
yield copyFileToDist(rootDir, locationWithinProject, '/next/esm');
}
});
}
exports.includeStyles = includeStyles;
function buildExists(rootDir) {
return __awaiter(this, void 0, void 0, function* () {
const distSrcPath = path.join(rootDir, 'dist/src');
yield fs.promises.access(distSrcPath).catch(() => __awaiter(this, void 0, void 0, function* () {
console.error('No build found. Creating directories...');
yield fs.promises.mkdir(distSrcPath, { recursive: true });
}));
return true;
});
}
function copyFileToDist(rootDir_1, filepath_1) {
return __awaiter(this, arguments, void 0, function* (rootDir, filepath, buildType = '') {
const src = path.join(rootDir, 'src', filepath);
const dist = path.join(rootDir, `dist${buildType}/src`, filepath);
// Ensure directory exists
const dir = path.dirname(dist);
if (!fs.existsSync(dir)) {
yield fs.promises.mkdir(dir, { recursive: true });
}
const srcFileHandle = yield fs.promises.open(src, 'r');
const srcContent = yield srcFileHandle.readFile({ encoding: 'utf-8' });
fs.promises
.open(dist, 'w')
.then((destFile) => __awaiter(this, void 0, void 0, function* () {
destFile.write(srcContent, 0, 'utf-8');
destFile.close();
}))
.catch((error) => {
console.error(error);
})
.finally(() => {
srcFileHandle.close();
});
});
}
/** Walks the components directory and includes SCSS files in the dist directory */
// TODO Adjust Typing for strings and the "never" types
function walkDirForScssModules(_a) {
return __awaiter(this, arguments, void 0, function* ({ pathname, }) {
const scssFiles = [];
const dirContents = yield fs.promises.readdir(pathname);
/** Push all SCSS files found in the current directory */
scssFiles.push(...dirContents.reduce((acc, file) => {
if (file.endsWith('.scss'))
acc.push(`${pathname}/${file}`);
return acc;
}, []));
/** Get a list of the directories at the current path to recursively traverse */
const dirNames = [];
for (const dirName of dirContents) {
const isDir = yield isDirectory(`${pathname}/${dirName}`);
if (isDir)
dirNames.push(dirName);
}
/** Recursively collect all SCSS files from the next path segment */
for (const dirName of dirNames) {
const nextPath = `${pathname}/${dirName}`;
const collectedFiles = yield walkDirForScssModules({ pathname: nextPath });
scssFiles.push(...collectedFiles);
}
/** Return the rollup of all SCSS files in the specified directory */
return scssFiles;
});
}
function isDirectory(pathname) {
return __awaiter(this, void 0, void 0, function* () {
const stats = yield fs.promises.lstat(pathname);
return stats.isDirectory();
});
}