File size: 1,804 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env node
// @ts-check

const { spawn } = require('child_process')

;(async function () {
  const target = process.argv[process.argv.length - 1]

  let turboResult = ''
  const turboCommand = `pnpm dlx turbo@${process.env.TURBO_VERSION || 'latest'}`

  await new Promise((resolve, reject) => {
    const child = spawn(
      '/bin/bash',
      ['-c', `${turboCommand} run cache-build-native --dry=json -- ${target}`],
      {
        stdio: 'pipe',
      }
    )

    child.stderr.on('data', (data) => {
      process.stderr.write(data)
    })

    child.stdout.on('data', (data) => {
      process.stdout.write(data)
      turboResult += data.toString()
    })

    child.on('exit', (code, signal) => {
      if (code || signal) {
        return reject(
          new Error(`invalid exit code ${code} or signal ${signal}`)
        )
      }
      resolve(0)
    })
  })

  const turboData = JSON.parse(turboResult)

  const task = turboData.tasks.find((t) => t.command !== '<NONEXISTENT>')

  if (!task) {
    console.warn(`Failed to find related turbo task`, turboResult)
    return
  }

  // pull cache if it was available
  if (task.cache.local || task.cache.remote) {
    console.log('Cache Status', task.taskId, task.hash, task.cache)
    await new Promise((resolve, reject) => {
      const child = spawn(
        '/bin/bash',
        ['-c', `${turboCommand} run cache-build-native -- ${target}`],
        {
          stdio: 'inherit',
        }
      )
      child.on('exit', (code, signal) => {
        if (code || signal) {
          return reject(
            new Error(`invalid exit code ${code} or signal ${signal}`)
          )
        }
        resolve(0)
      })
    })
  } else {
    console.warn(`No turbo cache was available, continuing...`)
    console.warn(task)
  }
})()