@@ -27,9 +27,13 @@ const repoFiles = {
27
27
'.github/workflows/release-please.yml' : './release-please.yml' ,
28
28
}
29
29
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
+ }
33
37
34
38
const filesToDelete = [
35
39
// remove any eslint config files that aren't local to the project
@@ -43,10 +47,25 @@ const defaultConfig = {
43
47
windowsCI : true ,
44
48
}
45
49
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 ) => {
47
66
for ( let [ target , source ] of Object . entries ( files ) ) {
67
+ target = findReplace ( join ( targetDir , target ) , replacements )
48
68
source = join ( contentDir , source )
49
- target = join ( targetDir , target )
50
69
// if the target is a subdirectory of the path, mkdirp it first
51
70
if ( dirname ( target ) !== targetDir ) {
52
71
await fs . mkdir ( dirname ( target ) , {
@@ -55,7 +74,7 @@ const copyFiles = async (targetDir, files) => {
55
74
force : true ,
56
75
} )
57
76
}
58
- await fs . copyFile ( source , target , { owner : 'inherit' } )
77
+ await copyFile ( source , target , replacements )
59
78
}
60
79
}
61
80
@@ -94,34 +113,20 @@ const copyContent = async (path, rootPath, config) => {
94
113
95
114
// only workspace now
96
115
97
- // TODO: await copyFiles(path, workspaceFiles )
116
+ // TODO: await copyFiles(path, workspaceModuleFiles )
98
117
// if we ever have workspace specific files
99
118
119
+ // transform and copy all workspace repo files
100
120
if ( config . applyWorkspaceRepoFiles ) {
101
- // copy and edit workspace repo file (ci github action)
102
121
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 ) ,
111
126
} )
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 ( / % % p k g p a t h % % / g, relPath )
120
- workflowData = workflowData . replace ( / % % p k g n a m e % % / g, workspaceName )
121
-
122
- await fs . writeFile ( join ( workflowPath , workspaceFile ) , workflowData )
123
127
}
124
128
}
129
+
125
130
copyContent . moduleFiles = moduleFiles
126
131
copyContent . repoFiles = repoFiles
127
132
0 commit comments