|
| 1 | +import { afterEach, describe, expect, it } from 'vitest'; |
| 2 | +import fs from 'node:fs/promises'; |
| 3 | +import os from 'node:os'; |
| 4 | +import path from 'node:path'; |
| 5 | +import { syncPluginPackageJSON } from '../utils/plugin-package-json.js'; |
| 6 | + |
| 7 | +const tempDirs: string[] = []; |
| 8 | + |
| 9 | +const createTempDir = async () => { |
| 10 | + const dir = await fs.mkdtemp(path.join(os.tmpdir(), 'rozenite-plugin-')); |
| 11 | + tempDirs.push(dir); |
| 12 | + return dir; |
| 13 | +}; |
| 14 | + |
| 15 | +const writeJson = async (filePath: string, value: unknown) => { |
| 16 | + await fs.writeFile(filePath, JSON.stringify(value, null, 2) + '\n'); |
| 17 | +}; |
| 18 | + |
| 19 | +afterEach(async () => { |
| 20 | + await Promise.all( |
| 21 | + tempDirs |
| 22 | + .splice(0) |
| 23 | + .map((dir) => fs.rm(dir, { recursive: true, force: true })), |
| 24 | + ); |
| 25 | +}); |
| 26 | + |
| 27 | +describe('syncPluginPackageJSON', () => { |
| 28 | + it('preserves unknown exports while updating managed entries', async () => { |
| 29 | + const projectRoot = await createTempDir(); |
| 30 | + |
| 31 | + await writeJson(path.join(projectRoot, 'package.json'), { |
| 32 | + name: 'demo-plugin', |
| 33 | + type: 'module', |
| 34 | + main: './dist/react-native.cjs', |
| 35 | + module: './dist/react-native.js', |
| 36 | + types: './dist/react-native.d.ts', |
| 37 | + exports: { |
| 38 | + '.': { |
| 39 | + types: './dist/react-native.d.ts', |
| 40 | + import: './dist/react-native.js', |
| 41 | + require: './dist/react-native.cjs', |
| 42 | + }, |
| 43 | + './metro': { |
| 44 | + types: './dist/metro.d.ts', |
| 45 | + import: './dist/metro.js', |
| 46 | + require: './dist/metro.cjs', |
| 47 | + }, |
| 48 | + './custom': './src/custom.ts', |
| 49 | + }, |
| 50 | + }); |
| 51 | + |
| 52 | + await fs.writeFile( |
| 53 | + path.join(projectRoot, 'react-native.ts'), |
| 54 | + 'export {}\n', |
| 55 | + ); |
| 56 | + await fs.writeFile(path.join(projectRoot, 'metro.ts'), 'export {}\n'); |
| 57 | + |
| 58 | + const result = await syncPluginPackageJSON(projectRoot); |
| 59 | + const packageJson = JSON.parse( |
| 60 | + await fs.readFile(path.join(projectRoot, 'package.json'), 'utf8'), |
| 61 | + ); |
| 62 | + |
| 63 | + expect(result.updatedFields).toEqual([ |
| 64 | + 'main', |
| 65 | + 'module', |
| 66 | + 'types', |
| 67 | + 'exports', |
| 68 | + ]); |
| 69 | + expect(packageJson.main).toBe('./dist/react-native/index.cjs'); |
| 70 | + expect(packageJson.module).toBe('./dist/react-native/index.js'); |
| 71 | + expect(packageJson.types).toBe('./dist/react-native/index.d.ts'); |
| 72 | + expect(packageJson.exports).toEqual({ |
| 73 | + '.': { |
| 74 | + types: './dist/react-native/index.d.ts', |
| 75 | + import: './dist/react-native/index.js', |
| 76 | + require: './dist/react-native/index.cjs', |
| 77 | + }, |
| 78 | + './metro': { |
| 79 | + types: './dist/metro/index.d.ts', |
| 80 | + import: './dist/metro/index.js', |
| 81 | + require: './dist/metro/index.cjs', |
| 82 | + }, |
| 83 | + './custom': './src/custom.ts', |
| 84 | + './package.json': './package.json', |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + it('removes only the managed metro export when no metro target exists', async () => { |
| 89 | + const projectRoot = await createTempDir(); |
| 90 | + |
| 91 | + await writeJson(path.join(projectRoot, 'package.json'), { |
| 92 | + name: 'demo-plugin', |
| 93 | + type: 'module', |
| 94 | + main: './dist/react-native/index.cjs', |
| 95 | + module: './dist/react-native/index.js', |
| 96 | + types: './dist/react-native/index.d.ts', |
| 97 | + exports: { |
| 98 | + '.': { |
| 99 | + types: './dist/react-native/index.d.ts', |
| 100 | + import: './dist/react-native/index.js', |
| 101 | + require: './dist/react-native/index.cjs', |
| 102 | + }, |
| 103 | + './metro': { |
| 104 | + types: './dist/metro/index.d.ts', |
| 105 | + import: './dist/metro/index.js', |
| 106 | + require: './dist/metro/index.cjs', |
| 107 | + }, |
| 108 | + './custom': './src/custom.ts', |
| 109 | + }, |
| 110 | + }); |
| 111 | + |
| 112 | + await fs.writeFile( |
| 113 | + path.join(projectRoot, 'react-native.ts'), |
| 114 | + 'export {}\n', |
| 115 | + ); |
| 116 | + |
| 117 | + const result = await syncPluginPackageJSON(projectRoot); |
| 118 | + const packageJson = JSON.parse( |
| 119 | + await fs.readFile(path.join(projectRoot, 'package.json'), 'utf8'), |
| 120 | + ); |
| 121 | + |
| 122 | + expect(result.updatedFields).toEqual(['exports']); |
| 123 | + expect(packageJson.exports).toEqual({ |
| 124 | + '.': { |
| 125 | + types: './dist/react-native/index.d.ts', |
| 126 | + import: './dist/react-native/index.js', |
| 127 | + require: './dist/react-native/index.cjs', |
| 128 | + }, |
| 129 | + './custom': './src/custom.ts', |
| 130 | + './package.json': './package.json', |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + it('leaves root entry fields untouched when there is no react native target', async () => { |
| 135 | + const projectRoot = await createTempDir(); |
| 136 | + |
| 137 | + await writeJson(path.join(projectRoot, 'package.json'), { |
| 138 | + name: 'demo-plugin', |
| 139 | + type: 'module', |
| 140 | + main: './custom-main.cjs', |
| 141 | + module: './custom-module.js', |
| 142 | + types: './custom-types.d.ts', |
| 143 | + exports: { |
| 144 | + '.': './custom-entry.js', |
| 145 | + './metro': { |
| 146 | + types: './dist/metro/index.d.ts', |
| 147 | + import: './dist/metro/index.js', |
| 148 | + require: './dist/metro/index.cjs', |
| 149 | + }, |
| 150 | + './custom': './src/custom.ts', |
| 151 | + }, |
| 152 | + }); |
| 153 | + |
| 154 | + const result = await syncPluginPackageJSON(projectRoot); |
| 155 | + const packageJson = JSON.parse( |
| 156 | + await fs.readFile(path.join(projectRoot, 'package.json'), 'utf8'), |
| 157 | + ); |
| 158 | + |
| 159 | + expect(result.updatedFields).toEqual(['exports']); |
| 160 | + expect(packageJson.main).toBe('./custom-main.cjs'); |
| 161 | + expect(packageJson.module).toBe('./custom-module.js'); |
| 162 | + expect(packageJson.types).toBe('./custom-types.d.ts'); |
| 163 | + expect(packageJson.exports).toEqual({ |
| 164 | + '.': './custom-entry.js', |
| 165 | + './custom': './src/custom.ts', |
| 166 | + }); |
| 167 | + }); |
| 168 | +}); |
0 commit comments