Skip to content

Commit 0ef4b78

Browse files
committed
Replace postinstall script with util
1 parent 542bc96 commit 0ef4b78

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

cli/lib/data.js

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import {readFile} from 'node:fs/promises'
1+
import {readFile, access} from 'node:fs/promises'
2+
import {execFile as execFileCallback} from 'node:child_process'
3+
import {promisify} from 'node:util'
24
import {dirname, resolve} from 'node:path'
35
import {fileURLToPath} from 'node:url'
46
import {sdk} from '@radio4000/sdk'
57
import fuzzysort from 'fuzzysort'
68
import * as config from './config.js'
79
import {channelSchema, trackSchema} from './schema.js'
810

11+
const execFile = promisify(execFileCallback)
912
const __dirname = dirname(fileURLToPath(import.meta.url))
1013

1114
// V1 data paths
12-
const V1_CHANNELS_PATH = resolve(__dirname, '../../data/channels_v1.json')
13-
const V1_TRACKS_PATH = resolve(__dirname, '../../data/tracks_v1.json')
15+
const dataDir = resolve(__dirname, '../../data')
16+
const tarballPath = resolve(dataDir, 'data_v1.tar.gz')
17+
const channelsPath = resolve(dataDir, 'channels_v1.json')
18+
const tracksPath = resolve(dataDir, 'tracks_v1.json')
1419

1520
// Cache for v1 data
1621
let v1ChannelsCache = null
@@ -90,9 +95,19 @@ const rejectV1Mutation = (type, id) => {
9095

9196
// ===== V1 DATA LOADERS =====
9297

98+
async function ensureV1DataExtracted() {
99+
try {
100+
await access(channelsPath)
101+
await access(tracksPath)
102+
} catch {
103+
await execFile('tar', ['xzf', tarballPath, '-C', dataDir])
104+
}
105+
}
106+
93107
export async function loadV1Channels() {
94108
if (v1ChannelsCache) return v1ChannelsCache
95-
const content = await readFile(V1_CHANNELS_PATH, 'utf-8')
109+
await ensureV1DataExtracted()
110+
const content = await readFile(channelsPath, 'utf-8')
96111
v1ChannelsCache = JSON.parse(content).map((item) =>
97112
channelSchema.parse({...item, source: 'v1'})
98113
)
@@ -101,7 +116,8 @@ export async function loadV1Channels() {
101116

102117
export async function loadV1Tracks() {
103118
if (v1TracksCache) return v1TracksCache
104-
const content = await readFile(V1_TRACKS_PATH, 'utf-8')
119+
await ensureV1DataExtracted()
120+
const content = await readFile(tracksPath, 'utf-8')
105121
v1TracksCache = JSON.parse(content)
106122
.map((item) => {
107123
try {

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
"main": "cli/main.js",
77
"scripts": {
88
"check": "biome check --write",
9-
"test": "bun test --only-failures",
10-
"postinstall": "tar xzf data/data_v1.tar.gz -C data"
9+
"test": "bun test --only-failures"
1110
},
1211
"repository": {
1312
"type": "git",

0 commit comments

Comments
 (0)