UNPKG

piral-cli

Version:

The standard CLI for creating and building a Piral instance or a Pilet.

57 lines 2.31 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const path_1 = require("path"); const css_conflict_inspector_1 = require("css-conflict-inspector"); const common_1 = require("../common"); async function getPiletCssPaths(main, baseDir) { const paths = [main, `dist/${main}`, `${main}/index.js`, `dist/${main}/index.js`, 'index.js', 'dist/index.js']; for (const path of paths) { const outDir = (0, path_1.dirname)((0, path_1.resolve)(baseDir, path)); const exists = await (0, common_1.checkExists)(outDir); if (exists) { const files = await (0, common_1.getFileNames)(outDir); return files.filter((m) => m.endsWith('.css')).map((m) => (0, path_1.resolve)(outDir, m)); } } return []; } async function getCssScore(dir, file) { const content = await (0, common_1.readText)(dir, file); const result = (0, css_conflict_inspector_1.analyzeCss)(content); return result.score; } /** * Checks if a pilet might cause a CSS conflict. A score of 100 means that * there is the least chance of a CSS conflict, while a score of 0 means that * a CSS conflict is most likely. * * Negative values yield a warning if the CSS score is below the given number. * * Positive values yield an error if the CSS score is below the given number. * * A value of 0 turns this validation off. * * By default, a pilet's stylesheet having a CSS score of below 50 will result in a warning. */ async function default_1(context, options = -50) { if (options !== 0 && typeof options === 'number') { const threshold = Math.abs(options); const { main } = context.piletPackage; const paths = await getPiletCssPaths(main, context.root); for (const path of paths) { const dir = (0, path_1.dirname)(path); const file = (0, path_1.basename)(path); const score = await getCssScore(dir, file); if (score < threshold) { const notify = options > 0 ? context.error : context.warning; notify(` The CSS in "${file}" might lead to conflicts. Minimum: ${threshold} points. Received: ${score} points. `); } } } } //# sourceMappingURL=pilet-has-non-conflicting-css.js.map