lixin-web
Version:
vue and bootstrap
83 lines (78 loc) • 2.06 kB
JavaScript
// usage: node build\fileRename.js r
const fs = require('fs')
const path = require('path')
const program = require('commander')
const inquirer = require('inquirer')
const chalk = require('chalk')
program
.command('rename')
.alias('r')
.description('rename img files')
.option('-p, --prefix', 'prefix eg:a_')
.option('-s, --suffix', 'suffix eg:_hover')
.option('-i, --inputdir', 'input dir')
.action(option => {
var config = Object.assign({
suffix:'',
inputdir:''
}, option)
var promps = []
if(!config.inputdir) {
promps.push({
type: 'input',
name: 'inputdir',
message: 'please input file input dir',
// validate: function (input){
// if(!input) {
// return chalk.red('can not empty')
// }
// return true
// }
})
}
if(!config.suffix) {
promps.push({
type: 'input',
name: 'suffix',
message: 'please input file name suffix',
// validate: function (input){
// if(!input) {
// return chalk.red('can not empty')
// }
// return true
// }
})
}
if(!config.prefix) {
promps.push({
type: 'input',
name: 'prefix',
message: 'please input file name prefix',
validate: function (input){
if(!input) {
return chalk.red('can not empty')
}
return true
}
})
}
inquirer.prompt(promps).then(function (option) {
fs.readdir(option.inputdir, (err, i) => {
if (err) throw err;
const go = n => {
if (n >= i.length) return
fs.readFile(`${option.inputdir}/${i[n]}`,(err, data) => {
if (err) throw err;
const ext = path.extname(`${i[n]}`)
fs.writeFile(`${option.inputdir}/${option.prefix}${i[n].replace(ext,'')}${option.suffix}${ext}`,data,(err, data) => {
if (err) throw err;
console.log(chalk.green(`create new ${option.prefix}${i[n].replace(ext,'')}${option.suffix}${ext}`))
go(n + 1)
})
})
}
go(0)
})
})
})
program.parse(process.argv)