Skip to content

Commit 8ce857f

Browse files
committed
Make files from file templates. Recursively make directories > files.
1 parent 5838aaf commit 8ce857f

File tree

9 files changed

+210
-219
lines changed

9 files changed

+210
-219
lines changed

bin/make.js

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,24 @@ const each = async (array, callback) => {
5353
}
5454
}
5555

56-
/** Log a message from MSG. */
56+
/**
57+
* Log a message from the messages config. Parses variables in message.
58+
*
59+
* @param {String} type The key of the message to write.
60+
*/
5761
const logInfo = (type) => (config.messages[type]) ?
5862
cnsl.describe(parseVariables(config.messages[type].join(''))) : false;
5963

6064
/**
61-
* Create the directory for the pattern if it doesn't exist
65+
* Create the directory for the pattern if it doesn't exist.
6266
*
63-
* @param {String} dir The directory to write
67+
* @param {String} dir The directory to write.
6468
* @param {String} type The pattern type: elements, components, objects
65-
* @param {Function} callback They file writing function
66-
*
67-
* @return {[type]} false if directory exists
69+
* @param {Function} callback They file writing function.
6870
*/
6971
const directory = (dir, type, callback) => {
7072
if (!fs.existsSync(dir)) {
71-
fs.mkdirSync(dir);
73+
fs.mkdirSync(dir, {recursive: true});
7274

7375
callback(true);
7476
} else {
@@ -77,19 +79,21 @@ const directory = (dir, type, callback) => {
7779
};
7880

7981
/**
80-
* Make the file for the pattern if it doesn't exist
82+
* Make the file for the pattern if it doesn't exist.
8183
*
82-
* @param {String} dir The directory to write
83-
* @param {String} type The pattern type: elements, components, objects
84-
* @param {String} pattern The name of the pattern
85-
*
86-
* @return {Boolean} false if already exists, true if created
84+
* @param {String} dir The directory to write.
85+
* @param {String} type The pattern type: elements, components, objects.
86+
* @param {String} pattern The name of the pattern.
8787
*/
88-
const write = (dir, filetype, callback) => {
88+
const write = async (dir, filetype, callback) => {
8989
let file = parseVariables(config.files[filetype]);
9090

9191
if (!fs.existsSync(`${dir}/${file}`)) {
92-
let content = parseVariables(config.templates[filetype]);
92+
let ext = config.files[filetype].split('.')[1];
93+
let templatePath = resolve(`config/make/${filetype}.${ext}`, false);
94+
let template = fs.readFileSync(templatePath, 'utf8');
95+
96+
let content = parseVariables(template);
9397

9498
fs.writeFile(`${dir}/${file}`, content, err => {
9599
if (err) {
@@ -108,11 +112,9 @@ const write = (dir, filetype, callback) => {
108112
/**
109113
* Delegate to create the pattern directory, if it exists, log message.
110114
*
111-
* @param {String} type The pattern type
112-
* @param {String} dir The directory to write
113-
* @param {String} pattern The name of the pattern
114-
*
115-
* @return {Boolean} False if nothing is created
115+
* @param {String} type The pattern type.
116+
* @param {String} dir The directory to write.
117+
* @param {String} pattern The name of the pattern.
116118
*/
117119
const defaults = (type, pattern, callback) => {
118120
let relative = Path.join(config.dirs.src, type);
@@ -140,7 +142,7 @@ const defaults = (type, pattern, callback) => {
140142
};
141143

142144
/**
143-
* Ask if we want to make the 'config' file.
145+
* Ask if we want to make the optional files.
144146
*
145147
* @param {String} filetype The pattern type: elements, components, objects
146148
* @param {String} pattern The name of the pattern
@@ -155,7 +157,7 @@ const optional = (filetype, pattern, prompt) => {
155157
let absolute = Path.join(config.dirs.base, relative);
156158

157159
prompt.question(
158-
`${alerts.question} Would you like to create a ${alerts.str.string(filetype)} file for ${alerts.str.string(pattern)}? (y/n)`,
160+
`${alerts.question} Make a ${alerts.str.string(filetype)} file for ${alerts.str.string(pattern)}? y/n`,
159161
(answer) => {
160162
if (yes(answer))
161163
write(absolute, filetype, (success, file) => {
@@ -191,7 +193,7 @@ const run = async () => {
191193

192194
// Make the standard files, then...
193195
defaults(TYPE, PATTERN, () => {
194-
// .. ask to make the option files
196+
// ... ask to make the option files
195197
(async () => {
196198
await each(config.optional, async (type) => {
197199
await optional(type, PATTERN, prompt);
@@ -205,7 +207,7 @@ const run = async () => {
205207
}
206208
};
207209

208-
/** @type {Object} Export our methods */
210+
/** @type {Object} Export our methods */
209211
module.exports = {
210212
run: run,
211213
config: config

0 commit comments

Comments
 (0)