(name: string, target?: string, packageDir?: string)
| 7 | import { resolvePkg } from './resolvePkg' |
| 8 | |
| 9 | export async function getPackedPackage(name: string, target?: string, packageDir?: string): Promise<string | void> { |
| 10 | packageDir = |
| 11 | packageDir || (await resolvePkg(name, { basedir: process.cwd() })) || (await resolvePkg(name, { basedir: target })) |
| 12 | |
| 13 | if (!packageDir) { |
| 14 | const pkgPath = up({ cwd: target }) |
| 15 | if (pkgPath) { |
| 16 | const pkgJson = JSON.parse(readFileSync(pkgPath, { encoding: 'utf-8' })) |
| 17 | if (pkgJson.name === name) { |
| 18 | packageDir = path.dirname(pkgPath) |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | if (!packageDir && fs.existsSync(path.join(process.cwd(), 'package.json'))) { |
| 24 | packageDir = process.cwd() |
| 25 | } |
| 26 | |
| 27 | if (!packageDir) { |
| 28 | throw new Error(`Error in getPackage: Could not resolve package ${name} from ${process.cwd()} target ${target}`) |
| 29 | } |
| 30 | |
| 31 | const tmpDir = target ?? tempy.directory() // thanks Sindre |
| 32 | |
| 33 | const pkgFiles = await packlist({ path: packageDir }) |
| 34 | |
| 35 | // we compute the paths of the files that would get npm published |
| 36 | |
| 37 | // we copy each file that we found in pkg to a new destination |
| 38 | for (const file of pkgFiles) { |
| 39 | const src = path.join(packageDir, file) |
| 40 | const dest = path.join(tmpDir, file) |
| 41 | |
| 42 | await fs.promises.mkdir(path.dirname(dest), { recursive: true }) |
| 43 | await fs.promises.copyFile(src, dest) |
| 44 | } |
| 45 | |
| 46 | return path.join(tmpDir) |
| 47 | } |
no test coverage detected