generated from DEFRA/ffc-grants-addvalue-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rename.js
132 lines (114 loc) · 4.6 KB
/
rename.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
124
125
126
127
128
129
130
131
132
#!/usr/bin/env node
const fs = require('fs')
const readline = require('readline')
const originalDescription = 'description-of-project-goes-here'
const originalNamespace = 'ffc-demo'
const originalProjectName = 'ffc-template-node'
function processInput (args) {
const [, , projectName, description] = args
if (args.length === 2) {
console.error('Please enter a new name and description for the project e.g. ffc-demo-web "Web frontend for demo workstream".')
process.exit(1)
}
if (args.length !== 4 || !projectName || projectName.split('-').length < 3 || !description) {
const errMsg = [
'Please enter a new name and description for the project.',
'The name must contain two hypens and be of the form "<program>-<worksream>-<repo>" e.g. "ffc-demo-web".',
'The description must not be empty and be wrapped in quotes e.g. "excellent new description".'
]
console.error(errMsg.join('\n'))
process.exit(1)
}
return { description, projectName }
}
async function confirmRename (projectName, description) {
const affirmativeAnswer = 'yes'
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
return new Promise((resolve, _reject) => {
rl.question(`Do you want to rename the project to '${projectName}', with a description of '${description}'?\nType '${affirmativeAnswer}' to confirm\n`, (answer) => {
rl.close()
resolve(answer === affirmativeAnswer)
})
})
}
async function getHelmDir () {
const projectName = await fs.promises.readdir('./helm')
return `./helm/${projectName}`
}
function getScriptDir () {
return './scripts'
}
async function getHelmFiles () {
// assuming the only dir in ./helm is the project name.
// getting the name here removes dependency on it being updated.
const helmDir = await getHelmDir()
const baseFiles = ['Chart.yaml', 'values.yaml']
const templateFiles = ['templates/_container.yaml', 'templates/cluster-ip-service.yaml', 'templates/config-map.yaml', 'templates/deployment.yaml']
const files = [...baseFiles, ...templateFiles]
return files.map((file) => {
return `${helmDir}/${file}`
})
}
function getRootFiles () {
return ['docker-compose.yaml', 'docker-compose.override.yaml', 'docker-compose.test.yaml', 'docker-compose.test.watch.yaml', 'package.json', 'package-lock.json']
}
async function getScriptFiles () {
const scriptDir = getScriptDir()
const files = ['test']
return files.map((file) => {
return `${scriptDir}/${file}`
})
}
function getNamespace (projectName) {
const firstIndex = projectName.indexOf('-')
const secondIndex = projectName.indexOf('-', firstIndex + 1)
return projectName.substring(0, secondIndex)
}
async function renameDirs (projectName) {
const helmDir = await getHelmDir()
await fs.promises.rename(helmDir, `./helm/${projectName}`)
}
async function updateProjectName (projectName) {
const rootFiles = getRootFiles()
const helmFiles = await getHelmFiles()
const scriptFiles = await getScriptFiles()
const filesToUpdate = [...rootFiles, ...helmFiles, ...scriptFiles]
const namespace = getNamespace(projectName)
console.log(`Updating projectName from '${originalProjectName}', to '${projectName}'. In...`)
await Promise.all(filesToUpdate.map(async (file) => {
console.log(file)
const content = await fs.promises.readFile(file, 'utf8')
const projectNameRegex = new RegExp(originalProjectName, 'g')
const namespaceRegex = new RegExp(originalNamespace, 'g')
const updatedContent = content.replace(projectNameRegex, projectName).replace(namespaceRegex, namespace)
return fs.promises.writeFile(file, updatedContent)
}))
console.log('Completed projectName update.')
}
async function updateProjectDescription (description) {
const helmDir = await getHelmDir()
const filesToUpdate = ['package.json', `${helmDir}/Chart.yaml`]
console.log(`Updating description from '${originalDescription}', to '${description}'. In...`)
await Promise.all(filesToUpdate.map(async (file) => {
console.log(file)
const content = await fs.promises.readFile(file, 'utf8')
const updatedContent = content.replace(originalDescription, description)
return fs.promises.writeFile(file, updatedContent)
}))
console.log('Completed description update.')
}
async function rename () {
const { description, projectName } = processInput(process.argv)
const renameProject = await confirmRename(projectName, description)
if (renameProject) {
await renameDirs(projectName)
await updateProjectName(projectName)
await updateProjectDescription(description)
} else {
console.log('Project has not been renamed.')
}
}
rename()