MCPcopy
hub / github.com/angular/angular-cli / InsertChange

Class InsertChange

packages/schematics/angular/utility/change.ts:46–73  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

44 * Will add text to the source code.
45 */
46export class InsertChange implements Change {
47 order: number;
48 description: string;
49
50 constructor(
51 public path: string,
52 public pos: number,
53 public toAdd: string,
54 ) {
55 if (pos < 0) {
56 throw new Error('Negative positions are invalid');
57 }
58 this.description = `Inserted ${toAdd} into position ${pos} of ${path}`;
59 this.order = pos;
60 }
61
62 /**
63 * This method does not insert spaces if there is none in the original string.
64 */
65 apply(host: Host): Promise<void> {
66 return host.read(this.path).then((content) => {
67 const prefix = content.substring(0, this.pos);
68 const suffix = content.substring(this.pos);
69
70 return host.write(this.path, `${prefix}${this.toAdd}${suffix}`);
71 });
72 }
73}
74
75/**
76 * Will remove text from the source code.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected