-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblock.js
184 lines (157 loc) · 4.49 KB
/
block.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
'use strict';
const fs = require('fs')
const path = require('path')
const colors = require('colors')
const readline = require('readline')
const rl = readline.createInterface(process.stdin, process.stdout);
// folder with all blocks
const BLOCKS_DIR = path.join(__dirname, 'sourse/pug/blocks');
//////////////////////////////////////////////////////////////////////////////////////////////////
// default content for files in new block
const fileSources = {
pug: `mixin {blockName}(data)
// start {blockName}
+b.SECTION.{blockName}.section#{blockName}&attributes(attributes)
.container
+b.section-title.text-center
h2 {blockName}
.row
// end {blockName}`,
scss: `// start .{blockName}
.{blockName} \{
// --sPT: ;
// --sPB: ;
// --sTPB: ;
@include media-breakpoint-up(xl) {}
@include media-breakpoint-up(lg) {}
@include media-breakpoint-up(md) {}
@include media-breakpoint-up(sm) {}
@include media-breakpoint-between(md, xl) {}
@include media-breakpoint-only(xl) {}
@include media-breakpoint-down(xl) {}
} // end.{blockName}`
,
// js: `let {blockName}Vue = new Vue({
// el: '#{blockName}',
// data: {
// imgSRc: 'img/',
// },
// methods: {
// },
// created: function () {
// },
// computed: {
// },
// })`
};
function validateBlockName(blockName) {
return new Promise((resolve, reject) => {
const isValid = /^(\d|\w|-)+$/.test(blockName);
if (isValid) {
resolve(isValid);
} else {
const errMsg = (
`ERR>>> An incorrect block name '${blockName}'\n` +
`ERR>>> A block name must include letters, numbers & the minus symbol.`
);
reject(errMsg);
}
});
}
function directoryExist(blockPath, blockName) {
return new Promise((resolve, reject) => {
fs.stat(blockPath, notExist => {
if (notExist) {
resolve();
} else {
reject(`ERR>>> The block '${blockName}' already exists.`.red);
}
});
});
}
function createDir(dirPath) {
return new Promise((resolve, reject) => {
fs.mkdir(dirPath, err => {
if (err) {
reject(`ERR>>> Failed to create a folder '${dirPath}'`.red);
} else {
resolve();
}
});
});
}
function createFiles(blocksPath, blockName) {
const promises = [];
Object.keys(fileSources).forEach(ext => {
const fileSource = fileSources[ext].replace(/\{blockName}/g, blockName);
const filename = `_${blockName}.${ext}`;
const filePath = path.join(blocksPath, filename);
promises.push(
new Promise((resolve, reject) => {
fs.writeFile(filePath, fileSource, 'utf8', err => {
if (err) {
reject(`ERR>>> Failed to create a file '${filePath}'`.red);
} else {
resolve();
}
});
})
);
});
return Promise.all(promises);
}
function getFiles(blockPath) {
return 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);
}
});
});
}
function printErrorMessage(errText) {
console.log(errText);
rl.close();
}
// //////////////////////////////////////////////////////////////////////////
function initMakeBlock(blockName) {
const blockPath = path.join(BLOCKS_DIR, blockName);
return validateBlockName(blockName)
.then(() => directoryExist(blockPath, blockName))
.then(() => createDir(blockPath))
.then(() => createFiles(blockPath, blockName))
.then(() => getFiles(blockPath))
.then(files => {
const line = '-'.repeat(48 + blockName.length);
console.log(line);
console.log(`The block has just been created in 'sourse/pug/blocks/${blockName}'`);
console.log(line);
// Displays a list of files created
files.forEach(file => console.log(file.yellow));
rl.close();
});
}
// //////////////////////////////////////////////////////////////////////////
//
// Start here
//
// Command line arguments
const blockNameFromCli = process.argv
.slice(2)
// join all arguments to one string (to simplify the capture user input errors)
.join(' ');
// If the user pass the name of the block in the command-line options
// that create a block. Otherwise - activates interactive mode
if (blockNameFromCli !== '') {
initMakeBlock(blockNameFromCli).catch(printErrorMessage);
}
else {
rl.setPrompt('Block name: '.magenta);
rl.prompt();
rl.on('line', (line) => {
const blockName = line.trim();
initMakeBlock(blockName).catch(printErrorMessage);
});
}