Skip to content

Commit 3881544

Browse files
committed
feat(logs): Refactor logs for pattern_engines
1 parent eee7152 commit 3881544

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed

core/lib/pattern_engines.js

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const chalk = require('chalk');
66
const engineMatcher = /^patternengine-node-(.*)$/;
77
const scopeMatch = /^@(.*)$/;
88
const isDir = fPath => lstatSync(fPath).isDirectory();
9+
const logger = require('./log');
910

1011
const enginesDirectories = [{
1112
displayName: 'the core',
@@ -20,7 +21,7 @@ const enginesDirectories = [{
2021
function isEngineModule(filePath) {
2122
const baseName = path.basename(filePath);
2223
const engineMatch = baseName.match(engineMatcher);
23-
24+
2425
if (engineMatch) { return engineMatch[1]; }
2526
return false;
2627
}
@@ -43,12 +44,12 @@ function isScopedPackage(filePath) {
4344
* @return {Array<Engine>} An array of engine objects
4445
*/
4546
function resolveEngines(dir) {
46-
47+
4748
// Guard against non-existent directories.
4849
if (!existsSync(dir)) {
4950
return []; // Silence is golden …
5051
}
51-
52+
5253
/**
5354
* @name walk
5455
* @desc Traverse the given path and gather possible engines
@@ -57,14 +58,14 @@ function resolveEngines(dir) {
5758
* @return {Array<Engine>} - The final array of engines
5859
*/
5960
const walk = (fPath, engines) => {
60-
61+
6162
/**
6263
* @name dirList
6364
* @desc A list of all directories in the given path
6465
* @type {Array<string>}
6566
*/
6667
const dirList = readdirSync(fPath).filter(p => isDir(path.join(fPath, p)));
67-
68+
6869
/**
6970
* @name e
7071
* @desc For the current dir get all engines
@@ -79,7 +80,7 @@ function resolveEngines(dir) {
7980
}
8081
})
8182
);
82-
83+
8384
/**
8485
* 1. Flatten all engines from inner recursions and current dir
8586
* 2. Filter the dirList for scoped packages
@@ -92,7 +93,7 @@ function resolveEngines(dir) {
9293
.map(scope => walk(path.join(fPath, scope), e)) // 3
9394
);
9495
};
95-
96+
9697
return walk(dir, []);
9798
}
9899

@@ -117,27 +118,23 @@ function findEngineModulesInDirectory(dir) {
117118
// methods and properites below should therefore be on its prototype.
118119

119120
const PatternEngines = Object.create({
120-
121+
121122
loadAllEngines: function (patternLabConfig) {
122123
var self = this;
123-
124+
124125
// Try to load engines! We scan for engines at each path specified above. This
125126
// function is kind of a big deal.
126127
enginesDirectories.forEach(function (engineDirectory) {
127128
const enginesInThisDir = findEngineModulesInDirectory(engineDirectory.path);
128-
if (patternLabConfig.debug) {
129-
console.log(chalk.bold(`Loading engines from ${engineDirectory.displayName}...\n`));
130-
}
131-
129+
130+
logger.debug(`Loading engines from ${engineDirectory.displayName}...\n`);
131+
132132
// find all engine-named things in this directory and try to load them,
133133
// unless it's already been loaded.
134134
enginesInThisDir.forEach(function (engineDiscovery) {
135135
let errorMessage;
136136
const successMessage = "good to go";
137-
if (patternLabConfig.debug) {
138-
chalk.green(successMessage);
139-
}
140-
137+
141138
try {
142139
// Give it a try! load 'er up. But not if we already have,
143140
// of course. Also pass the pattern lab config object into
@@ -157,23 +154,18 @@ const PatternEngines = Object.create({
157154
errorMessage = err.message;
158155
} finally {
159156
// report on the status of the engine, one way or another!
160-
if (patternLabConfig.debug) {
161-
console.log(` ${engineDiscovery.name}:`, errorMessage ? chalk.red(errorMessage) : successMessage);
162-
}
157+
logger.info(`Pattern Engine ${engineDiscovery.name}: ${errorMessage ? errorMessage : successMessage}`);
163158
}
164159
});
165-
console.log('');
166160
});
167-
161+
168162
// Complain if for some reason we haven't loaded any engines.
169163
if (Object.keys(self).length === 0) {
170-
throw new Error('No engines loaded! Something is seriously wrong.');
171-
}
172-
if (patternLabConfig.debug) {
173-
console.log(chalk.bold('Done loading engines.\n'));
164+
logger.error('No engines loaded! Something is seriously wrong.');
174165
}
166+
logger.debug(`Done loading engines`);
175167
},
176-
168+
177169
getEngineNameForPattern: function (pattern) {
178170
// avoid circular dependency by putting this in here. TODO: is this slow?
179171
const of = require('./object_factory');
@@ -182,7 +174,7 @@ const PatternEngines = Object.create({
182174
const engineNames = Object.keys(this);
183175
for (let i = 0; i < engineNames.length; i++) {
184176
const engine = this[engineNames[i]];
185-
177+
186178
if (Array.isArray(engine.engineFileExtension)) {
187179
if (engine.engineFileExtension.includes(pattern.fileExtension)) {
188180
return engine.engineName;
@@ -195,12 +187,12 @@ const PatternEngines = Object.create({
195187
}
196188
}
197189
}
198-
190+
199191
// otherwise, assume it's a plain mustache template string and act
200192
// accordingly
201193
return 'mustache';
202194
},
203-
195+
204196
getEngineForPattern: function (pattern) {
205197
if (pattern.isPseudoPattern) {
206198
return this.getEngineForPattern(pattern.basePattern);
@@ -209,7 +201,7 @@ const PatternEngines = Object.create({
209201
return this[engineName];
210202
}
211203
},
212-
204+
213205
// combine all found engines into a single array of supported extensions
214206
getSupportedFileExtensions: function () {
215207
const engineNames = Object.keys(PatternEngines);
@@ -218,19 +210,19 @@ const PatternEngines = Object.create({
218210
});
219211
return [].concat.apply([], allEnginesExtensions);
220212
},
221-
213+
222214
isFileExtensionSupported: function (fileExtension) {
223215
const supportedExtensions = PatternEngines.getSupportedFileExtensions();
224216
return (supportedExtensions.lastIndexOf(fileExtension) !== -1);
225217
},
226-
218+
227219
// given a filename, return a boolean: whether or not the filename indicates
228220
// that the file is pseudopattern JSON
229221
isPseudoPatternJSON: function (filename) {
230222
const extension = path.extname(filename);
231223
return (extension === '.json' && filename.indexOf('~') > -1);
232224
},
233-
225+
234226
// takes a filename string, not a full path; a basename (plus extension)
235227
// ignore _underscored patterns, dotfiles, and anything not recognized by a
236228
// loaded pattern engine. Pseudo-pattern .json files ARE considered to be
@@ -242,7 +234,7 @@ const PatternEngines = Object.create({
242234
(extension === '.json' && !PatternEngines.isPseudoPatternJSON(filename))) {
243235
return false;
244236
}
245-
237+
246238
// not a hidden pattern, let's dig deeper
247239
const supportedPatternFileExtensions = PatternEngines.getSupportedFileExtensions();
248240
return (supportedPatternFileExtensions.lastIndexOf(extension) !== -1 ||

0 commit comments

Comments
 (0)