-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
96 lines (90 loc) · 2.93 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
var customize = require('customize-watch')
var Q = require('q')
var write = require('customize-write-files')
var fs = require('fs')
var httpGet = require('get-promise')
var yaml = require('js-yaml')
// preconfigured Customize instance.
module.exports = customize()
.registerEngine('handlebars', require('customize-engine-handlebars'))
.registerEngine('less', require('customize-engine-less'))
.registerEngine('uglify', require('customize-engine-uglify'))
// Customize type for adding methods
var Customize = customize.Customize
Customize.prototype.build = function (jsonFile, targetDir) {
var withData = this.merge({
handlebars: {
data: loadFromFileOrHttp(jsonFile)
.catch(function (err) {
// Augment error for identification in the cli script
err.cause = 'bootprint-load-data'
throw err
})
}
})
return new Bootprint(withData, targetDir)
}
/**
* The old Bootprint interface
* @constructor
*/
function Bootprint (withData, targetDir) {
/**
* Run Bootprint and write the result to the specified target directory
* @param options {object} options passed to Customize#run()
* @returns {Promise} a promise for the completion of the build
*/
this.generate = function generate (options) {
return withData.run(options).then(write(targetDir))
}
/**
* Run the file watcher to watch all files loaded into the
* current Bootprint-configuration.
* The watcher run Bootprint every time one the the input files, templates or helpers changes.
* @returns {EventEmitter} an EventEmitter that sends an `update`-event after each
* build, but before the files are written to disc.
*/
this.watch = function () {
return withData.watch().on('update', write(targetDir))
}
}
/**
* Helper method for loading the bootprint-data
* @param fileOrUrlOrData
* @returns {*}
* @private
*/
function loadFromFileOrHttp (fileOrUrlOrData) {
// If this is not a string,
// it is probably already the raw data.
if (typeof fileOrUrlOrData !== 'string') {
return Q(fileOrUrlOrData)
}
// otherwise load data from url or file
if (fileOrUrlOrData.match(/^https?:\/\//)) {
// Use the "request" package to download data
return httpGet(fileOrUrlOrData, {
redirect: true,
headers: {
'User-Agent': 'Bootprint/' + require('./package').version
}
}).then(function (result) {
if (result.status !== 200) {
var error = new Error('HTTP request failed with code ' + result.status)
error.result = result
throw error
}
return yaml.safeLoad(result.data, {json: true})
}, function (error) {
if (error.status) {
throw new Error('Got ' + error.status + ' ' + error.data + ' when requesting ' + error.url, 'E_HTTP')
} else {
throw error
}
})
} else {
return Q.nfcall(fs.readFile, fileOrUrlOrData, 'utf8').then(function (data) {
return yaml.safeLoad(data, {json: true})
})
}
}