apollo-angular
Version:
Use your GraphQL data in your Angular app, with the Apollo Client
42 lines • 2.16 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.addModuleImportToRootModule = void 0;
const schematics_1 = require("@angular-devkit/schematics");
const change_1 = require("@schematics/angular/utility/change");
const ast_utils_1 = require("@schematics/angular/utility/ast-utils");
const ng_ast_utils_1 = require("@schematics/angular/utility/ng-ast-utils");
const _1 = require(".");
/**
* Import and add module to the root module.
* @param host {Tree} The source tree.
* @param importedModuleName {String} The name of the imported module.
* @param importedModulePath {String} The location of the imported module.
* @param projectName {String} The name of the project.
*/
function addModuleImportToRootModule(host, importedModuleName, importedModulePath, projectName) {
const mainPath = _1.getMainPath(host, projectName);
const appModulePath = ng_ast_utils_1.getAppModulePath(host, mainPath);
addModuleImportToModule(host, appModulePath, importedModuleName, importedModulePath);
}
exports.addModuleImportToRootModule = addModuleImportToRootModule;
/**
* Import and add module to specific module path.
* @param host {Tree} The source tree.
* @param moduleToImportIn {String} The location of the module to import in.
* @param importedModuleName {String} The name of the imported module.
* @param importedModulePath {String} The location of the imported module.
*/
function addModuleImportToModule(host, moduleToImportIn, importedModuleName, importedModulePath) {
const moduleSource = _1.getTypeScriptSourceFile(host, moduleToImportIn);
if (!moduleSource) {
throw new schematics_1.SchematicsException(`Module not found: ${moduleToImportIn}`);
}
const changes = ast_utils_1.addImportToModule(moduleSource, // XXX @schematics/angular went mad
moduleToImportIn, importedModuleName, importedModulePath);
const recorder = host.beginUpdate(moduleToImportIn);
changes
.filter((change) => change instanceof change_1.InsertChange)
.forEach((change) => recorder.insertLeft(change.pos, change.toAdd));
host.commitUpdate(recorder);
}
//# sourceMappingURL=ast.js.map