-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.mjs
35 lines (31 loc) · 968 Bytes
/
index.mjs
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
import path from 'path'
import lodash from 'lodash'
const caseOptions = ['camel', 'kebab', 'lower', 'upper', 'snake', 'start']
class RenamerCase {
description () {
return 'Renamer plugin to set the case of one or more files.'
}
optionDefinitions () {
return [
{
name: 'case',
description: `Renames the file using the specified case. Possible values: ${caseOptions.join(', ')}.`
}
]
}
replace (filePath, options) {
const file = path.parse(filePath)
let output = filePath
if (options.case) {
if (!caseOptions.includes(options.case)) {
throw new Error(`Invalid case, possible values: ${caseOptions.join(', ')}.`)
}
const caseFunction = lodash[lodash.camelCase(options.case + '-case')]
if (caseFunction) {
output = path.join(file.dir, file.name.replace(file.name, caseFunction(file.name)) + file.ext)
}
}
return output
}
}
export default RenamerCase