-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask.js
98 lines (60 loc) · 2.48 KB
/
task.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
const glob = require('glob');
const sass = require('node-sass');
const path = require('path');
const fs = require('fs-extra');
const _ = require('lodash');
module.exports = (logger, dirname, config) => {
return () => {
return new Promise((resolve, reject) => {
// Validate config
if ( ! config.dir || ! config.outDir ) return reject(new Error('SASS plugin misconfiguration! dir and outDir must be present.'));
// Empty outDir if necessary
if ( config.cleanOutDir ) {
fs.emptyDirSync(path.join(dirname, config.outDir));
}
// Search `config.dir` for `.scss` files
glob('**/*.scss', { cwd: path.join(dirname, config.dir) }, (error, files) => {
if ( error ) return reject(error);
const promises = [];
for ( const file of files ) {
// Skip partial files
if ( path.basename(file).substr(0, 1) === '_' ) continue;
promises.push(new Promise((resolve, reject) => {
// Compile the SASS file into CSS
const finalOptions = _.cloneDeep(config);
delete finalOptions.file;
delete finalOptions.outFile;
delete finalOptions.data;
delete finalOptions.dir;
delete finalOptions.outDir;
delete finalOptions.cleanOutDir;
finalOptions.file = path.join(dirname, config.dir, file);
finalOptions.outFile = path.join(dirname, config.outDir, file.replace('.scss', '.css'));
sass.render(finalOptions, (error, result) => {
if ( error ) return reject(error);
// Write the CSS file to `config.outdir`
fs.outputFile(path.join(dirname, config.outDir, file.replace('.scss', '.css')), result.css, error => {
if ( error ) return reject(error);
// Write the map if needed
if ( result.map && config.sourceMap ) {
fs.outputFile(path.join(dirname, config.outDir, file.replace('.scss', '.css.map')), result.map, error => {
if ( error ) return reject(error);
resolve();
});
}
else resolve();
});
});
}));
}
logger(`Processing ${promises.length} files...`);
Promise.all(promises)
.then(() => {
logger(`All ${promises.length} files were processed.`);
resolve();
})
.catch(reject);
});
});
};
};