|
| 1 | +#!/usr/bin/env node |
| 2 | +// Updates the README.md dependency blocks from the Appodeal Wizard API. |
| 3 | +// API output is used verbatim; only the iOS Podfile target gets RN linking injected. |
| 4 | + |
| 5 | +import { readFile, writeFile } from 'node:fs/promises'; |
| 6 | +import { fileURLToPath } from 'node:url'; |
| 7 | +import { dirname, join } from 'node:path'; |
| 8 | + |
| 9 | +// API base URL — required, supplied via the APPODEAL_API_URL env var (set by the |
| 10 | +// GitHub Action / your shell). No fallback: fail loudly rather than hit a guessed host. |
| 11 | +const API = (() => { |
| 12 | + const raw = process.env.APPODEAL_API_URL; |
| 13 | + if (!raw) { |
| 14 | + throw new Error('APPODEAL_API_URL env var is required'); |
| 15 | + } |
| 16 | + return raw.replace(/\/+$/, ''); |
| 17 | +})(); |
| 18 | +const ROOT = join(dirname(fileURLToPath(import.meta.url)), '..'); |
| 19 | +const README = join(ROOT, 'README.md'); |
| 20 | + |
| 21 | +// Category codes returned by the /sdks endpoint. |
| 22 | +const CATEGORY_NETWORK = 2; |
| 23 | +const CATEGORY_SERVICE = 3; |
| 24 | + |
| 25 | +/** Read the single source-of-truth version from package.json. */ |
| 26 | +async function getVersion() { |
| 27 | + const pkg = JSON.parse(await readFile(join(ROOT, 'package.json'), 'utf8')); |
| 28 | + if (!pkg.version) throw new Error('package.json has no "version" field'); |
| 29 | + return pkg.version; |
| 30 | +} |
| 31 | + |
| 32 | +async function apiFetch(path, options = {}) { |
| 33 | + const res = await fetch(`${API}${path}`, { |
| 34 | + headers: { 'content-type': 'application/json', accept: '*/*' }, |
| 35 | + signal: AbortSignal.timeout(30_000), |
| 36 | + ...options, |
| 37 | + }); |
| 38 | + if (!res.ok) { |
| 39 | + throw new Error(`API ${path} -> HTTP ${res.status} ${res.statusText}`); |
| 40 | + } |
| 41 | + return res; |
| 42 | +} |
| 43 | + |
| 44 | +/** Collect every version id from a list of {versions:[{id}]} entries. */ |
| 45 | +function collectVersionIds(entries) { |
| 46 | + return (entries ?? []).flatMap((e) => (e.versions ?? []).map((v) => v.id)); |
| 47 | +} |
| 48 | + |
| 49 | +/** Run the 3-step recommended pipeline and return the rendered dependency text. */ |
| 50 | +async function fetchDependencyBlock(platform, version, lang) { |
| 51 | + // Step 1 — recommended mediations. |
| 52 | + const mediationsRes = await apiFetch( |
| 53 | + `/v4/${platform}/${version}/mediations?recommended=true` |
| 54 | + ); |
| 55 | + const mediations = collectVersionIds((await mediationsRes.json()).mediations); |
| 56 | + |
| 57 | + // Step 2 — recommended sdks for those mediations, split into networks / services. |
| 58 | + const sdksRes = await apiFetch(`/v4/${platform}/${version}/sdks?recommended=true`, { |
| 59 | + method: 'POST', |
| 60 | + body: JSON.stringify({ mediations, networks: [], services: [] }), |
| 61 | + }); |
| 62 | + const sdks = (await sdksRes.json()).sdks ?? []; |
| 63 | + const networks = collectVersionIds(sdks.filter((s) => s.category === CATEGORY_NETWORK)); |
| 64 | + const services = collectVersionIds(sdks.filter((s) => s.category === CATEGORY_SERVICE)); |
| 65 | + |
| 66 | + // Step 3 — render. iOS has no language suffix (always Ruby). |
| 67 | + const path = |
| 68 | + platform === 'ios' |
| 69 | + ? `/v4/${platform}/${version}/dependencies` |
| 70 | + : `/v4/${platform}/${version}/dependencies/${lang}`; |
| 71 | + const depsRes = await apiFetch(path, { |
| 72 | + method: 'POST', |
| 73 | + body: JSON.stringify({ mediations, networks, services }), |
| 74 | + }); |
| 75 | + return (await depsRes.text()).replace(/\t/g, ' '); |
| 76 | +} |
| 77 | + |
| 78 | +/** Wrap rendered code in a fenced Markdown block. */ |
| 79 | +function fenced(lang, body) { |
| 80 | + return `${lang}\n${body.trimEnd()}\n\`\`\``; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Inject the React Native linking lines into the iOS Podfile target. The Wizard renders |
| 85 | + * a plain native target (`target 'Sample' do ... end`); RN apps additionally need the |
| 86 | + * autolinking calls, otherwise the copied Podfile won't build. Everything else from the |
| 87 | + * API response is left untouched. |
| 88 | + */ |
| 89 | +function addReactNativeLinking(podfile) { |
| 90 | + const linking = [ |
| 91 | + '', |
| 92 | + ' use_modular_headers!', |
| 93 | + '', |
| 94 | + ' config = use_native_modules!', |
| 95 | + ' use_react_native!(:path => config[:reactNativePath])', |
| 96 | + ].join('\n'); |
| 97 | + // The target block is the last one in the response; insert before its closing `end`. |
| 98 | + const trimmed = podfile.trimEnd(); |
| 99 | + const patched = trimmed.replace(/\n[ \t]*end$/, `${linking}\nend`); |
| 100 | + if (patched === trimmed) { |
| 101 | + throw new Error('Could not locate the iOS Podfile target `end` to inject RN linking'); |
| 102 | + } |
| 103 | + return patched; |
| 104 | +} |
| 105 | + |
| 106 | +/** |
| 107 | + * Replace everything between the HTML-comment markers for `name` with `block`. |
| 108 | + * Markers live OUTSIDE the fenced code block so they stay invisible in rendered Markdown: |
| 109 | + * <!-- appodeal-deps:NAME:start ... --> ...block... <!-- appodeal-deps:NAME:end --> |
| 110 | + */ |
| 111 | +function replaceBetweenMarkers(readme, name, block) { |
| 112 | + const startRe = new RegExp(`<!--\\s*appodeal-deps:${name}:start\\b[^>]*-->`); |
| 113 | + const endRe = new RegExp(`<!--\\s*appodeal-deps:${name}:end\\s*-->`); |
| 114 | + const startMatch = readme.match(startRe); |
| 115 | + if (!startMatch) throw new Error(`Marker appodeal-deps:${name}:start not found in README.md`); |
| 116 | + const startIdx = startMatch.index + startMatch[0].length; |
| 117 | + |
| 118 | + const endMatch = readme.slice(startIdx).match(endRe); |
| 119 | + if (!endMatch) throw new Error(`Marker appodeal-deps:${name}:end not found after start in README.md`); |
| 120 | + const endIdx = startIdx + endMatch.index; |
| 121 | + |
| 122 | + return `${readme.slice(0, startIdx)}\n${block}\n${readme.slice(endIdx)}`; |
| 123 | +} |
| 124 | + |
| 125 | +async function main() { |
| 126 | + const version = await getVersion(); |
| 127 | + console.log(`Updating README dependency lists for Appodeal SDK ${version}`); |
| 128 | + |
| 129 | + // android uses kts (`kt`) verbatim; ios ignores the language, returns Ruby, and gets |
| 130 | + // the React Native linking injected into its target. |
| 131 | + const android = await fetchDependencyBlock('android', version, 'kt'); |
| 132 | + const ios = addReactNativeLinking(await fetchDependencyBlock('ios', version)); |
| 133 | + |
| 134 | + let readme = await readFile(README, 'utf8'); |
| 135 | + readme = replaceBetweenMarkers(readme, 'android', fenced('``` kotlin', android)); |
| 136 | + readme = replaceBetweenMarkers(readme, 'ios', fenced('```ruby', ios)); |
| 137 | + |
| 138 | + await writeFile(README, readme, 'utf8'); |
| 139 | + console.log('Done: README dependency blocks updated.'); |
| 140 | +} |
| 141 | + |
| 142 | +main().catch((err) => { |
| 143 | + console.error(`\n✖ ${err.message}`); |
| 144 | + process.exit(1); |
| 145 | +}); |
0 commit comments