Skip to content

Commit ad4be0d

Browse files
authored
feat: add release-please workflow to workspace repo files (#58)
1 parent 9a671ff commit ad4be0d

File tree

5 files changed

+68
-30
lines changed

5 files changed

+68
-30
lines changed

.github/workflows/release-please.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ jobs:
1414
- uses: google-github-actions/release-please-action@v2
1515
id: release
1616
with:
17-
package-name: conventional-test
1817
release-type: node
1918
# If you change changelog-types be sure to also update commitlintrc.js
2019
changelog-types: >
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# This file is automatically added by @npmcli/template-oss. Do not edit.
2+
3+
name: Node Workspace Release Please %%pkgname%%
4+
5+
on:
6+
push:
7+
paths:
8+
- %%pkgpath%%/**
9+
branches:
10+
- release-next
11+
- latest
12+
13+
jobs:
14+
release-please:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- uses: google-github-actions/release-please-action@v2
18+
id: release
19+
with:
20+
release-type: node
21+
monorepo-tags: true
22+
path: %%pkgpath%%/**
23+
# If you change changelog-types be sure to also update commitlintrc.js
24+
changelog-types: >
25+
[{"type":"feat","section":"Features","hidden":false},
26+
{"type":"fix","section":"Bug Fixes","hidden":false},
27+
{"type":"docs","section":"Documentation","hidden":false},
28+
{"type":"deps","section":"Dependencies","hidden":false},
29+
{"type":"chore","hidden":true}]

lib/content/release-please.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ jobs:
1414
- uses: google-github-actions/release-please-action@v2
1515
id: release
1616
with:
17-
package-name: conventional-test
1817
release-type: node
1918
# If you change changelog-types be sure to also update commitlintrc.js
2019
changelog-types: >

lib/postinstall/copy-content.js

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,13 @@ const repoFiles = {
2727
'.github/workflows/release-please.yml': './release-please.yml',
2828
}
2929

30-
// currently no workspace moduleFiles
31-
// const workspaceContent = {}
32-
// const workspaceRootContent = {}
30+
// currently no workspace module files
31+
// const workspaceModuleFiles = {}
32+
33+
const workspaceRepoFiles = {
34+
'.github/workflows/release-please-%%pkgfsname%%.yml': './release-please-workspace.yml',
35+
'.github/workflows/ci-%%pkgfsname%%.yml': './ci-workspace.yml',
36+
}
3337

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

46-
const copyFiles = async (targetDir, files) => {
50+
const findReplace = (str, replace = {}) => {
51+
for (const [f, r] of Object.entries(replace)) {
52+
str = str.replace(new RegExp(f, 'g'), r)
53+
}
54+
return str
55+
}
56+
57+
const copyFile = async (source, target, replacements) => {
58+
if (replacements) {
59+
const content = await fs.readFile(source, { encoding: 'utf-8' })
60+
return fs.writeFile(target, findReplace(content, replacements), { owner: 'inherit' })
61+
}
62+
return fs.copyFile(source, target, { owner: 'inherit' })
63+
}
64+
65+
const copyFiles = async (targetDir, files, replacements) => {
4766
for (let [target, source] of Object.entries(files)) {
67+
target = findReplace(join(targetDir, target), replacements)
4868
source = join(contentDir, source)
49-
target = join(targetDir, target)
5069
// if the target is a subdirectory of the path, mkdirp it first
5170
if (dirname(target) !== targetDir) {
5271
await fs.mkdir(dirname(target), {
@@ -55,7 +74,7 @@ const copyFiles = async (targetDir, files) => {
5574
force: true,
5675
})
5776
}
58-
await fs.copyFile(source, target, { owner: 'inherit' })
77+
await copyFile(source, target, replacements)
5978
}
6079
}
6180

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

95114
// only workspace now
96115

97-
// TODO: await copyFiles(path, workspaceFiles)
116+
// TODO: await copyFiles(path, workspaceModuleFiles)
98117
// if we ever have workspace specific files
99118

119+
// transform and copy all workspace repo files
100120
if (config.applyWorkspaceRepoFiles) {
101-
// copy and edit workspace repo file (ci github action)
102121
const workspacePkg = (await PackageJson.load(path)).content
103-
const workspaceName = workspacePkg.name
104-
let workspaceFile = `ci-${workspaceName.replace(/\//g, '-')}.yml`
105-
workspaceFile = workspaceFile.replace(/@/g, '')
106-
const workflowPath = join(rootPath, '.github', 'workflows')
107-
await fs.mkdir(workflowPath, {
108-
owner: 'inherit',
109-
recursive: true,
110-
force: true,
122+
await copyFiles(rootPath, workspaceRepoFiles, {
123+
'%%pkgname%%': workspacePkg.name,
124+
'%%pkgfsname%%': workspacePkg.name.replace(/\//g, '-').replace(/@/g, ''),
125+
'%%pkgpath%%': path.substring(rootPath.length + 1),
111126
})
112-
113-
let workflowData = await fs.readFile(
114-
join(contentDir, './ci-workspace.yml'),
115-
{ encoding: 'utf-8' }
116-
)
117-
118-
const relPath = path.substring(rootPath.length + 1)
119-
workflowData = workflowData.replace(/%%pkgpath%%/g, relPath)
120-
workflowData = workflowData.replace(/%%pkgname%%/g, workspaceName)
121-
122-
await fs.writeFile(join(workflowPath, workspaceFile), workflowData)
123127
}
124128
}
129+
125130
copyContent.moduleFiles = moduleFiles
126131
copyContent.repoFiles = repoFiles
127132

test/postinstall/copy-content.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,18 @@ t.test('handles workspaces', async (t) => {
9494
await t.resolves(fs.stat(join(root, '.eslintrc.js')))
9595
// should have made the workspace action in the root
9696
await t.resolves(fs.stat(join(root, '.github', 'workflows', 'ci-amazinga.yml')))
97+
await t.resolves(fs.stat(join(root, '.github', 'workflows', 'release-please-amazinga.yml')))
9798
await t.resolves(fs.stat(join(root, '.github', 'ISSUE_TEMPLATE', 'bug.yml')))
9899

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

102-
await t.resolves(fs.stat(join(root, '.github', 'workflows', 'ci-somenamespace-amazingb.yml')))
103+
const workspacebCi = join(root, '.github', 'workflows', 'ci-somenamespace-amazingb.yml')
104+
await t.resolves(fs.stat(workspacebCi))
105+
const workspacebCiContent = await fs.readFile(workspacebCi, { encoding: 'utf-8' })
106+
t.match(workspacebCiContent, '@somenamespace/amazingb')
107+
t.match(workspacebCiContent, join('workspace', 'b'))
108+
t.notMatch(workspacebCiContent, '%%')
103109
})
104110

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

0 commit comments

Comments
 (0)