-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.js
200 lines (168 loc) · 5.37 KB
/
make.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
const program = require('commander');
const fs = require('fs');
const path = require('path');
const {
promisify
} = require('util');
/* eslint-disable global-require */
const sources = {
block: require('./templates/block'),
component: require('./templates/component'),
page: require('./templates/page'),
scss: require('./templates/scss'),
js: require('./templates/js'),
};
/* eslint-enable global-require */
const dirPath = {
block: path.resolve('app/blocks'),
page: path.resolve('app/pages'),
component: path.resolve('app/components'),
};
const validateName = (name, kind) => (
new Promise((resolve, reject) => {
const isValid = /^(\d|\w|-)+$/.test(name);
if (isValid) {
resolve(isValid);
} else {
const errMsg = (
`ERR>>> An incorrect ${kind} name '${name}'\n` +
'ERR>>> A block name must include letters, numbers & the minus symbol.'
);
reject(errMsg);
}
})
);
const directoryExist = (blockPath, name, kind) => (
new Promise((resolve, reject) => {
fs.stat(blockPath, (notExist) => {
if (notExist) {
resolve();
} else {
console.log(blockPath);
reject(`ERR>>> The ${kind} '${name}' already exists.`);
}
});
})
);
const createDir = blockPath => (
new Promise((resolve, reject) => {
fs.mkdir(blockPath, (err) => {
if (err) {
reject(`ERR>>> Failed to create a folder '${dirPath}'`);
} else {
resolve();
}
});
})
);
const generateFileSources = (name, kind, js) => {
const data = {};
data.pug = sources[kind](name).trim();
data.scss = sources.scss(name).trim();
if (js) {
data.js = sources.js(name).trim();
}
return Promise.resolve(data);
};
const createFiles = (blockPath, name, files) => (
Object.keys(files).map((ext) => {
const filePath = path.join(blockPath, `${name}.${ext}`);
const fileSource = files[ext];
return new Promise((resolve, reject) => {
fs.writeFile(filePath, fileSource, 'utf8', (err) => {
if (err) {
reject(`ERR>>> Failed to create a file '${filePath}'`);
} else {
resolve();
}
});
});
})
);
const getFiles = blockPath => (
new Promise((resolve, reject) => {
fs.readdir(blockPath, (err, files) => {
if (err) {
reject(`ERR>>> Failed to get a file list from a folder '${blockPath}'`);
} else {
resolve(files);
}
});
})
);
const appendToIncludes = (kind, blockName) => {
const filePath = kind === 'page' ? './app/pages/sitemap/sitemap.pug' : './app/layouts/_internalIncludes.pug';
if (['component', 'block', 'page'].indexOf(kind) === -1) {
return;
}
const file = fs.readFileSync(filePath, 'utf8');
const includeString = kind === 'page' ? ` +e.A.link(href="${blockName}.html") Страница ${blockName}` : `include ../${kind}s/${blockName}/${blockName}`;
let lines = file.split(/\n/);
if (lines.slice(-1)[0].length < 1) {
lines.pop();
}
if (lines.indexOf(includeString) !== -1) {
return console.log(`>>> ${kind} ${blockName} already included to '${filePath}'`);
}
lines.push(includeString);
const nextFile = lines.join('\n');
fs.writeFileSync(filePath, nextFile, 'utf8');
};
const make = (name, kind, js) => {
const blockPath = path.join(dirPath[kind], name);
return validateName(name, kind)
.then(() => directoryExist(blockPath, name, kind))
.then(() => createDir(blockPath))
.then(() => generateFileSources(name, kind, js))
.then(files => createFiles(blockPath, name, files))
.then(() => getFiles(blockPath))
.then((files) => {
const line = '-'.repeat(48 + name.length);
console.log(line);
console.log(`The block has just been created in 'app/${kind}s/${name}'`);
console.log(line);
// Displays a list of files created
files.forEach(file => console.log(file));
})
.then(() => ({
kind,
name
}));
};
const printError = err => console.log(err);
program
.command('block [blockNames...]')
.option('--js', 'Generate script file')
.action(async (blockNames, opts) => {
if (blockNames === undefined) {
return console.log('Please enter blockName');
}
const promises = blockNames.map(name => make(name, 'block', opts.js));
const blocks = await Promise.all(promises).catch(printError);
blocks.forEach(block => appendToIncludes(block.kind, block.name));
});
program
.command('component [componentNames...]')
.option('--js', 'Generate script file')
.action(async (componentNames, opts) => {
if (componentNames === undefined) {
return console.log('Please enter componentName');
}
const promises = componentNames.map(name => make(name, 'component', opts.js));
Promise.all(promises).catch(printError);
const blocks = await Promise.all(promises).catch(printError);
blocks.forEach(block => appendToIncludes(block.kind, block.name));
});
program
.command('page [pageNames...]')
.option('--js', 'Generate script file')
.action(async (pageNames, opts) => {
if (pageNames === undefined) {
return console.log('Please enter pageName');
}
const promises = pageNames.map(name => make(name, 'page', opts.js));
Promise.all(promises).catch(printError);
const blocks = await Promise.all(promises).catch(printError);
blocks.forEach(block => appendToIncludes(block.kind, block.name));
});
program.parse(process.argv);