-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·48 lines (40 loc) · 1.59 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
40
41
42
43
44
45
46
47
48
#!/usr/bin/env node
const program = require('commander');
const path = require('path');
const fs = require('fs');
const files = require('./src/files');
const transpile = require('./src/p');
program
.version('1.0.0')
.option('-p, --path [path]', 'Path in which transpiler should look for components.')
.option('-e, --extends [extends]', 'Base clas to extend by transpiled component. Defaults to `Polymer.Element`.')
.parse(process.argv);
const ignored = ['/demo', 'test', 'node_modules', 'bower_components', 'index', 'all-imports', 'behavior'];
const travel = (dir) => {
fs.readdir(dir, (err, list) => {
if (err) throw err;
if (!list.length) return console.warn('Empty directory');
for (let i = 0, listLen = list.length; i < listLen; i++) {
const filePath = path.resolve(dir, list[i]);
fs.stat(filePath, (err, stat) => {
if (err) throw err;
if (stat.isDirectory()) {
travel(filePath);
} else {
const isIgnored = Boolean(ignored.filter(ignore => filePath.indexOf(ignore) !== -1).length);
if (path.extname(filePath) !== '.html' || isIgnored) return;
fs.readFile(filePath, 'utf8', (err, data) => {
const transpiled = transpile(data, program.extends);
if (!transpiled) {
console.log(`${filePath} is already a class`);
return;
}
fs.writeFile(filePath, transpiled, 'utf-8', (err) => console.log(`${filePath} was transpiled and saved.`));
});
}
});
}
});
};
const userPath = program.path ? program.path : './';
travel(userPath);