-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-block.js
155 lines (130 loc) · 3.81 KB
/
make-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
'use strict';
import fs from 'fs';
import path from 'path';
import { createInterface } from 'readline';
const rl = createInterface(process.stdin, process.stdout);
// folder with all blocks
const BLOCKS_DIR = path.join(__dirname, 'app/blocks');
// //////////////////////////////////////////////////////////////////////////////////////////////////
// default content for files in new block
const fileSources = {
jade: `mixin {blockName}()\n\t+b.{blockName}&attributes(attributes)\n\t\tblock\n`,
styl: `.{blockName}\n\tdisplay block\n`
};
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.`);
}
});
});
}
function createDir(dirPath) {
return new Promise((resolve, reject) => {
fs.mkdir(dirPath, err => {
if (err) {
reject(`ERR>>> Failed to create a folder '${dirPath}'`);
} 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}'`);
} 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(candidateBlockName) {
const blockNames = candidateBlockName.trim().split(/\s+/);
const makeBlock = 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 'app/blocks/${blockName}'`);
console.log(line);
// Displays a list of files created
files.forEach(file => console.log(file));
rl.close();
});
};
if (blockNames.length === 1) {
return makeBlock(blockNames[0]);
}
const promises = blockNames.map(name => makeBlock(name));
return Promise.all(promises);
}
// //////////////////////////////////////////////////////////////////////////
//
// 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(s) name: ');
rl.prompt();
rl.on('line', (line) => {
initMakeBlock(line).catch(printErrorMessage);
});
}