1
+ // In the root of this repo, execute: `npx ts-node scripts/modernExtendRefactor.ts`
2
+
1
3
import { Project , QuoteKind , SyntaxKind } from 'ts-morph' ;
2
4
3
5
const project = new Project ( {
@@ -15,9 +17,12 @@ project.getSourceFiles().forEach((sourceFile) => {
15
17
if ( sourceFile . getBaseName ( ) === 'index.ts' ) return ;
16
18
console . log ( `Handling ${ sourceFile . getBaseName ( ) } ` ) ;
17
19
20
+ if ( sourceFile . getBaseName ( ) . includes ( 'philips' ) ) {
21
+ return ;
22
+ }
23
+
18
24
let changed = true ;
19
25
let save = false ;
20
- const type = 'mullerLichtLight' ;
21
26
while ( changed ) {
22
27
changed = false ;
23
28
const definitions = sourceFile . getVariableStatementOrThrow ( 'definitions' ) . getDescendantsOfKind ( SyntaxKind . ObjectLiteralExpression ) ;
@@ -27,99 +32,31 @@ project.getSourceFiles().forEach((sourceFile) => {
27
32
const childs = definition . getChildrenOfKind ( SyntaxKind . PropertyAssignment ) ;
28
33
const model = childs . find ( ( c ) => c . getFirstChildByKind ( SyntaxKind . Identifier ) ?. getText ( ) === 'model' ) ;
29
34
const extend = childs . find ( ( c ) => c . getFirstChildByKind ( SyntaxKind . Identifier ) ?. getText ( ) === 'extend' ) ;
30
- // const exposes = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'exposes');
31
- // const configure = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'configure');
35
+ const extendArray = extend ?. getFirstChildByKind ( SyntaxKind . ArrayLiteralExpression ) ;
36
+ const exposes = childs . find ( ( c ) => c . getFirstChildByKind ( SyntaxKind . Identifier ) ?. getText ( ) === 'exposes' ) ;
37
+ const exposesArray = exposes ?. getFirstChildByKind ( SyntaxKind . ArrayLiteralExpression ) ;
38
+ const configure = childs . find ( ( c ) => c . getFirstChildByKind ( SyntaxKind . Identifier ) ?. getText ( ) === 'configure' ) ;
32
39
const fromZigbee = childs . find ( ( c ) => c . getFirstChildByKind ( SyntaxKind . Identifier ) ?. getText ( ) === 'fromZigbee' ) ;
40
+ const fromZigbeeArray = fromZigbee ?. getFirstChildByKind ( SyntaxKind . ArrayLiteralExpression ) ;
33
41
const toZigbee = childs . find ( ( c ) => c . getFirstChildByKind ( SyntaxKind . Identifier ) ?. getText ( ) === 'toZigbee' ) ;
34
- // const meta = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'meta');
35
- // const endpoint = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'endpoint');
36
-
37
- // const ota = childs.find((c) => c.getFirstChildByKind(SyntaxKind.Identifier)?.getText() === 'ota');
38
42
39
- if ( extend ?. getFullText ( ) . includes ( 'extend.light_onoff' ) && ! fromZigbee && toZigbee ?. getFullText ( ) . includes ( 'tint_scene' ) ) {
40
- console . log ( `Handling ${ model ?. getFullText ( ) . trim ( ) } ` ) ;
41
- toZigbee ?. remove ( ) ;
42
- const newOpts : { [ s : string ] : unknown } = { } ; // {endpoints: eval(`(${endpoint.getFullText().split('return ')[1].split(';')[0]})`) };
43
- // configure?.remove();
44
- // endpoint?.remove();
45
- const extendFeatures = extend . getFullText ( ) . split ( '(' ) [ 0 ] . split ( '_' ) ;
46
- if ( extendFeatures . includes ( 'colortemp' ) ) {
47
- newOpts . colorTemp = { range : null } ;
48
- }
49
- if ( extendFeatures . includes ( 'color' ) ) {
50
- newOpts . color = true ;
51
- }
52
- if ( extendFeatures . includes ( 'gradient' ) ) {
53
- newOpts . gradient = true ;
54
- }
55
- let opts = extend ?. getFullText ( ) . split ( '(' ) [ 1 ] . slice ( 0 , - 1 ) . trim ( ) ;
56
- if ( opts ) {
57
- if ( opts [ opts . length - 1 ] === ',' ) {
58
- opts = opts . substring ( 0 , opts . length - 1 ) ;
59
- }
60
- const evalOpts = Object . entries ( eval ( `(${ opts } )` ) ) ;
61
- for ( const [ key , value ] of evalOpts ) {
62
- if ( key === 'colorTempRange' ) {
63
- newOpts . colorTemp = { ...( newOpts . colorTemp as object ) , range : value } ;
64
- } else if ( key === 'disableColorTempStartup' ) {
65
- // @ts -expect-error ignore
66
- newOpts . colorTemp = { ...newOpts . colorTemp , startup : ! value } ;
67
- } else if ( key === 'disablePowerOnBehavior' ) {
68
- newOpts . powerOnBehavior = ! value ;
69
- } else if ( key === 'disableEffect' ) {
70
- newOpts . effect = ! value ;
71
- } else if ( key === 'disableHueEffects' ) {
72
- newOpts . hueEffect = ! value ;
73
- } else if ( key === 'supportsHueAndSaturation' || key === 'preferHueAndSaturation' ) {
74
- // @ts -expect-error ignore
75
- newOpts . color = { ...newOpts . color , modes : evalOpts . preferHueAndSaturation ? [ 'hs' , 'xy' ] : [ 'xy' , 'hs' ] } ;
76
- } else if ( key === 'extraEffects' ) {
77
- // @ts -expect-error ignore
78
- newOpts . gradient = { ...newOpts . gradient , extraEffects : value } ;
79
- } else if ( key === 'noConfigure' ) {
80
- // ignore
81
- } else {
82
- throw new Error ( `Unsupported ${ key } - ${ value } ` ) ;
83
- }
84
- }
43
+ const fzIlluminance = fromZigbeeArray ?. getElements ( ) . find ( ( el ) => el . getText ( ) === 'fz.illuminance' ) ;
44
+ if ( fzIlluminance ) {
45
+ fromZigbeeArray ?. removeElement ( fzIlluminance ) ;
46
+ const exposesIlluminance = exposesArray ?. getElements ( ) . find ( ( el ) => el . getText ( ) === 'e.illuminance()' ) ;
47
+ if ( exposesIlluminance ) {
48
+ exposesArray ?. removeElement ( exposesIlluminance ) ;
85
49
}
86
- // if (meta) {
87
- // for (const [key, value] of Object.entries(eval(`(${meta?.getFullText().replace('meta: ', '')})`))) {
88
- // if (key === 'turnsOffAtBrightness1') {
89
- // newOpts.turnsOffAtBrightness1 = value;
90
- // } else if (key === 'applyRedFix') {
91
- // // @ts -expect-error ignore
92
- // newOpts.color = {...newOpts.color, applyRedFix: value};
93
- // } else if (key === 'supportsEnhancedHue') {
94
- // // @ts -expect-error ignore
95
- // newOpts.color = {...newOpts.color, enhancedHue: value};
96
- // } else if (key === 'multiEndpoint' || key === 'disableDefaultResponse') {
97
- // // ignore
98
- // } else {
99
- // throw new Error(`Unsupported ${key} - ${value}`);
100
- // }
101
- // }
102
- // // meta.remove();
103
- // }
104
- // if (ota) {
105
- // ota.remove();
106
- // }
107
50
108
- localTotalDefinitionsWithModernExtend += 1 ;
109
- extend . replaceWithText (
110
- `extend: [${ type } (${ JSON . stringify ( newOpts )
111
- . split ( `"` )
112
- . join ( '' )
113
- . replace ( `range:null` , `range:undefined` )
114
- . replace ( `xy` , `'xy'` )
115
- . replace ( `hs` , `'hs'` )
116
- . replace ( `({})` , `()` ) } )]`,
117
- ) ;
118
- changed = true ;
51
+ if ( ! extend ) {
52
+ definition . addPropertyAssignment ( {
53
+ name : 'extend' ,
54
+ initializer : '[m.illuminance()]' ,
55
+ } ) ;
56
+ } else {
57
+ extendArray ?. addElement ( 'm.illuminance()' ) ;
58
+ }
119
59
save = true ;
120
- break ;
121
- } else if ( extend ?. getFirstChildByKind ( SyntaxKind . ArrayLiteralExpression ) ) {
122
- localTotalDefinitionsWithModernExtend += 1 ;
123
60
}
124
61
}
125
62
@@ -130,22 +67,10 @@ project.getSourceFiles().forEach((sourceFile) => {
130
67
}
131
68
132
69
if ( save ) {
133
- const modernExtendImport = sourceFile
134
- . getImportDeclarations ( )
135
- . find ( ( d ) => d . getModuleSpecifierSourceFile ( ) ?. getBaseName ( ) === 'modernExtend.ts' ) ;
136
- if ( ! modernExtendImport ) {
137
- sourceFile . addImportDeclaration ( { moduleSpecifier : '../lib/modernExtend' , namedImports : [ type ] } ) ;
138
- } else {
139
- if ( ! modernExtendImport . getNamedImports ( ) . find ( ( i ) => i . getName ( ) === type ) ) {
140
- modernExtendImport . addNamedImport ( type ) ;
141
- }
142
- }
143
-
144
- const extendImport = sourceFile . getImportDeclarations ( ) . find ( ( d ) => d . getModuleSpecifierSourceFile ( ) ?. getBaseName ( ) === 'extend.ts' ) ;
145
- if ( ! sourceFile . getFullText ( ) . includes ( 'extend.' ) && extendImport ) {
146
- extendImport . remove ( ) ;
147
- }
148
-
70
+ sourceFile . addImportDeclaration ( {
71
+ moduleSpecifier : '../lib/modernExtend' ,
72
+ namespaceImport : 'm' ,
73
+ } ) ;
149
74
sourceFile . saveSync ( ) ;
150
75
}
151
76
} ) ;
0 commit comments