UNPKG

@ngrx/schematics

Version:

NgRx Schematics for Angular

1 lines 9.75 kB
{"version":3,"file":"change.js","sourceRoot":"","sources":["../../../../../modules/schematics/schematics-core/utility/change.ts"],"names":[],"mappings":";;;AAgJA,kDAYC;AAED,gDAOC;AAED,oDAiBC;AAED,sCAQC;AAlKD;;GAEG;AACH,MAAa,UAAU;IACrB,WAAW,GAAG,eAAe,CAAC;IAC9B,KAAK,GAAG,QAAQ,CAAC;IACjB,IAAI,GAAG,IAAI,CAAC;IACZ,KAAK;QACH,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;IAC3B,CAAC;CACF;AAPD,gCAOC;AAED;;GAEG;AACH,MAAa,YAAY;IAKd;IACA;IACA;IANT,KAAK,CAAS;IACd,WAAW,CAAS;IAEpB,YACS,IAAY,EACZ,GAAW,EACX,KAAa;QAFb,SAAI,GAAJ,IAAI,CAAQ;QACZ,QAAG,GAAH,GAAG,CAAQ;QACX,UAAK,GAAL,KAAK,CAAQ;QAEpB,IAAI,GAAG,GAAG,CAAC,EAAE,CAAC;YACZ,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;QACpD,CAAC;QACD,IAAI,CAAC,WAAW,GAAG,YAAY,KAAK,kBAAkB,GAAG,OAAO,IAAI,EAAE,CAAC;QACvE,IAAI,CAAC,KAAK,GAAG,GAAG,CAAC;IACnB,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,IAAU;QACd,OAAO,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,OAAO,EAAE,EAAE;YAC3C,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;YAC9C,MAAM,MAAM,GAAG,OAAO,CAAC,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;YAE3C,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,EAAE,GAAG,MAAM,GAAG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,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QACzB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,MAAM,QAAQ,GAAG,oBAAoB,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,CAAC,CAAC;IAC3D,IAAI,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;IAC5B,OAAO,IAAI,CAAC;AACd,CAAC","sourcesContent":["import * as ts from 'typescript';\nimport { Tree, UpdateRecorder } from '@angular-devkit/schematics';\nimport { Path } from '@angular-devkit/core';\n\n/* istanbul ignore file */\n/**\n * @license\n * Copyright Google Inc. All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.io/license\n */\nexport interface Host {\n write(path: string, content: string): Promise<void>;\n read(path: string): Promise<string>;\n}\n\nexport interface Change {\n apply(host: Host): Promise<void>;\n\n // The file this change should be applied to. Some changes might not apply to\n // a file (maybe the config).\n readonly path: string | null;\n\n // The order this change should be applied. Normally the position inside the file.\n // Changes are applied from the bottom of a file to the top.\n readonly order: number;\n\n // The description of this change. This will be outputted in a dry or verbose run.\n readonly description: string;\n}\n\n/**\n * An operation that does nothing.\n */\nexport class NoopChange implements Change {\n description = 'No operation.';\n order = Infinity;\n path = null;\n apply() {\n return Promise.resolve();\n }\n}\n\n/**\n * Will add text to the source code.\n */\nexport class InsertChange implements Change {\n order: number;\n description: string;\n\n constructor(\n public path: string,\n public pos: number,\n public toAdd: string\n ) {\n if (pos < 0) {\n throw new Error('Negative positions are invalid');\n }\n this.description = `Inserted ${toAdd} into position ${pos} of ${path}`;\n this.order = pos;\n }\n\n /**\n * This method does not insert spaces if there is none in the original string.\n */\n apply(host: Host) {\n return host.read(this.path).then((content) => {\n const prefix = content.substring(0, this.pos);\n const suffix = content.substring(this.pos);\n\n return host.write(this.path, `${prefix}${this.toAdd}${suffix}`);\n });\n }\n}\n\n/**\n * Will remove text from the source code.\n */\nexport class RemoveChange implements Change {\n order: number;\n description: string;\n\n constructor(\n public path: string,\n public pos: number,\n public end: number\n ) {\n if (pos < 0 || end < 0) {\n throw new Error('Negative positions are invalid');\n }\n this.description = `Removed text in position ${pos} to ${end} of ${path}`;\n this.order = pos;\n }\n\n apply(host: Host): Promise<void> {\n return host.read(this.path).then((content) => {\n const prefix = content.substring(0, this.pos);\n const suffix = content.substring(this.end);\n\n // TODO: throw error if toRemove doesn't match removed string.\n return host.write(this.path, `${prefix}${suffix}`);\n });\n }\n}\n\n/**\n * Will replace text from the source code.\n */\nexport class ReplaceChange implements Change {\n order: number;\n description: string;\n\n constructor(\n public path: string,\n public pos: number,\n public oldText: string,\n public newText: string\n ) {\n if (pos < 0) {\n throw new Error('Negative positions are invalid');\n }\n this.description = `Replaced ${oldText} into position ${pos} of ${path} with ${newText}`;\n this.order = pos;\n }\n\n apply(host: Host): Promise<void> {\n return host.read(this.path).then((content) => {\n const prefix = content.substring(0, this.pos);\n const suffix = content.substring(this.pos + this.oldText.length);\n const text = content.substring(this.pos, this.pos + this.oldText.length);\n\n if (text !== this.oldText) {\n return Promise.reject(\n new Error(`Invalid replace: \"${text}\" != \"${this.oldText}\".`)\n );\n }\n\n // TODO: throw error if oldText doesn't match removed string.\n return host.write(this.path, `${prefix}${this.newText}${suffix}`);\n });\n }\n}\n\nexport function createReplaceChange(\n sourceFile: ts.SourceFile,\n node: ts.Node,\n oldText: string,\n newText: string\n): ReplaceChange {\n return new ReplaceChange(\n sourceFile.fileName,\n node.getStart(sourceFile),\n oldText,\n newText\n );\n}\n\nexport function createRemoveChange(\n sourceFile: ts.SourceFile,\n node: ts.Node,\n from = node.getStart(sourceFile),\n to = node.getEnd()\n): RemoveChange {\n return new RemoveChange(sourceFile.fileName, from, to);\n}\n\nexport function createChangeRecorder(\n tree: Tree,\n path: string,\n changes: Change[]\n): UpdateRecorder {\n const recorder = tree.beginUpdate(path);\n for (const change of changes) {\n if (change instanceof InsertChange) {\n recorder.insertLeft(change.pos, change.toAdd);\n } else if (change instanceof RemoveChange) {\n recorder.remove(change.pos, change.end - change.pos);\n } else if (change instanceof ReplaceChange) {\n recorder.remove(change.pos, change.oldText.length);\n recorder.insertLeft(change.pos, change.newText);\n }\n }\n return recorder;\n}\n\nexport function commitChanges(tree: Tree, path: string, changes: Change[]) {\n if (changes.length === 0) {\n return false;\n }\n\n const recorder = createChangeRecorder(tree, path, changes);\n tree.commitUpdate(recorder);\n return true;\n}\n"]}