75 lines
2.1 KiB
JavaScript
Executable file
75 lines
2.1 KiB
JavaScript
Executable file
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
|
|
function parseBoolean(value, defaultValue) {
|
|
const normalized = String(value ?? '').trim().toLowerCase()
|
|
if (!normalized) return defaultValue
|
|
if (['1', 'true', 'yes', 'on', 'enabled'].includes(normalized)) return true
|
|
if (['0', 'false', 'no', 'off', 'disabled'].includes(normalized)) return false
|
|
return defaultValue
|
|
}
|
|
|
|
function parseArgs(argv) {
|
|
const options = {
|
|
out: '',
|
|
fallbackEnabled: null,
|
|
superTokensReady: null,
|
|
}
|
|
for (let i = 2; i < argv.length; i += 1) {
|
|
const token = String(argv[i] || '').trim()
|
|
if (token === '--out' && argv[i + 1]) {
|
|
options.out = String(argv[i + 1]).trim()
|
|
i += 1
|
|
continue
|
|
}
|
|
if (token === '--fallback-enabled' && argv[i + 1]) {
|
|
options.fallbackEnabled = parseBoolean(argv[i + 1], true)
|
|
i += 1
|
|
continue
|
|
}
|
|
if (token === '--supertokens-ready' && argv[i + 1]) {
|
|
options.superTokensReady = parseBoolean(argv[i + 1], false)
|
|
i += 1
|
|
continue
|
|
}
|
|
}
|
|
return options
|
|
}
|
|
|
|
function buildRuntimeConfig(options = {}) {
|
|
const fallbackEnabled = options.fallbackEnabled !== null
|
|
? options.fallbackEnabled
|
|
: parseBoolean(process.env.VITE_AUTH_FALLBACK_ENABLED, true)
|
|
const superTokensReady = options.superTokensReady !== null
|
|
? options.superTokensReady
|
|
: parseBoolean(process.env.VITE_AUTH_SUPERTOKENS_READY, false)
|
|
|
|
return {
|
|
auth: {
|
|
source: 'ui_runtime_config',
|
|
fallback: {
|
|
enabled: fallbackEnabled,
|
|
},
|
|
supertokens: {
|
|
ready: superTokensReady,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
function writeRuntimeConfig(outPath, config) {
|
|
const resolved = path.resolve(process.cwd(), outPath)
|
|
fs.mkdirSync(path.dirname(resolved), { recursive: true })
|
|
fs.writeFileSync(resolved, `${JSON.stringify(config, null, 2)}\n`, 'utf8')
|
|
return resolved
|
|
}
|
|
|
|
function main(argv = process.argv) {
|
|
const options = parseArgs(argv)
|
|
const config = buildRuntimeConfig(options)
|
|
const outputPath = writeRuntimeConfig(options.out || 'public/config.json', config)
|
|
process.stdout.write(`[runtime-config] wrote ${outputPath}\n`)
|
|
}
|
|
|
|
main()
|
|
|