-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.js
159 lines (124 loc) · 5.55 KB
/
generator.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
151
152
153
154
155
156
157
158
159
const path = require('path');
const fs = require('fs-extra');
const tmp = require('tmp');
const ejs = require('ejs');
const utils = require('./utils');
const patterns = require('./patterns');
const EJS_EXTENSION = '.ejs';
const DS_Store = '.DS_Store';
const getStuffName = (currentPath) => {
return path.basename(currentPath);
};
const onError = (message, err, reject) => {
throw console.error(message, err);
reject(err);
}
const createTempFolder = () => new Promise((resolve, reject) => {
tmp.dir(function _tempDirCreated(err, path) {
if (err) onError('Error creating a temporal folder: ', err, reject);
resolve(path);
});
});
const delimitate = (exp, delimiter) => {
return new RegExp('\\'+delimiter+exp+'\\'+delimiter, 'gi');
}
const resourcePatronize = (resource, stuffName, delimiter, vars) => {
let res = resource.replace(delimitate('name', delimiter), stuffName);
String.prototype.replaceFunctionPattern = function (pattern, func) {
return this.replace(delimitate(pattern+'\\(.*\\)', delimiter), w => func(w.replace(delimitate(pattern+'\\((.*)\\)', delimiter), '$1')))
}
const replacer = (key, value) => {
if(value instanceof Function){
return res.replaceFunctionPattern(key, value);
}else
return res.replace(delimitate(key, delimiter), value);
}
for(let v in vars){
res = replacer(v, vars[v]);
};
for(let p in patterns.list){
if(patterns.notAvailableOnFilenames.indexOf(p) === -1)
res = replacer(p, patterns.list[p]);
};
return res;
};
const generateFileFromTemplate = (stuffName, resourcePath, destinyPath, delimiter, vars) => new Promise((resolve, reject) => {
fs.readFile(resourcePath, 'utf8', (err, data) => {
if(err) onError('Error reading the file "'+file+'"', err, reject);
try {
global[utils.GLOBAL_CURRENT_EJS_FILE_CONTEXT] = {
absoluteStuffPath,
cacheStuffFolderPath,
destinyPath
};
const component = ejs.render(data, {
...vars,
"name": stuffName,
...patterns.list
}, {delimiter: delimiter});
const p = destinyPath.replace(new RegExp(EJS_EXTENSION+'$'), '');
fs.writeFile(p, component);
resolve();
}catch(err){
onError('Opss!', err, reject);
}
});
});
const copyResourcesToTempFolder = (stuffName, folderPath, destinyFolderPath, delimiter, vars) => new Promise((resolve, reject) => {
fs.readdir(folderPath, (err, resources) => {
if (err) onError('Could not list the directory: ', err, reject);
resources.forEach(resource => {
let resourcePath = path.join(folderPath, resource);
let destinyPath = path.join(destinyFolderPath, resourcePatronize(resource, stuffName, delimiter, vars));
fs.stat(resourcePath, function (err, stat) {
if (err) onError('Error getting info. of the file "'+resource+'"', err, reject);
if (stat.isFile()){
const extension = path.extname(resourcePath);
if(extension === EJS_EXTENSION){
generateFileFromTemplate(stuffName, resourcePath, destinyPath, delimiter, vars)
.then(resolve)
.catch(reject);
}else{
if(path.basename(resource) !== DS_Store)
fs.copy(resourcePath, destinyPath)
.then(resolve)
.catch(err => onError('Could not copy "'+resource+'"', err, reject));
else resolve();
}
}else if (stat.isDirectory()){
fs.mkdir(destinyPath, {}, (err) => {
if (err) onError('Could not create the folder "'+resource+'"', err, reject);
copyResourcesToTempFolder(stuffName, resourcePath, destinyPath, delimiter, vars)
.then(resolve)
.catch(reject);
});
}
});
});
});
});
const moveToPackageDestiny = (folderPath, destinyFolderPath) => new Promise((resolve, reject) => {
fs.move(folderPath, destinyFolderPath, { overwrite: true }, err => {
if (err)
return onError('Error moving xtuff to destiny folder', err, reject);
fs.remove(folderPath, err => {
if (err) return onError('Error removing tmp folder', err, reject);
resolve();
});
});
});
let absoluteStuffPath;
let cacheStuffFolderPath;
module.exports = (generatorName, stuffPath, delimiter = '%', vars = {}) => {
const generatorTemplateFolder = path.join(utils.getTemplatesFolder(), generatorName);
if (fs.existsSync(generatorTemplateFolder)) {
const stuffName = getStuffName(stuffPath);
absoluteStuffPath = !path.isAbsolute(stuffPath) ? path.join(process.env.INIT_CWD, stuffPath) : stuffPath;
createTempFolder().then(cacheFolderPath => {
cacheStuffFolderPath = cacheFolderPath;
copyResourcesToTempFolder(stuffName, generatorTemplateFolder, cacheFolderPath, delimiter, vars).then(() => {
moveToPackageDestiny(cacheFolderPath, absoluteStuffPath);
});
});
}else console.error('Component folder not found :(', generatorTemplateFolder);
}