|
| 1 | +import {cp, mkdir, readFile, rm, stat, writeFile} from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +const rootDir = process.cwd(); |
| 5 | +const distRoot = path.resolve(rootDir, "dist"); |
| 6 | +const jsrTypesRoot = path.resolve(rootDir, "generated", "jsr-types"); |
| 7 | +const outRoot = path.resolve(rootDir, "generated", "jsr-publish"); |
| 8 | +const outDistRoot = path.join(outRoot, "dist"); |
| 9 | + |
| 10 | +async function assertDirectory(dir, message) { |
| 11 | + try { |
| 12 | + const value = await stat(dir); |
| 13 | + if (!value.isDirectory()) { |
| 14 | + throw new Error(message); |
| 15 | + } |
| 16 | + } catch { |
| 17 | + throw new Error(message); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +async function main() { |
| 22 | + await assertDirectory(distRoot, "Build output is missing. Run `npm run build` first."); |
| 23 | + await assertDirectory(jsrTypesRoot, "JSR types are missing. Run `npm run build:jsr-types` first."); |
| 24 | + |
| 25 | + const pkg = JSON.parse(await readFile(path.resolve(rootDir, "package.json"), "utf8")); |
| 26 | + const imports = Object.fromEntries( |
| 27 | + Object.entries(pkg.dependencies ?? {}).map(([name, version]) => [name, `npm:${name}@${version}`]), |
| 28 | + ); |
| 29 | + |
| 30 | + await rm(outRoot, {recursive: true, force: true}); |
| 31 | + await mkdir(outRoot, {recursive: true}); |
| 32 | + |
| 33 | + await cp(distRoot, outDistRoot, { |
| 34 | + recursive: true, |
| 35 | + filter(source) { |
| 36 | + return !source.endsWith(".d.ts"); |
| 37 | + }, |
| 38 | + }); |
| 39 | + await cp(jsrTypesRoot, outDistRoot, {recursive: true}); |
| 40 | + await cp(path.resolve(rootDir, "README.md"), path.join(outRoot, "README.md")); |
| 41 | + await cp(path.resolve(rootDir, "LICENSE"), path.join(outRoot, "LICENSE")); |
| 42 | + |
| 43 | + await writeFile(path.join(outRoot, "index.js"), [ |
| 44 | + "/* @ts-self-types=\"./index.d.ts\" */", |
| 45 | + "", |
| 46 | + "export * from \"./dist/index.es.js\";", |
| 47 | + "", |
| 48 | + ].join("\n")); |
| 49 | + |
| 50 | + await writeFile(path.join(outRoot, "index.d.ts"), "export * from \"./dist/index\";\n"); |
| 51 | + |
| 52 | + const jsrConfig = { |
| 53 | + $schema: "https://jsr.io/schema/config-file.v1.json", |
| 54 | + name: pkg.name, |
| 55 | + version: pkg.version, |
| 56 | + imports, |
| 57 | + exports: { |
| 58 | + ".": "./index.js", |
| 59 | + }, |
| 60 | + publish: { |
| 61 | + include: [ |
| 62 | + "index.js", |
| 63 | + "index.d.ts", |
| 64 | + "dist/**/*.js", |
| 65 | + "dist/**/*.d.ts", |
| 66 | + "README.md", |
| 67 | + "LICENSE", |
| 68 | + ], |
| 69 | + exclude: [ |
| 70 | + "!**", |
| 71 | + ], |
| 72 | + }, |
| 73 | + }; |
| 74 | + |
| 75 | + await writeFile(path.join(outRoot, "jsr.json"), `${JSON.stringify(jsrConfig, null, 2)}\n`); |
| 76 | +} |
| 77 | + |
| 78 | +await main(); |
0 commit comments