-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (127 loc) · 3.29 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env node
'use strict'
/**
* Dependencies
*/
const fs = require('fs');
const path = require('path');
const chokidar = require('chokidar');
const cssVars = require('css-vars-from-json');
/**
* @pttrn Dependencies
*/
const pttrn = `${process.env.PWD}/node_modules/@nycopportunity/pttrn`;
const alerts = require(`${pttrn}/config/alerts`);
const args = require(`${pttrn}/bin/util/args`).args;
const cnsl = require(`${pttrn}/bin/util/console`);
const resolve = require(`${pttrn}/bin/util/resolve`);
/**
* Get options for the command.
*
* @return {Object} The options object.
*/
const options = function() {
return {
modules: resolve('config/properties', true, false),
globs: [
`${process.env.PWD}/config/properties.js`,
`${process.env.PWD}/config/tokens.js`
]
}
};
/**
* Our Chokidar Watcher
*
* @source https://github.com/paulmillr/chokidar
*/
const watcher = chokidar.watch(options().globs, {
usePolling: false,
awaitWriteFinish: {
stabilityThreshold: 750
}
});
/**
* Write file to the distribution folder
*
* @param {String} file The file source
* @param {Object} data The data to pass to the file
*
* @return {Undefined} The result of fs.writeFileSync()
*/
const write = async (mod, data) => {
try {
let dist = mod.dist;
if (!fs.existsSync(path.dirname(dist))) {
fs.mkdirSync(path.dirname(dist), {recursive: true});
}
let written = fs.writeFileSync(dist, data);
cnsl.describe(`${alerts.tokens} Properties written to ${alerts.str.path(dist)}`);
return written;
} catch (err) {
cnsl.error(`Failed (write): ${err.stack}`);
}
}
/**
* A sample file read and replace method
*
* @param {String} mod Module containing the CSS Properties
*
* @return {String} File contents
*/
const replace = async mod => {
let ruleset = (mod.hasOwnProperty('ruleset')) ? mod['ruleset'] : ':root';
return `${ruleset} { ${cssVars(mod['properties'])} }`;
};
/**
* The main task bus for transforming contents of a source file
*
* @param {String} file Path to source file
*
* @return {String} Transformed data
*/
const main = async mod => {
try {
let data = await replace(mod); // Do something with the file data here
await write(mod, data);
return data;
} catch (err) {
cnsl.error(`Failed (main): ${err.stack}`);
}
};
/**
* Runner for the sample script. If the -w or --watch flag is passed it will
* run the watcher method. If a single filename is passed it will run the
* main task on the file.
*
* @type {Function}
*/
const run = async () => {
if (args.watch) {
try {
watcher.on('change', async changed => {
cnsl.watching(`Detected change on ${alerts.str.path(changed)}`);
let opts = options();
for (let i = 0; i < opts.modules.length; i++) {
await main(opts.modules[i]);
}
});
cnsl.watching(`Properties watching ${alerts.str.ext(options().globs.join(', '))}`);
} catch (err) {
cnsl.error(`Failed (run): ${err.stack}`);
}
} else {
let opts = options();
for (let i = 0; i < opts.modules.length; i++) {
await main(opts.modules[i]);
}
process.exit(); // One-off commands must exit
}
};
/**
* Export our methods
*
* @type {Object}
*/
module.exports = {
run: run
};