-
Notifications
You must be signed in to change notification settings - Fork 17
/
copy-content.js
123 lines (105 loc) · 3.77 KB
/
copy-content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const { dirname, join, resolve } = require('path')
const fs = require('@npmcli/fs')
const PackageJson = require('@npmcli/package-json')
const contentDir = resolve(__dirname, '..', 'content')
// keys are destination paths in the target project
// values are paths to contents relative to '../content/'
const moduleFiles = {
'.eslintrc.js': './eslintrc.js',
'.gitignore': './gitignore',
'.npmrc': './npmrc',
'SECURITY.md': './SECURITY.md',
}
const repoFiles = {
'.commitlintrc.js': './commitlintrc.js',
'.github/workflows/ci.yml': './ci.yml',
'.github/ISSUE_TEMPLATE/bug.yml': './bug.yml',
'.github/ISSUE_TEMPLATE/config.yml': './config.yml',
'.github/CODEOWNERS': './CODEOWNERS',
'.github/dependabot.yml': './dependabot.yml',
'.github/workflows/audit.yml': './audit.yml',
'.github/workflows/pull-request.yml': './pull-request.yml',
'.github/workflows/release-please.yml': './release-please.yml',
}
// currently no workspace moduleFiles
// const workspaceContent = {}
// const workspaceRootContent = {}
const filesToDelete = [
// remove any eslint config files that aren't local to the project
/^\.eslintrc\.(?!(local\.)).*/,
]
const defaultConfig = {
applyRootRepoFiles: true,
applyWorkspaceRepoFiles: true,
applyRootModuleFiles: true,
windowsCI: true,
}
const copyFiles = async (targetDir, files) => {
for (let [target, source] of Object.entries(files)) {
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), {
owner: 'inherit',
recursive: true,
force: true,
})
}
await fs.copyFile(source, target, { owner: 'inherit' })
}
}
// given a root directory, copy all files in the content map
// after purging any files we need to delete
const copyContent = async (path, rootPath, config) => {
config = { ...defaultConfig, ...config }
const isWorkspace = path !== rootPath
const contents = await fs.readdir(path)
if (isWorkspace || config.applyRootModuleFiles) {
// delete files and copy moduleFiles if it's a workspace
// or if we enabled doing so for the root
for (const file of contents) {
if (filesToDelete.some((p) => p.test(file))) {
await fs.rm(join(path, file))
}
}
await copyFiles(path, moduleFiles)
}
if (!isWorkspace) {
if (config.applyRootRepoFiles) {
await copyFiles(rootPath, repoFiles)
if (!config.windowsCI) {
// copyFiles already did the mkdir so we can just fs.copyFile now
await fs.copyFile(join(contentDir, 'ci-no-windows.yml'), join(rootPath, 'ci.yml'))
}
}
return
}
// only workspace now
// TODO: await copyFiles(path, workspaceFiles)
// if we ever have workspace specific 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,
})
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
module.exports = copyContent