|
| 1 | +// In the root of this repo, execute: `npx ts-node scripts/refactor.ts` |
| 2 | + |
| 3 | +import {assert} from 'console'; |
| 4 | + |
| 5 | +import {ArrayLiteralExpression, Project, QuoteKind, SyntaxKind} from 'ts-morph'; |
| 6 | + |
| 7 | +const project = new Project({ |
| 8 | + manipulationSettings: { |
| 9 | + quoteKind: QuoteKind.Single, |
| 10 | + insertSpaceAfterOpeningAndBeforeClosingNonemptyBraces: false, |
| 11 | + }, |
| 12 | +}); |
| 13 | +project.addSourceFilesAtPaths('src/devices/*.ts'); |
| 14 | + |
| 15 | +//#region Refactor modernExtend temperature() to m.temperature() |
| 16 | +project.getSourceFiles().forEach((sourceFile) => { |
| 17 | + if (sourceFile.getBaseName() === 'index.ts') return; |
| 18 | + console.log(`Handling ${sourceFile.getBaseName()}`); |
| 19 | + |
| 20 | + let save = false; |
| 21 | + |
| 22 | + const definitions = sourceFile |
| 23 | + .getVariableStatementOrThrow('definitions') |
| 24 | + .getDeclarations()[0] |
| 25 | + .getInitializerOrThrow() |
| 26 | + .asKindOrThrow(SyntaxKind.ArrayLiteralExpression); |
| 27 | + |
| 28 | + for (const definition of definitions.getElements()) { |
| 29 | + const childs = definition.getChildrenOfKind(SyntaxKind.PropertyAssignment); |
| 30 | + const fingerprint = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'fingerprint'); |
| 31 | + const model = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'model'); |
| 32 | + const fingerprintArray = fingerprint?.getFirstChildByKind(SyntaxKind.ArrayLiteralExpression); |
| 33 | + if (fingerprintArray) { |
| 34 | + const lookup: {[s: string]: Set<string>} = {}; |
| 35 | + let match = true; |
| 36 | + for (const f of fingerprintArray.getElements()) { |
| 37 | + let modelID: string | undefined; |
| 38 | + let manufacturerName: string | undefined; |
| 39 | + for (const p of f.getChildrenOfKind(SyntaxKind.PropertyAssignment)) { |
| 40 | + if (p.getName() === 'modelID') { |
| 41 | + modelID = p.getInitializer()?.getText(); |
| 42 | + } else if (p.getName() === 'manufacturerName') { |
| 43 | + manufacturerName = p.getInitializer()?.getText(); |
| 44 | + } else { |
| 45 | + match = false; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if (modelID && manufacturerName && manufacturerName.includes('_T')) { |
| 50 | + if (!(modelID in lookup)) lookup[modelID] = new Set(); |
| 51 | + lookup[modelID].add(manufacturerName); |
| 52 | + } else { |
| 53 | + match = false; |
| 54 | + console.log(`skip ${model?.getText()} (${modelID}, ${manufacturerName} ${manufacturerName?.includes('_T')})`); |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + if (match) { |
| 60 | + if (Object.keys(lookup).length == 1) { |
| 61 | + const key = Object.keys(lookup)[0]; |
| 62 | + fingerprintArray.replaceWithText(`tuya.fingerprint(${key}, [${[...lookup[key]]}])`); |
| 63 | + } else { |
| 64 | + let txt: string[] = []; |
| 65 | + for (const [modelID, manufacturers] of Object.entries(lookup)) { |
| 66 | + txt.push(`...tuya.fingerprint(${modelID}, [${[...manufacturers]}])`); |
| 67 | + } |
| 68 | + fingerprintArray.replaceWithText(`[` + txt.join(', ') + `]`); |
| 69 | + } |
| 70 | + save = true; |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + if (save) { |
| 76 | + const modernExtendImport = sourceFile.getImportDeclarations().filter((d) => d.getModuleSpecifierSourceFile()?.getBaseName() === 'tuya.ts'); |
| 77 | + let match = false; |
| 78 | + for (const i of modernExtendImport) { |
| 79 | + match = true; |
| 80 | + i.remove(); |
| 81 | + } |
| 82 | + |
| 83 | + sourceFile.addImportDeclaration({ |
| 84 | + moduleSpecifier: '../lib/tuya', |
| 85 | + namespaceImport: 'tuya', |
| 86 | + }); |
| 87 | + sourceFile.saveSync(); |
| 88 | + } |
| 89 | +}); |
| 90 | + |
| 91 | +//#region Refactor modernExtend temperature() to m.temperature() |
| 92 | +// project.getSourceFiles().forEach((sourceFile) => { |
| 93 | +// if (sourceFile.getBaseName() === 'index.ts') return; |
| 94 | +// console.log(`Handling ${sourceFile.getBaseName()}`); |
| 95 | + |
| 96 | +// let save = false; |
| 97 | + |
| 98 | +// const modernExtendImport = sourceFile |
| 99 | +// .getImportDeclarations() |
| 100 | +// .filter((d) => d.getModuleSpecifierSourceFile()?.getBaseName() === 'modernExtend.ts'); |
| 101 | + |
| 102 | +// for (const i of modernExtendImport) { |
| 103 | +// for (const n of i.getNamedImports()) { |
| 104 | +// const references = sourceFile.getDescendantsOfKind(SyntaxKind.Identifier).filter((identifier) => identifier.getText() === n.getText()); |
| 105 | +// references.forEach((reference) => { |
| 106 | +// if (reference.getParent().getKind() == SyntaxKind.CallExpression) { |
| 107 | +// reference.replaceWithText(`m.${reference.getText()}`); |
| 108 | +// } |
| 109 | +// }); |
| 110 | +// } |
| 111 | +// } |
| 112 | + |
| 113 | +// save = true; |
| 114 | + |
| 115 | +// if (save) { |
| 116 | +// const modernExtendImport = sourceFile |
| 117 | +// .getImportDeclarations() |
| 118 | +// .filter((d) => d.getModuleSpecifierSourceFile()?.getBaseName() === 'modernExtend.ts'); |
| 119 | +// let match = false; |
| 120 | +// for (const i of modernExtendImport) { |
| 121 | +// match = true; |
| 122 | +// i.remove(); |
| 123 | +// } |
| 124 | + |
| 125 | +// if (match) { |
| 126 | +// sourceFile.addImportDeclaration({ |
| 127 | +// moduleSpecifier: '../lib/modernExtend', |
| 128 | +// namespaceImport: 'm', |
| 129 | +// }); |
| 130 | +// } |
| 131 | + |
| 132 | +// sourceFile.saveSync(); |
| 133 | +// } |
| 134 | +// }); |
0 commit comments