-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (34 loc) · 1.2 KB
/
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
33
34
35
36
37
38
39
const fs = require('fs')
const path = require('path')
const glob = require('glob')
const chalk = require('chalk')
const decaf = require('decaffeinate')
// npm install decaffeinate glob
const currentDirectory = path.join(process.cwd(), './**/*')
console.log(chalk.green('Directory:'), chalk.yellow(currentDirectory))
glob(currentDirectory, function( err, files ) {
const failed = []
files.forEach(file => {
if (file.match(/node_modules/) || !file.match(/.(cjsx|coffee)$/)) {
return
}
if (fs.statSync(file)) {
const raw = fs.readFileSync(file, 'utf8')
try {
const output = decaf.convert(raw)
fs.writeFileSync(file.replace(/\.(cjsx|coffee)/, '.js'), output.code)
fs.unlink(file)
console.log(chalk.green('+'), file)
} catch(e) {
console.log(chalk.red('-', file))
console.error(chalk.red(e.message))
failed.push(file)
}
}
})
if (failed.length) {
console.log(chalk.red('FAILED FILES:\n'), failed.join('\n'))
} else {
console.log(chalk.green('All files were successfully converted!'))
}
})