-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·75 lines (56 loc) · 1.92 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env node
const fs = require('fs');
const path = require('path');
const globby = require('globby');
const Emblem = require('emblem');
const prettier = require('prettier');
const ProgressBar = require('progress');
async function run() {
let inputPath = process.argv[2];
let paths, progressbar;
if (inputPath && isFile(inputPath)) {
paths = [inputPath];
console.log(` ⚙️ Converting "${path.basename(inputPath)}" to Handlebars...`);
} else {
let cwd = path.resolve(inputPath ? inputPath : process.cwd());
console.log(` 🔍 Looking for Emblem.js files in ${cwd}...`);
let pattern = path.join(cwd, '**/*.{em,embl,emblem}');
paths = await globby([pattern]);
if (paths.length === 0) {
console.log(' ⚠️ No Emblem.js files were found!');
return;
}
console.log(` ⚙️ Converting ${paths.length} files to Handlebars...`);
console.log();
progressbar = new ProgressBar(' [:bar] :percent :etas ', { total: paths.length });
}
for (let oldPath of paths) {
// convert path to `.hbs` extension
let oldExtension = path.extname(oldPath);
let newPath = oldPath.slice(0, oldPath.length - oldExtension.length) + '.hbs';
// read old template file contents
let emblem = fs.readFileSync(oldPath, 'utf8');
// convert Emblem.js to Handlebars
let hbs = Emblem.compile(emblem, { quiet: true });
// run Handlebars template through `prettier`
let prettyHbs = prettier.format(hbs, { parser: 'glimmer' });
// write new Handlebars template file
fs.writeFileSync(newPath, prettyHbs, 'utf8');
// remove old Emblem.js template file
fs.unlinkSync(oldPath);
if (progressbar) {
progressbar.tick();
}
}
if (progressbar) {
progressbar.terminate();
}
console.log(' ✅ Done!');
}
function isFile(path) {
let stat = fs.statSync(path);
return stat && stat.isFile();
}
run().catch(error => {
console.error(error);
});