Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | 12x 12x 12x 12x 406x 406x 406x 406x 406x 409x 406x 406x 406x 406x 406x 818x 818x 818x 289x 289x 289x 289x 289x 120x 120x 120x 120x 120x 289x 120x 409x 409x 409x 409x 409x 409x 233x 176x 176x 409x 409x 210x 210x 3x 3x 207x | import {SchematicsException, Tree} from '@angular-devkit/schematics'; import {Project, QuoteKind, SourceFile, VariableDeclaration} from 'ts-morph'; import {StringUtils} from './string-utils'; export class CodeUtils { static readSourceFile(host: Tree, file: string): SourceFile { const text = host.read(file); Iif (text === null) { throw new SchematicsException(`File ${file} does not exist.`); } const sourceText = text.toString('utf-8'); return CodeUtils.getSourceFile(file, sourceText); } static writeSourceFile(host: Tree, file: string, sourceFile: SourceFile): void { host.overwrite(file, sourceFile.getFullText()); } static formatText(block: any): void { block.formatText({ indentSize: 2 }); } static getSourceFile(file: string, content: string): SourceFile { const project = new Project({ useInMemoryFileSystem: true, manipulationSettings: { quoteKind: QuoteKind.Single } }); project.createSourceFile(file, content); let sourceFile = project.getSourceFile(file); Iif (!sourceFile) { sourceFile = project.createSourceFile(file, content); } return sourceFile; } static getVariableDeclaration(sourceFile: SourceFile, variableName: string): VariableDeclaration { let varDeclaration = sourceFile.getVariableDeclaration(variableName); Iif (!varDeclaration) { throw new Error(`Cannot find variable '${variableName}' in file '${sourceFile.getFilePath()}'`); } return varDeclaration; } static insertInVariableArray(sourceFile: SourceFile, variableName: string, str: string): void { let varDeclaration = CodeUtils.getVariableDeclaration(sourceFile, variableName); let block = varDeclaration.getText(); block = CodeUtils.insertInArray(block, str); varDeclaration.replaceWithText(block); CodeUtils.formatText(CodeUtils.getVariableDeclaration(sourceFile, variableName)); } static insertInVariableObject(sourceFile: SourceFile, variableName: string, str: string): void { let varDeclaration = CodeUtils.getVariableDeclaration(sourceFile, variableName); let block = varDeclaration.getText(); block = CodeUtils.insertInObject(block, str); varDeclaration.replaceWithText(block); CodeUtils.formatText(CodeUtils.getVariableDeclaration(sourceFile, variableName)); } static insertInArray(block: string, str: string): string { return CodeUtils.insertInBlock(block, str, '[', ']'); } static insertInObject(block: string, str: string): string { return CodeUtils.insertInBlock(block, str, '{', '}'); } private static insertInBlock(block: string, str: string, startDelimiter: string, endDelimiter: string): string { let startObj = block.lastIndexOf(startDelimiter); let endObj = block.lastIndexOf(endDelimiter); let objStr = block.substring(startObj, endObj + 1); // Retrouver le dernier caractère let startNdx = StringUtils.findLast(objStr.substr(0, objStr.length - 1), /[a-z0-9\}\)]/i); let comma = ""; if (startNdx < 0) { startNdx = 1; } else { startNdx++; comma = ','; } objStr = [objStr.slice(0, startNdx), `${comma}\r\n${str}`, objStr.slice(startNdx)].join(''); return [block.slice(0, startObj), objStr, block.slice(endObj + 1)].join(''); } static addImport(sourceFile: SourceFile, importName: string, importFile: string) { Iif (!importName) { sourceFile.addImportDeclaration({ moduleSpecifier: importFile }); } else if (importName.includes(" as ")) { Iif (importName.startsWith('*')) { sourceFile.addImportDeclaration({ defaultImport: importName, moduleSpecifier: importFile }); } else { sourceFile.addImportDeclaration({ namedImports: [importName], moduleSpecifier: importFile }); } } else { sourceFile.addImportDeclaration({ namedImports: [{name: importName}], moduleSpecifier: importFile }); } } } |