@ngxpert/hot-toast
Version:
Smoking hot Notifications for Angular. Lightweight, customizable and beautiful by default.
114 lines • 5.97 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.addThemeToAppStyles = addThemeToAppStyles;
const schematics_1 = require("@angular-devkit/schematics");
const workspace_1 = require("@schematics/angular/utility/workspace");
const utility_1 = require("@schematics/angular/utility");
const project_targets_1 = require("../../utils/project-targets");
const get_project_1 = require("../../utils/get-project");
const path = require("path");
const project_1 = require("../../utils/project");
const BASE_CSS_FILEPATH = `@ngxpert/hot-toast/styles.css`;
const THEME_CSS_FILEPATH = (theme) => `@ngxpert/hot-toast/themes/${theme}.css`;
const BASE_SCSS_IMPORT = `
/* Importing @ngxpert/hot-toast base styles. */
@use '@ngxpert/hot-toast/styles';
`;
const THEME_SCSS_IMPORT = (theme) => `
/* Importing @ngxpert/hot-toast ${theme} theme. */
@use '@ngxpert/hot-toast/themes/${theme}';
`;
const SUPPORTED_STYLE_EXTENSIONS = new Set(['.scss', '.sass']);
/** Add pre-built styles (and optionally a theme) to the main project style file. */
function addThemeToAppStyles(options) {
return (host, context) => __awaiter(this, void 0, void 0, function* () {
var _a;
const workspace = yield (0, utility_1.readWorkspace)(host);
const projectName = options.project || workspace.extensions.defaultProject.toString();
const project = workspace.projects.get(projectName);
if (!project) {
throw new schematics_1.SchematicsException(`Unable to find project '${projectName}' in the workspace`);
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const styleFilePath = (0, project_1.getProjectStyleFile)(project) || '';
const styleFileExtension = path.extname(styleFilePath);
const isScssOrSass = SUPPORTED_STYLE_EXTENSIONS.has(styleFileExtension);
const selectedTheme = ((_a = options.theme) !== null && _a !== void 0 ? _a : 'none');
if (isScssOrSass) {
let patch = BASE_SCSS_IMPORT;
if (selectedTheme !== 'none') {
patch += THEME_SCSS_IMPORT(selectedTheme);
}
return addNgxpertHotToastToStylesFile(styleFilePath, patch);
}
// Non-SCSS project: inject CSS files into angular.json styles array.
return insertPrebuiltStyles(options.project, selectedTheme, context.logger);
});
}
/** Insert pre-built CSS file(s) into the angular.json file. */
function insertPrebuiltStyles(project, theme, logger) {
const rules = [
addThemeStyleToTarget(project, 'build', BASE_CSS_FILEPATH, logger),
addThemeStyleToTarget(project, 'test', BASE_CSS_FILEPATH, logger),
];
if (theme !== 'none') {
const themeCssPath = THEME_CSS_FILEPATH(theme);
rules.push(addThemeStyleToTarget(project, 'build', themeCssPath, logger), addThemeStyleToTarget(project, 'test', themeCssPath, logger));
}
return (0, schematics_1.chain)(rules);
}
/** Adds a theming style entry to the given project target options. */
function addThemeStyleToTarget(projectName, targetName, assetPath, logger) {
return (0, workspace_1.updateWorkspace)((workspace) => {
const project = (0, get_project_1.getProjectFromWorkspace)(workspace, projectName);
// Do not update the builder options in case the target does not use the default CLI builder.
if (!validateDefaultTargetBuilder(project, targetName, logger)) {
return;
}
const targetOptions = (0, project_targets_1.getProjectTargetOptions)(project, targetName);
const styles = targetOptions['styles'];
if (!styles) {
targetOptions['styles'] = [assetPath];
}
else {
styles.unshift(assetPath);
}
});
}
/**
* Validates that the specified project target is configured with the default builders which are
* provided by the Angular CLI. If the configured builder does not match the default builder,
* this function can either throw or just show a warning.
*/
function validateDefaultTargetBuilder(project, targetName, logger) {
const targets = targetName === 'test' ? (0, project_targets_1.getProjectTestTargets)(project) : (0, project_targets_1.getProjectBuildTargets)(project);
const isDefaultBuilder = targets.length > 0;
if (!isDefaultBuilder && targetName === 'build') {
throw new schematics_1.SchematicsException(`Your project is not using the default builders for ` +
`"${targetName}". The Angular Material schematics cannot add a theme to the workspace ` +
`configuration if the builder has been changed.`);
}
else if (!isDefaultBuilder) {
logger.warn(`Your project is not using the default builders for "${targetName}". This ` +
`means that we cannot add the configured theme to the "${targetName}" target.`);
}
return isDefaultBuilder;
}
function addNgxpertHotToastToStylesFile(styleFilePath, styleFilePatch) {
return (host) => {
const styleContent = host.read(styleFilePath).toString('utf-8');
const recorder = host.beginUpdate(styleFilePath);
recorder.insertRight(styleContent.length, styleFilePatch);
host.commitUpdate(recorder);
};
}
//# sourceMappingURL=theming.js.map