UNPKG

angular-datatables

Version:
164 lines 7.52 kB
"use strict"; /** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getSourceFile = exports.addModuleImportToModule = exports.addModuleImportToRootModule = exports.removePackageJsonDependency = exports.addAssetToAngularJson = exports.addPackageToPackageJson = exports.getProjectTargetOptions = exports.expectProjectStyleFile = exports.getProjectFromWorkspace = exports.addStyleToTarget = exports.installPackageJsonDependencies = void 0; const ts = require("typescript"); const ast_utils_1 = require("@schematics/angular/utility/ast-utils"); const change_1 = require("@schematics/angular/utility/change"); const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils"); const index_1 = require("@schematics/angular/utility/test/index"); const project_main_file_1 = require("./project-main-file"); const tasks_1 = require("@angular-devkit/schematics/tasks"); const schematics_1 = require("@angular-devkit/schematics"); function installPackageJsonDependencies() { return (host, context) => { context.addTask(new tasks_1.NodePackageInstallTask()); context.logger.log('info', `🔍 Installing packages...`); return host; }; } exports.installPackageJsonDependencies = installPackageJsonDependencies; function addStyleToTarget(project, targetName, host, assetPath, workspace) { const targetOptions = getProjectTargetOptions(project, targetName); if (!targetOptions.styles) { targetOptions.styles = [assetPath]; } else { const existingStyles = targetOptions.styles .map((style) => { return typeof style === 'string' ? style : style.input; }); const hasBootstrapStyle = existingStyles.find((style) => { return style.includes(assetPath); }); if (!hasBootstrapStyle) { targetOptions.styles.unshift(assetPath); } } host.overwrite('angular.json', JSON.stringify(workspace, null, 2)); } exports.addStyleToTarget = addStyleToTarget; function getProjectFromWorkspace(workspace, projectName) { /* tslint:disable-next-line: no-non-null-assertion */ const project = workspace.projects[projectName || workspace.defaultProject]; if (!project) { throw new Error(`Could not find project in workspace: ${projectName}`); } return project; } exports.getProjectFromWorkspace = getProjectFromWorkspace; function expectProjectStyleFile(project, filePath) { expect(getProjectTargetOptions(project, 'build').styles).toContain(filePath, `Expected "${filePath}" to be added to the project styles in the workspace.`); } exports.expectProjectStyleFile = expectProjectStyleFile; function getProjectTargetOptions(project, buildTarget) { const targetConfig = project.architect && project.architect[buildTarget] || project.targets && project.targets[buildTarget]; if (targetConfig && targetConfig.options) { return targetConfig.options; } throw new Error(`Cannot determine project target configuration for: ${buildTarget}.`); } exports.getProjectTargetOptions = getProjectTargetOptions; function sortObjectByKeys(obj) { return Object .keys(obj) .sort() /* tslint:disable-next-line: no-any */ .reduce((result, key) => (result[key] = obj[key]) && result, {}); } /** * This function has been borrowed from: * https://github.com/valor-software/ngx-bootstrap/tree/development/schematics/src/utils/index.ts * * Note: This function accepts an additional parameter `isDevDependency` so we * can place a given dependency in the correct dependencies array inside package.json */ function addPackageToPackageJson(host, pkg, version, isDevDependency = false) { if (host.exists('package.json')) { /* tslint:disable-next-line: no-non-null-assertion */ const sourceText = host.read('package.json').toString('utf-8'); const json = JSON.parse(sourceText); if (!json.dependencies) { json.dependencies = {}; } if (!json.devDependencies) { json.dependencies = {}; } // update UI that `pkg` wasn't re-added to package.json if (json.dependencies[pkg] || json.devDependencies[pkg]) return false; if (!json.dependencies[pkg] && !isDevDependency) { json.dependencies[pkg] = version; json.dependencies = sortObjectByKeys(json.dependencies); } if (!json.devDependencies[pkg] && isDevDependency) { json.devDependencies[pkg] = version; json.devDependencies = sortObjectByKeys(json.devDependencies); } host.overwrite('package.json', JSON.stringify(json, null, 2)); return true; } return false; } exports.addPackageToPackageJson = addPackageToPackageJson; function addAssetToAngularJson(host, assetType, assetPath) { /* tslint:disable-next-line: no-non-null-assertion */ const sourceText = host.read('angular.json').toString('utf-8'); const json = JSON.parse(sourceText); if (!json) return false; const projectName = Object.keys(json['projects'])[0]; const projectObject = json.projects[projectName]; const targets = projectObject.targets || projectObject.architect; const targetLocation = targets.build.options[assetType]; // update UI that `assetPath` wasn't re-added to angular.json if (targetLocation.indexOf(assetPath) != -1) return false; targetLocation.push(assetPath); host.overwrite('angular.json', JSON.stringify(json, null, 2)); return true; } exports.addAssetToAngularJson = addAssetToAngularJson; function removePackageJsonDependency(tree, dependencyName) { const packageContent = JSON.parse(index_1.getFileContent(tree, '/package.json')); delete packageContent.dependencies[dependencyName]; tree.overwrite('/package.json', JSON.stringify(packageContent, null, 2)); } exports.removePackageJsonDependency = removePackageJsonDependency; function addModuleImportToRootModule(host, moduleName, src, project) { const modulePath = ng_ast_utils_1.getAppModulePath(host, project_main_file_1.getProjectMainFile(project)); addModuleImportToModule(host, modulePath, moduleName, src); } exports.addModuleImportToRootModule = addModuleImportToRootModule; function addModuleImportToModule(host, modulePath, moduleName, src) { const moduleSource = getSourceFile(host, modulePath); if (!moduleSource) { throw new schematics_1.SchematicsException(`Module not found: ${modulePath}`); } const changes = ast_utils_1.addImportToModule(moduleSource, modulePath, moduleName, src); const recorder = host.beginUpdate(modulePath); changes.forEach((change) => { if (change instanceof change_1.InsertChange) { recorder.insertLeft(change.pos, change.toAdd); } }); host.commitUpdate(recorder); } exports.addModuleImportToModule = addModuleImportToModule; function getSourceFile(host, path) { const buffer = host.read(path); if (!buffer) { throw new schematics_1.SchematicsException(`Could not find file for path: ${path}`); } const content = buffer.toString(); return ts.createSourceFile(path, content, ts.ScriptTarget.Latest, true); } exports.getSourceFile = getSourceFile; //# sourceMappingURL=index.js.map