-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetro.js
More file actions
62 lines (56 loc) · 2.25 KB
/
Copy pathmetro.js
File metadata and controls
62 lines (56 loc) · 2.25 KB
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
// Optional Metro integration: regenerate protobuf code whenever Metro starts,
// so a fresh `metro start` always has up-to-date types without a separate step.
// For live regeneration while editing, run `proto:generate --watch` alongside.
//
// // metro.config.js
// const { getDefaultConfig } = require('@react-native/metro-config')
// const { withNitroProtobuf } = require('@klaappinc/react-native-nitro-protobuf/metro')
// module.exports = withNitroProtobuf(getDefaultConfig(__dirname), { protoDir: 'proto' })
//
// Codegen failures are reported as a warning and never crash Metro (the config
// is returned unchanged), so a transient schema error doesn't block the bundler.
const { execFileSync } = require('child_process')
const fs = require('fs')
const path = require('path')
/**
* @param {object} config A Metro config object.
* @param {object} [options]
* @param {string} [options.protoDir='proto']
* @param {string} [options.outDir]
* @param {string} [options.tsOut]
* @param {boolean} [options.bigint]
* @param {'string'|'number'} [options.enums]
* @param {boolean} [options.skipProtoc] Regenerate TS types + registry only.
* @param {string} [options.cwd=process.cwd()]
* @returns the same config (unchanged), after running codegen once.
*/
function withNitroProtobuf(config, options = {}) {
const cwd = options.cwd ?? process.cwd()
const protoDir = options.protoDir ?? 'proto'
if (!fs.existsSync(path.resolve(cwd, protoDir))) {
console.warn(
`[nitro-protobuf] metro: proto dir "${protoDir}" not found; skipping codegen.`
)
return config
}
const args = ['generate', '--protoDir', protoDir]
if (options.outDir) args.push('--outDir', options.outDir)
if (options.tsOut) args.push('--tsOut', options.tsOut)
if (options.bigint) args.push('--bigint')
if (options.enums) args.push('--enums', options.enums)
if (options.skipProtoc) args.push('--skipProtoc')
try {
execFileSync(
process.execPath,
[path.join(__dirname, 'scripts', 'generate-protos.mjs'), ...args],
{ cwd, stdio: 'inherit' }
)
} catch (e) {
console.warn(
'[nitro-protobuf] metro: codegen failed (continuing):',
e instanceof Error ? e.message : String(e)
)
}
return config
}
module.exports = { withNitroProtobuf }