-
Notifications
You must be signed in to change notification settings - Fork 2
/
wxss2less.js
53 lines (48 loc) · 1.47 KB
/
wxss2less.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env node
'use strict';
const path = require('path')
const fs = require('fs')
const shell = require('shelljs')
const colors = require('colors')
// get the egret dist path
let targetPath = process.cwd()
if (process.argv.length >= 3) {
targetPath = process.argv[process.argv.length - 1]
}
targetPath = path.resolve(targetPath)
// read all file in a dir
let readDir = (filepath) => {
let files = fs.readdirSync(filepath)
let result = []
files.map(fn => {
let thisPath = path.resolve(filepath, fn)
let stats = fs.lstatSync(thisPath)
if (stats.isFile()) {
if (fn.search('.wxss') === fn.length - 5) {
result.push(thisPath)
}
} else if (stats.isDirectory()) {
result = result.concat(readDir(thisPath))
}
})
return result
}
let wxssFiles = readDir(targetPath)
let successCnt = 0
// start to rename file
wxssFiles.map(wf => {
let nwf = wf.substr(0, wf.length - 4) + 'less'
let r = shell.mv(wf, nwf)
if (!r || r.code !== 0) {
console.log('[FAILED]'.red, 'rename'.yellow, wf.cyan, 'to', path.basename(nwf).cyan)
} else {
// replace .wxss to .less in file content
let content = fs.readFileSync(nwf, {
encoding: 'utf8'
})
fs.writeFileSync(nwf, content.replace(/\.wxss/g, '\.less'))
successCnt++
console.log('[SUCCESS]'.green, 'rename'.yellow, wf.cyan, 'to', path.basename(nwf).cyan)
}
})
console.log('total', String(wxssFiles.length).cyan, '.wxss files,', String(successCnt).green, 'renamed as less')