|
| 1 | +import * as core from '@actions/core'; |
| 2 | +import { |
| 3 | + commitAndPush, |
| 4 | + createChangeset, |
| 5 | + getBranchName, |
| 6 | + getBumps, |
| 7 | + getChangedFiles, |
| 8 | + getChangesetFilename, |
| 9 | + listPackages, |
| 10 | +} from './renovateChangesets'; |
| 11 | +import { relative as relativePath, resolve as resolvePath } from 'path'; |
| 12 | + |
| 13 | +async function main() { |
| 14 | + core.info('Running Renovate Changesets'); |
| 15 | + |
| 16 | + const isMultipleWorkspaces = core.getBooleanInput('multiple-workspaces', { |
| 17 | + required: false, |
| 18 | + }); |
| 19 | + |
| 20 | + const branchName = await getBranchName(); |
| 21 | + |
| 22 | + if (!branchName.startsWith('renovate/')) { |
| 23 | + core.info('Not a renovate branch, skipping'); |
| 24 | + return; |
| 25 | + } |
| 26 | + |
| 27 | + const allPackages = await listPackages({ |
| 28 | + isMultipleWorkspaces, |
| 29 | + includeRoots: true, |
| 30 | + }); |
| 31 | + |
| 32 | + // Need to remove the topmost package if we're in a multi-workspace setup |
| 33 | + const packageList = isMultipleWorkspaces |
| 34 | + ? allPackages.filter(p => p.dir !== process.cwd()) |
| 35 | + : allPackages; |
| 36 | + |
| 37 | + const changedFiles = await getChangedFiles(); |
| 38 | + |
| 39 | + // Group file changes by workspace, and drop workspaces without changes |
| 40 | + const changedFilesByWorkspace = new Map<string, string[]>( |
| 41 | + packageList |
| 42 | + .filter(p => p.isRoot) |
| 43 | + .map(p => [ |
| 44 | + p.dir, |
| 45 | + changedFiles |
| 46 | + .filter(f => f.startsWith(p.relativeDir)) |
| 47 | + .map(f => relativePath(p.dir, f)), |
| 48 | + ]) |
| 49 | + .filter((workspaceChanges): workspaceChanges is [string, string[]] => { |
| 50 | + const [_, files] = workspaceChanges; |
| 51 | + return files.length > 0; |
| 52 | + }), |
| 53 | + ); |
| 54 | + |
| 55 | + // Check if those workspaces have changesets |
| 56 | + const changedWorkspacesWithChangeset = new Map<string, boolean>( |
| 57 | + Array.from(changedFilesByWorkspace.entries()).map(([workspace, files]) => [ |
| 58 | + workspace, |
| 59 | + files.some(f => f.startsWith('.changeset/')), |
| 60 | + ]), |
| 61 | + ); |
| 62 | + |
| 63 | + // If all packages have a changeset already then exit early. |
| 64 | + if ( |
| 65 | + !changedWorkspacesWithChangeset.size || |
| 66 | + Array.from(changedWorkspacesWithChangeset.values()).every(v => v) |
| 67 | + ) { |
| 68 | + core.info( |
| 69 | + 'No changesets to create, or all workspaces have changesets already', |
| 70 | + ); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // Get all package.jsons that were changed |
| 75 | + const changedPackageJsons = new Map< |
| 76 | + string, |
| 77 | + { |
| 78 | + path: string; |
| 79 | + localPath: string; |
| 80 | + packageJson: { name: string; version: string }; |
| 81 | + }[] |
| 82 | + >( |
| 83 | + Array.from(changedFilesByWorkspace.entries()) |
| 84 | + .map(([workspace, files]) => [ |
| 85 | + workspace, |
| 86 | + files.filter(f => f.endsWith('package.json')), |
| 87 | + ]) |
| 88 | + .filter((workspaceChanges): workspaceChanges is [string, string[]] => { |
| 89 | + const [_, files] = workspaceChanges; |
| 90 | + return files.length > 0; |
| 91 | + }) |
| 92 | + .map(([workspace, files]) => [ |
| 93 | + workspace, |
| 94 | + files.map(f => ({ |
| 95 | + path: f, |
| 96 | + localPath: relativePath(process.cwd(), resolvePath(workspace, f)), |
| 97 | + packageJson: require(resolvePath(workspace, f)), |
| 98 | + })), |
| 99 | + ]), |
| 100 | + ); |
| 101 | + |
| 102 | + if (!changedPackageJsons.size) { |
| 103 | + core.info('Seems that no package.jsons were changed in this PR'); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + // Get the bumps that happened in the last commit made by rennovate in the diff |
| 108 | + const bumps = await Promise.all( |
| 109 | + Array.from(changedPackageJsons.entries()).map( |
| 110 | + async ([workspace, packages]) => { |
| 111 | + const changes = await getBumps(packages.map(p => p.localPath)); |
| 112 | + |
| 113 | + return { |
| 114 | + workspace, |
| 115 | + packages, |
| 116 | + changes, |
| 117 | + }; |
| 118 | + }, |
| 119 | + ), |
| 120 | + ); |
| 121 | + |
| 122 | + const changesetFilename = await getChangesetFilename(); |
| 123 | + const changesetFiles: string[] = []; |
| 124 | + |
| 125 | + // Create a changeset for each of the workspaces in the right place |
| 126 | + for (const bump of bumps) { |
| 127 | + const changesetFilePath = resolvePath(bump.workspace, changesetFilename); |
| 128 | + changesetFiles.push(changesetFilePath); |
| 129 | + |
| 130 | + await createChangeset( |
| 131 | + changesetFilePath, |
| 132 | + bump.changes, |
| 133 | + bump.packages.map(p => p.packageJson.name), |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + // Commit and push all the changesets. |
| 138 | + await commitAndPush(changesetFiles); |
| 139 | +} |
| 140 | + |
| 141 | +main().catch(error => { |
| 142 | + core.error(error.stack); |
| 143 | + core.setFailed(String(error)); |
| 144 | + process.exit(1); |
| 145 | +}); |
0 commit comments