-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
70 lines (61 loc) · 1.86 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
const Promise = require("bluebird");
const Client = require('./lib/client');
const Node = require('./lib/node');
const types = require('./lib/node-types');
const fs = Promise.promisifyAll(require('fs'));
const lwip = Promise.promisifyAll(require('lwip'));
function Magnolia(options) {
this.client = new Client(options);
}
Magnolia.prototype.node = function(path, type) {
return new Node(this.client, path, type);
}
Magnolia.prototype.page = function(path, template) {
return new types.Page(this.client, path, template);
}
Magnolia.prototype.gatoPage = function(path) {
let page = this.page(path, 'gato-template-txstate2015:pages/standard-template');
page.addNode(this.node('contentParagraph', 'mgnl:area'))
.addNode(this.component('0', 'gato-template:components/rows/full'))
.addNode(this.area('column1'));
return page;
}
Magnolia.prototype.area = function(path, template) {
return new types.Area(this.client, path, template);
}
Magnolia.prototype.component = function(path, template) {
return new types.Component(this.client, path, template);
}
Magnolia.prototype.asset = function(path, options) {
return new types.Asset(this.client, path, options);
}
Magnolia.prototype.image = function(path, file, type) {
if (!type) {
type = file.slice(file.lastIndexOf('.') + 1);
}
let buffer, size;
return fs.readFileAsync(file)
.then(b => {
buffer = b;
})
.then(() => fs.statAsync(file))
.then(stats => {
size = stats.size;
return lwip.openAsync(file, type);
})
.then(image => {
let asset = this.asset(path, {
type: type,
fileName: file,
mimeType: `image/${type}`,
data: buffer
});
asset.down('jcr:content').setProperties({
width: image.width(),
height: image.height(),
size: size
});
return asset;
});
}
module.exports = Magnolia;