-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
69 lines (60 loc) · 1.85 KB
/
build.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
const fs = require('fs')
const Sass = require('sass')
const { SourceFile, DependencyTree } = require('./builder.js')
/*
* Functions to generate files
*/
// Generates css files from scss files
function buildCSS (generatedFilePath, primarySource, secondarySources) {
fs.promises.writeFile(generatedFilePath, Sass.renderSync({
data: primarySource.getContents(),
importer: [
function (url) {
return {
contents: secondarySources[url].getContents()
}
}
],
sourceComments: true
}).css
).then(() => {
console.log(`generated ${generatedFilePath}`)
}).catch((err) => {
console.log(`ERROR: Failed to generate ${generatedFilePath}`)
console.error(err)
})
}
const sources = {
'index.scss': new SourceFile('./css/scss/index.scss')
}
const buildTrees = [
new DependencyTree(
'./css/index.css',
sources['index.scss'],
[],
buildCSS
)
]
// Lazily builds generated files from dependencies
// @param {DependencyTree[]} buildTrees A list of the files to be generated and their dependencies represented as trees
// @throws {TypeError} when an argument is of the wrong type
function build (buildTrees) {
if (!(buildTrees instanceof Array)) {
throw new TypeError('Param buildTrees must be an array')
}
buildTrees.forEach((buildTree, index) => {
if (!(buildTree instanceof DependencyTree)) {
throw new TypeError(`Param buildTrees must contain only DependencyTree objects. Encountered incompatible object at index: ${index}`)
}
})
console.log('INFO: Generated files will only be updated if the source files were last modified later than the generated files.')
buildTrees.forEach((buildTree) => {
buildTree.statFiles()
.then(() => {
if (buildTree.isOutdated()) {
buildTree.generateFile()
}
})
})
}
build(buildTrees)