|
| 1 | +import { existsSync } from 'node:fs'; |
| 2 | +import { cp, mkdir, readFile, readdir, rm, writeFile } from 'node:fs/promises'; |
| 3 | +import { dirname, extname, join, resolve } from 'node:path'; |
| 4 | +import { fileURLToPath } from 'node:url'; |
| 5 | + |
| 6 | +const __filename = fileURLToPath(import.meta.url); |
| 7 | +const __dirname = dirname(__filename); |
| 8 | +const ROOT = resolve(__dirname, '..'); |
| 9 | +const SRC_SDK = join(ROOT, 'src', 'sdk'); |
| 10 | +const DIST_SDK = join(ROOT, 'dist', 'jsr'); |
| 11 | + |
| 12 | +async function main(): Promise<void> { |
| 13 | + // Clean dist/sdk if it already exists |
| 14 | + if (existsSync(DIST_SDK)) { |
| 15 | + await rm(DIST_SDK, { recursive: true }); |
| 16 | + } |
| 17 | + await mkdir(DIST_SDK, { recursive: true }); |
| 18 | + |
| 19 | + await copyDir(SRC_SDK, DIST_SDK); |
| 20 | + |
| 21 | + for (const file of ['LICENSE', 'README.md']) { |
| 22 | + const src = join(ROOT, file); |
| 23 | + if (existsSync(src)) { |
| 24 | + await cp(src, join(DIST_SDK, file)); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + await processFiles(DIST_SDK); |
| 29 | + |
| 30 | + console.log('JSR package prepared in dist/jsr'); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Recursively copies a directory, skipping __tests__ subdirectories, UMD files and package.*.json files. |
| 35 | + */ |
| 36 | +async function copyDir(src: string, dest: string): Promise<void> { |
| 37 | + const entries = await readdir(src, { withFileTypes: true }); |
| 38 | + await mkdir(dest, { recursive: true }); |
| 39 | + |
| 40 | + for (const entry of entries) { |
| 41 | + if ( |
| 42 | + entry.name === '__tests__' || |
| 43 | + entry.name.includes('.umd') || |
| 44 | + entry.name.includes('package.') |
| 45 | + ) |
| 46 | + continue; |
| 47 | + |
| 48 | + const srcPath = join(src, entry.name); |
| 49 | + const destPath = join(dest, entry.name); |
| 50 | + |
| 51 | + if (entry.isDirectory()) { |
| 52 | + await copyDir(srcPath, destPath); |
| 53 | + } else { |
| 54 | + await cp(srcPath, destPath); |
| 55 | + } |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * Recursively processes all TypeScript files in a directory, applying |
| 61 | + * the required transformations for JSR compatibility. |
| 62 | + */ |
| 63 | +async function processFiles(dir: string): Promise<void> { |
| 64 | + const entries = await readdir(dir, { withFileTypes: true }); |
| 65 | + |
| 66 | + for (const entry of entries) { |
| 67 | + const fullPath = join(dir, entry.name); |
| 68 | + |
| 69 | + if (entry.isDirectory()) { |
| 70 | + await processFiles(fullPath); |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + // Only process TypeScript files (.ts, .tsx, .mts, .cts, etc.) |
| 75 | + if (!/\.[mc]?tsx?$/.test(entry.name)) continue; |
| 76 | + |
| 77 | + let content = await readFile(fullPath, 'utf-8'); |
| 78 | + |
| 79 | + // Remove `// @ts-ignore` comment lines (including optional trailing text) |
| 80 | + content = content.replace(/^\s*\/\/\s*@ts-ignore\b.*\r?\n/gm, ''); |
| 81 | + |
| 82 | + // Add file extensions to relative imports and exports |
| 83 | + content = addExtensions(content, fullPath); |
| 84 | + |
| 85 | + // Replace @vue/runtime-core → vue |
| 86 | + content = content.replace(/(['"])@vue\/runtime-core\1/g, '$1vue$1'); |
| 87 | + |
| 88 | + await writeFile(fullPath, content); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * Adds file extensions to relative import/export specifiers that lack them. |
| 94 | + * |
| 95 | + * Handles: |
| 96 | + * import { foo } from './bar' → './bar.ts' |
| 97 | + * export { foo } from './bar' → './bar.ts' |
| 98 | + * export type { Foo } from './bar' → './bar.ts' |
| 99 | + * import('./bar') → './bar.ts' |
| 100 | + */ |
| 101 | +function addExtensions(content: string, filePath: string): string { |
| 102 | + const dir = dirname(filePath); |
| 103 | + |
| 104 | + const replacer = (_match: string, prefix: string, specifier: string, suffix: string): string => { |
| 105 | + // Skip specifiers that already have a known file extension |
| 106 | + const ext = extname(specifier); |
| 107 | + if ( |
| 108 | + ext && |
| 109 | + ['.ts', '.tsx', '.mts', '.js', '.jsx', '.mjs', '.json', '.svelte', '.css'].includes(ext) |
| 110 | + ) { |
| 111 | + return `${prefix}${specifier}${suffix}`; |
| 112 | + } |
| 113 | + |
| 114 | + const resolved = resolveExtension(dir, specifier); |
| 115 | + if (resolved) { |
| 116 | + return `${prefix}${specifier}${resolved}${suffix}`; |
| 117 | + } |
| 118 | + |
| 119 | + return `${prefix}${specifier}${suffix}`; |
| 120 | + }; |
| 121 | + |
| 122 | + // Static imports/exports: from './foo' or from "../foo" |
| 123 | + content = content.replace(/(from\s+['"])(\.\.?\/[^'"]*?)(['"])/g, replacer); |
| 124 | + |
| 125 | + // Dynamic imports: import('./foo') |
| 126 | + content = content.replace(/(import\(\s*['"])(\.\.?\/[^'"]*?)(['"]\s*\))/g, replacer); |
| 127 | + |
| 128 | + return content; |
| 129 | +} |
| 130 | + |
| 131 | +/** |
| 132 | + * Resolves the file extension for a relative specifier by checking the |
| 133 | + * filesystem for matching files in the dist directory. |
| 134 | + * |
| 135 | + * Returns the extension string (e.g. '.ts') to append, or '' if no match found. |
| 136 | + */ |
| 137 | +function resolveExtension(dir: string, specifier: string): string { |
| 138 | + const base = resolve(dir, specifier); |
| 139 | + const extensions = ['.ts', '.tsx', '.mts']; |
| 140 | + |
| 141 | + // Direct file match: ./foo → ./foo.ts |
| 142 | + for (const ext of extensions) { |
| 143 | + if (existsSync(base + ext)) { |
| 144 | + return ext; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + // Directory index match: ./foo → ./foo/index.ts |
| 149 | + for (const ext of extensions) { |
| 150 | + if (existsSync(join(base, `index${ext}`))) { |
| 151 | + return `/index${ext}`; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + return ''; |
| 156 | +} |
| 157 | + |
| 158 | +main().catch((err) => { |
| 159 | + console.error('Failed to prepare JSR package:', err); |
| 160 | + process.exit(1); |
| 161 | +}); |
0 commit comments