Skip to content

Instantly share code, notes, and snippets.

@potato4d
Last active December 28, 2019 05:44
Show Gist options
  • Save potato4d/a6c53924f7efda99bbf83ecf8daffadd to your computer and use it in GitHub Desktop.
Save potato4d/a6c53924f7efda99bbf83ecf8daffadd to your computer and use it in GitHub Desktop.
import { promises as fs } from 'fs'
import { execSync } from 'child_process'
const camelToKebab = require('camel-to-kebab')
function getFileNames(scope: string, opt: string): string[] {
return execSync(`git ls-files ${scope} | grep ${opt}`)
.toString()
.split('\n')
.filter(v => v)
}
async function run() {
const componentList = getFileNames('src/components/', `'.vue'`).map(path => {
return {
kebabName: camelToKebab(path.split('/')[path.split('/').length - 1].replace('.vue', '')),
pascalName: path.split('/')[path.split('/').length - 1].replace('.vue', ''),
path
}
})
const targetList = getFileNames('.', `'.vue'`)
await Promise.all([
targetList.map(async t => {
let f = await fs.readFile(t, { encoding: 'utf8' })
const base = f
componentList.forEach(component => {
f = f.replace(
new RegExp(`<(\/?)(${component.kebabName})`, 'g'),
`<$1${component.pascalName}`
)
})
if (base !== f) {
console.log(t)
await fs.writeFile(t, f, { encoding: 'utf8' })
}
})
])
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment