import { Tree } from '@angular-devkit/schematics'; export default function createComponent(host: Tree, path: string, name: string) { const componentPath = `${path}/${name}`; host.createDir(componentPath); host.create(`${componentPath}/${name}.component.ts`, `import { Component } from '@angular/core';\n@Component({ selector: '${name}', templateUrl: './${name}.component.html' })\nexport class ${name}Component {}\n`); host.create(`${componentPath}/${name}.component.html`, `${name} works!
\n`); }
import { Tree } from '@angular-devkit/schematics'; export default function addImportStatement(host: Tree, filePath: string, importStatement: string) { const fileBuffer = host.read(filePath); if (fileBuffer === null) { throw new Error(`Could not read file: ${filePath}`); } const content = fileBuffer.toString(); const newContent = `${importStatement}\n${content}`; host.overwrite(filePath, newContent); }This code reads the contents of the file, adds the new import statement to the top of the file, and then overwrites the original file with the updated content. Overall, the @angular-devkit/schematics Tree is a powerful library that allows developers to easily manipulate the file system within their Angular schematics.