-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmake.js
More file actions
executable file
·224 lines (186 loc) · 5.85 KB
/
make.js
File metadata and controls
executable file
·224 lines (186 loc) · 5.85 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/usr/bin/env node
/**
* Dependencies
*/
const fs = require('fs');
const Path = require('path');
const Readline = require('readline');
const resolve = require(`${__dirname}/util/resolve`);
const cnsl = require(`${__dirname}/util/console`);
const alerts = resolve('config/alerts');
const config = resolve('config/make');
const global = resolve('config/global');
/**
* Constants
*/
const MAKE = process.argv.indexOf('make');
const TYPE = (process.argv[MAKE + 1] === 'utility')
? 'utilities' : `${process.argv[MAKE + 1]}s`;
const PATTERN = process.argv[MAKE + 2];
const FILE = process.argv[MAKE + 3];
const FILENAMES = Object.keys(config.files)
.filter(f => config.optional.indexOf(f) === -1);
let prompt = Readline.createInterface({
input: process.stdin,
output: process.stdout
});
/**
* Parse variables in the config/make.js template strings.
*/
const parseVariables = (str) => str
.split('{{ type }}').join(TYPE)
.split('{{ prefix }}').join(config.prefixes[TYPE])
.split('{{ pattern }}').join(PATTERN)
.split('{{ Pattern }}').join(
PATTERN
.split('-').join(' ')
.split('_').join(' ')
.charAt(0).toUpperCase() + PATTERN.slice(1)
);
/** Evaluate a yes answer */
const yes = str => str.indexOf('y') === 0;
/** An async forEach function */
const each = async (array, callback) => {
for (let i = 0; i < array.length; i++) {
await callback(array[i], i, array);
}
}
/**
* Log a message from the messages config. Parses variables in message.
*
* @param {String} type The key of the message to write.
*/
const logInfo = (type) => (config.messages[type]) ?
cnsl.describe(parseVariables(config.messages[type].join(''))) : false;
/**
* Create the directory for the pattern if it doesn't exist.
*
* @param {String} dir The directory to write.
* @param {String} type The pattern type: elements, components, objects
* @param {Function} callback They file writing function.
*/
const directory = (dir, type, callback) => {
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, {recursive: true});
callback(true);
} else {
callback(false);
}
};
/**
* Make the file for the pattern if it doesn't exist.
*
* @param {String} dir The directory to write.
* @param {String} type The pattern type: elements, components, objects.
* @param {String} pattern The name of the pattern.
*/
const write = async (dir, filetype, callback) => {
let file = parseVariables(config.files[filetype]);
if (!fs.existsSync(`${dir}/${file}`)) {
let ext = config.files[filetype].split('.')[1];
let templatePath = resolve(`config/make/${filetype}.${ext}`, false);
let template = fs.readFileSync(templatePath, 'utf8');
let content = parseVariables(template);
// Make the directory for the file if it doesn't exist
if (!fs.existsSync(`${dir}`)) {
fs.mkdirSync(dir, {recursive: true});
}
fs.writeFile(`${dir}/${file}`, content, err => {
if (err) {
cnsl.error(`Make failed (write): ${err.stack}`);
return false;
}
});
callback(true, `${file}`);
} else {
callback(false, `${file}`);
}
};
/**
* Delegate to create the pattern directory, if it exists, log message.
*
* @param {String} type The pattern type.
* @param {String} dir The directory to write.
* @param {String} pattern The name of the pattern.
*/
const defaults = (type, pattern, callback) => {
let relative = Path.join(global.src, type);
let absolute = Path.join(global.base, relative, pattern);
directory(absolute, type, (success) => {
if (success) {
FILENAMES.forEach(filetype => {
write(absolute, filetype, (success, filename) => {
if (success) {
cnsl.success(`Created ${alerts.str.path('./' + Path.join(relative, pattern, filename))}`);
logInfo(filetype);
}
});
});
callback();
} else {
cnsl.error(`${alerts.str.path('./' + relative + '/' + pattern)} already exists.`);
callback();
}
});
};
/**
* Ask if we want to make the optional files.
*
* @param {String} filetype The pattern type: elements, components, objects
* @param {String} pattern The name of the pattern
* @param {[type]} prompt The Readline prompt to ask if you want to creat a config
*/
const optional = (filetype, pattern, prompt) => {
return new Promise(resolve => {
let isPattern = config.patterns.indexOf(filetype) > -1;
let path = (isPattern) ? config.paths.pattern : config.paths[filetype]; // use the patterns default path instead
let relative = parseVariables(path);
let absolute = Path.join(global.base, relative);
prompt.question(
`${alerts.question} Make a ${alerts.str.string(filetype)} file for ${alerts.str.string(pattern)}? y/n ↵ `,
(answer) => {
if (yes(answer))
write(absolute, filetype, (success, file) => {
if (success) {
cnsl.success(`${alerts.str.path('./' + Path.join(relative, file))} was made.`);
logInfo(filetype);
} else {
cnsl.error(`${alerts.str.path('./' + Path.join(relative, file))} already exists.`);
}
});
resolve();
}
);
});
};
/**
* The main runner for the script
*/
const run = async () => {
try {
if (FILE) {
(async () => {
await optional(FILE, PATTERN, prompt);
prompt.close();
})();
return;
}
// Make the standard files, then...
defaults(TYPE, PATTERN, () => {
// ... ask to make the option files
(async () => {
await each(config.optional, async (type) => {
await optional(type, PATTERN, prompt);
});
prompt.close();
})();
});
} catch (err) {
cnsl.error(`Make failed (run): ${err.stack}`);
}
};
/** @type {Object} Export our methods */
module.exports = {
run: run,
config: config
};