diff options
| author | adamdotdevin <[email protected]> | 2025-07-22 11:50:51 -0500 |
|---|---|---|
| committer | adamdotdevin <[email protected]> | 2025-07-22 11:50:51 -0500 |
| commit | 10c8b495907069461f5dc464f6285321290c8b14 (patch) | |
| tree | 9fed07910dd7d99688c0261a364d810a500e21d3 /packages/sdk/scripts/utils/postprocess-files.cjs | |
| parent | 500cea5ce7fa635a924cd9abea63aaf672f7645d (diff) | |
| download | opencode-10c8b495907069461f5dc464f6285321290c8b14.tar.gz opencode-10c8b495907069461f5dc464f6285321290c8b14.zip | |
chore: generate sdk into packages/sdk
Diffstat (limited to 'packages/sdk/scripts/utils/postprocess-files.cjs')
| -rw-r--r-- | packages/sdk/scripts/utils/postprocess-files.cjs | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/packages/sdk/scripts/utils/postprocess-files.cjs b/packages/sdk/scripts/utils/postprocess-files.cjs new file mode 100644 index 000000000..deae575e3 --- /dev/null +++ b/packages/sdk/scripts/utils/postprocess-files.cjs @@ -0,0 +1,94 @@ +// @ts-check +const fs = require('fs'); +const path = require('path'); + +const distDir = + process.env['DIST_PATH'] ? + path.resolve(process.env['DIST_PATH']) + : path.resolve(__dirname, '..', '..', 'dist'); + +async function* walk(dir) { + for await (const d of await fs.promises.opendir(dir)) { + const entry = path.join(dir, d.name); + if (d.isDirectory()) yield* walk(entry); + else if (d.isFile()) yield entry; + } +} + +async function postprocess() { + for await (const file of walk(distDir)) { + if (!/(\.d)?[cm]?ts$/.test(file)) continue; + + const code = await fs.promises.readFile(file, 'utf8'); + + // strip out lib="dom", types="node", and types="react" references; these + // are needed at build time, but would pollute the user's TS environment + const transformed = code.replace( + /^ *\/\/\/ *<reference +(lib="dom"|types="(node|react)").*?\n/gm, + // replace with same number of characters to avoid breaking source maps + (match) => ' '.repeat(match.length - 1) + '\n', + ); + + if (transformed !== code) { + console.error(`wrote ${path.relative(process.cwd(), file)}`); + await fs.promises.writeFile(file, transformed, 'utf8'); + } + } + + const newExports = { + '.': { + require: { + types: './index.d.ts', + default: './index.js', + }, + types: './index.d.mts', + default: './index.mjs', + }, + }; + + for (const entry of await fs.promises.readdir(distDir, { withFileTypes: true })) { + if (entry.isDirectory() && entry.name !== 'src' && entry.name !== 'internal' && entry.name !== 'bin') { + const subpath = './' + entry.name; + newExports[subpath + '/*.mjs'] = { + default: subpath + '/*.mjs', + }; + newExports[subpath + '/*.js'] = { + default: subpath + '/*.js', + }; + newExports[subpath + '/*'] = { + import: subpath + '/*.mjs', + require: subpath + '/*.js', + }; + } else if (entry.isFile() && /\.[cm]?js$/.test(entry.name)) { + const { name, ext } = path.parse(entry.name); + const subpathWithoutExt = './' + name; + const subpath = './' + entry.name; + newExports[subpathWithoutExt] ||= { import: undefined, require: undefined }; + const isModule = ext[1] === 'm'; + if (isModule) { + newExports[subpathWithoutExt].import = subpath; + } else { + newExports[subpathWithoutExt].require = subpath; + } + newExports[subpath] = { + default: subpath, + }; + } + } + await fs.promises.writeFile( + 'dist/package.json', + JSON.stringify( + Object.assign( + /** @type {Record<String, unknown>} */ ( + JSON.parse(await fs.promises.readFile('dist/package.json', 'utf-8')) + ), + { + exports: newExports, + }, + ), + null, + 2, + ), + ); +} +postprocess(); |
