@switchbot/homebridge-switchbot 2.1.0-beta.8 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -2
- package/dist/device/bot.d.ts.map +1 -1
- package/dist/device/bot.js +12 -3
- package/dist/device/bot.js.map +1 -1
- package/dist/device/ceilinglight.d.ts.map +1 -1
- package/dist/device/ceilinglight.js +12 -3
- package/dist/device/ceilinglight.js.map +1 -1
- package/dist/device/colorbulb.d.ts.map +1 -1
- package/dist/device/colorbulb.js +12 -2
- package/dist/device/colorbulb.js.map +1 -1
- package/dist/device/contact.d.ts.map +1 -1
- package/dist/device/contact.js +8 -2
- package/dist/device/contact.js.map +1 -1
- package/dist/device/curtain.d.ts.map +1 -1
- package/dist/device/curtain.js +12 -3
- package/dist/device/curtain.js.map +1 -1
- package/dist/device/humidifier.d.ts.map +1 -1
- package/dist/device/humidifier.js +12 -3
- package/dist/device/humidifier.js.map +1 -1
- package/dist/device/lock.d.ts.map +1 -1
- package/dist/device/lock.js +12 -3
- package/dist/device/lock.js.map +1 -1
- package/dist/device/meter.d.ts.map +1 -1
- package/dist/device/meter.js +8 -2
- package/dist/device/meter.js.map +1 -1
- package/dist/device/meterplus.d.ts.map +1 -1
- package/dist/device/meterplus.js +8 -2
- package/dist/device/meterplus.js.map +1 -1
- package/dist/device/motion.d.ts.map +1 -1
- package/dist/device/motion.js +8 -2
- package/dist/device/motion.js.map +1 -1
- package/dist/device/plug.d.ts.map +1 -1
- package/dist/device/plug.js +12 -3
- package/dist/device/plug.js.map +1 -1
- package/dist/device/striplight.d.ts.map +1 -1
- package/dist/device/striplight.js +12 -3
- package/dist/device/striplight.js.map +1 -1
- package/dist/platform.d.ts +0 -1
- package/dist/platform.d.ts.map +1 -1
- package/dist/platform.js +64 -87
- package/dist/platform.js.map +1 -1
- package/package.json +1 -1
- package/src/device/bot.ts +9 -3
- package/src/device/ceilinglight.ts +9 -3
- package/src/device/colorbulb.ts +9 -2
- package/src/device/contact.ts +6 -2
- package/src/device/curtain.ts +9 -3
- package/src/device/humidifier.ts +9 -3
- package/src/device/lock.ts +9 -3
- package/src/device/meter.ts +6 -2
- package/src/device/meterplus.ts +6 -2
- package/src/device/motion.ts +6 -2
- package/src/device/plug.ts +9 -3
- package/src/device/striplight.ts +9 -3
- package/src/platform.ts +69 -92
- package/.github/npm-version-script.js +0 -81
package/src/device/meterplus.ts
CHANGED
|
@@ -176,7 +176,9 @@ export class MeterPlus {
|
|
|
176
176
|
* Parse the device status from the SwitchBot api
|
|
177
177
|
*/
|
|
178
178
|
async parseStatus(): Promise<void> {
|
|
179
|
-
if (this.
|
|
179
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
180
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
181
|
+
} else if (this.BLE) {
|
|
180
182
|
await this.BLEparseStatus();
|
|
181
183
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
182
184
|
await this.openAPIparseStatus();
|
|
@@ -231,7 +233,9 @@ export class MeterPlus {
|
|
|
231
233
|
* Asks the SwitchBot API for the latest device information
|
|
232
234
|
*/
|
|
233
235
|
async refreshStatus(): Promise<void> {
|
|
234
|
-
if (this.
|
|
236
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
237
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
238
|
+
} else if (this.BLE) {
|
|
235
239
|
await this.BLERefreshStatus();
|
|
236
240
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
237
241
|
await this.openAPIRefreshStatus();
|
package/src/device/motion.ts
CHANGED
|
@@ -142,7 +142,9 @@ export class Motion {
|
|
|
142
142
|
* Parse the device status from the SwitchBot api
|
|
143
143
|
*/
|
|
144
144
|
async parseStatus(): Promise<void> {
|
|
145
|
-
if (this.
|
|
145
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
146
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
147
|
+
} else if (this.BLE) {
|
|
146
148
|
await this.BLEparseStatus();
|
|
147
149
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
148
150
|
await this.openAPIparseStatus();
|
|
@@ -213,7 +215,9 @@ export class Motion {
|
|
|
213
215
|
* Asks the SwitchBot API for the latest device information
|
|
214
216
|
*/
|
|
215
217
|
async refreshStatus(): Promise<void> {
|
|
216
|
-
if (this.
|
|
218
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
219
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
220
|
+
} else if (this.BLE) {
|
|
217
221
|
await this.BLERefreshStatus();
|
|
218
222
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
219
223
|
await this.openAPIRefreshStatus();
|
package/src/device/plug.ts
CHANGED
|
@@ -125,7 +125,9 @@ export class Plug {
|
|
|
125
125
|
* Parse the device status from the SwitchBot api
|
|
126
126
|
*/
|
|
127
127
|
async parseStatus(): Promise<void> {
|
|
128
|
-
if (this.
|
|
128
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
129
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
130
|
+
} else if (this.BLE) {
|
|
129
131
|
await this.BLEparseStatus();
|
|
130
132
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
131
133
|
await this.openAPIparseStatus();
|
|
@@ -164,7 +166,9 @@ export class Plug {
|
|
|
164
166
|
* Asks the SwitchBot API for the latest device information
|
|
165
167
|
*/
|
|
166
168
|
async refreshStatus(): Promise<void> {
|
|
167
|
-
if (this.
|
|
169
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
170
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
171
|
+
} else if (this.BLE) {
|
|
168
172
|
await this.BLERefreshStatus();
|
|
169
173
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
170
174
|
await this.openAPIRefreshStatus();
|
|
@@ -308,7 +312,9 @@ export class Plug {
|
|
|
308
312
|
*/
|
|
309
313
|
|
|
310
314
|
async pushChanges(): Promise<void> {
|
|
311
|
-
if (this.
|
|
315
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
316
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
|
|
317
|
+
} else if (this.BLE) {
|
|
312
318
|
await this.BLEpushChanges();
|
|
313
319
|
} else if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
314
320
|
await this.openAPIpushChanges();
|
package/src/device/striplight.ts
CHANGED
|
@@ -212,7 +212,9 @@ export class StripLight {
|
|
|
212
212
|
* Parse the device status from the SwitchBot api
|
|
213
213
|
*/
|
|
214
214
|
async parseStatus(): Promise<void> {
|
|
215
|
-
|
|
215
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
216
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} parseStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
217
|
+
} else/*if (this.BLE) {
|
|
216
218
|
await this.BLEparseStatus();
|
|
217
219
|
} else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
218
220
|
await this.openAPIparseStatus();
|
|
@@ -276,7 +278,9 @@ export class StripLight {
|
|
|
276
278
|
* Asks the SwitchBot API for the latest device information
|
|
277
279
|
*/
|
|
278
280
|
async refreshStatus(): Promise<void> {
|
|
279
|
-
|
|
281
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
282
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} refreshStatus enableCloudService: ${this.device.enableCloudService}`);
|
|
283
|
+
} else/*if (this.BLE) {
|
|
280
284
|
await this.BLERefreshStatus();
|
|
281
285
|
} else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
282
286
|
await this.openAPIRefreshStatus();
|
|
@@ -423,7 +427,9 @@ export class StripLight {
|
|
|
423
427
|
*
|
|
424
428
|
*/
|
|
425
429
|
async pushChanges(): Promise<void> {
|
|
426
|
-
|
|
430
|
+
if (!this.device.enableCloudService && this.OpenAPI) {
|
|
431
|
+
this.errorLog(`${this.device.deviceType}: ${this.accessory.displayName} pushChanges enableCloudService: ${this.device.enableCloudService}`);
|
|
432
|
+
} else /*if (this.BLE) {
|
|
427
433
|
await this.BLEpushChanges();
|
|
428
434
|
} else*/ if (this.OpenAPI && this.platform.config.credentials?.token) {
|
|
429
435
|
await this.openAPIpushChanges();
|
package/src/platform.ts
CHANGED
|
@@ -42,7 +42,6 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
42
42
|
public readonly accessories: PlatformAccessory[] = [];
|
|
43
43
|
|
|
44
44
|
version = require('../package.json').version || '1.12.8'; // eslint-disable-line @typescript-eslint/no-var-requires
|
|
45
|
-
deviceStatus!: any;
|
|
46
45
|
debugMode!: boolean;
|
|
47
46
|
platformLogging?: string;
|
|
48
47
|
|
|
@@ -281,117 +280,95 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
281
280
|
let rawData = '';
|
|
282
281
|
res.on('data', (d) => {
|
|
283
282
|
rawData += d;
|
|
284
|
-
this.deviceStatus = d;
|
|
285
283
|
this.debugLog(`d: ${d}`);
|
|
286
284
|
});
|
|
287
285
|
res.on('end', () => {
|
|
288
286
|
try {
|
|
289
287
|
const devicesAPI = JSON.parse(rawData);
|
|
290
288
|
this.debugLog(`devicesAPI: ${superStringify(devicesAPI.body)}`);
|
|
291
|
-
|
|
292
289
|
// SwitchBot Devices
|
|
293
|
-
|
|
290
|
+
if (devicesAPI.body.deviceList.length !== 0) {
|
|
294
291
|
this.infoLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
|
|
295
292
|
} else {
|
|
296
293
|
this.debugLog(`Total SwitchBot Devices Found: ${devicesAPI.body.deviceList.length}`);
|
|
297
|
-
}
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
this.infoLog(`Total SwitchBot Devices Found: ${deviceLists.length}`);
|
|
307
|
-
}
|
|
308
|
-
if (!this.config.options?.devices) {
|
|
309
|
-
this.debugLog(`SwitchBot Device Config Not Set: ${superStringify(this.config.options?.devices)}`);
|
|
310
|
-
const devices = deviceLists.map((v: any) => v);
|
|
311
|
-
for (const device of devices) {
|
|
312
|
-
if (device.deviceType) {
|
|
313
|
-
if (device.configDeviceName) {
|
|
314
|
-
device.deviceName = device.configDeviceName;
|
|
315
|
-
}
|
|
316
|
-
this.createDevice(device);
|
|
294
|
+
}
|
|
295
|
+
const deviceLists = devicesAPI.body.deviceList;
|
|
296
|
+
if (!this.config.options?.devices) {
|
|
297
|
+
this.debugLog(`SwitchBot Device Config Not Set: ${JSON.stringify(this.config.options?.devices)}`);
|
|
298
|
+
const devices = deviceLists.map((v: any) => v);
|
|
299
|
+
for (const device of devices) {
|
|
300
|
+
if (device.deviceType) {
|
|
301
|
+
if (device.configDeviceName) {
|
|
302
|
+
device.deviceName = device.configDeviceName;
|
|
317
303
|
}
|
|
304
|
+
this.createDevice(device);
|
|
318
305
|
}
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
this.createDevice(device);
|
|
306
|
+
}
|
|
307
|
+
} else if (this.config.credentials?.token && this.config.options.devices) {
|
|
308
|
+
this.debugLog(`SwitchBot Device Config Set: ${superStringify(this.config.options?.devices)}`);
|
|
309
|
+
const deviceConfigs = this.config.options?.devices;
|
|
310
|
+
|
|
311
|
+
const mergeBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
|
|
312
|
+
a1.map((itm: { deviceId: string }) => ({
|
|
313
|
+
...a2.find(
|
|
314
|
+
(item: { deviceId: string }) =>
|
|
315
|
+
item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item,
|
|
316
|
+
),
|
|
317
|
+
...itm,
|
|
318
|
+
}));
|
|
319
|
+
|
|
320
|
+
const devices = mergeBydeviceId(deviceLists, deviceConfigs);
|
|
321
|
+
this.debugLog(`SwitchBot Devices: ${superStringify(devices)}`);
|
|
322
|
+
for (const device of devices) {
|
|
323
|
+
if (device.deviceType) {
|
|
324
|
+
if (device.configDeviceName) {
|
|
325
|
+
device.deviceName = device.configDeviceName;
|
|
340
326
|
}
|
|
327
|
+
this.createDevice(device);
|
|
341
328
|
}
|
|
342
|
-
} else {
|
|
343
|
-
this.errorLog('SwitchBot Token Supplied, Issue with Auth.');
|
|
344
329
|
}
|
|
345
330
|
} else {
|
|
346
|
-
this.errorLog(
|
|
331
|
+
this.errorLog('SwitchBot Token Supplied, Issue with Auth.');
|
|
347
332
|
}
|
|
333
|
+
|
|
348
334
|
// IR Devices
|
|
349
|
-
|
|
335
|
+
if (devicesAPI.body.infraredRemoteList.length !== 0) {
|
|
350
336
|
this.infoLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
|
|
351
337
|
} else {
|
|
352
338
|
this.debugLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
const devices = irDeviceLists.map((v: any) => v);
|
|
367
|
-
for (const device of devices) {
|
|
368
|
-
if (device.remoteType) {
|
|
369
|
-
this.createIRDevice(device);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
} else {
|
|
373
|
-
this.debugLog(`IR Device Config Set: ${superStringify(this.config.options?.irdevices)}`);
|
|
374
|
-
const irDeviceConfig = this.config.options?.irdevices;
|
|
375
|
-
|
|
376
|
-
const mergeIRBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
|
|
377
|
-
a1.map((itm: { deviceId: string }) => ({
|
|
378
|
-
...a2.find(
|
|
379
|
-
(item: { deviceId: string }) =>
|
|
380
|
-
item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item,
|
|
381
|
-
),
|
|
382
|
-
...itm,
|
|
383
|
-
}));
|
|
384
|
-
|
|
385
|
-
const devices = mergeIRBydeviceId(irDeviceLists, irDeviceConfig);
|
|
386
|
-
this.debugLog(`IR Devices: ${superStringify(devices)}`);
|
|
387
|
-
for (const device of devices) {
|
|
388
|
-
if (device.remoteType) {
|
|
389
|
-
this.createIRDevice(device);
|
|
390
|
-
}
|
|
339
|
+
}
|
|
340
|
+
if (devicesAPI.body.infraredRemoteList.length !== 0) {
|
|
341
|
+
this.infoLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
|
|
342
|
+
} else {
|
|
343
|
+
this.debugLog(`Total IR Devices Found: ${devicesAPI.body.infraredRemoteList.length}`);
|
|
344
|
+
}
|
|
345
|
+
const irDeviceLists = devicesAPI.body.infraredRemoteList;
|
|
346
|
+
if (!this.config.options?.irdevices) {
|
|
347
|
+
this.debugLog(`IR Device Config Not Set: ${JSON.stringify(this.config.options?.irdevices)}`);
|
|
348
|
+
const devices = irDeviceLists.map((v: any) => v);
|
|
349
|
+
for (const device of devices) {
|
|
350
|
+
if (device.remoteType) {
|
|
351
|
+
this.createIRDevice(device);
|
|
391
352
|
}
|
|
392
353
|
}
|
|
393
354
|
} else {
|
|
394
|
-
this.
|
|
355
|
+
this.debugLog(`IR Device Config Set: ${superStringify(this.config.options?.irdevices)}`);
|
|
356
|
+
const irDeviceConfig = this.config.options?.irdevices;
|
|
357
|
+
|
|
358
|
+
const mergeIRBydeviceId = (a1: { deviceId: string }[], a2: any[]) =>
|
|
359
|
+
a1.map((itm: { deviceId: string }) => ({
|
|
360
|
+
...a2.find(
|
|
361
|
+
(item: { deviceId: string }) =>
|
|
362
|
+
item.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') === itm.deviceId.toUpperCase().replace(/[^A-Z0-9]+/g, '') && item,
|
|
363
|
+
),
|
|
364
|
+
...itm,
|
|
365
|
+
}));
|
|
366
|
+
|
|
367
|
+
const devices = mergeIRBydeviceId(irDeviceLists, irDeviceConfig);
|
|
368
|
+
this.debugLog(`IR Devices: ${superStringify(devices)}`);
|
|
369
|
+
for (const device of devices) {
|
|
370
|
+
this.createIRDevice(device);
|
|
371
|
+
}
|
|
395
372
|
}
|
|
396
373
|
} catch (e:any) {
|
|
397
374
|
this.errorLog(`API Request: ${e}, Submit Bugs Here: ` + 'https://tinyurl.com/SwitchBotBug');
|
|
@@ -497,7 +474,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
497
474
|
}
|
|
498
475
|
|
|
499
476
|
private createIRDevice(device: irdevice & devicesConfig) {
|
|
500
|
-
switch (device.remoteType
|
|
477
|
+
switch (device.remoteType) {
|
|
501
478
|
case 'TV':
|
|
502
479
|
case 'DIY TV':
|
|
503
480
|
case 'Projector':
|
|
@@ -1776,7 +1753,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
1776
1753
|
} else if (device.connectionType === 'BLE') {
|
|
1777
1754
|
// BLE: Curtains
|
|
1778
1755
|
registerCurtain = true;
|
|
1779
|
-
this.debugLog(`deviceName: ${device.deviceName} [Curtain Config] connectionType: ${device.connectionType}`);
|
|
1756
|
+
this.debugLog(`deviceName: ${device.deviceName} [Curtain Config] connectionType: ${device.connectionType}, group: ${device.group}`);
|
|
1780
1757
|
this.debugWarnLog(`Device: ${device.deviceName} registerCurtains: ${registerCurtain}`);
|
|
1781
1758
|
} else {
|
|
1782
1759
|
registerCurtain = false;
|
|
@@ -1800,7 +1777,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
1800
1777
|
async registerDevice(device: device & devicesConfig) {
|
|
1801
1778
|
device.connectionType = await this.connectionType(device);
|
|
1802
1779
|
let registerDevice: boolean;
|
|
1803
|
-
if (!device.hide_device && device.
|
|
1780
|
+
if (!device.hide_device && device.connectionType === 'BLE/OpenAPI') {
|
|
1804
1781
|
if (device.deviceType === 'Curtain') {
|
|
1805
1782
|
registerDevice = await this.registerCurtains(device);
|
|
1806
1783
|
this.debugWarnLog(`Device: ${device.deviceName} Curtain registerDevice: ${registerDevice}`);
|
|
@@ -1819,7 +1796,7 @@ export class SwitchBotPlatform implements DynamicPlatformPlugin {
|
|
|
1819
1796
|
this.debugWarnLog(`Device: ${device.deviceName} registerDevice: ${registerDevice}`);
|
|
1820
1797
|
}
|
|
1821
1798
|
this.debugWarnLog(`Device: ${device.deviceName} connectionType: ${device.connectionType}, will display in HomeKit`);
|
|
1822
|
-
} else if (!device.hide_device && device.
|
|
1799
|
+
} else if (!device.hide_device && device.connectionType === 'OpenAPI') {
|
|
1823
1800
|
if (device.deviceType === 'Curtain') {
|
|
1824
1801
|
registerDevice = await this.registerCurtains(device);
|
|
1825
1802
|
this.debugWarnLog(`Device: ${device.deviceName} Curtain registerDevice: ${registerDevice}`);
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
#!/bin/env node
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* This scripts queries the npm registry to pull out the latest version for a given tag.
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const fs = require("fs");
|
|
8
|
-
const semver = require("semver");
|
|
9
|
-
const child_process = require("child_process");
|
|
10
|
-
const assert = require("assert");
|
|
11
|
-
|
|
12
|
-
const BRANCH_VERSION_PATTERN = /^([A-Za-z]*)-(\d+.\d+.\d+)$/
|
|
13
|
-
|
|
14
|
-
// Load the contents of the package.json file
|
|
15
|
-
const packageJSON = JSON.parse(fs.readFileSync("package.json", "utf8"));
|
|
16
|
-
|
|
17
|
-
let refArgument = process.argv[2];
|
|
18
|
-
let tagArgument = process.argv[3] || "latest";
|
|
19
|
-
|
|
20
|
-
if (refArgument == null) {
|
|
21
|
-
console.error("ref argument is missing");
|
|
22
|
-
console.error("Usage: npm-version-script.js <ref> [tag]");
|
|
23
|
-
process.exit(1);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Queries the NPM registry for the latest version for the provided tag.
|
|
28
|
-
* @param tag The tag to query for.
|
|
29
|
-
* @returns {string} Returns the version.
|
|
30
|
-
*/
|
|
31
|
-
function getTagVersionFromNpm(tag) {
|
|
32
|
-
try {
|
|
33
|
-
return child_process.execSync(`npm info ${packageJSON.name} version --tag="${tag}"`).toString("utf8").trim();
|
|
34
|
-
} catch (e) {
|
|
35
|
-
throw e;
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function desiredTargetVersion(ref) {
|
|
40
|
-
// ref is a GitHub action ref string
|
|
41
|
-
if (ref.startsWith("refs/pull/")) {
|
|
42
|
-
throw Error("The version script was executed inside a PR!");
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
assert(ref.startsWith("refs/heads/"))
|
|
46
|
-
let branchName = ref.slice("refs/heads/".length);
|
|
47
|
-
|
|
48
|
-
let results = branchName.match(BRANCH_VERSION_PATTERN);
|
|
49
|
-
if (results != null) {
|
|
50
|
-
if (results[1] !== tagArgument) {
|
|
51
|
-
console.warn(`The base branch name (${results[1]}) differs from the tag name ${tagArgument}`);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return results[2];
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// legacy mode were we use the `betaVersion` property in the package.json
|
|
58
|
-
if (branchName === "beta" && packageJSON.betaVersion) {
|
|
59
|
-
return packageJSON.betaVersion
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
throw new Error("Malformed branch name for ref: " + ref + ". Can't derive the base version. Use a branch name like: beta-x.x.x!");
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
// derive the base version from the branch ref
|
|
66
|
-
const baseVersion = desiredTargetVersion(refArgument);
|
|
67
|
-
|
|
68
|
-
// query the npm registry for the latest version of the provided tag name
|
|
69
|
-
const latestReleasedVersion = getTagVersionFromNpm(tagArgument); // e.g. 0.7.0-beta.12
|
|
70
|
-
const latestReleaseBase = semver.inc(latestReleasedVersion, "patch"); // will produce 0.7.0 (removing the preid, needed for the equality check below)
|
|
71
|
-
|
|
72
|
-
let publishTag;
|
|
73
|
-
if (semver.eq(baseVersion, latestReleaseBase)) { // check if we are releasing another version for the latest beta
|
|
74
|
-
publishTag = latestReleasedVersion; // set the current latest beta to be incremented
|
|
75
|
-
} else {
|
|
76
|
-
publishTag = baseVersion; // start of with a new beta version
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
// save the package.json
|
|
80
|
-
packageJSON.version = publishTag;
|
|
81
|
-
fs.writeFileSync("package.json", JSON.stringify(packageJSON, null, 2));
|