This repository was archived by the owner on Dec 19, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsass.js
More file actions
117 lines (104 loc) · 3.07 KB
/
sass.js
File metadata and controls
117 lines (104 loc) · 3.07 KB
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
#!/usr/bin/env node
'use strict';
// Require modules
const path = require('path');
const fs = require('fs');
const sass = require('node-sass');
const write = require('write');
const del = require('del');
const postcss = require('postcss')
const readdir = require('readdir');
const inquirer = require('inquirer');
const chalk = require('chalk');
const chokidar = require('chokidar');
// Logging
const prefix = chalk.magenta.bold(' > ');
const print = msg => console.log(prefix + msg);
const printErr = msg => console.error(prefix + chalk.red(msg));
// Files
const baseInput = 'sass/style.sass';
const baseOutput = 'assets/css/style.min.css';
/**
* Compiles the .sass files for the given project.
*/
function compile(basePath) {
// Project files
const input = path.resolve('packages', basePath, baseInput);
const output = path.resolve('packages', basePath, baseOutput);
print('Running compilation for: ' + chalk.green(basePath));
// Watch file or run once?
if (process.argv.indexOf('--watch') !== -1) {
print(`Now watching for file changes at ${chalk.green(input)}...`)
chokidar.watch(path.join('packages', basePath, '**/*.sass'))
.on('change', path => run());
} else {
run();
}
function run() {
// delete old file
del(output)
// compile sass
.then(_paths => {
return new Promise((resolve, reject) => {
print(`Compile sass from file ${chalk.cyan(input)}...`);
sass.render({ file: input }, (err, result) => {
if (err) reject(err);
else resolve(result.css.toString());
});
});
})
// optimize css
.then(result => {
print('Optimize css using cssnano...');
return postcss([
require('cssnano')({ preset: 'advanced' })
])
.process(result, { from: undefined });
})
// write css to file
.then(result => {
print(`Write css to ${output}...`);
return new Promise((resolve, reject) => {
write(output, result, err => {
if (err) reject(err);
else resolve();
});
});
})
// finish handlers
.then(() => print('Done.'))
.catch(err => printErr(err.formatted));
}
}
// Use last?
if (process.argv.indexOf('--last') !== -1) {
if (fs.existsSync('.last-package')) {
compile(fs.readFileSync('.last-package').toString());
} else {
printErr('Run without --last at least once!');
}
}
// Get available packages
else {
readdir.read('packages/', ['*/'], readdir.INCLUDE_DIRECTORIES + readdir.NON_RECURSIVE,
(err, directories) => {
if (err) throw err;
else {
// Choose the target package
inquirer.prompt([
{
type: 'list',
name: 'project',
message: 'Select the project to compile the SASS files for:',
choices: directories.sort()
}
])
// Compile!
.then(result => {
fs.writeFileSync('.last-package', result.project);
compile(result.project)
})
.catch(printErr);
}
});
}