-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·207 lines (184 loc) · 5.58 KB
/
cli.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env node
const chokidar = require("chokidar");
const escapeRegex = require("escape-string-regexp");
const mkdirp = require("mkdirp");
const fs = require("fs-extra");
const getDirName = require("path").dirname;
const recursive = require("recursive-readdir");
const debounce = require("lodash").debounce;
const parseArgs = require("minimist");
const chalk = require("chalk");
const p = require("path");
const extractCss = require("./src/extractCss");
const cleanTemplate = require("./src/cleanTemplate");
const mergeStyles = require("./src/mergeStyles");
/* Get the process args / config file */
const args = parseArgs(process.argv);
let config = {};
try {
config = require(process.cwd() + "/yoink.config")
}
catch {}
/* Assign params based on configuration method */
let params = {};
// The src folder to watch for changes
params.src = args.src || config.src || writeError(Error("Source not set"));
params.src = params.src.substr(-1) != p.sep ? params.src += p.sep : params.src;
// The output folder for 'cleaned' templates
params.dest = args.dest || config.dest || writeError(Error("Destination not set"));
params.dest = params.dest.substr(-1) != p.sep ? params.dest += p.sep : params.dest;
// The destination folder for the merged CSS
params.css_dest = args.css_dest || config.css_dest || writeError(Error("CSS Destination not set"));
// The class prefix that will be replaced with a uuid
params.prefix = escapeRegex(args.prefix || config.prefix || "--");
if(args.watch || config.watch){
/* Set chokidar to watch the src directory */
const watcher = chokidar.watch(params.src, { ignoreInitial: true });
watcher
.on("add", processFile)
.on("change", processFile)
.on("ready", debounce(processAll, 300))
.on("unlink", handleRemove)
.on("unlinkDir", handleRemove)
.on(
"error",
function() {
writeError(Error("Yoink: An error occurred."));
},
300
);
} else {
processAll();
}
/**
* Check if a file is processable then extract css, remove styles from template then merge styles to a single file
* @param {string} str Path
*/
function processFile(path) {
if (!fs.lstatSync(path).isDirectory()) {
fs.readFile(path, function read(err, data) {
if (err) {
writeError(err);
}
if (!path.startsWith(".")) {
extractCss(data.toString(), path, params);
writeTemplateFile(
cleanTemplate(data.toString(), path, params),
path
);
countProcessed("processed");
}
});
}
}
/**
* Write a single template file to the output destination
* @param {string} str Content
* @param {string} str Path
*/
function writeTemplateFile(content, path) {
writeFile(params.dest + removeSrcFromPath(params.src, path), content, function(err) {
if (err) {
writeError(err);
}
});
}
/**
* Handles the removeal of files / folders
* @param {string} str Path
*/
function handleRemove(path) {
removeFromCache(path);
removeFromDist(path);
}
/**
* Removes a folder from the cache folder if deleted from the src folder
* @param {string} str Path
*/
function removeFromCache(path) {
try {
if (fs.existsSync("yoink-cache/" + path)) {
fs.remove("yoink-cache/" + path, function() {
mergeStyles(params.css_dest);
});
}
} catch {
writeError(Error("Cache error"));
}
}
/**
* Removes a folder from the dist folder if deleted from the src folder
* @param {string} str Path
*/
function removeFromDist(path) {
try {
if (fs.existsSync(params.dest + removeSrcFromPath(params.src, path))) {
fs.remove(params.dest + removeSrcFromPath(params.src, path));
countProcessed("removed:");
}
} catch {
writeError(Error("Remove error"));
}
}
/**
* Removes the src directory from the output path
* @param {string} str Path
*/
function removeSrcFromPath(src, path) {
src = p.normalize(src);
return path.replace(src, '');
}
/**
* Process all files in the source folder.
*/
function processAll() {
fs.remove("yoink-cache", function() {
fs.remove(params.dest, function() {
fs.readdirSync(params.src).forEach(path => {
processFile(params.src + path);
});
recursive(params.src, [".DS_Store"], function(err, files) {
files.forEach(function(path) {
if (!fs.lstatSync(path).isDirectory()) {
processFile(path);
}
});
});
});
});
}
/**
* Small wrapper around fs.writeFile to craeat directory and catch errors
* @param {string} str Path
* @param {string} str Contents
* @param {requestCallback} cb
*/
function writeFile(path, contents, cb) {
mkdirp(getDirName(path), function(err) {
if (err) return cb(err);
fs.writeFile(path, contents, cb);
});
}
/**
* Small utilty to write errrors to console
* @param {string} str err
*/
function writeError(err) {
console.log(chalk.red("Yoink Error: ") + err.message || err);
process.exit(1);
}
/* Define count vars */
let count = 0;
let countTimer = null;
/**
* Count processed files to then output to console
* @param {string} str Msg
*/
function countProcessed(msg) {
count++;
clearTimeout(countTimer);
countTimer = setTimeout(() => {
console.log(chalk.green("Yoink " + msg + " " + count + " file(s)"));
count = 0;
}, 500);
}