-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
32 lines (30 loc) · 994 Bytes
/
index.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
const path = require('path')
const lodash = require('lodash')
const caseOptions = [ 'camel', 'kebab', 'lower', 'upper', 'snake', 'start' ]
module.exports = PluginBase => class RenamerCase extends PluginBase {
description () {
return 'Renamer plugin to set the case of a filename.'
}
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
}
}