Skip to content

Commit

Permalink
fix: update check-project to use semantic-release-monorepo again (#1537)
Browse files Browse the repository at this point in the history
Updates default config for semantic-release-monorepo
  • Loading branch information
achingbrain authored May 30, 2024
1 parent 00bebd4 commit aa65b02
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 69 deletions.
9 changes: 8 additions & 1 deletion src/align-versions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ const tasks = new Listr([
* @param {GlobalOptions & ReleaseOptions} ctx
*/
task: async (ctx) => {
if (!process.env.CI) {
console.info('⚠ This run was not triggered in a known CI environment, running in dry-run mode.') // eslint-disable-line no-console
return
}

const rootDir = process.cwd()
const workspaces = pkg.workspaces

Expand All @@ -46,6 +51,8 @@ const tasks = new Listr([
const manifestPath = path.join(packageDir, 'package.json')
const manifest = fs.readJSONSync(path.join(packageDir, 'package.json'))

console.info('check project', manifest.name)

for (const type of dependencyTypes) {
for (const [dep, version] of Object.entries(siblingVersions)) {
if (manifest[type] != null && manifest[type][dep] != null && manifest[type][dep] !== version) {
Expand All @@ -71,7 +78,7 @@ const tasks = new Listr([
}

if (!process.env.CI) {
console.info('CI env var is not set, not pushing to git') // eslint-disable-line no-console
// do not push to remote repo if in dry-run mode
return
}

Expand Down
2 changes: 1 addition & 1 deletion src/check-project/check-monorepo-readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ export async function checkMonorepoReadme (projectDir, repoUrl, webRoot, default
apiDocs = parseMarkdown(APIDOCS(pkg))
}

const structure = parseMarkdown(STRUCTURE(projectDir, projectDirs))
const structure = parseMarkdown(STRUCTURE(projectDir, projectDirs, webRoot))

readme.children = [
...header,
Expand Down
90 changes: 67 additions & 23 deletions src/check-project/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,23 @@ async function processMonorepo (projectDir, manifest, branchName, repoUrl, ciFil
const projectDirs = []
const webRoot = `${repoUrl}/tree/${branchName}`

const { releaseType } = await prompt.get({
properties: {
releaseType: {
description: 'Monorepo release type: semantic-release | release-please',
required: true,
conform: (value) => {
return ['semantic-release', 'release-please'].includes(value)
},
default: usesReleasePlease() ? 'release-please' : 'semantic-release'
}
}
})

if (releaseType !== 'release-please' && releaseType !== 'semantic-release') {
throw new Error('Invalid release type specified')
}

for (const subProjectDir of await getSubprojectDirectories(projectDir, workspaces)) {
const stat = await fs.stat(subProjectDir)

Expand All @@ -133,15 +150,30 @@ async function processMonorepo (projectDir, manifest, branchName, repoUrl, ciFil

console.info('Found monorepo project', pkg.name)

await processModule(subProjectDir, pkg, branchName, repoUrl, homePage, ciFile, manifest)
await processModule({
projectDir: subProjectDir,
manifest: pkg,
branchName,
repoUrl,
homePage,
ciFile,
rootManifest: manifest,
releaseType
})

projectDirs.push(subProjectDir)
}

await alignMonorepoProjectDependencies(projectDirs)
await configureMonorepoProjectReferences(projectDirs)

let proposedManifest = await monorepoManifest(manifest, repoUrl, repoUrl, branchName)
let proposedManifest = await monorepoManifest({
manifest,
repoUrl,
homePage: repoUrl,
branchName,
releaseType
})
proposedManifest = sortManifest(proposedManifest)

await ensureFileHasContents(projectDir, 'package.json', JSON.stringify(proposedManifest, null, 2))
Expand Down Expand Up @@ -324,7 +356,9 @@ function addReferences (deps, references, refs) {
* @param {string} ciFile
*/
async function processProject (projectDir, manifest, branchName, repoUrl, ciFile) {
await processModule(projectDir, manifest, branchName, repoUrl, repoUrl, ciFile)
const releaseType = 'semantic-release'

await processModule({ projectDir, manifest, branchName, repoUrl, homePage: repoUrl, ciFile, releaseType })
await checkBuildFiles(projectDir, branchName, repoUrl)
}

Expand All @@ -336,16 +370,32 @@ function isAegirProject (manifest) {
}

/**
*
* @param {string} projectDir
* @param {any} manifest
* @param {string} branchName
* @param {string} repoUrl
* @param {string} homePage
* @param {string} ciFile
* @param {any} [rootManifest]
* @typedef {object} ProcessModuleContext
* @property {string} projectDir
* @property {any} manifest
* @property {string} branchName
* @property {string} repoUrl
* @property {string} homePage
* @property {string} ciFile
* @property {any} [rootManifest]
* @property {"semantic-release" | "release-please"} releaseType
*/

/**
* @typedef {object} ProcessManifestContext
* @property {any} manifest
* @property {string} branchName
* @property {string} repoUrl
* @property {string} homePage
* @property {"semantic-release" | "release-please"} releaseType
*/
async function processModule (projectDir, manifest, branchName, repoUrl, homePage = repoUrl, ciFile, rootManifest) {

/**
* @param {ProcessModuleContext} context
*/
async function processModule (context) {
const { projectDir, manifest, branchName, repoUrl, homePage = repoUrl, ciFile, rootManifest, releaseType } = context

if (!isAegirProject(manifest) && manifest.name !== 'aegir') {
throw new Error(`"${projectDir}" is not an aegir project`)
}
Expand Down Expand Up @@ -412,29 +462,23 @@ async function processModule (projectDir, manifest, branchName, repoUrl, homePag

if (typescript) {
console.info('TypeScript project detected')
proposedManifest = await typescriptManifest(manifest, branchName, repoUrl, homePage)
proposedManifest = await typescriptManifest({ manifest, branchName, repoUrl, homePage, releaseType })
} else if (typedESM) {
console.info('Typed ESM project detected')
proposedManifest = await typedESMManifest(manifest, branchName, repoUrl, homePage)
proposedManifest = await typedESMManifest({ manifest, branchName, repoUrl, homePage, releaseType })
} else if (typedCJS) {
console.info('Typed CJS project detected')
proposedManifest = await typedCJSManifest(manifest, branchName, repoUrl, homePage)
proposedManifest = await typedCJSManifest({ manifest, branchName, repoUrl, homePage, releaseType })
} else if (untypedESM) {
console.info('Untyped ESM project detected')
proposedManifest = await untypedESMManifest(manifest, branchName, repoUrl, homePage)
proposedManifest = await untypedESMManifest({ manifest, branchName, repoUrl, homePage, releaseType })
} else if (untypedCJS) {
console.info('Untyped CJS project detected')
proposedManifest = await untypedCJSManifest(manifest, branchName, repoUrl, homePage)
proposedManifest = await untypedCJSManifest({ manifest, branchName, repoUrl, homePage, releaseType })
} else {
throw new Error('Cannot determine project type')
}

// remove release config from monorepo projects as multi-semantic-release
// wants it defined in the root manifest
if (rootManifest != null) {
proposedManifest.release = undefined
}

proposedManifest = sortManifest(proposedManifest)

await ensureFileHasContents(projectDir, 'package.json', JSON.stringify(proposedManifest, null, 2))
Expand Down
41 changes: 29 additions & 12 deletions src/check-project/manifests/monorepo.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
import { semanticReleaseConfig } from '../semantic-release-config.js'
import {
sortFields,
constructManifest
} from '../utils.js'

/**
* @param {any} manifest
* @param {string} repoUrl
* @param {string} homePage
* @param {string} branchName
* @param {import('../index.js').ProcessManifestContext} context
*/
export async function monorepoManifest (manifest, repoUrl, homePage, branchName) {
export async function monorepoManifest (context) {
const { manifest, repoUrl, homePage } = context

const scripts = {
...manifest.scripts
}

const devDependencies = manifest.devDependencies ?? {}

if (context.releaseType === 'semantic-release') {
scripts.release = 'run-s build npm:release docs'
scripts['npm:release'] = 'aegir run release'
scripts.docs = 'aegir docs'

delete manifest.release
devDependencies['npm-run-all'] = '^4.1.5'
}

if (context.releaseType === 'release-please') {
scripts.release = 'run-s build npm:release docs'
scripts['npm:release'] = 'aegir exec --bail false npm -- publish'
scripts['release:rc'] = 'aegir release-rc'
scripts.docs = 'aegir docs'

devDependencies['npm-run-all'] = '^4.1.5'
}

let proposedManifest = constructManifest(manifest, {
private: true,
release: (
Object.values(manifest.scripts ?? {})
.some(script => script.includes('semantic-release') || script.includes('aegir release'))
)
? semanticReleaseConfig(branchName)
: undefined
scripts
}, repoUrl, homePage)

const rest = {
Expand Down
25 changes: 19 additions & 6 deletions src/check-project/manifests/typed-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@ import {
const merge = mergeOptions.bind({ ignoreUndefined: true })

/**
* @param {any} manifest
* @param {string} branchName
* @param {string} repoUrl
* @param {string} [homePage]
* @param {import('../index.js').ProcessManifestContext} context
*/
export async function typedCJSManifest (manifest, branchName, repoUrl, homePage = repoUrl) {
export async function typedCJSManifest (context) {
const { manifest, branchName, repoUrl, homePage } = context
let release
const scripts = {
...manifest.scripts
}

if (context.releaseType === 'semantic-release') {
scripts.release = 'aegir release'
release = semanticReleaseConfig(branchName)
}

if (context.releaseType === 'release-please') {
delete scripts.release
}

let proposedManifest = constructManifest(manifest, {
main: 'src/index.js',
types: 'dist/src/index.d.ts',
Expand Down Expand Up @@ -41,7 +53,8 @@ export async function typedCJSManifest (manifest, branchName, repoUrl, homePage
project: true
}
}, manifest.eslintConfig),
release: (manifest.scripts?.release?.includes('semantic-release') || manifest.scripts?.release?.includes('aegir release')) ? semanticReleaseConfig(branchName) : undefined
scripts,
release
}, repoUrl, homePage)

const rest = {
Expand Down
25 changes: 19 additions & 6 deletions src/check-project/manifests/typed-esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,24 @@ import {
const merge = mergeOptions.bind({ ignoreUndefined: true })

/**
* @param {any} manifest
* @param {string} branchName
* @param {string} repoUrl
* @param {string} [homePage]
* @param {import('../index.js').ProcessManifestContext} context
*/
export async function typedESMManifest (manifest, branchName, repoUrl, homePage = repoUrl) {
export async function typedESMManifest (context) {
const { manifest, branchName, repoUrl, homePage } = context
let release
const scripts = {
...manifest.scripts
}

if (context.releaseType === 'semantic-release') {
scripts.release = 'aegir release'
release = semanticReleaseConfig(branchName)
}

if (context.releaseType === 'release-please') {
delete scripts.release
}

let proposedManifest = constructManifest(manifest, {
type: 'module',
types: './dist/src/index.d.ts',
Expand Down Expand Up @@ -55,7 +67,8 @@ export async function typedESMManifest (manifest, branchName, repoUrl, homePage
sourceType: 'module'
}
}, manifest.eslintConfig),
release: (manifest.scripts?.release?.includes('semantic-release') || manifest.scripts?.release?.includes('aegir release')) ? semanticReleaseConfig(branchName) : undefined
release,
scripts
}, repoUrl, homePage)

const rest = {
Expand Down
21 changes: 15 additions & 6 deletions src/check-project/manifests/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,21 @@ import {
const merge = mergeOptions.bind({ ignoreUndefined: true })

/**
* @param {any} manifest
* @param {string} branchName
* @param {string} repoUrl
* @param {string} [homePage]
* @param {import('../index.js').ProcessManifestContext} context
*/
export async function typescriptManifest (manifest, branchName, repoUrl, homePage = repoUrl) {
export async function typescriptManifest (context) {
const { manifest, branchName, repoUrl, homePage } = context
let release

if (context.releaseType === 'semantic-release') {
manifest.scripts.release = 'aegir release'
release = semanticReleaseConfig(branchName)
}

if (context.releaseType === 'release-please') {
delete manifest.scripts.release
}

let proposedManifest = constructManifest(manifest, {
type: 'module',
types: './dist/src/index.d.ts',
Expand All @@ -42,7 +51,7 @@ export async function typescriptManifest (manifest, branchName, repoUrl, homePag
sourceType: 'module'
}
}, manifest.eslintConfig),
release: (manifest.scripts?.release?.includes('semantic-release') || manifest.scripts?.release?.includes('aegir release')) ? semanticReleaseConfig(branchName) : undefined
release
}, repoUrl, homePage)

if (proposedManifest.exports != null && Object.keys(proposedManifest.exports).length > 1) {
Expand Down
25 changes: 19 additions & 6 deletions src/check-project/manifests/untyped-cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,24 @@ import {
const merge = mergeOptions.bind({ ignoreUndefined: true })

/**
* @param {any} manifest
* @param {string} branchName
* @param {string} repoUrl
* @param {string} [homePage]
* @param {import('../index.js').ProcessManifestContext} context
*/
export async function untypedCJSManifest (manifest, branchName, repoUrl, homePage = repoUrl) {
export async function untypedCJSManifest (context) {
const { manifest, branchName, repoUrl, homePage } = context
let release
const scripts = {
...manifest.scripts
}

if (context.releaseType === 'semantic-release') {
scripts.release = 'aegir release'
release = semanticReleaseConfig(branchName)
}

if (context.releaseType === 'release-please') {
delete scripts.release
}

let proposedManifest = constructManifest(manifest, {
main: 'src/index.js',
files: [
Expand All @@ -26,7 +38,8 @@ export async function untypedCJSManifest (manifest, branchName, repoUrl, homePag
project: true
}
}, manifest.eslintConfig),
release: (manifest.scripts?.release?.includes('semantic-release') || manifest.scripts?.release?.includes('aegir release')) ? semanticReleaseConfig(branchName) : undefined
release,
scripts
}, repoUrl, homePage)

const rest = {
Expand Down
Loading

0 comments on commit aa65b02

Please sign in to comment.