23
23
* The examples that depend on @tensorflow/tfjs-node are currently not
24
24
* handled by this script.
25
25
*
26
- * Usage example:
27
- * ./update-tfjs-version 0.13.2
26
+ * Usage examples:
27
+ *
28
+ * - To update only the examples that do not use tfjs-node or tfjs-node-gpu:
29
+ * ./update-tfjs-version 1.2.8
30
+ * - To update only the examples that use tfjs-node or tfjs-node-gpu:
31
+ * ./update-tfjs-version --node 1.2.8
28
32
*/
29
33
30
34
const fs = require ( 'fs' ) ;
31
35
const path = require ( 'path' ) ;
32
36
33
37
const tfjsTag = '@tensorflow/tfjs' ;
34
38
const tfjsNodeTag = '@tensorflow/tfjs-node' ;
39
+ const tfjsNodeGpuTag = '@tensorflow/tfjs-node-gpu' ;
35
40
36
- if ( process . argv . length !== 3 ) {
37
- console . log ( 'Usage: update-tfjs-version <TARGET_TFJS_VER>`' ) ;
41
+ const argv = process . argv . slice ( ) ; // Avoid mutating `process.argv`.
42
+ if ( ! ( argv . length === 3 || argv . length === 4 ) ) {
43
+ // Invalid syntax. Print usage help.
44
+ console . log ( 'Usage: update-tfjs-version [--node] <TARGET_TFJS_VER>`' ) ;
45
+ console . log ( ) ;
46
+ console . log ( 'Args:' ) ;
47
+ console . log (
48
+ ' --node: Update only examples that use tfjs-node/tfjs-node-gpu.' ) ;
49
+ console . log (
50
+ ' If this flag is not specified, only those examples' ) ;
51
+ console . log (
52
+ ' that don\'t use tfjs-node or tfjs-node-gpu will be' ) ;
53
+ console . log (
54
+ ' updated.' ) ;
38
55
process . exit ( 1 ) ;
39
56
}
40
- const targetTfjsVer = process . argv [ 2 ] ;
57
+
58
+ let isNodeExamples = false ;
59
+ if ( argv . indexOf ( '--node' ) !== - 1 ) {
60
+ argv . splice ( argv . indexOf ( '--node' ) , 1 ) ;
61
+ isNodeExamples = true ;
62
+ console . log ( 'Will update only Node.js examples\n' ) ;
63
+ }
64
+
65
+ const targetTfjsVer = argv [ 2 ] ;
66
+ const targetVer = `^${ targetTfjsVer } ` ;
41
67
42
68
const dirItems = fs . readdirSync ( __dirname ) ;
43
69
for ( const item of dirItems ) {
@@ -52,24 +78,69 @@ for (const item of dirItems) {
52
78
}
53
79
54
80
const devDeps = packageJson [ 'devDependencies' ] ;
55
- if ( deps [ tfjsNodeTag ] != null ||
56
- devDeps != null && devDeps [ tfjsNodeTag ] != null ) {
57
- console . log (
58
- `*** Skipping example with dependency or devDependency ` +
59
- `on tfjs-node: ${ item } ` ) ;
60
- continue ;
61
- }
62
-
63
- const targetVer = `^${ targetTfjsVer } ` ;
64
- if ( deps [ tfjsTag ] != null ) {
65
- if ( deps [ tfjsTag ] === targetVer ) {
66
- console . log ( `${ item } : Already at target version (${ targetVer } )` ) ;
67
- } else {
68
- const oldVer = deps [ tfjsTag ] ;
69
- deps [ tfjsTag ] = targetVer ;
81
+ if ( isNodeExamples ) {
82
+ const depCPU = deps == null ? null : deps [ tfjsNodeTag ] ;
83
+ const depGPU = deps == null ? null : deps [ tfjsNodeGpuTag ] ;
84
+ const devDepCPU = devDeps == null ? null : devDeps [ tfjsNodeTag ] ;
85
+ const devDepGPU = devDeps == null ? null : devDeps [ tfjsNodeGpuTag ] ;
86
+ // Update only examples that use tfjs-node / tfjs-node-gpu.
87
+ if ( ( devDepCPU != null || devDepGPU != null ) ||
88
+ ( depCPU != null || depGPU != null ) ) {
89
+ console . log ( `${ item } :` ) ;
90
+ if ( devDepCPU != null ) {
91
+ devDeps [ tfjsNodeTag ] = targetVer ;
92
+ console . log (
93
+ ` devDependencies["${ tfjsNodeTag } "]: ` +
94
+ `${ devDepCPU } --> ${ targetVer } ` ) ;
95
+ }
96
+ if ( devDepGPU != null ) {
97
+ devDeps [ tfjsNodeGpuTag ] = targetVer ;
98
+ console . log (
99
+ ` devDependencies["${ tfjsNodeTag } "]: ` +
100
+ `${ devDepGPU } --> ${ targetVer } ` ) ;
101
+ }
102
+ if ( deps [ tfjsTag ] != null ) {
103
+ const oldVer = deps [ tfjsTag ] ;
104
+ deps [ tfjsTag ] = targetVer ;
105
+ console . log ( ` dependencies["${ tfjsTag } "]: ${ oldVer } --> ${ targetVer } ` ) ;
106
+ }
107
+ if ( depCPU != null ) {
108
+ deps [ tfjsNodeTag ] = targetVer ;
109
+ console . log (
110
+ ` dependencies["${ tfjsNodeTag } "]: ` +
111
+ `${ depCPU } --> ${ targetVer } ` ) ;
112
+ }
113
+ if ( depGPU != null ) {
114
+ deps [ tfjsNodeGpuTag ] = targetVer ;
115
+ console . log (
116
+ ` dependencies["${ tfjsNodeGpuTag } "]: ` +
117
+ `${ depGPU } --> ${ targetVer } ` ) ;
118
+ }
70
119
fs . writeFileSync (
71
- packageJsonPath , JSON . stringify ( packageJson , null , 2 ) + '\n' ) ;
72
- console . log ( `${ item } : ${ oldVer } --> ${ targetVer } ` ) ;
120
+ packageJsonPath , JSON . stringify ( packageJson , null , 2 ) + '\n' ) ;
121
+ console . log ( '\n' ) ;
122
+ }
123
+ } else {
124
+ // Update only tfjs examples, i.e., the ones that don't use tfjs-node
125
+ // or tfjs-node-gpu.
126
+ if ( deps [ tfjsNodeTag ] != null ||
127
+ devDeps != null && devDeps [ tfjsNodeTag ] != null ) {
128
+ console . log (
129
+ `*** Skipping example with dependency or devDependency ` +
130
+ `on tfjs-node: ${ item } ` ) ;
131
+ continue ;
132
+ }
133
+
134
+ if ( deps [ tfjsTag ] != null ) {
135
+ if ( deps [ tfjsTag ] === targetVer ) {
136
+ console . log ( `${ item } : Already at target version (${ targetVer } )` ) ;
137
+ } else {
138
+ const oldVer = deps [ tfjsTag ] ;
139
+ deps [ tfjsTag ] = targetVer ;
140
+ fs . writeFileSync (
141
+ packageJsonPath , JSON . stringify ( packageJson , null , 2 ) + '\n' ) ;
142
+ console . log ( `${ item } : dependencies["${ tfjsTag } "]: ${ oldVer } --> ${ targetVer } ` ) ;
143
+ }
73
144
}
74
145
}
75
146
}
0 commit comments