forked from snipcart/metalsmith-directus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
142 lines (115 loc) · 4.26 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
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
var _ = require('lodash');
var each = require('async').each;
var debug = require('debug')('metalsmith-directus');
var slug = require('slug-component');
var SDK = require('directus-sdk-javascript');
module.exports = plugin;
function plugin(options){
enforcep(options, 'accessToken');
enforcep(options, 'baseURL');
return function(files, metalsmith, done){
var keys = Object.keys(files);
each(keys, processFile, done);
//TODO: switch to bluebird promises
function processFile(file, fileProcessedCallback){
var fileMetadata = files[file];
if (!fileMetadata.directus) {
fileProcessedCallback();
return;
}
enforcep(fileMetadata.directus, 'table');
/*
* entries will contain the entries fetched from the API
* for this file
*/
fileMetadata.directus.entries = [];
var client = new SDK(
options.accessToken,
options.baseURL,
1);
var query = {
"params": fileMetadata.directus.filter,
"table": fileMetadata.directus.table
}
client.getItems(query)
.then(onSuccessfulEntriesFetch(fileMetadata.directus, fileProcessedCallback))
.catch(onErroneousEntriesFetch(fileProcessedCallback));
debug('Processed file ' + file );
}
function onSuccessfulEntriesFetch(options, done) {
return function(data){
each(data.rows,
entryProcessor({
entries : options.entries,
template : options.entry_template,
table : options.table,
filenameField : options.entry_filename_pattern,
asPermalink : options.permalink_style,
useTemplateExtension : options.use_template_extension
}),
done);
};
}
function onErroneousEntriesFetch(done) {
return function(err) {
debug('An unexpected error happened while trying to fetch the entries (' + err.message +')');
done();
};
}
function entryProcessor(options) {
return function (entry, entryProcessedCallback){
var file;
var extension = (options.useTemplateExtension) ? options.template.split('.').slice(1).pop() : 'html';
var getFilename = function getFilename (entry) {
var filename = file.id + "." + extension;
var getParams = function getParams (pattern) {
var matcher = /:([\w]+\.[\w]+)/g;
var ret = [];
var m;
while (m == matcher.exec(pattern)) ret.push(m[1]);
return ret;
};
if (options.filenameField) {
var pattern = options.filenameField;
var params = getParams(pattern);
params.forEach(function(element, index){
var element_parts = element.split('.');
var prefix = element_parts[0];
var item = element_parts[1];
if (entry[prefix] && void 0 !== entry[prefix][item]) {
pattern = pattern.replace(":" + element, slug(entry[prefix][item].toString()));
}
});
// Check all have been processed
if (getParams(pattern).join('') === '') {
filename = (options.asPermalink) ? pattern + "/index.html" : pattern + "." + extension;
}
}
return filename;
};
/*
* Create a "virtual" (virtual because it doesn't exist in the src/ dir)
* file that will be processed by metalsmith
*/
file = {
contents : "", // Contents needs to be defined beacuse other plugins expect it
data : entry,
id : entry.id,
template : options.template
};
if (options.template){
files[getFilename(entry)] = file;
}
options.entries.push(file);
entryProcessedCallback();
};
}
};
function exists(value){
return value !== null;
}
function enforcep(object, property) {
if (!exists(object[property]))
throw new TypeError('Expected property ' + property);
}
}