-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
91 lines (83 loc) · 2 KB
/
index.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
#!/usr/bin/env node
const fs = require('fs');
const glob = require('glob');
const mkdirp = require('mkdirp');
const path = require('path');
const sass = require('node-sass');
const { sniperImporter } = require('./importer');
const program = require('./cli');
let includePaths = program.includePath.map(include => path.resolve(process.cwd()+'/'+include));
let files = program.args[0];
/**
* Render File
*
* @param {String} file
*/
function renderFile(file) {
'use strict';
sass.render({
file: file,
// data: '@import "./sass/global-styling.scss";',
includePaths: includePaths,
outputStyle: program.outputStyle,
importer: sniperImporter(),
}, function (err, result) {
if (err) {
console.log(err.formatted);
} else {
// Build path to destination file.
let dest = path.resolve(
path.join(program.output,
path.relative(
path.resolve(files), file)
)
).replace(path.extname(file), '.css');
// Create destination dir.
mkdirp(path.dirname(dest)).then(() => {
fs.writeFile(dest, result.css.toString(), function (err) {
if (err) {
return console.log(err);
}
console.log(dest+' written.');
});
});
}
});
}
/**
* Is a Directory
*
* @param {String} filePath
* @returns {Boolean}
* @api private
*/
function isDirectory(filePath) {
let isDir = false;
try {
const absolutePath = path.resolve(filePath);
isDir = fs.statSync(absolutePath).isDirectory();
} catch (e) {
isDir = e.code === 'ENOENT';
}
return isDir;
}
/**
* Glob sass/scss files and render.
*
* @param {String} files
*/
function run(files) {
'use strict';
if (isDirectory(files)) {
let globPath = path.resolve(files, '**/*.{sass,scss}');
glob(globPath, { ignore: '**/_*', follow: true }, function (err, files) {
for (let file of files) {
renderFile(file);
}
});
} else {
renderFile(path.resolve(files));
}
}
// Render files.
run(files);