-
Notifications
You must be signed in to change notification settings - Fork 321
Expand file tree
/
Copy pathpatch_api_spec_version.js
More file actions
68 lines (51 loc) · 1.46 KB
/
patch_api_spec_version.js
File metadata and controls
68 lines (51 loc) · 1.46 KB
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
#!/usr/bin/env node
const fs = require(`fs`)
const path = require(`path`)
const REPO_ROOT = path.resolve(__dirname, `..`)
const packageVersionPlaceholders = {
'sync-service': `__PLACEHOLDER_SYNC_SERVICE_VERSION__`,
}
function getPackageVersion(packageName) {
const packagePath = path.join(
REPO_ROOT,
`packages`,
packageName,
`package.json`
)
return JSON.parse(fs.readFileSync(packagePath, `utf8`)).version
}
function replaceVersionPlaceholders(content) {
let updatedContent = content
for (const [packageName, placeholder] of Object.entries(
packageVersionPlaceholders
)) {
const version = getPackageVersion(packageName)
updatedContent = updatedContent.replace(
new RegExp(placeholder, `g`),
version
)
}
return updatedContent
}
function main() {
// Get the file path from command line arguments
const filePath = process.argv[2]
if (!filePath) {
console.error(`Error: Please provide a file path`)
process.exit(1)
}
try {
// Read the file
const content = fs.readFileSync(filePath, `utf8`)
// Replace placeholders
const updatedContent = replaceVersionPlaceholders(content)
// Write the updated content back to the file
fs.writeFileSync(filePath, updatedContent)
console.log(`Successfully updated version placeholders in ${filePath}`)
} catch (error) {
console.error(`Error processing file: ${error.message}`)
process.exit(1)
}
}
// Execute the main function
main()