-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstyles.js
More file actions
executable file
·122 lines (98 loc) · 2.66 KB
/
styles.js
File metadata and controls
executable file
·122 lines (98 loc) · 2.66 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
118
119
120
121
122
#!/usr/bin/env node
/**
* Dependencies
*/
const path = require('path');
const chokidar = require('chokidar');
const tokens = require(`${__dirname}/tokens`);
const sass = require(`${__dirname}/sass`);
const postcss = require(`${__dirname}/postcss`);
const args = require(`${__dirname}/util/args`).args;
const cnsl = require(`${__dirname}/util/console`);
const resolve = require(`${__dirname}/util/resolve`);
const alerts = resolve('config/alerts');
const global = resolve('config/global');
/**
* Get Sass modules and options
*
* @return {Options} The configuration required for the styles command
*/
const options = () => {
let modules = resolve('config/sass', true, false);
if (process.env.NODE_ENV === 'development') {
modules = modules.filter(file => file.devModule);
}
return {
modules: modules,
globs: [
resolve('config/tokens', false),
resolve('config/tailwindcss', false),
resolve('config/sass', false),
resolve('config/postcss', false),
path.join(global.base, global.src, '/**/*.scss')
]
}
};
/**
* Methods
*/
/**
* Run each task on the modules
*
* @param {Array} modules The contents of config/sass.js
*/
const main = async (modules) => {
try {
await tokens.run();
await sass.run(modules);
await postcss.run(modules);
} catch (err) {
cnsl.error(`Styles failed: ${err.stack}`);
}
};
/**
* Our Chokidar Watcher
*
* @type {Source} https://github.com/paulmillr/chokidar
*/
const watcher = chokidar.watch(options().globs, {
ignored: tokens.options().output.replace(/"/g, ''),
...global.chokidar
});
/**
* Tne runner for single commands and the watcher
*/
const run = async () => {
let opts = options();
try {
if (args.watch) {
watcher.on('change', changed => {
let styls = [];
opts = options();
cnsl.watching(`Detected change on ${alerts.str.path(changed)}`);
opts.modules = opts.modules;
// Only run main on changed modules by default
if (process.env.NODE_ENV === 'production') {
styls = opts.modules;
} else {
let filtered = opts.modules.filter(s => path.basename(changed) === path.basename(s.file));
styls = (filtered.length) ? filtered : opts.modules;
}
main(styls);
});
cnsl.watching(`Styles watching ${alerts.str.ext(options().globs.join(', '))}`);
} else {
await main(options().modules);
cnsl.success(`Styles finished`);
process.exit();
}
} catch (err) {
cnsl.error(`Styles failed: ${err.stack}`);
}
};
/** @type {Object} Export our method */
module.exports = {
globs: options().globs,
main: main,
run: run
};