Compare commits
2 commits
main
...
main-xmr-c
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
55b0816060 | ||
|
|
0cbe6a0132 |
1 changed files with 50 additions and 25 deletions
|
|
@ -11,9 +11,11 @@
|
||||||
|
|
||||||
const fs = require('fs')
|
const fs = require('fs')
|
||||||
const path = require('path')
|
const path = require('path')
|
||||||
const sh = require('shelljs')
|
|
||||||
|
|
||||||
sh.config.fatal = true
|
const VERBOSE = process.argv.includes('--verbose')
|
||||||
|
const DRY_RUN = process.argv.includes('--dry') || process.argv.includes('--dry-run')
|
||||||
|
|
||||||
|
const ROOT_DIR = path.join(__dirname, '..')
|
||||||
|
|
||||||
// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
|
// Blame TC39... https://github.com/benjamingr/RegExp.escape/issues/37
|
||||||
function regExpQuote(string) {
|
function regExpQuote(string) {
|
||||||
|
|
@ -24,8 +26,6 @@ function regExpQuoteReplacement(string) {
|
||||||
return string.replace(/\$/g, '$$')
|
return string.replace(/\$/g, '$$')
|
||||||
}
|
}
|
||||||
|
|
||||||
const DRY_RUN = false
|
|
||||||
|
|
||||||
function walkAsync(directory, excludedDirectories, fileCallback, errback) {
|
function walkAsync(directory, excludedDirectories, fileCallback, errback) {
|
||||||
if (excludedDirectories.has(path.parse(directory).base)) {
|
if (excludedDirectories.has(path.parse(directory).base)) {
|
||||||
return
|
return
|
||||||
|
|
@ -56,22 +56,46 @@ function walkAsync(directory, excludedDirectories, fileCallback, errback) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function replaceRecursively(directory, excludedDirectories, allowedExtensions, original, replacement) {
|
function replaceRecursively(directory, excludedDirectories, allowedExtensions, original, replacement) {
|
||||||
original = new RegExp(regExpQuote(original), 'g')
|
const updateFile = filepath => {
|
||||||
replacement = regExpQuoteReplacement(replacement)
|
if (!allowedExtensions.has(path.parse(filepath).ext) && VERBOSE) {
|
||||||
const updateFile = DRY_RUN ?
|
console.log(`EXCLUDED: ${filepath}`)
|
||||||
filepath => {
|
return
|
||||||
if (allowedExtensions.has(path.parse(filepath).ext)) {
|
|
||||||
console.log(`FILE: ${filepath}`)
|
|
||||||
} else {
|
|
||||||
console.log(`EXCLUDED:${filepath}`)
|
|
||||||
}
|
|
||||||
} :
|
|
||||||
filepath => {
|
|
||||||
if (allowedExtensions.has(path.parse(filepath).ext)) {
|
|
||||||
sh.sed('-i', original, replacement, filepath)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fs.readFile(filepath, 'utf8', (error, originalData) => {
|
||||||
|
if (error) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
const newData = originalData.replace(
|
||||||
|
new RegExp(regExpQuote(original), 'g'),
|
||||||
|
regExpQuoteReplacement(replacement)
|
||||||
|
)
|
||||||
|
|
||||||
|
if (originalData === newData) {
|
||||||
|
if (VERBOSE) {
|
||||||
|
console.log(`SKIPPED: ${filepath}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if (VERBOSE) {
|
||||||
|
console.log(`FILE: ${filepath}`)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DRY_RUN) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
fs.writeFile(filepath, newData, 'utf8', err => {
|
||||||
|
if (err) {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
walkAsync(directory, excludedDirectories, updateFile, err => {
|
walkAsync(directory, excludedDirectories, updateFile, err => {
|
||||||
console.error('ERROR while traversing directory!:')
|
console.error('ERROR while traversing directory!:')
|
||||||
console.error(err)
|
console.error(err)
|
||||||
|
|
@ -80,22 +104,22 @@ function replaceRecursively(directory, excludedDirectories, allowedExtensions, o
|
||||||
}
|
}
|
||||||
|
|
||||||
function main(args) {
|
function main(args) {
|
||||||
if (args.length !== 2) {
|
const [oldVersion, newVersion] = args
|
||||||
console.error('USAGE: change-version old_version new_version')
|
|
||||||
|
if (!oldVersion || !newVersion) {
|
||||||
|
console.error('USAGE: change-version old_version new_version [--verbose] [--dry[-run]]')
|
||||||
console.error('Got arguments:', args)
|
console.error('Got arguments:', args)
|
||||||
process.exit(1)
|
process.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
const oldVersion = args[0]
|
|
||||||
const newVersion = args[1]
|
|
||||||
const EXCLUDED_DIRS = new Set([
|
const EXCLUDED_DIRS = new Set([
|
||||||
'.git',
|
'.git',
|
||||||
'_gh_pages',
|
'_gh_pages',
|
||||||
'node_modules',
|
'node_modules',
|
||||||
'vendor'
|
'resources'
|
||||||
])
|
])
|
||||||
const INCLUDED_EXTENSIONS = new Set([
|
const INCLUDED_EXTENSIONS = new Set([
|
||||||
// This extension allowlist is how we avoid modifying binary files
|
// This extensions list is how we avoid modifying binary files
|
||||||
'',
|
'',
|
||||||
'.css',
|
'.css',
|
||||||
'.html',
|
'.html',
|
||||||
|
|
@ -106,7 +130,8 @@ function main(args) {
|
||||||
'.txt',
|
'.txt',
|
||||||
'.yml'
|
'.yml'
|
||||||
])
|
])
|
||||||
replaceRecursively('.', EXCLUDED_DIRS, INCLUDED_EXTENSIONS, oldVersion, newVersion)
|
|
||||||
|
replaceRecursively(ROOT_DIR, EXCLUDED_DIRS, INCLUDED_EXTENSIONS, oldVersion, newVersion)
|
||||||
}
|
}
|
||||||
|
|
||||||
main(process.argv.slice(2))
|
main(process.argv.slice(2))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue