|
1 | 1 | import * as path from 'path'; |
2 | | -import * as fs from 'fs'; |
3 | 2 | import * as logSymbols from 'log-symbols'; |
4 | 3 |
|
5 | 4 | import { sh } from './sh'; |
6 | 5 |
|
7 | | -/** |
8 | | - * Rewrites paths in package.json to be relative to the dist directory. |
9 | | - * e.g. "./dist/index.esm.js" becomes "./index.esm.js" |
10 | | - */ |
11 | | -function rewritePackageJsonForDist(packageJsonContent: string): string { |
12 | | - return packageJsonContent.replace(/"\.\/(dist)\//g, '"./'); |
13 | | -} |
14 | | - |
15 | 6 | export async function packLocalPackage(rootDir: string, tempDir: string, packageName: string) { |
16 | 7 | const packagePath = path.resolve(rootDir, 'packages', packageName.split('/')[1]); |
17 | | - const distPath = path.resolve(packagePath, 'dist'); |
18 | | - const distPathExists = !!(await fs.promises.stat(distPath).catch(() => false)); |
19 | 8 |
|
20 | | - if (!distPathExists) { |
21 | | - throw new Error(`A directory with artifacts (${distPath}) does not exist`); |
| 9 | + if (!packagePath) { |
| 10 | + throw new Error(`A package directory (${packageName}) does not exist`); |
22 | 11 | } |
23 | 12 |
|
24 | | - // Copy package.json into dist with paths rewritten to be relative to dist. |
25 | | - // The old @nx/rollup:rollup executor did this automatically; with plain rollup we do it manually. |
26 | | - const packageJsonContent = await fs.promises.readFile(path.resolve(packagePath, 'package.json'), 'utf-8'); |
27 | | - await fs.promises.writeFile(path.resolve(distPath, 'package.json'), rewritePackageJsonForDist(packageJsonContent)); |
28 | | - |
29 | 13 | // Use `npm pack` because `yarn pack` incorrectly calculates the included files when the |
30 | 14 | // files to include/exclude are specified by .npmignore rather than package.json `files`. |
31 | 15 | // (--quiet outputs only the .tgz filename, not all the included files) |
32 | | - const packFile = (await sh(`npm pack --quiet ${distPath}`, tempDir, true)).trim(); |
| 16 | + const packFile = (await sh(`npm pack --quiet ${packagePath}`, tempDir, true)).trim(); |
33 | 17 | console.log(logSymbols.success, `Package "${packageName}" was packed`); |
34 | 18 |
|
35 | | - // Clean up the temporary package.json in dist |
36 | | - await fs.promises.unlink(path.resolve(distPath, 'package.json')); |
37 | | - |
38 | 19 | return { |
39 | 20 | packageName, |
40 | 21 | file: packFile, |
|
0 commit comments