forked from MagicBoxSoftware/vs-cordova-3-1-ignore-files
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-excess-files.js
150 lines (135 loc) · 5.25 KB
/
remove-excess-files.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
Copyright 2015 Magic Box Software. All rights reserved.
Licensed under the MIT license.
*/
module.exports = function (context) {
var fs = require("fs");
var glob = context.requireCordovaModule('glob');
//Pattern to be removed globally every time the app is built
var remPatterns = ["*.ts", "*.nupkg", "_references.js", "packages.config", "Project_Readme.html"];
var isRelease = false;
//If this is not a debug build we may not need some files
if(context.cmdLine.indexOf('configuration Debug') === -1){
remPatterns.push("releaseignore*");
isRelease = true;
}
//Anchor to platform root
context.opts.cordova.platforms.forEach(function(platform) {
//Platform www directory path
var pwww = "platforms/" + platform + "/assets/www/";
//Remove unminified js and css if minified exists.
jsCssPattern = "@(*.css|*.js)";
glob(pwww + "**/" + jsCssPattern, function(err, jsCssFiles) {
if(err) throw err;
deleteUnminifieds(fs, jsCssFiles);
});
//Get the global releaseignores - if exists - push members onto the default delete array
if(fs.existsSync(pwww + "releaseignoreglobal.txt")){
readFileToArray(fs, pwww, "releaseignoreglobal.txt").forEach(function(line){
remPatterns.push(line);
});
//Delete this file to avoid confusion below
console.log("Deleting " + pwww + "releaseignoreglobal.txt");
fs.unlinkSync(pwww + "releaseignoreglobal.txt");
}
//Format final remove all patterns
var globPatternBuilder = "@(";
remPatterns.forEach(function(pattern){globPatternBuilder += pattern + "|";});
globPatternBuilder += ")";
var globPattern = globPatternBuilder.replace("|)", ")");
// Remove globally all files matching the globPattern pattern
glob(pwww + "**/" + globPattern, function(err, remFiles) {
if(err) throw err;
deleteMiscFiles(fs, remFiles, isRelease);
});
});
}
//Deletes all annecessary files and files identified in ignorereleaseXXX files
function deleteMiscFiles(fs, remFiles, isRelease){
remFiles.forEach(function(remFile) {
//if this is a release handle dir level ignores as they pass by
if(isRelease && remFile.indexOf("releaseignore") !== -1){
if(remFile.indexOf("releaseignoredir") !== -1){
deleteDir(fs, remFile);
return;
}else{
deleteIgFiles(fs, remFile);
}
}
console.log("--[Deleting misc] " + remFile);
fs.unlinkSync(remFile);
});
}
//Deletes unminified js and css files if a minified version exists in the same directory
function deleteUnminifieds(fs, jsCssFiles){
jsCssFiles.forEach(function(jsCssFile){
var ckMinFile;
if(/.*[^\.min]\.js/i.test(jsCssFile)){ckMinFile = jsCssFile.replace(".js", ".min.js");}
else if(/.*[^\.min]\.css/i.test(jsCssFile)){ckMinFile = jsCssFile.replace(".css", ".min.css");}
else{return;}
if(jsCssFiles.indexOf(ckMinFile) !== -1){
console.log("--[Deleting unminified] " + jsCssFile);
fs.unlinkSync(jsCssFile);
};
});
}
//Generic method to delete an array of files
function deleteArrayOfFiles(fs, filesArr, caption, targetDir, checkFilesExist){
filesArr.forEach(function(file){
var fileToDelete;
if(typeof targetDir !== 'undefined' && targetDir){fileToDelete = targetDir + file;}else{fileToDelete = file;}
if(typeof checkFilesExist !== 'undefined' && targetDir && !fs.existsSync(fileToDelete)){return;}
console.log("--[" + caption + "] " + fileToDelete);
fs.unlinkSync(fileToDelete);
});
}
//Deletes the files listed in a ignorerelease.txt file
function deleteIgFiles(fs, igFile){
var targetDir = igFile.replace("releaseignore.txt", "");
deleteArrayOfFiles(fs, readFileToArray(fs, targetDir, "releaseignore.txt"), "Deleting ignore file", targetDir, true);
}
//Deletes all files in the directory where a releaseignoredir.txt file exists
function deleteDir(fs, igFile){
var targetDir = igFile.replace("releaseignoredir.txt", "");
var recursive = true;
if(fs.readFileSync(igFile, "utf8").indexOf("recursive=false") !== -1){recursive = false;}
var displayDirSrc = targetDir.split("/assets");
fList = fs.readdirSync(targetDir);
fList.forEach(function(file){
//If recursion is required
if(fs.statSync(targetDir + file).isDirectory()){
if(!recursive){return;}
console.log('removing dir -> ' + (targetDir + file));
deleteDirR(fs, targetDir + file + '/');
}
else{
console.log("--[Deleting dir " + displayDirSrc[1] + "] -> " + file);
fs.unlinkSync(targetDir + file);
}
});
}
//Recursive directory delete. We need not worry about the rmdir because Cordova ignores empty directories
function deleteDirR(fs, dir){
var displayDirSrc = dir.split("/assets");
fList = fs.readdirSync(dir);
fList.forEach(function(file){
if(fs.statSync(dir + file).isDirectory()){
console.log('removing dir -> ' + (dir + file));
deleteDirR(fs, dir + file + '/');
}
else{
console.log("--[Deleting dir " + displayDirSrc[1] + "] -> " + file);
fs.unlinkSync(dir + file);
}
});
}
//Reads a file from the file system, splits the lines into an array and strips control characters
function readFileToArray(fs, fPath, fName){
var rawFile = fs.readFileSync(fPath + fName, "utf8");
var retAry = [];
var srcAry = rawFile.split("\n");
srcAry.forEach(function(line){
retAry.push(line.replace(/^\s+|\s+$/g, ''));
});
return retAry;
}