-
Notifications
You must be signed in to change notification settings - Fork 1
/
console.js
executable file
·171 lines (129 loc) · 4.25 KB
/
console.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env node
let fs = require('fs')
let path = require('path')
let program = require('commander')
let util = require('util')
let sass = require('node-sass')
let _ = require('lodash')
let { parse, stringify } = require('scss-parser')
let createQueryWrapper = require('./query-ast')
let writeStyles = require('./scss-template.js')
let handlebars = require('handlebars')
let open = require('open')
let Candidates = require('./candidates')
//
/**
* Hello, CLI.
*/
program
.version(require('./package.json').version)
.usage('[options] <file>')
.option('-m, --hdm', 'HDM file shortcuts')
.option('-e, --edit', 'HDM Edit UI file shortcuts')
.option('-d --demo', 'Display demo of sample scss variable file')
.option('-b, --bench', 'measure and output timing data')
.parse(process.argv);
let file = ''
if(program.hdm) {
file = path.resolve(`${program.args[0]}/media-platform/fre-hdm/sites/${program.args[1]}/assets/scss/_variables.scss`)
} else if(program.edit) {
file = path.resolve(`${program.args[0]}/media-platform/edit/assets/scss/includes/_constants.scss`)
} else if(program.demo) {
file = path.resolve(path.join(__dirname, 'demos/sample.scss'));
} else {
file = path.resolve(program.args[0]);
}
// The file must exist!
if(!fs.existsSync(file)) {
console.error(`Couldn't load '${file}'.`);
process.exit(1);
}
let ast = parse(fs.readFileSync(file, 'utf-8'));
let stuff = new Candidates(ast)
let mapVars = stuff.maps()
let colorVars = stuff.colors()
let borderVars = stuff.borders()
let final = {}
let test = (css, map) => {
final = {}
for (let val in map) {
if(css.search(val) != -1) {
final[val.substr(1)] = map[val]
}
}
return final
}
let styleData = (maps, colors, borders) => {
data = { maps, colors, borders }
}
let deps = []
if(program.hdm) {
deps = [
path.resolve(`${program.args[0]}/media-platform/mp-bower-assets/scss/_utils.scss`),
path.resolve(`${program.args[0]}/media-platform/mp-bower-assets/scss/_system.scss`),
path.resolve(`${program.args[0]}/media-platform/fre-hdm/assets/scss/_variables.scss`),
path.resolve(`${program.args[0]}/media-platform/fre-hdm/sites/${program.args[1]}/assets/scss/_variables.scss`)
]
} else if(program.edit) {
deps = [
path.resolve(`${program.args[0]}/media-platform/mp-bower-assets/scss/_utils.scss`),
path.resolve(`${program.args[0]}/media-platform/mp-bower-assets/scss/_system.scss`),
path.resolve(`${program.args[0]}/media-platform/mp-bower-assets/scss/_animations.scss`),
path.resolve(`${program.args[0]}/media-platform/mp-bower-assets/scss/_breakpoint-events.scss`),
path.resolve(`${program.args[0]}/media-platform/edit/assets/scss/includes/_constants.scss`)
]
} else if(program.demo) {
deps.push(file)
} else {
deps = program.args.slice(1)
deps.push(program.args[0])
}
writeStyles(mapVars, colorVars, borderVars, deps)
fs.readFile(path.join(__dirname, 'demos/styles.scss'), 'utf8', (err, contents) => {
sass.render({
data: contents,
}, (err, result) => {
if(err) {
console.log("Check if scss files and import order are valid")
}
let cssString = result.css.toString()
let mapData = test(cssString, mapVars)
let colorData = test(cssString, colorVars)
let borderData = test(cssString, borderVars)
_.forIn(colorData, (val, key) => { // TO DO: Separate border classes from color classes
if(borderData[key]) {
delete colorData[key]
}
})
styleData(mapData, colorData, borderData)
fs.writeFile(path.join(__dirname, 'public/css/styles.css'), cssString, function(err) {
if(err) {
return console.log(err);
}
});
fs.readFile(path.join(__dirname, 'demos/demo-template.html'), 'utf-8', (error, source) => {
handlebars.registerHelper('append', (key) => {
key = '$' + key.replace(/-stylejam-/g, ' : ')
return key
})
handlebars.registerHelper('prepend', (key) => {
key = '$' + key
return key
})
let template = handlebars.compile(source)
let html = template(data)
fs.writeFile(path.join(__dirname, 'public/index.html'), html, function(err) {
if(err) {
return console.log(err);
}
})
open(path.join(__dirname, 'public/index.html'))
})
})
})
if(program.bench) {
console.time('stylejam');
}
if(program.bench) {
console.timeEnd('stylejam');
}