|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import { promisify } from 'node:util'; |
| 10 | +import path from 'node:path'; |
10 | 11 | import { exec, execSync } from 'node:child_process'; |
11 | 12 |
|
12 | 13 | const execPromise = promisify(exec); |
@@ -37,13 +38,70 @@ export const updateWorkspaceVersion = async (workspace: string, version: string) |
37 | 38 | return execPromise(`yarn workspace ${workspace} version ${version}`); |
38 | 39 | }; |
39 | 40 |
|
40 | | -export const execPublish = (workspace: string, tag: string, otp?: string) => { |
41 | | - if (!tag) { |
42 | | - throw new Error('Tag must be defined'); |
| 41 | +export interface YarnPackRawDetail { |
| 42 | + base?: string; |
| 43 | + location?: string; |
| 44 | + output?: string; |
| 45 | +} |
| 46 | + |
| 47 | +export interface YarnPackDetails { |
| 48 | + /** |
| 49 | + * An absolute base path to the workspace root directory |
| 50 | + */ |
| 51 | + base: string; |
| 52 | + /** |
| 53 | + * An array of absolute paths to files packed in the tgz archive |
| 54 | + */ |
| 55 | + files: string[]; |
| 56 | + /** |
| 57 | + * An absolute path to the output tgz archive |
| 58 | + */ |
| 59 | + output: string; |
| 60 | +} |
| 61 | + |
| 62 | +export const yarnPack = async (workspace: string)=> { |
| 63 | + const result = await execPromise(`yarn workspace ${workspace} pack --json`); |
| 64 | + const rawDetails = JSON.parse( |
| 65 | + `[${result.stdout.replace(/\n/g, ',').slice(0, -1)}]` |
| 66 | + ) as Array<YarnPackRawDetail>; |
| 67 | + const details: YarnPackDetails = { |
| 68 | + base: '', |
| 69 | + files: [], |
| 70 | + output: '', |
| 71 | + }; |
| 72 | + for (const rawDetail of rawDetails) { |
| 73 | + if (rawDetail.base) { |
| 74 | + details.base = rawDetail.base; |
| 75 | + } |
| 76 | + if (rawDetail.location) { |
| 77 | + details.files.push(rawDetail.location); |
| 78 | + } |
| 79 | + if (rawDetail.output) { |
| 80 | + details.output = rawDetail.output; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Validate the returned data |
| 85 | + if (!details.base) { |
| 86 | + throw new Error( |
| 87 | + 'yarn pack did not return the base path for the workspace. ' + |
| 88 | + 'This likely means that the command\'s JSON output changed format. ' + |
| 89 | + 'Please check the current yarn pack API and update the code ' |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + if (!details.output) { |
| 94 | + throw new Error( |
| 95 | + 'yarn pack did not return the path for the output tgz archive. ' + |
| 96 | + 'This likely means that the command\'s JSON output changed format. ' + |
| 97 | + 'Please check the current yarn pack API and update the code ' |
| 98 | + ); |
43 | 99 | } |
44 | 100 |
|
45 | | - const otpStr = otp ? `--otp ${otp}` : ''; |
46 | | - return execSync(`yarn workspace ${workspace} npm publish --access public --tag ${tag} ${otpStr}`, { stdio: 'inherit', encoding: 'utf8' }); |
| 101 | + // By default, the returned location property is a path relative |
| 102 | + // to the workspace root directory. We want absolute paths instead. |
| 103 | + details.files = details.files.map((file) => path.join(details.base, file)); |
| 104 | + return details; |
47 | 105 | }; |
48 | 106 |
|
49 | 107 | export const getAuthenticatedUser = async () => { |
|
0 commit comments