@ngxpert/hot-toast
Version:
Smoking hot Notifications for Angular. Lightweight, customizable and beautiful by default.
115 lines • 6.09 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 NGXPERT_CSS_FILEPATH = `@ngxpert/hot-toast/styles.css`;
const SUPPORTED_NGXPER_HOT_TOAST_STYLE_IMPORTS = {
'.sass': `
/* Importing @ngxpert/hot-toast SCSS file. */
@use '@ngxpert/hot-toast/styles'
`,
'.scss': `
/* Importing @ngxpert/hot-toast SCSS file. */
@use '@ngxpert/hot-toast/styles';
`,
};
/** Add pre-built styles to the main project style file. */
function addThemeToAppStyles(options) {
return (host, context) => __awaiter(this, void 0, void 0, function* () {
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 '${project}' 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 styleFilePatch = SUPPORTED_NGXPER_HOT_TOAST_STYLE_IMPORTS[styleFileExtension];
if (styleFilePatch) {
return addNgxpertHotToastToStylesFile(styleFilePath, styleFilePatch);
}
else {
// found supported styles
return insertPrebuiltTheme(options.project, context.logger);
}
});
}
/** Insert a pre-built theme into the angular.json file. */
function insertPrebuiltTheme(project, logger) {
return (0, schematics_1.chain)([
addThemeStyleToTarget(project, 'build', NGXPERT_CSS_FILEPATH, logger),
addThemeStyleToTarget(project, 'test', NGXPERT_CSS_FILEPATH, logger),
]);
}
/** Adds a theming style entry to the given project target options. */
function addThemeStyleToTarget(projectName, targetName, assetPath, logger) {
// @ts-expect-error ignore the error
return (0, workspace_1.updateWorkspace)((workspace) => {
// @ts-expect-error ignore the error
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;
// Because the build setup for the Angular CLI can be customized by developers, we can't know
// where to put the theme file in the workspace configuration if custom builders are being
// used. In case the builder has been changed for the "build" target, we throw an error and
// exit because setting up a theme is a primary goal of `ng-add`. Otherwise if just the "test"
// builder has been changed, we warn because a theme is not mandatory for running tests
// with Material. See: https://github.com/angular/components/issues/14176
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) {
// for non-build targets we gracefully report the error without actually aborting the
// setup schematic. This is because a theme is not mandatory for running tests.
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