This repository has been archived by the owner on Nov 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
96 lines (73 loc) · 2.67 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 fs = require('fs-extra'),
path = require('path'),
keystonePath = path.join(module.paths[1],'./keystone'),
keystoneTypesDirPath = path.join(keystonePath,'fields'+path.sep+'types'),
keystoneLibFieldTypesFile = path.join(keystonePath,'lib'+path.sep+'fieldTypes.js'),
keystoneAdminFieldsFile = path.join(keystonePath,'admin'+path.sep+'src'+path.sep+'fields.js');
function copyTypesLibs(dirPath){
var typesDirs = fs.readdirSync(dirPath).filter(function(file) {
return fs.statSync(path.join(dirPath, file)).isDirectory();
});
typesDirs.forEach(function(dir){
var keystonePath = path.join(keystoneTypesDirPath, dir),
overwrite = true;
try{
stats = fs.statSync(keystonePath);
fs.removeSync(keystonePath)
}catch(err){
overwrite=false;
}
fs.copySync(path.join(dirPath, dir), path.join(keystoneTypesDirPath, dir));
console.log((overwrite ? 'Overwrite ' : 'Added ')+'custom fieldType: ' + dir);
});
}
function GetAvailableTypes(){
return fs.readdirSync(keystoneTypesDirPath).filter(function(file) {
return fs.statSync(path.join(keystoneTypesDirPath, file)).isDirectory();
}).map(function(type){
var typeDirPath = path.join(keystoneTypesDirPath,type),
name = fs.readdirSync(path.join(typeDirPath)).filter(function(file) {
return fs.statSync(path.join(typeDirPath, file)).isFile();
})[0];
return name ? {
Dir : type,
FullNameType : name.replace('Type','').replace('Field','').replace('.js','')
} : null;
});
}
function WriteLibFile(Types){
var fileText = 'var fields = { \n';
Types.forEach(function(type,index){
if (type){
fileText += '\t get '+type.FullNameType+' () { return require(\'../fields/types/'+type.Dir+'/'+type.FullNameType+'Type\'); }';
fileText += (index!= Types.length-1) ? ', \n' : '\n';
}
})
fileText += '};\nmodule.exports = fields;'
fs.writeFileSync(keystoneLibFieldTypesFile, fileText, 'utf8');
console.log('Updated: ' + keystoneLibFieldTypesFile);
}
function WriteAdminFile(Types){
var fileText = 'module.exports = {\n';
Types.forEach(function(type,index){
if (type){
fileText += '\t'+type.Dir+':\trequire(\'../../fields/types/'+type.Dir+'/'+type.FullNameType+'Field\')';
fileText += (index!= Types.length-1) ? ', \n' : '\n';
}
})
fileText += '};'
fs.writeFileSync(keystoneAdminFieldsFile, fileText, 'utf8');
console.log('Updated: ' + keystoneAdminFieldsFile);
}
module.exports = {
loadFromDir: function(dirPath) {
dirPath = dirPath || 'fieldTypes';
var stats = fs.statSync(dirPath);
if (!stats.isDirectory())
throw 'Path is not a directory';
copyTypesLibs(dirPath);
var availableTypes = GetAvailableTypes()
WriteLibFile(availableTypes);
WriteAdminFile(availableTypes);
}
}