@nxext/stencil
Version:
Nx plugin for stenciljs
57 lines • 3.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.componentGenerator = componentGenerator;
const tslib_1 = require("tslib");
const devkit_1 = require("@nx/devkit");
const path_1 = require("path");
const insert_statement_1 = require("../../utils/insert-statement");
const generator_1 = require("../storybook-configuration/generator");
const internal_1 = require("@nx/js/internal");
function componentGenerator(host, options) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
if (!/[-]/.test(options.name)) {
throw new Error((0, devkit_1.stripIndents) `
"${options.name}" tag must contain a dash (-) to work as a valid web component. Please refer to
https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name for more info.
`);
}
const componentFileName = (0, devkit_1.names)(options.name).fileName;
const className = (0, devkit_1.names)(options.name).className;
const projectConfig = (0, devkit_1.readProjectConfiguration)(host, options.project);
// package.json-backed (TS-solution) projects don't carry an explicit
// `sourceRoot` - getProjectSourceRoot falls back to `<root>/src` for
// them, and returns the explicit value unchanged for legacy project.json
// projects.
const sourceRoot = (0, internal_1.getProjectSourceRoot)(projectConfig, host);
const componentDirectory = options.directory
? (0, devkit_1.joinPathFragments)(`${options.directory}/${componentFileName}`)
: componentFileName;
const componentOptions = ((projectConfig || {}).generators || {
'@nxext/stencil:component': {},
})['@nxext/stencil:component'];
if (!componentOptions) {
devkit_1.logger.info((0, devkit_1.stripIndents) `
Style options for components not set, please run "nx migrate @nxext/stencil"
`);
}
options = Object.assign(Object.assign({}, options), componentOptions);
(0, devkit_1.generateFiles)(host, (0, path_1.join)(__dirname, './files/component'), (0, devkit_1.joinPathFragments)(`${sourceRoot}/components/${componentDirectory}`), {
componentFileName: componentFileName,
className: className,
style: options.style,
});
const storiesPath = `${sourceRoot}/components/${componentDirectory}/${componentFileName}.stories.tsx`;
if (!host.exists(`${projectConfig.root}/.storybook`)) {
host.delete((0, devkit_1.joinPathFragments)(storiesPath));
}
else {
const classPathValue = `${(0, generator_1.getProjectTsImportPath)(host, options.project)}/${componentFileName}`;
(0, insert_statement_1.insertStatement)(host, storiesPath, `import { ${className} } from '${classPathValue}'`);
}
if (!options.skipFormat) {
yield (0, devkit_1.formatFiles)(host);
}
});
}
exports.default = componentGenerator;
//# sourceMappingURL=component.js.map