|
| 1 | +// Packs the library (run `npm run build` first), installs it in a temporary folder and loads it like users do: |
| 2 | +// every export of require('twitter-api-v2') must also be importable by name from an ES module. |
| 3 | +import { execFileSync } from 'child_process'; |
| 4 | +import { mkdtempSync, rmSync, writeFileSync } from 'fs'; |
| 5 | +import { tmpdir } from 'os'; |
| 6 | +import { join } from 'path'; |
| 7 | + |
| 8 | +const check = ` |
| 9 | +import { createRequire } from 'module'; |
| 10 | +
|
| 11 | +const required = createRequire(import.meta.url)('twitter-api-v2'); |
| 12 | +const imported = await import('twitter-api-v2'); |
| 13 | +const missing = Object.keys(required).filter(name => !(name in imported)); |
| 14 | +
|
| 15 | +if (missing.length) { |
| 16 | + console.error('Exports that ES modules cannot import by name: ' + missing.join(', ')); |
| 17 | + process.exit(1); |
| 18 | +} |
| 19 | +console.log('All ' + Object.keys(required).length + ' exports load with require() and import, on Node ' + process.version); |
| 20 | +`; |
| 21 | + |
| 22 | +const folder = mkdtempSync(join(tmpdir(), 'twitter-api-v2-package-')); |
| 23 | + |
| 24 | +try { |
| 25 | + const [{ filename }] = JSON.parse(execFileSync('npm', ['pack', '--json', '--pack-destination', folder], { encoding: 'utf8' })); |
| 26 | + writeFileSync(join(folder, 'package.json'), '{ "private": true }'); |
| 27 | + execFileSync('npm', ['install', '--no-audit', '--no-fund', join(folder, filename)], { cwd: folder, stdio: 'inherit' }); |
| 28 | + writeFileSync(join(folder, 'check.mjs'), check); |
| 29 | + execFileSync(process.execPath, ['check.mjs'], { cwd: folder, stdio: 'inherit' }); |
| 30 | +} catch { |
| 31 | + process.exitCode = 1; |
| 32 | +} finally { |
| 33 | + rmSync(folder, { recursive: true, force: true }); |
| 34 | +} |
0 commit comments