1 | "use strict";
|
2 | const PLUGIN_NAME = "gulp-gwd";
|
3 | const through = require("through2");
|
4 | const gutil = require("gulp-util");
|
5 | const PluginError = gutil.PluginError;
|
6 | const File = gutil.File;
|
7 | const fs = require('graceful-fs')
|
8 | const path = require("path");
|
9 | const fsUtils = require("nodejs-fs-utils");
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const ignoreExts = ["", ".cmd", ".db", ".psd", ".fla"];
|
17 | const ignoreFiles = ["gwd_workspace.json"];
|
18 |
|
19 | var assets = [];
|
20 | var checkedAssets = [];
|
21 | var folder = "";
|
22 | var logFunc = function(str){ console.log(str) };
|
23 |
|
24 | module.exports = function(options) {
|
25 | var indexExists = false;
|
26 | var htmlFiles = [];
|
27 | var removedFiles = [];
|
28 |
|
29 | options = options || {};
|
30 | options.step = options.step || "";
|
31 | options.ignored = options.ignored || [];
|
32 | options.required = options.required || [];
|
33 | if (options.logFunc !== null && typeof options.logFunc === "function"){
|
34 | logFunc = options.logFunc;
|
35 | }
|
36 |
|
37 | function filter(file, encoding, callback) {
|
38 | if (file.isNull()) { callback(null, file); return; }
|
39 | if (file.isStream()) { callback(new PluginError(PLUGIN_NAME, "Streaming not supported")); return; }
|
40 | try {
|
41 | var filepath = String(file.history);
|
42 | if (~ignoreExts.indexOf(path.extname(filepath)) || ~ignoreFiles.indexOf(path.basename(filepath))){ callback(); return; }
|
43 | this.push(file);
|
44 | } catch (err) {
|
45 | this.emit("error", new PluginError(PLUGIN_NAME, err));
|
46 | }
|
47 |
|
48 | callback();
|
49 | }
|
50 | function getFiles(file, encoding, callback) {
|
51 | if (file.isNull()) { callback(null, file); return; }
|
52 | if (file.isStream()) { callback(new PluginError(PLUGIN_NAME, "Streaming not supported")); return; }
|
53 | try {
|
54 | var filepath = String(file.history);
|
55 | var extname = path.extname(filepath);
|
56 | var filename = path.basename(filepath);
|
57 |
|
58 | if (extname === ".html") {
|
59 | if (filename !== "index.html"){
|
60 | htmlFiles.push(String(filepath));
|
61 | } else {
|
62 | checkedAssets.push(String(filepath));
|
63 | indexExists = true;
|
64 | }
|
65 | } else {
|
66 | if (!~options.ignored.indexOf(filename)){
|
67 | assets.push(String(filepath));
|
68 | } else {
|
69 | checkedAssets.push(String(filepath));
|
70 | }
|
71 | }
|
72 | this.push(file);
|
73 |
|
74 | } catch (err) {
|
75 | this.emit("error", new PluginError(PLUGIN_NAME, err));
|
76 | }
|
77 | callback();
|
78 | }
|
79 | function renameHtml(callback) {
|
80 | if (indexExists === true) { callback(); return }
|
81 | if (htmlFiles.length === 1){
|
82 | var oldPath = path.normalize(htmlFiles[0]);
|
83 | var newPath = path.join(path.dirname(oldPath), "index.html");
|
84 | if (fs.existsSync(oldPath)){
|
85 | fs.createReadStream(oldPath).pipe(fs.createWriteStream(newPath));
|
86 | checkedAssets.push(String(newPath));
|
87 | }
|
88 | } else if (htmlFiles.length > 1){
|
89 | this.emit("error", new PluginError(PLUGIN_NAME, "Found " + htmlFiles.length + " .html files, just 1 needed"));
|
90 | } else {
|
91 | this.emit("error", new PluginError(PLUGIN_NAME, "HTML file is missing"));
|
92 | }
|
93 | callback();
|
94 | };
|
95 |
|
96 | function replacer(str, group, offset) {
|
97 | if (group.indexOf("data:") === 0 || path.isAbsolute(group) === true) return str;
|
98 | var newName = String(path.basename(group));
|
99 | var oldPath = path.join(folder, group);
|
100 |
|
101 | if (group !== newName){
|
102 | var newPath = path.join(folder, newName);
|
103 |
|
104 | if (!~checkedAssets.indexOf(newPath)){
|
105 | checkedAssets.push(newPath);
|
106 | if (fs.existsSync(oldPath) && fs.statSync(oldPath).isFile() === true) {
|
107 | fs.renameSync(oldPath, newPath);
|
108 | }
|
109 | }
|
110 | return str.replace(group, newName);
|
111 |
|
112 | } else {
|
113 | if (!~checkedAssets.indexOf(oldPath)){
|
114 | checkedAssets.push(oldPath);
|
115 | }
|
116 | return str;
|
117 | }
|
118 |
|
119 | }
|
120 | function index(file, encoding, callback) {
|
121 | if (file.isNull()) { callback(null, file); return; }
|
122 | if (file.isStream()) { callback(new PluginError(PLUGIN_NAME, "Streaming not supported")); return; }
|
123 | try {
|
124 | var filepath = String(file.history);
|
125 | var filename = path.basename(filepath);
|
126 | if (filename !== "index.html" || folder !== ""){ callback(); return; }
|
127 | folder = path.dirname(filepath);
|
128 |
|
129 | var regAssets = [];
|
130 | for (var i = 0; i < assets.length; i++) {
|
131 | regAssets[i] = path.relative(folder, assets[i]).replace(/(\\|\/)/, unescape('[\\/|\\\\]'));
|
132 | }
|
133 | var assetsRegExp = new RegExp(unescape("[\"\'\()][\\/|\\\\]?") + "(" + regAssets.join("|") + ")[\"\'\)]", "g");
|
134 |
|
135 | var data = file.contents.toString();
|
136 | data = data.replace(assetsRegExp, replacer);
|
137 | file.contents = new Buffer(data);
|
138 | this.push(file);
|
139 |
|
140 | } catch (err) {
|
141 | this.emit("error", new PluginError(PLUGIN_NAME, err));
|
142 | }
|
143 | callback();
|
144 | }
|
145 |
|
146 | function output_filter(file, encoding, callback) {
|
147 | if (file.isNull()) { callback(null, file); return; }
|
148 | if (file.isStream()) { callback(new PluginError(PLUGIN_NAME, "Streaming not supported")); return; }
|
149 | try {
|
150 | var filepath = String(file.history);
|
151 | var filename = path.basename(filepath);
|
152 |
|
153 | if (~checkedAssets.indexOf(String(filepath))){
|
154 | this.push(file);
|
155 | } else {
|
156 | var extname = path.extname(filename);
|
157 | if (extname !== ".js" && extname !== ".css" && extname !== ".html") removedFiles.push(filename);
|
158 | }
|
159 | } catch (err) {
|
160 | this.emit("error", new PluginError(PLUGIN_NAME, err));
|
161 | }
|
162 |
|
163 | callback();
|
164 | }
|
165 | function output_report(callback) {
|
166 | if (removedFiles.length > 0){
|
167 | gutil.log(gutil.colors.bold.yellow("Removed files: " + removedFiles.join(", ")));
|
168 | }
|
169 | callback();
|
170 | };
|
171 | function finish(file, encoding, callback) {
|
172 | if (file.isNull()) { callback(null, file); return; }
|
173 | if (file.isStream()) { callback(new PluginError(PLUGIN_NAME, "Streaming not supported")); return; }
|
174 | try {
|
175 | var filepath = String(file.history);
|
176 | var filename = path.basename(filepath);
|
177 | if (filename !== "index.html"){ callback(); return; }
|
178 |
|
179 | var outputFolder = path.dirname(filepath);
|
180 | var outputFolderSize = fsUtils.fsizeSync(outputFolder);
|
181 |
|
182 |
|
183 | var all = fs.readdirSync(outputFolder);
|
184 | for (var i = 0; i < all.length; i++) {
|
185 | var elemPath = path.join(outputFolder, all[i]);
|
186 | if (fs.statSync(elemPath).isDirectory() === true && fsUtils.fsizeSync(elemPath) === 0){
|
187 | fsUtils.rmdirsSync(elemPath);
|
188 | }
|
189 | }
|
190 | this.push(file);
|
191 |
|
192 | if (options.required.length > 0) {
|
193 | for (var i = 0; i < options.required.length; i++) {
|
194 | var req = options.required[i];
|
195 | var found = false;
|
196 | for (var j = 0; j < req[0].length; j++) {
|
197 | if (~checkedAssets.indexOf(path.join(folder, req[0][j]))){
|
198 | found = true;
|
199 | break;
|
200 | }
|
201 | }
|
202 | if (found === false){
|
203 | |
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 | logFunc("<br><span style='color:red'>" + req[1] + " is missing</span>");
|
211 | }
|
212 | }
|
213 | }
|
214 | } catch (err) {
|
215 | this.emit("error", new PluginError(PLUGIN_NAME, err));
|
216 | }
|
217 |
|
218 | callback();
|
219 | }
|
220 | if (options.step === "filter"){
|
221 | return through.obj(filter);
|
222 | } else if (options.step === "init"){
|
223 | return through.obj(getFiles, renameHtml);
|
224 | } else if (options.step === "output"){
|
225 | return through.obj(output_filter, output_report);
|
226 | } else if (options.step === "finish"){
|
227 | return through.obj(finish);
|
228 | } else {
|
229 | return through.obj(index);
|
230 | }
|
231 | }; |
\ | No newline at end of file |