-
Notifications
You must be signed in to change notification settings - Fork 7
/
sass-director.js
executable file
·97 lines (76 loc) · 2.56 KB
/
sass-director.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
#!/usr/bin/env node
function mkdir(path) {
function create(path) {
if (path) {
if (!fs.existsSync(path) || !fs.statSync(path).isDirectory()) {
try {
fs.mkdirSync(path);
} catch (error) {
return false;
}
}
}
return true;
}
path.replace(/^\/+/, '').split(/\/+/).reduce(function (previousValue, currentValue) {
create('/' + previousValue);
return previousValue + '/' + currentValue;
});
return create(path);
}
function exit(code, message) {
console.log(message);
process.exit(code || 0);
}
var
fs = require('fs'),
path = require('path'),
manifestFile = 2 in process.argv ? path.resolve(process.argv[2]) : '',
baseDirectory = 3 in process.argv ? path.resolve(process.argv[3]).replace(/\/+$/, '') : path.dirname(manifestFile);
if (process.argv.length < 3) {
exit(1, 'Usage: sass-director <manifest-file> <base-directory>');
}
if (!fs.existsSync(manifestFile)) {
exit(1, 'Sorry, sass-director could not access ' + manifestFile + '.');
}
if (!mkdir(baseDirectory)) {
exit(1, 'Sorry, sass-director could not access ' + baseDirectory + '.');
}
fs.readFile(manifestFile, 'utf8', function (error, data) {
if (error) {
exit(1, 'Sorry, sass-director could not access ' + manifestFile + '.');
}
var
importStatements = data.match(/^[ \t]*@import[ \t]+(['"])(.+?)\1/mg),
importDirectories = [],
importBasenames = [];
if (importStatements) {
importStatements.forEach(function (importStatement) {
var
importPath = importStatement.match(/^[ \t]*@import[ \t]+(['"])(.+?)\1/)[2];
importPath = importPath.match(/\.scss$/) ? importPath : importPath + '.scss';
var
importDirectory = baseDirectory + '/' + path.dirname(importPath),
importBasename = importDirectory + '/_' + path.basename(importPath);
if (!fs.existsSync(importDirectory) && importDirectories.indexOf(importDirectory) === -1) {
importDirectories.push(importDirectory);
}
if (!fs.existsSync(importBasename) && importDirectories.indexOf(importBasename) === -1) {
importBasenames.push(importBasename);
}
});
}
importDirectories.forEach(function (importDirectory) {
if (!mkdir(importDirectory)) {
exit(1, 'Sorry, sass-director could not create the ' + importDirectory + ' directory.');
}
});
importBasenames.forEach(function (importBasename) {
try {
fs.closeSync(fs.openSync(importBasename, 'w'));
} catch (error) {
exit(1, 'Sorry, sass-director could not create the ' + importBasename + ' file.');
}
});
exit(0, 'Hurray, sass-director created ' + importDirectories.length + ' directories and ' + importBasenames.length + ' files!');
});