-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix-es2015-modules.js
47 lines (38 loc) · 1.2 KB
/
fix-es2015-modules.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
/** Path fix function based one made byhttps://github.com/QuantumInformation */
'use strict';
const FileHound = require('filehound');
const fs = require('fs');
const args = process.argv.slice(2);
let folderToFix = './dist/';
if (args.length === 1) {
folderToFix = args[0];
}
const files = FileHound.create()
.path(folderToFix)
.ext('js')
.find();
files.then(filePaths => {
filePaths.forEach(filepath => {
fs.readFile(filepath, 'utf8', (errorReadingFile, data) => {
if (!data.match(/(import|export) .* from/g)) {
return;
}
const scriptImports = data.match(/((import|export) .* from\s+['"])(.*)(?=['"])/g);
let newData = data;
scriptImports.forEach(script => {
newData = newData.replace(script, script + '.js');
});
// let newData = data.replace(/((import|export) .* from\s+['"])(.*)(?=['"])/g, '$1.js');
if (errorReadingFile) {
throw errorReadingFile;
}
fs.writeFile(filepath, newData, function(errorWritingFile) {
if (errorWritingFile) {
throw errorWritingFile;
}
// tslint:disable-next-line:no-console
console.log(`js extension added to ${filepath}`);
});
});
});
});