@nova-ui/bits
Version:
SolarWinds Nova Framework
144 lines • 7.19 kB
JavaScript
;
// © 2022 SolarWinds Worldwide, LLC. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
Object.defineProperty(exports, "__esModule", { value: true });
const core_1 = require("@angular-devkit/core");
const schematics_1 = require("@angular-devkit/schematics");
const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
const change_1 = require("@schematics/angular/utility/change");
const find_module_1 = require("@schematics/angular/utility/find-module");
const parse_name_1 = require("@schematics/angular/utility/parse-name");
const validation_1 = require("@schematics/angular/utility/validation");
const project_1 = require("../utility/project");
const schematics_helper_1 = require("../utility/schematics-helper");
function addDeclarationToNgModule(options) {
return (host) => {
if (options.skipImport || !options.module) {
return host;
}
const modulePath = options.module;
let moduleSource = (0, schematics_helper_1.readIntoSourceFile)(host, modulePath);
const componentPath = `/${options.path}/` +
(options.flat ? "" : core_1.strings.dasherize(options.name) + "/") +
core_1.strings.dasherize(options.name) +
".component";
const relativePath = (0, find_module_1.buildRelativePath)(modulePath, componentPath);
const componentName = core_1.strings.classify(`${options.name}Component`);
// default modules to be imported
const modules = [
{ item: "NuiTableModule", path: "@nova-ui/bits" },
{ item: "NuiIconModule", path: "@nova-ui/bits" },
];
if (options.dataSource === "serverSide") {
modules.push({
item: "HttpClientModule",
path: "@angular/common/http",
});
if (options.pagingMode === "none") {
throw new Error("Cannot display serverSide dataSource without a pagingMode");
}
}
// additional module(s) used only for search functionality
if (options.enableSearch) {
modules.push({ item: "NuiSearchModule", path: "@nova-ui/bits" });
}
// additional module(s) used only for pagination functionality
if (options.pagingMode === "pagination") {
modules.push({ item: "NuiPaginatorModule", path: "@nova-ui/bits" });
}
else if (options.pagingMode === "virtualScroll") {
modules.push({
item: "ScrollingModule",
path: "@angular/cdk/scrolling",
});
modules.push({ item: "NuiProgressModule", path: "@nova-ui/bits" });
}
(0, schematics_helper_1.updateModuleChanges)(host, options, moduleSource, modules, [], [{ item: componentName, path: relativePath }]);
if (options.export) {
// Need to refresh the AST because we overwrote the file in the host.
moduleSource = (0, schematics_helper_1.readIntoSourceFile)(host, modulePath);
const exportRecorder = host.beginUpdate(modulePath);
const exportChanges = (0, ast_utils_1.addExportToModule)(moduleSource, modulePath, core_1.strings.classify(`${options.name}Component`), relativePath);
for (const change of exportChanges) {
if (change instanceof change_1.InsertChange) {
exportRecorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(exportRecorder);
}
return host;
};
}
function default_1(options) {
return (host) => {
if (!options.project) {
throw new schematics_1.SchematicsException("Option (project) is required.");
}
const project = (0, project_1.getProject)(host, options.project);
if (options.path === undefined) {
options.path = (0, project_1.buildDefaultPath)(project);
}
options.module = (0, find_module_1.findModuleFromOptions)(host, options);
const parsedPath = (0, parse_name_1.parseName)(options.path, options.name);
options.name = parsedPath.name;
options.path = parsedPath.path;
options.selector =
options.selector || (0, schematics_helper_1.buildSelector)(options, project.prefix);
options.dataSourceName = options.dataSourceName || options.name;
(0, validation_1.validateHtmlSelector)(options.selector);
const templateSource = (0, schematics_1.apply)((0, schematics_1.url)("./files"), [
(0, schematics_1.template)({
...core_1.strings,
"if-flat": (s) => (options.flat ? "" : s),
...options,
}),
(0, schematics_1.filter)((path) => {
if (options.virtualScrollStrategy !== "custom" &&
path.endsWith("virtual-scroll-custom-strategy.service.ts")) {
return false;
}
if (options.dataSource === "custom" &&
(path.endsWith("data-source.service.ts") ||
path.endsWith("types.ts"))) {
return false;
}
if ((options.dataSource === "none" ||
options.dataSource === "clientSide") &&
path.endsWith("data-source.service.ts")) {
return false;
}
return (options.dataSourceName === options.name ||
(options.dataSourceName !== options.name &&
!path.endsWith("data-source.service.ts") &&
!path.endsWith("types.ts") &&
!path.endsWith("data.ts")));
}),
(0, schematics_1.move)(`${parsedPath.path}/${parsedPath.name}`),
]);
return (0, schematics_1.chain)([
(0, schematics_1.branchAndMerge)((0, schematics_1.chain)([
addDeclarationToNgModule(options),
(0, schematics_1.mergeWith)(templateSource),
])),
]);
};
}
exports.default = default_1;
//# sourceMappingURL=index.js.map