-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
65 lines (56 loc) · 2.08 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
const fs = require("fs");
const path = require("path");
/**
*
* @param {Object} obj contains the list of files to be concatinated
* returns string
*/
function concatdata(obj) {
let data = '';
for(let prop in obj)
{
const objlength = obj[prop] && obj[prop].length;
if(objlength) {
for(let fileName of obj[prop]) {
/**
* checking if file is a javascript files
* if file is not a javascript file it is assumed that is is a string
* to be appended directly
*/
if(path.extname(fileName) === ".js" || path.extname(fileName) === ".txt") {
data+= fs.readFileSync(fileName,'utf8') + "\n";
} else {
data+= fileName + "\n";
}
}
}
}
return data;
}
function concatFiles(useroptions) {
return {
name: 'rollup-plugin-concatfiles',
writeBundle: function () {
if(useroptions.files) {
for(let concatinatedFile in useroptions.files) {
let content = concatdata(useroptions.files[concatinatedFile]);
/**
* concatination works only for js files
* If the path provided by user does not exist
* then it would be created recursively
* */
if(path.extname(concatinatedFile) == ".js") {
let directorpath = concatinatedFile.substring(0, concatinatedFile.lastIndexOf("/"));
if(directorpath !== "" && !fs.existsSync(directorpath) ) {
fs.mkdirSync(directorpath, { recursive: true });
}
fs.writeFileSync(concatinatedFile,content);
}
}
} else {
console.log('\x1b[31m%s\x1b[0m', "please enter in the valid format as per the readme.");
}
}
};
}
exports.concatFiles = concatFiles;