Skip to content

Commit

Permalink
refactor: use @manypkg/get-packages
Browse files Browse the repository at this point in the history
  • Loading branch information
balazsorban44 committed Oct 2, 2023
1 parent b3f4068 commit 28d19ac
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 43 deletions.
1 change: 1 addition & 0 deletions packages/monorepo-release/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"dependencies": {
"@commitlint/parse": "17.7.0",
"@manypkg/get-packages": "^2.2.0",
"git-log-parser": "1.2.0",
"semver": "7.5.4",
"stream-to-array": "2.3.0",
Expand Down
41 changes: 12 additions & 29 deletions packages/monorepo-release/src/analyze.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,18 @@
import type { Commit, GrouppedCommits, PackageToRelease } from "./types.js"
import type { Config } from "./config.js"
import { bold } from "yoctocolors"
import { log, pkgJson, execSync } from "./utils.js"
import { log, execSync } from "./utils.js"
import semver from "semver"
import * as commitlint from "@commitlint/parse"
import gitLog from "git-log-parser"
import streamToArray from "stream-to-array"
import fs from "node:fs"
import path from "node:path"

async function getPackages(rootDir: string, workspaceDirs: string[]) {
const packages: Record<string, string> = {}

for await (const workspaceDir of workspaceDirs) {
const packagesPath = path.join(rootDir, workspaceDir)
const packageDirs = fs.readdirSync(packagesPath)
for await (const packageDir of packageDirs) {
const packagePath = path.join(workspaceDir, packageDir)
const packageJSON = await pkgJson.read(packagePath).catch(log.error)
if (packageJSON && !packageJSON.private) {
packages[packageJSON.name] = packagePath
}
}
}

return packages
}
import { getPackages } from "@manypkg/get-packages"

export async function analyze(config: Config): Promise<PackageToRelease[]> {
const { BREAKING_COMMIT_MSG, RELEASE_COMMIT_MSG, RELEASE_COMMIT_TYPES } =
config

const packages = await getPackages(process.cwd(), config.packageDirectories)
const packageList = Object.values(packages)
const packageList = await getPackages(process.cwd())

log.info("Identifying latest tag...")
const latestTag = execSync("git describe --tags --abbrev=0", {
Expand Down Expand Up @@ -98,8 +78,8 @@ export async function analyze(config: Config): Promise<PackageToRelease[]> {
}
const packageCommits = commitsSinceLatestTag.filter(({ commit }) => {
const changedFiles = getChangedFiles(commit.short)
return packageList.some((packageFolder) =>
changedFiles.some((changedFile) => changedFile.startsWith(packageFolder)),
return packageList.packages.some(({ relativeDir }) =>
changedFiles.some((changedFile) => changedFile.startsWith(relativeDir)),
)
})

Expand All @@ -114,10 +94,11 @@ export async function analyze(config: Config): Promise<PackageToRelease[]> {
(acc, commit) => {
const changedFilesInCommit = getChangedFiles(commit.commit.short)

for (const [pkg, src] of Object.entries(packages)) {
for (const { relativeDir, packageJson } of packageList.packages) {
const { name: pkg } = packageJson
if (
changedFilesInCommit.some((changedFile) =>
changedFile.startsWith(src),
changedFile.startsWith(relativeDir),
)
) {
if (!(pkg in acc)) {
Expand Down Expand Up @@ -169,7 +150,9 @@ export async function analyze(config: Config): Promise<PackageToRelease[]> {
? "minor" // x.1.x
: "patch" // x.x.1

const packageJson = await pkgJson.read(packages[pkgName])
const { packageJson, relativeDir } = packageList.packages.find(
(pkg) => pkg.packageJson.name === pkgName,
)!
const oldVersion = packageJson.version!
const newSemVer = semver.parse(semver.inc(oldVersion, releaseType))!

Expand All @@ -178,7 +161,7 @@ export async function analyze(config: Config): Promise<PackageToRelease[]> {
oldVersion,
newVersion: `${newSemVer.major}.${newSemVer.minor}.${newSemVer.patch}`,
commits,
path: packages[pkgName],
relativeDir,
})
}

Expand Down
3 changes: 0 additions & 3 deletions packages/monorepo-release/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { pkgJson } from "./utils.js"

export interface Config {
releaseBranches: string[]
packageDirectories: string[]
BREAKING_COMMIT_MSG: string
RELEASE_COMMIT_MSG: string
RELEASE_COMMIT_TYPES: string[]
Expand All @@ -15,8 +14,6 @@ const json = await pkgJson.read("./")

export const defaultConfig: Config = {
releaseBranches: ["main"],
// TODO: Read from pnpm-workspace.yaml
packageDirectories: ["packages"],
BREAKING_COMMIT_MSG: "BREAKING CHANGE:",
RELEASE_COMMIT_MSG: "chore(release): bump package version(s) [skip ci]",
RELEASE_COMMIT_TYPES: ["feat", "fix"],
Expand Down
8 changes: 4 additions & 4 deletions packages/monorepo-release/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ async function sortByDependency(pkgs: PackageToRelease[]) {
const pkgsWithDeps = new Map<string, string[]>()

for await (const pkg of pkgs) {
const { dependencies } = await pkgJson.read(pkg.path)
const { dependencies } = await pkgJson.read(pkg.relativeDir)
pkgsWithDeps.set(pkg.name, Object.keys(dependencies ?? {}))
}

Expand All @@ -31,7 +31,7 @@ export async function publish(packages: PackageToRelease[], options: Config) {
log.info(
`Writing version "${pkg.newVersion}" to package.json for package \`${pkg.name}\``,
)
await pkgJson.update(pkg.path, { version: pkg.newVersion })
await pkgJson.update(pkg.relativeDir, { version: pkg.newVersion })
log.info("package.json file has been written, publishing...")
}

Expand All @@ -44,11 +44,11 @@ export async function publish(packages: PackageToRelease[], options: Config) {
} else {
execSync(
"echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > .npmrc",
{ cwd: pkg.path },
{ cwd: pkg.relativeDir },
)
}

execSync(npmPublish, { cwd: pkg.path })
execSync(npmPublish, { cwd: pkg.relativeDir })
}

if (dryRun) {
Expand Down
8 changes: 1 addition & 7 deletions packages/monorepo-release/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ export interface Parsed {
raw: string
}

export interface Package {
name: string
srcDir: string
peerDependencies?: string[]
}

export interface BranchConfig {
prerelease: boolean
ghRelease: boolean
Expand All @@ -50,7 +44,7 @@ export interface PackageToRelease {
newVersion: string
oldVersion: string
commits: GrouppedCommits
path: string
relativeDir: string
}

export interface GrouppedCommits {
Expand Down
Loading

0 comments on commit 28d19ac

Please sign in to comment.