forked from NativeScript/nativescript-dev-sass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconverter.js
90 lines (77 loc) · 2.44 KB
/
converter.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
exports.convert = convert;
var fs = require('fs');
var path = require('path');
var sass = require('node-sass');
var glob = require('glob');
function convert(logger, projectDir, options) {
return new Promise(function (resolve, reject) {
options = options || {};
var sassFilesPath = path.join(projectDir, 'app/**/*.scss');
var sassImportPaths = [
path.join(projectDir, 'app/'),
path.join(projectDir, 'node_modules/')
];
//console.log("SASS Import Path", sassImportPaths);
var sassFiles = glob.sync(sassFilesPath, {follow: true}).filter(function (filePath) {
if (!(sassImportPaths.find(function (element) {return path.dirname(filePath) === element}))) {
sassImportPaths.push(path.dirname(filePath));
}
var parts = filePath.split('/');
var filename = parts[parts.length - 1];
return filePath.indexOf("App_Resources") === -1 && filename.indexOf("_") !== 0;
});
if (sassFiles.length === 0) {
//No sass files in project; skip parsing
resolve();
} else {
var i = 0;
var loopSassFilesAsync = function (sassFiles) {
parseSass(sassFiles[i], sassImportPaths, function (e) {
if (e !== undefined) {
//Error in the LESS parser; Reject promise
reject(Error(sassFiles[i] + ' SASS CSS pre-processing failed. Error: ' + e));
}
i++; //Increment loop counter
if (i < sassFiles.length) {
loopSassFilesAsync(sassFiles);
} else {
//All files have been processed; Resolve promise
resolve();
}
});
}
loopSassFilesAsync(sassFiles);
}
});
}
function parseSass(filePath, importPaths, callback) {
var sassFileContent = fs.readFileSync(filePath, {encoding: 'utf8'});
var cssFilePath = filePath.replace('.scss', '.css');
if (sassFileContent.trim().length === 0) {
// No SASS content write an empty file
fs.writeFile(cssFilePath, '', 'utf8', function () {
callback();
});
return;
}
sass.render({
data: sassFileContent,
includePaths: importPaths,
outFile: cssFilePath,
outputStyle: 'compressed'
}, function (e, output) {
if (e) {
//Callback with error
callback(e);
}
if (output && output.css) {
output = output.css;
} else {
output = '';
}
fs.writeFile(cssFilePath, output, 'utf8', function () {
//File done writing
callback();
});
});
}