|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | + |
| 4 | +const pkg = require('../package.json'); |
| 5 | + |
| 6 | +const BASE_PATH = path.resolve(__dirname, '..'); |
| 7 | +const SOURCE_ENTRY = path.join(BASE_PATH, pkg.main); |
| 8 | +const SOURCE_TYPES = path.join(BASE_PATH, 'index.d.ts'); |
| 9 | +const DESTINATION = 'cjs'; |
| 10 | +const DESTINATION_ENTRY = path.join(BASE_PATH, DESTINATION, 'index.cjs'); |
| 11 | +const DESTINATION_TYPES = path.join(BASE_PATH, DESTINATION, 'index.d.cts'); |
| 12 | + |
| 13 | +function getFilename(filename) { |
| 14 | + return filename.replace(`${BASE_PATH}/`, ''); |
| 15 | +} |
| 16 | + |
| 17 | +try { |
| 18 | + if (!fs.existsSync(path.join(BASE_PATH, DESTINATION))) { |
| 19 | + fs.mkdirSync(path.join(BASE_PATH, DESTINATION)); |
| 20 | + } |
| 21 | + |
| 22 | + fs.copyFileSync(SOURCE_ENTRY, DESTINATION_ENTRY); |
| 23 | + |
| 24 | + const contents = fs |
| 25 | + .readFileSync(DESTINATION_ENTRY, { encoding: 'utf8' }) |
| 26 | + .replace(/\/\/# sourceMappingURL=(.*)/, (match, value) => |
| 27 | + match.replace(value, 'index.cjs.map'), |
| 28 | + ); |
| 29 | + |
| 30 | + fs.writeFileSync(DESTINATION_ENTRY, contents, { encoding: 'utf8' }); |
| 31 | + |
| 32 | + console.log( |
| 33 | + `Copied ${getFilename(SOURCE_ENTRY)} to ${getFilename(DESTINATION_ENTRY)}`, |
| 34 | + ); |
| 35 | + |
| 36 | + const types = fs |
| 37 | + .readFileSync(SOURCE_TYPES, { encoding: 'utf8' }) |
| 38 | + .replace( |
| 39 | + /export default function memoize/, |
| 40 | + () => 'declare function memoize', |
| 41 | + ) |
| 42 | + .concat('\n\nexport = memoize;'); |
| 43 | + |
| 44 | + fs.writeFileSync(DESTINATION_TYPES, types, { encoding: 'utf8' }); |
| 45 | + |
| 46 | + console.log( |
| 47 | + `Copied ${getFilename(SOURCE_TYPES)} to ${getFilename(DESTINATION_TYPES)}`, |
| 48 | + ); |
| 49 | +} catch (error) { |
| 50 | + console.error(error); |
| 51 | + |
| 52 | + process.exit(1); |
| 53 | +} |
0 commit comments