forked from decaporg/decap-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoconfigure.collection.js
executable file
·150 lines (135 loc) · 3.79 KB
/
autoconfigure.collection.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
143
144
145
146
147
148
149
150
import fs from "fs";
import path from "path";
import process from "process";
import yaml from 'js-yaml';
import deepEqual from 'deep-equal';
import { formatByExtension } from "../src/formats/formats";
const looksLikeMarkdown = /(\[.+\]\(.+\)|\n?[#]+ [a-z0-9])/; // eslint-disable-line
const looksLikeAnImage = /^[^ ]+\.(png|jpg|svg|gif|jpeg)/;
function capitalize(name) {
return name.substr(0, 1).toUpperCase() + name.substr(1);
}
function inferWidget(name, value) {
if (value == null) {
return { widget: 'string' };
}
if (value instanceof Date) {
return { widget: value.toJSON().match(/T00:00:00\.000Z$/) ? 'date' : 'datetime' };
}
if (value instanceof Array) {
if (typeof value[0] === 'string') {
return { widget: 'list' };
}
return { widget: 'list', fields: inferFields(value) };
}
if (typeof value === 'object') {
return { widget: 'object', fields: inferFields([value]) };
}
if (value === false || value === true) {
return { widget: 'checkbox' };
}
if (typeof value === 'number') {
return { widget: 'number' };
}
if (name === 'body' || value.match(looksLikeMarkdown)) {
return { widget: 'markdown' };
}
if (value.match(/\n/)) {
return { widget: 'text' };
}
if (value.match(looksLikeAnImage)) {
return { widget: 'image' };
}
return { widget: 'string' };
}
function inferField(name, value) {
return Object.assign({
label: capitalize(name.replace(/_-/g, ' ')),
name,
}, inferWidget(name, value));
}
function inferFields(entries) {
const fields = {};
entries.forEach((entry) => {
if (entry == null) { return; }
Object.keys(entry).forEach((fieldName) => {
const field = inferField(fieldName, entry[fieldName]);
if (fields[fieldName]) {
fields[fieldName] = combineFields(fields[fieldName], field);
} else {
fields[fieldName] = field;
}
});
});
return Object.keys(fields).map(key => fields[key]);
}
const widgetRank = {
markdown: 1,
text: 2,
string: 3,
image: 4,
datetime: 4,
date: 5,
number: 5,
object: 7,
list: 7,
};
function compareWidget(a, b) {
return widgetRank[a] - widgetRank[b];
}
function combineFields(a, b) {
if (b == null && a) {
return a;
}
if (a == null && b) {
return b;
}
if (deepEqual(a, b)) {
return a;
}
if (a.widget === b.widget) {
if (a.fields && b.fields) {
const newFields = {};
a.fields.forEach((field) => {
newFields[field.name] = combineFields(field, b.fields.find(f => f.name === field.name));
});
b.fields.forEach((field) => {
if (!newFields[field.name]) {
newFields[field.name] = field;
}
});
return Object.assign({}, a, { fields: Object.keys(newFields).map(k => newFields[k]) });
}
return a;
}
return [a, b].sort((fieldA, fieldB) => compareWidget(fieldB.widget, fieldA.widget))[0];
}
if (process.argv.length !== 3) {
console.log("Usage: autoconfigure.collections.js <path-to-my-folder>");
process.exit(1);
}
const folder = process.argv[2].replace(/\/$/, '');
const files = fs.readdirSync(folder);
const extensions = {};
files.forEach((file) => {
const ext = file.split(".").pop();
if (ext) {
extensions[ext] = extensions[ext] || 0;
extensions[ext] += 1;
}
});
const name = folder.split('/').filter(s => s).pop();
const extension = Object.keys(extensions).sort((a, b) => extensions[b] - extensions[a])[0];
const format = formatByExtension(extension);
const entries = files.filter(name => name.split(".").pop() === extension).slice(0, 100).map(file => (
format.fromFile(fs.readFileSync(path.join(folder, file), { encoding: 'utf8' }))
));
const fields = inferFields(entries);
const collection = {
label: capitalize(name),
name,
folder,
extension,
fields,
};
console.log(yaml.safeDump([collection], { flowLevel: 3 }));