|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | + |
| 6 | +const fixtureName = (process.env.FIXTURE || '').trim(); |
| 7 | +const outputPath = path.join('src', 'pkjs', 'active-fixture.generated.js'); |
| 8 | + |
| 9 | +function fail(message) { |
| 10 | + console.error(message); |
| 11 | + process.exit(1); |
| 12 | +} |
| 13 | + |
| 14 | +function writeFixtureModule(value) { |
| 15 | + fs.writeFileSync( |
| 16 | + outputPath, |
| 17 | + 'module.exports = ' + JSON.stringify(value, null, 2) + ';\n' |
| 18 | + ); |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Fail unless a fixture time field is an integer in range. |
| 23 | + * |
| 24 | + * @param {string} pathLabel Human-readable fixture path. |
| 25 | + * @param {number} value Time field value. |
| 26 | + * @param {number} min Minimum allowed value. |
| 27 | + * @param {number} max Maximum allowed value. |
| 28 | + */ |
| 29 | +function assertIntInRange(pathLabel, value, min, max) { |
| 30 | + if (!Number.isInteger(value) || value < min || value > max) { |
| 31 | + fail(pathLabel + ' must be an integer from ' + min + '-' + max); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * Convert local fixture fields anchored to watch.now into Unix seconds. |
| 37 | + * |
| 38 | + * @param {Object} watchNow Fixture watch.now value. |
| 39 | + * @param {Object} overrides Local date/time overrides. |
| 40 | + * @returns {number} Unix seconds. |
| 41 | + */ |
| 42 | +function dateFromWatchNow(watchNow, overrides) { |
| 43 | + const date = new Date( |
| 44 | + watchNow.year, |
| 45 | + watchNow.month - 1, |
| 46 | + watchNow.day + (overrides.dayOffset || 0), |
| 47 | + overrides.hour || 0, |
| 48 | + overrides.minute || 0, |
| 49 | + overrides.second || 0, |
| 50 | + 0 |
| 51 | + ); |
| 52 | + |
| 53 | + return Math.floor(date.getTime() / 1000); |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Normalize readable fixture weather fields into the runtime shape. |
| 58 | + * |
| 59 | + * @param {Object} fixture Parsed fixture. |
| 60 | + */ |
| 61 | +function normalizeWeather(fixture) { |
| 62 | + const watchNow = fixture && fixture.watch && fixture.watch.now; |
| 63 | + const weather = fixture && fixture.weather; |
| 64 | + |
| 65 | + if (!watchNow || !weather) { |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + if (typeof weather.startHour === 'number') { |
| 70 | + assertIntInRange('weather.startHour', weather.startHour, 0, 23); |
| 71 | + weather.startEpoch = dateFromWatchNow(watchNow, { |
| 72 | + dayOffset: weather.startDayOffset || 0, |
| 73 | + hour: weather.startHour, |
| 74 | + minute: 0, |
| 75 | + second: 0, |
| 76 | + }); |
| 77 | + delete weather.startHour; |
| 78 | + delete weather.startDayOffset; |
| 79 | + } |
| 80 | + |
| 81 | + if (Array.isArray(weather.sunEvents)) { |
| 82 | + weather.sunEvents = weather.sunEvents.map((event) => { |
| 83 | + if (typeof event.epoch === 'number') { |
| 84 | + return event; |
| 85 | + } |
| 86 | + |
| 87 | + assertIntInRange('weather.sunEvents.hour', event.hour, 0, 23); |
| 88 | + assertIntInRange('weather.sunEvents.minute', event.minute || 0, 0, 59); |
| 89 | + return { |
| 90 | + type: event.type, |
| 91 | + epoch: dateFromWatchNow(watchNow, { |
| 92 | + dayOffset: event.dayOffset || 0, |
| 93 | + hour: event.hour, |
| 94 | + minute: event.minute || 0, |
| 95 | + second: event.second || 0, |
| 96 | + }), |
| 97 | + }; |
| 98 | + }); |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +if (fixtureName === '') { |
| 103 | + writeFixtureModule(null); |
| 104 | + process.exit(0); |
| 105 | +} |
| 106 | + |
| 107 | +if (!/^[a-z0-9][a-z0-9-]*$/.test(fixtureName)) { |
| 108 | + fail('FIXTURE must be a fixture slug like "readme" or "rainy-night"'); |
| 109 | +} |
| 110 | + |
| 111 | +const fixturePath = path.join('fixtures', fixtureName + '.json'); |
| 112 | +if (!fs.existsSync(fixturePath)) { |
| 113 | + fail('Fixture not found: ' + fixturePath); |
| 114 | +} |
| 115 | + |
| 116 | +const fixture = JSON.parse(fs.readFileSync(fixturePath, 'utf8')); |
| 117 | +fixture.name = fixtureName; |
| 118 | +normalizeWeather(fixture); |
| 119 | +writeFixtureModule(fixture); |
0 commit comments