Skip to content
This repository was archived by the owner on Jun 27, 2019. It is now read-only.

Commit ed210a9

Browse files
committed
Added test for build module. Added tiny event-based logger. Small cleanups
1 parent de97ac3 commit ed210a9

File tree

9 files changed

+60
-21
lines changed

9 files changed

+60
-21
lines changed

bin/build.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ function build(config, options) {
2424
if (options && options.patternsOnly) { // 1
2525
debug(`patternlab→build: Building only patterns now into ${config.paths.public.root}`);
2626
return patternLab.patternsonly(function () {
27-
debug(`patternlab→build: Yay, your patterns were successfully built ☺`);
27+
console.log(`patternlab→build: Yay, your patterns were successfully built ☺`); // eslint-disable-line
2828
}, config.cleanPublic);
2929
} else { // 2
3030
debug(`patternlab→build: Building your project now into ${config.paths.public.root}`);
3131
return patternLab.build(function () {
32-
debug(`patternlab→build: Yay, your PatternLab project was successfully built ☺`);
32+
console.log(`patternlab→build: Yay, your PatternLab project was successfully built ☺`); // eslint-disable-line
3333
}, config.cleanPublic);
3434
}
3535
}

bin/generator.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ function replaceConfigPaths(config, projectDir, sourceDir, publicDir, exportDir)
9393
_.map(conf.paths.source, (value, key) => { conf.paths.source[key] = value.replace(/^\.\/source/g, path.join(projectDir, sourceDir)) });
9494
_.map(conf.paths.public, (value, key) => { conf.paths.public[key] = value.replace(/^\.\/public/g, path.join(projectDir, publicDir)) });
9595
conf.styleguide = 'node_modules/styleguidekit-assets-default/dist/';
96-
conf.patternlabFiles = 'node_modules/styleguidekit-mustache-default/views/';
96+
conf.patternlabFiles = 'node_modules/styleguidekit-mustache-default/views/';
9797
conf.patternExportDirectory = path.join(projectDir, exportDir);
9898
return conf;
9999
}
@@ -107,7 +107,7 @@ function replaceConfigPaths(config, projectDir, sourceDir, publicDir, exportDir)
107107
* @param {string} exportDir - The export root directory path.
108108
* @return {void}
109109
*/
110-
const scaffold = (projectDir, sourceDir, publicDir, exportDir) => wrapAsync(function* () {
110+
const scaffold = (projectDir, sourceDir, publicDir, exportDir) => wrapAsync(function*() {
111111
/**
112112
* Create mandatory files structure
113113
* 1. Create project source directory
@@ -117,7 +117,7 @@ const scaffold = (projectDir, sourceDir, publicDir, exportDir) => wrapAsync(func
117117
yield Promise.all([
118118
mkdirsAsync(path.resolve(projectDir, path.normalize(sourceDir))), // 1
119119
mkdirsAsync(path.resolve(projectDir, path.normalize(publicDir))), // 2
120-
mkdirsAsync(path.resolve(projectDir, path.normalize(exportDir))), // 3
120+
mkdirsAsync(path.resolve(projectDir, path.normalize(exportDir))) // 3
121121
]);
122122
});
123123

bin/init.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,11 @@ const init = options => wrapsAsync(function*() {
302302

303303
// Finally :>
304304
if (!edition && !starterkit) {
305-
debug(`patternlab→init: You haven't picked an edition nor a starterkit. PatternLab won't work without those. Please add them manually.`)
305+
console.log(`patternlab→init: You haven't picked an edition nor a starterkit. PatternLab won't work without those. Please add them manually.`); // eslint-disable-line
306306
} else {
307-
debug(`patternlab→init: Additional packages installed - ${edition ? 'edition: ' + edition : ''} ${starterkit ? ', starterkit: ' + starterkit.name : ''}`);
307+
console.log(`patternlab→init: Additional packages installed - ${edition ? 'edition: ' + edition : ''} ${starterkit ? ', starterkit: ' + starterkit.name : ''}`); // eslint-disable-line
308308
}
309-
debug(`patternlab→init: Yay ☺. PatternLab Node was successfully initialised in ${projectDir}`);
309+
console.log(`patternlab→init: Yay ☺. PatternLab Node was successfully initialised in ${projectDir}`); // eslint-disable-line
310310
return true;
311311
} catch (err) {
312312
error(`patternlab→init: Failed to init project. ${err}`);

bin/patternlab.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,17 @@ const exportPatterns = require('./export');
88
const init = require('./init');
99
const resolveConfig = require('./resolve_config');
1010
const serve = require('./serve');
11+
const log = require('./utils').log;
1112
const error = require('./utils').error;
1213
const wrapAsync = require('./utils').wrapAsync;
1314
const pkg = require('../package.json');
1415

15-
const trim = val => val.trim();
16+
// Register error logging
17+
log.on('error', msg => console.log(msg)); // eslint-disable-line
18+
19+
const registerDebugLogger = () => {
20+
log.on('debug', msg => console.log(msg)); // eslint-disable-line
21+
};
1622

1723
/**
1824
* Hook up cli version, usage and options
@@ -24,7 +30,8 @@ cli
2430
.action(function (cmd) {
2531
checkArgs(cmd);
2632
})
27-
.option('-c, --config <path>', 'Specify config file. Default looks up the project dir', trim, './patternlab-config.json');
33+
.option('-c, --config <path>', 'Specify config file. Default looks up the project dir', val => val.trim(), './patternlab-config.json')
34+
.option('-v, --verbose', 'Show verbose logging');
2835

2936
/**
3037
* build
@@ -37,6 +44,7 @@ cli
3744
.option('-p, --patterns-only', 'Whether to only build patterns')
3845
.action(options => wrapAsync(function*() {
3946
try {
47+
if (options.parent.verbose) registerDebugLogger();
4048
const config = yield resolveConfig(options.parent.config);
4149
yield copyFiles(config.paths);
4250
build(config, options);
@@ -54,6 +62,7 @@ cli
5462
.command('export')
5563
.description('Export a PatternLab patterns into a compressed format')
5664
.action(options => wrapAsync(function*() {
65+
if (options.parent.verbose) registerDebugLogger();
5766
const config = yield resolveConfig(options.parent.config);
5867
exportPatterns(config);
5968
}));
@@ -67,6 +76,7 @@ cli
6776
.description('Initialize a PatternLab project from scratch or import an edition and/or starterkit')
6877
.option('-f, --force', 'Overwrite existing files and folders') // TODO: Make a global --clean flag to avoid repetition
6978
.action(options => wrapAsync(function*() {
79+
if (options.parent.verbose) registerDebugLogger();
7080
yield init(options);
7181
}));
7282

@@ -79,18 +89,21 @@ cli
7989
.alias('browse')
8090
.description('Starts a server to inspect files in browser')
8191
.action(options => wrapAsync(function*() {
92+
if (options.parent.verbose) registerDebugLogger();
8293
const config = yield resolveConfig(options.parent.config);
8394
serve(config);
8495
}));
8596

8697
// Show additional help
8798
cli.on('--help', () => {
99+
/* eslint-disable */
88100
console.log(' Examples:');
89101
console.log('');
90102
console.log(' $ patternlab init # Initialize a PatternLab project.');
91103
console.log(' $ patternlab <cmd> # Builds the PatternLab from the current dir');
92104
console.log(' $ patternlab <cmd> --config <path/to/patternlab-config> # PatternLab from a config in a specified directory');
93105
console.log('');
106+
/* eslint-enable */
94107
});
95108

96109
// Parse at the end because Node emit is immediate

bin/resolve_config.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ const wrapAsync = require('./utils').wrapAsync;
1313
*/
1414
function resolveConfig(configPath) {
1515
return wrapAsync(function*() {
16-
1716
// If config path is set but not a string
1817
if (arguments.length && typeof configPath !== 'string') {
1918
throw new TypeError('patternlab→resolveConfig: If configPath is set, it is expected to be of type string.');

bin/serve.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22
const bs = require('browser-sync').create('PatternLab');
3-
const htmlInjector = require("bs-html-injector");
3+
const htmlInjector = require('bs-html-injector');
44
const path = require('path');
55
const _ = require('lodash');
66
const isValidConfig = require('./is_valid_config');

bin/utils.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,40 @@ const spawn = require('child-process-promise').spawn;
55
const glob = require('glob');
66
const path = require('path');
77
const colors = require('colors');
8+
const EventEmitter = require('events').EventEmitter;
89

910
/**
10-
* @func log
11-
* @desc Bind console to allow shorter log func.
11+
* @name log
12+
* @desc tiny event-based logger
13+
* @type {*}
1214
*/
13-
const log = console.log.bind(console); // eslint-disable-next-line
15+
const log = Object.assign({
16+
debug(msg) {
17+
this.emit('debug', colors.green(msg));
18+
},
19+
info(msg) {
20+
this.emit('info', msg);
21+
},
22+
error(msg) {
23+
this.emit('error', colors.red(msg));
24+
}
25+
}, EventEmitter.prototype);
1426

1527
/**
1628
* @func debug
1729
* @desc Coloured debug log
1830
* @param {*} msg - The variadic messages to log out.
1931
* @return {void}
2032
*/
21-
const debug = function (msg) {
22-
log(colors.green(msg));
23-
};
33+
const debug = log.debug.bind(log);
2434

2535
/**
2636
* @func error
2737
* @desc Coloured error log
2838
* @param {*} e - The variadic messages to log out.
2939
* @return {void}
3040
*/
31-
const error = function (e) {
32-
log(colors.red(e));
33-
};
41+
const error = log.debug.bind(log);
3442

3543
/**
3644
* @func wrapAsync
@@ -135,6 +143,7 @@ module.exports = {
135143
readJsonAsync: fs.readJson,
136144
error,
137145
debug,
146+
log,
138147
wrapAsync,
139148
checkAndInstallPackage,
140149
noop

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838
"Node",
3939
"Javascript"
4040
],
41+
"scripts": {
42+
"lint": "eslint bin/",
43+
"test": "tap test/*.test.js --reporter spec"
44+
},
4145
"repository": {
4246
"type": "git",
4347
"url": "git://github.com/pattern-lab/patternlab-node-cli.git"

test/build.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const proxyquire = require('proxyquire');
2+
const tap = require('tap');
3+
const config = require('../bin/generator').defaultPatternlabConfig;
4+
const patternLabMock = require('./mocks/patternlab.mock');
5+
6+
// Require build and mock patternlab.build() so that we only test the build module behavior
7+
const build = proxyquire('../bin/build', {'patternlab-node': patternLabMock});
8+
const opts = {patternsOnly: true};
9+
tap.throws(() => { build() }, {}, 'build throws when config is empty');
10+
tap.throws(() => { build(123) }, {}, 'build throws when config is not of type object');
11+
tap.throws(() => { build(undefined, opts) }, {}, 'build --patterns-only throws when config is empty');
12+
tap.throws(() => { build(undefined, opts) }, {}, 'build --patterns-only throws when config is not of type object');
13+
tap.type(build(config), 'boolean', 'build returns a bool');
14+
tap.type(build(config, opts), 'boolean', 'build --patterns-only returns a bool');

0 commit comments

Comments
 (0)