@doku-dev/doku-fragment
Version:
A new Angular UI library that moving away from Bootstrap and built from scratch.
88 lines (86 loc) • 3.96 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.addGlobalStyle = void 0;
const schematics_1 = require("@angular-devkit/schematics");
const utility_1 = require("@schematics/angular/utility");
const path = require("path");
const messages = require("../../messages");
const util_1 = require("../../util");
const DOKU_FRAGMENT_SCSS_FILEPATH = 'node_modules/@doku-dev/doku-fragment/assets/styles/index.scss';
const SUPPORTED_DOKU_FRAGMENT_STYLE_IMPORTS = {
'.sass': `/* Importing DOKU Fragment SCSS file. */
@use "@doku-dev/doku-fragment"
`,
'.scss': `/* Importing DOKU Fragment SCSS file. */
@use "@doku-dev/doku-fragment";
`,
};
/**
* Adding doku-fragment global style to `styles.scss` or `styles.sass`.
* If not possible, we're adding doku-fragment styles to `angular.json`.
*/
function addGlobalStyle(options) {
return (tree) => __awaiter(this, void 0, void 0, function* () {
const workspace = yield (0, utility_1.readWorkspace)(tree);
const project = yield (0, util_1.getActiveProject)(workspace, options.project);
if (!project) {
throw new schematics_1.SchematicsException(messages.noProject(messages.noSelectedProject()));
}
const mainStylePath = (0, util_1.getProjectStyleFile)(project) || '';
const mainStyleExtension = path.extname(mainStylePath);
const mainStylePatch = SUPPORTED_DOKU_FRAGMENT_STYLE_IMPORTS[mainStyleExtension];
if (mainStylePatch) {
return addDokuUIToStylesFile(mainStylePath, mainStylePatch);
}
addDokuUIToAngularJson(project);
return yield (0, utility_1.writeWorkspace)(tree, workspace);
});
}
exports.addGlobalStyle = addGlobalStyle;
/**
* Patches 'styles.scss' or 'styles.sass' to add DOKU Fragment snippet
*/
function addDokuUIToStylesFile(styleFilePath, styleFilePatch) {
return (tree) => {
var _a;
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const styleContent = tree.read(styleFilePath).toString('utf-8');
const existingImport = (_a = styleContent.match(/@use ["']@doku-dev\/doku-fragment["'];/gm)) === null || _a === void 0 ? void 0 : _a.length;
if (existingImport)
return;
const recorder = tree.beginUpdate(styleFilePath);
recorder.insertLeft(0, styleFilePatch);
tree.commitUpdate(recorder);
};
}
/**
* Patches 'angular.json' to add doku-fragment styles
*/
function addDokuUIToAngularJson(project) {
const targetOptions = (0, util_1.getProjectTargetOptions)(project, 'build');
const styles = targetOptions['styles'];
if (!styles) {
targetOptions['styles'] = [DOKU_FRAGMENT_SCSS_FILEPATH];
}
else {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const existingStyles = styles.map((s) => (typeof s === 'string' ? s : s['input']));
for (const [, stylePath] of existingStyles.entries()) {
// If the given asset is already specified in the styles, we don't need to do anything.
if (stylePath === DOKU_FRAGMENT_SCSS_FILEPATH) {
return;
}
}
styles.unshift(DOKU_FRAGMENT_SCSS_FILEPATH);
}
}
//# sourceMappingURL=add-style.js.map