-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathmain.js
More file actions
245 lines (218 loc) · 8.16 KB
/
Copy pathmain.js
File metadata and controls
245 lines (218 loc) · 8.16 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
const {promisify} = require('bluebird')
const fs = require('fs-extra-promise')
const glob = promisify(require('glob'))
const path = require('path')
const _ = require('lodash')
const chalk = require('chalk')
const chokidar = require('chokidar')
const {checksum, relativeFileChecksum} = require('./checksum')
const compileSingleBundle = require('./compileBundle')
const {debug, relativeSassPath, isSassPartial, onError} = require('./utils')
const CONFIG = require('./config')
const PATHS = CONFIG.paths
const VARIANTS = require('./variants')
const cache = require('./cache')
function cacheKey (bundleName, variant) {
return [bundleName, variant].join(CONFIG.manifest_key_seperator)
}
function cacheFor (bundleName, variant) {
return cache.bundles_with_deps.data[cacheKey(bundleName, variant)]
}
exports.allFingerprintsFor = function allFingerprintsFor (bundleName) {
return VARIANTS.reduce((accumulator, variant) => {
const cached = cacheFor(bundleName, variant)
if (cached) accumulator[variant] = cached
return accumulator
}, {})
}
function cssFileExists ({bundleName, variant, combinedChecksum, includesNoVariables}) {
const filename = cssFilename({bundleName, variant, combinedChecksum, includesNoVariables})
return fs.existsAsync(filename)
}
// This is a fast way to find all the bundles that need to be rebuilt on startup
async function findChangedBundles (bundles) {
const changedFiles = new Set()
const unchangedFiles = new Set()
const toCompile = {}
function fasterHasFileChanged (filename) {
if (unchangedFiles.has(filename)) return false
if (changedFiles.has(filename)) return true
const iHaveChanged = hasFileChanged(filename)
iHaveChanged ? changedFiles.add(filename) : unchangedFiles.add(filename)
if (iHaveChanged) debug(filename, 'changed')
return iHaveChanged
}
await Promise.all(bundles.map(async function (bundleName) {
let includesNoVariables
await Promise.all(VARIANTS.map(async function (variant) {
if (includesNoVariables) return
let thisVariantHasChanged = false
const cached = cacheFor(bundleName, variant)
debug('cached was', bundleName, variant, cached)
if (!cached) {
thisVariantHasChanged = true
changedFiles.add(bundleName)
} else {
if (cached.includesNoVariables) includesNoVariables = true
// check all files on disk included in this bundle to see if the've changed
for (let filename of cached.includedFiles) {
if (fasterHasFileChanged(filename)) {
thisVariantHasChanged = true
break
}
}
// check to actually make sure the css file exists
if (!thisVariantHasChanged && !(await cssFileExists({
bundleName,
variant,
combinedChecksum: cached.combinedChecksum,
includesNoVariables: cached.includesNoVariables
}))) {
thisVariantHasChanged = true
}
}
if (thisVariantHasChanged) {
_.set(toCompile, [bundleName, variant, 'compileSelf'], true)
}
}))
}))
return toCompile
}
exports.checkAll = async function checkAll () {
debug('checking all sass bundles to see if they need updating')
const bundles = await glob(PATHS.all_sass_bundles).map(relativeSassPath)
const changedBundles = await findChangedBundles(bundles)
if (_.isEmpty(changedBundles)) {
console.info(chalk.green('no sass changes detected'))
return
}
debug('these bundles have changed', changedBundles)
return processChangedBundles(changedBundles)
}
async function processChangedBundles (changedBundles) {
debug('processing these bundles', changedBundles)
await Promise.all(_.map(changedBundles, async function (variants, bundleName) {
let includesNoVariables
async function compileUnlessIncludesNoVariables ({variant}) {
if (includesNoVariables) return
const result = await compileBundle({variant, bundleName})
if (typeof includesNoVariables === 'undefined') {
includesNoVariables = result.includesNoVariables
if (includesNoVariables) {
VARIANTS.forEach((variant) => updateCache(_.defaults({variant}, result)))
}
}
return result
}
await Promise.all(Object.keys(variants).map(async function (variant) {
if (includesNoVariables) return
const compileSelf = variants[variant].compileSelf
if (compileSelf) await compileUnlessIncludesNoVariables({variant})
}))
}))
cache.saveAll()
}
function getChecksum (relativePath) {
let md5 = cache.file_checksums.data[relativePath]
if (!md5) {
md5 = relativeFileChecksum(relativePath)
cache.file_checksums.update(relativePath, md5)
}
}
async function compileBundle ({variant, bundleName}) {
const result = await compileSingleBundle({bundleName, variant})
const includedFiles = result.includedFiles.map(relativeSassPath)
if (watcher) {
result.includedFiles.forEach((f) => watcher.add(f))
}
const md5s = includedFiles.map(getChecksum)
const metaData = {
variant,
bundleName,
includedFiles,
combinedChecksum: checksum(result.css + md5s),
includesNoVariables: !includedFiles.some(filename => filename.includes('/_variant_variables.scss'))
}
updateCache(metaData)
const filename = cssFilename(metaData)
await fs.outputFileAsync(filename, result.css)
return metaData
}
function cssFilename ({bundleName, variant, combinedChecksum, includesNoVariables}) {
const {dir, name} = path.posix.parse(bundleName)
const baseDir = includesNoVariables ? 'no_variables' : variant
return path.join(PATHS.output_dir, baseDir, dir, `${name}-${combinedChecksum}.css`)
}
function updateCache ({variant, bundleName, combinedChecksum, includedFiles, includesNoVariables}) {
cache.bundles_with_deps.update(cacheKey(bundleName, variant), {combinedChecksum, includedFiles, includesNoVariables})
}
function onBundleDeleted (bundleName) {
cache.bundles_with_deps.clearMatching(bundleName)
cache.file_checksums.update(bundleName, undefined)
}
async function onFilesystemChange (eventType, filePath, details) {
try {
debug('onFilesystemChange', eventType, filePath, details.type)
if (details.type !== 'file' || details.event === 'unknown') return
filePath = relativeSassPath(filePath)
if (eventType === 'deleted') {
cache.file_checksums.update(filePath, undefined)
if (!isSassPartial(filePath)) onBundleDeleted(filePath)
unwatch(filePath)
return
}
if (hasFileChanged(filePath)) {
debug('changed contents', filePath)
return await processChangedBundles(whatToCompileIfFileChanges(filePath))
}
debug('unchanged', filePath)
} catch (e) {
onError(e)
}
}
function whatToCompileIfFileChanges (filename) {
let toCompile = {}
if (!isSassPartial(filename)) {
const bundleName = filename
for (const variant of VARIANTS) {
_.set(toCompile, [bundleName, variant, 'compileSelf'], true)
}
} else {
for (const key in cache.bundles_with_deps.data) {
if (_.includes(cache.bundles_with_deps.data[key].includedFiles, filename)) {
const [bundleName, variant] = key.split(CONFIG.manifest_key_seperator)
_.set(toCompile, [bundleName, variant, 'compileSelf'], true)
}
}
}
return toCompile
}
function hasFileChanged (relativePath) {
const cached = cache.file_checksums.data[relativePath]
const current = relativeFileChecksum(relativePath)
cache.file_checksums.update(relativePath, current)
return cached !== current
}
var watcher
const watched = new Set()
function watch (filename) {
if (!watcher || watched.has(filename)) return
watched.add(filename)
watcher.add(filename)
}
function unwatch (filename) {
if (!watcher || !watched.delete(filename)) return
debug('unwatching', filename)
watcher.unwatch(filename)
}
exports.startWatcher = function startWatcher () {
debug('watching for changes to any scss files')
watcher = chokidar
.watch(PATHS.brandable_variables_json, {persistent: true, cwd: PATHS.sass_dir}) // TODO: remove line
.on('add', (f) => debug('file added to watcher', f))
.add(PATHS.all_sass_bundles)
for (const key in cache.bundles_with_deps.data) {
cache.bundles_with_deps.data[key].includedFiles.forEach(watch)
}
watcher.on('raw', onFilesystemChange)
}