Skip to content

Commit

Permalink
feat: add release-please workflow to workspace repo files
Browse files Browse the repository at this point in the history
  • Loading branch information
lukekarrys committed Feb 24, 2022
1 parent 508be00 commit 05628c6
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 29 deletions.
29 changes: 29 additions & 0 deletions lib/content/release-please-workspace.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# This file is automatically added by @npmcli/template-oss. Do not edit.

name: Node Workspace Release Please %%pkgname%%

on:
push:
paths:
- %%pkgpath%%/**
branches:
- release-next
- latest

jobs:
release-please:
runs-on: ubuntu-latest
steps:
- uses: google-github-actions/release-please-action@v2
id: release
with:
release-type: node
monorepo-tags: true
path: %%pkgpath%%/**
# If you change changelog-types be sure to also update commitlintrc.js
changelog-types: >
[{"type":"feat","section":"Features","hidden":false},
{"type":"fix","section":"Bug Fixes","hidden":false},
{"type":"docs","section":"Documentation","hidden":false},
{"type":"deps","section":"Dependencies","hidden":false},
{"type":"chore","hidden":true}]
1 change: 0 additions & 1 deletion lib/content/release-please.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ jobs:
- uses: google-github-actions/release-please-action@v2
id: release
with:
package-name: conventional-test
release-type: node
# If you change changelog-types be sure to also update commitlintrc.js
changelog-types: >
Expand Down
59 changes: 32 additions & 27 deletions lib/postinstall/copy-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,13 @@ const repoFiles = {
'.github/workflows/release-please.yml': './release-please.yml',
}

// currently no workspace moduleFiles
// const workspaceContent = {}
// const workspaceRootContent = {}
// currently no workspace module files
// const workspaceModuleFiles = {}

const workspaceRepoFiles = {
'.github/workflows/release-please-%%pkgfsname%%.yml': './release-please-workspace.yml',
'.github/workflows/ci-%%pkgfsname%%.yml': './ci-workspace.yml',
}

const filesToDelete = [
// remove any eslint config files that aren't local to the project
Expand All @@ -43,10 +47,25 @@ const defaultConfig = {
windowsCI: true,
}

const copyFiles = async (targetDir, files) => {
const findReplace = (str, replace = {}) => {
for (const [f, r] of Object.entries(replace)) {
str = str.replace(new RegExp(f, 'g'), r)
}
return str
}

const copyFile = async (source, target, replacements) => {
if (replacements) {
const content = await fs.readFile(source, { encoding: 'utf-8' })
return fs.writeFile(target, findReplace(content, replacements), { owner: 'inherit' })
}
return fs.copyFile(source, target, { owner: 'inherit' })
}

const copyFiles = async (targetDir, files, replacements) => {
for (let [target, source] of Object.entries(files)) {
target = findReplace(join(targetDir, target), replacements)
source = join(contentDir, source)
target = join(targetDir, target)
// if the target is a subdirectory of the path, mkdirp it first
if (dirname(target) !== targetDir) {
await fs.mkdir(dirname(target), {
Expand All @@ -55,7 +74,7 @@ const copyFiles = async (targetDir, files) => {
force: true,
})
}
await fs.copyFile(source, target, { owner: 'inherit' })
await copyFile(source, target, replacements)
}
}

Expand Down Expand Up @@ -94,34 +113,20 @@ const copyContent = async (path, rootPath, config) => {

// only workspace now

// TODO: await copyFiles(path, workspaceFiles)
// TODO: await copyFiles(path, workspaceModuleFiles)
// if we ever have workspace specific files

// transform and copy all workspace repo files
if (config.applyWorkspaceRepoFiles) {
// copy and edit workspace repo file (ci github action)
const workspacePkg = (await PackageJson.load(path)).content
const workspaceName = workspacePkg.name
let workspaceFile = `ci-${workspaceName.replace(/\//g, '-')}.yml`
workspaceFile = workspaceFile.replace(/@/g, '')
const workflowPath = join(rootPath, '.github', 'workflows')
await fs.mkdir(workflowPath, {
owner: 'inherit',
recursive: true,
force: true,
await copyFiles(rootPath, workspaceRepoFiles, {
'%%pkgname%%': workspacePkg.name,
'%%pkgfsname%%': workspacePkg.name.replace(/\//g, '-').replace(/@/g, ''),
'%%pkgpath%%': path.substring(rootPath.length + 1),
})

let workflowData = await fs.readFile(
join(contentDir, './ci-workspace.yml'),
{ encoding: 'utf-8' }
)

const relPath = path.substring(rootPath.length + 1)
workflowData = workflowData.replace(/%%pkgpath%%/g, relPath)
workflowData = workflowData.replace(/%%pkgname%%/g, workspaceName)

await fs.writeFile(join(workflowPath, workspaceFile), workflowData)
}
}

copyContent.moduleFiles = moduleFiles
copyContent.repoFiles = repoFiles

Expand Down
8 changes: 7 additions & 1 deletion test/postinstall/copy-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,18 @@ t.test('handles workspaces', async (t) => {
await t.resolves(fs.stat(join(root, '.eslintrc.js')))
// should have made the workspace action in the root
await t.resolves(fs.stat(join(root, '.github', 'workflows', 'ci-amazinga.yml')))
await t.resolves(fs.stat(join(root, '.github', 'workflows', 'release-please-amazinga.yml')))
await t.resolves(fs.stat(join(root, '.github', 'ISSUE_TEMPLATE', 'bug.yml')))

const workspaceb = join(root, 'workspace', 'b')
await copyContent(workspaceb, root, config)

await t.resolves(fs.stat(join(root, '.github', 'workflows', 'ci-somenamespace-amazingb.yml')))
const workspacebCi = join(root, '.github', 'workflows', 'ci-somenamespace-amazingb.yml')
await t.resolves(fs.stat(workspacebCi))
const workspacebCiContent = await fs.readFile(workspacebCi, { encoding: 'utf-8' })
t.match(workspacebCiContent, '@somenamespace/amazingb')
t.match(workspacebCiContent, join('workspace', 'b'))
t.notMatch(workspacebCiContent, '%%')
})

t.test('handles workspaces with no root repo files', async (t) => {
Expand Down

0 comments on commit 05628c6

Please sign in to comment.