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

Commit 73257ee

Browse files
committed
Adding bin files
1 parent 2efd87a commit 73257ee

15 files changed

+1047
-28
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ Network Trash Folder
6767
Temporary Items
6868
.apdisk
6969

70-
# Patternlab CLI specific
70+
# PatternLab CLI specific
7171

7272
.DS_Store
7373
latest-change.txt

bin/build.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const pl = require('patternlab-node');
4+
const debug = require('./utils').debug;
5+
const isValidConfig = require('./is_valid_config');
6+
7+
/**
8+
* @func build
9+
* @desc Init patternLab core and build the PatternLab files.
10+
* @param {object} config - The passed PatternLab config.
11+
* @param {object} options - Additional opts to specify build mode.
12+
*/
13+
function build(config, options) {
14+
if (!isValidConfig) throw new TypeError('build→Expects config not to be empty and of type object.');
15+
16+
// Initiate PatternLab core with the config
17+
const patternLab = pl(config);
18+
19+
/**
20+
* Check whether a flag was passed for build
21+
* 1. Build only patterns
22+
* 2. Normal build
23+
*/
24+
if (options && options.patternsOnly) { // 1
25+
debug(`patternlab→build: Building only patterns now into ${config.paths.public.root}`);
26+
return patternLab.patternsonly(function () {
27+
debug(`patternlab→build: Yay, your patterns were successfully built ☺`);
28+
}, config.cleanPublic);
29+
} else { // 2
30+
debug(`patternlab→build: Building your project now into ${config.paths.public.root}`);
31+
return patternLab.build(function () {
32+
debug(`patternlab→build: Yay, your PatternLab project was successfully built ☺`);
33+
}, config.cleanPublic);
34+
}
35+
}
36+
37+
module.exports = build;

bin/check_args.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
3+
const error = require('./utils').error;
4+
5+
/**
6+
* @func checkArgs
7+
* @desc Checks if a <cmd> is allowed.
8+
* @param {string} cmd - Name of the command to check against.
9+
* @throws Will throw an error if the passed <cmd> is not allowed and exits with 1.
10+
* @return {object} - Returns true is all is good.
11+
*/
12+
function checkArgs(cmd) {
13+
/**
14+
* An immutable whitelist to check incoming <cmd> against
15+
* TODO: Need to complete the whitelist and add note to main file when adding new commands
16+
*/
17+
const CMD_WHITELIST = Object.freeze(['build', 'compile', 'init', 'export', 'serve', 'browse']);
18+
19+
// Check if a <cmd> was specified. If not → bail out
20+
if (typeof cmd === 'undefined') {
21+
error('patternlab: Sorry, no command <cmd> was specified! Type `patternlab --help` to view all available commands and options.');
22+
process.exit(1);
23+
}
24+
25+
// Check if <cmd> is on our command whitelist. If not → bail out. Probably Inquirer handles this scenario as well
26+
if (CMD_WHITELIST.indexOf(cmd)) {
27+
error('patternlab: Yikes, your command `' + cmd + '` is not supported. Type `patternlab --help` to view all available commands and options.');
28+
process.exit(1);
29+
}
30+
return true;
31+
}
32+
33+
module.exports = checkArgs;

bin/copy_source_files.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'use strict';
2+
3+
const copy = require('./utils').copy;
4+
const debug = require('./utils').debug;
5+
const wrapAsync = require('./utils').wrapAsync;
6+
7+
/**
8+
* @func copyFilesFromSourceToPublic
9+
* @desc Copies files from the source path to the public path.
10+
* @param {object} paths - The passed PatternLab config paths member.
11+
* @return {Array}
12+
*/
13+
const copyFilesFromSourceToPublic = paths => wrapAsync(function*() {
14+
// Copy files over
15+
const copiedFiles = [
16+
copy(paths.source.styleguide, '*', paths.public.root),
17+
copy(paths.source.js, '**/*.js', paths.public.js),
18+
copy(paths.source.css, '*.css', paths.public.css),
19+
copy(paths.source.images, '*', paths.public.images),
20+
copy(paths.source.fonts, '*', paths.public.fonts),
21+
copy(paths.source.root, 'favicon.ico', paths.public.root)
22+
];
23+
debug('patternlab→build: Your files were copied over to ' + paths.public.root);
24+
return yield Promise.all(copiedFiles);
25+
});
26+
27+
module.exports = copyFilesFromSourceToPublic;

bin/export.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict';
2+
3+
const fs = require('fs-extra');
4+
const path = require('path');
5+
const Archiver = require('archiver');
6+
const isValidConfig = require('./is_valid_config');
7+
const debug = require('./utils').debug;
8+
9+
/**
10+
* @func exportPatterns
11+
* @desc Exports the patterns into the patternExportDirectory.
12+
* @param {object} [config] - The passed PatternLab config.
13+
*/
14+
function exportPatterns(config) {
15+
if (!isValidConfig) throw new TypeError('export→Expects config not to be empty OR of type object if not empty.');
16+
17+
const archive = new Archiver('zip', {});
18+
const exportsPath = path.resolve('./', config.patternExportDirectory, 'patterns.zip');
19+
const output = fs.createWriteStream(exportsPath);
20+
21+
output.on('close', function () {
22+
debug('patternlab→export: Exported patterns in ' + exportsPath + ' - ' + archive.pointer() + ' total bytes.');
23+
});
24+
25+
archive.on('error', function (err) {
26+
throw new TypeError(`export→An error occured during zipping the patterns: ${err}`);
27+
});
28+
29+
archive.pipe(output);
30+
31+
archive.glob('?(_patterns|_data|_meta|_annotations)/**', {
32+
cwd: config.paths.source.root
33+
}, {})
34+
.finalize();
35+
}
36+
37+
module.exports = exportPatterns;

bin/generator.js

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const _ = require('lodash');
5+
const wrapAsync = require('./utils').wrapAsync;
6+
const mkdirsAsync = require('./utils').mkdirsAsync;
7+
const defaultPatternlabConfig = {
8+
'paths': {
9+
'source': {
10+
'root': './source/',
11+
'patterns': './source/_patterns/',
12+
'data': './source/_data/',
13+
'meta': './source/_meta/',
14+
'annotations': './source/_annotations/',
15+
'styleguide': 'node_modules/styleguidekit-assets-default/dist/',
16+
'patternlabFiles': 'node_modules/styleguidekit-mustache-default/views/',
17+
'js': './source/js',
18+
'images': './source/images',
19+
'fonts': './source/fonts',
20+
'css': './source/css/'
21+
},
22+
'public': {
23+
'root': './public/',
24+
'patterns': './public/patterns/',
25+
'data': './public/styleguide/data/',
26+
'annotations': './public/annotations/',
27+
'styleguide': './public/styleguide/',
28+
'js': './public/js',
29+
'images': './public/images',
30+
'fonts': './public/fonts',
31+
'css': './public/css'
32+
}
33+
},
34+
'styleGuideExcludes': [
35+
'templates',
36+
'pages'
37+
],
38+
'defaultPattern': 'all',
39+
'cleanPublic': false,
40+
'patternExtension': 'mustache',
41+
'ignored-extensions': ['scss', 'DS_Store', 'less'],
42+
'ignored-directories': ['scss'],
43+
'debug': false,
44+
'ishControlsHide': {
45+
's': false,
46+
'm': false,
47+
'l': false,
48+
'full': false,
49+
'random': false,
50+
'disco': false,
51+
'hay': true,
52+
'mqs': false,
53+
'find': false,
54+
'views-all': false,
55+
'views-annotations': false,
56+
'views-code': false,
57+
'views-new': false,
58+
'tools-all': false,
59+
'tools-docs': false
60+
},
61+
'ishMinimum': '240',
62+
'ishMaximum': '2600',
63+
'patternStateCascade': ['inprogress', 'inreview', 'complete'],
64+
'patternStates': {
65+
'molecules-single-comment': 'complete',
66+
'organisms-sticky-comment': 'inreview',
67+
'templates-article': 'complete'
68+
},
69+
'patternExportPatternPartials': [],
70+
'patternExportDirectory': './pattern_exports/',
71+
'baseurl': '',
72+
'cacheBust': true,
73+
'starterkitSubDir': 'dist',
74+
'outputFileSuffixes': {
75+
'rendered': '',
76+
'rawTemplate': '',
77+
'markupOnly': '.markup-only'
78+
}
79+
};
80+
81+
/**
82+
* @func replaceConfigPaths
83+
* @desc Immutable replace source and public paths in the passed config.
84+
* @param {object} config - The passed PatternLab config.
85+
* @param {string} projectDir - The project directory path, defaults to ./
86+
* @param {string} sourceDir - The source root directory path.
87+
* @param {string} publicDir - The public root directory path.
88+
* @param {string} exportDir - The export root directory path.
89+
* @return {object|Error} - Returns a modified config. Original stays unaltered.
90+
*/
91+
function replaceConfigPaths(config, projectDir, sourceDir, publicDir, exportDir) {
92+
const conf = _.assign({}, config);
93+
_.map(conf.paths.source, (value, key) => { conf.paths.source[key] = value.replace(/^\.\/source/g, path.join(projectDir, sourceDir)) });
94+
_.map(conf.paths.public, (value, key) => { conf.paths.public[key] = value.replace(/^\.\/public/g, path.join(projectDir, publicDir)) });
95+
conf.styleguide = 'node_modules/styleguidekit-assets-default/dist/';
96+
conf.patternlabFiles = 'node_modules/styleguidekit-mustache-default/views/';
97+
conf.patternExportDirectory = path.join(projectDir, exportDir);
98+
return conf;
99+
}
100+
101+
/**
102+
* @func scaffold
103+
* @desc Generate file and folder structure for a PatternLab project
104+
* @param {string} projectDir - The project root directory path.
105+
* @param {string} sourceDir - The source root directory path.
106+
* @param {string} publicDir - The public root directory path.
107+
* @param {string} exportDir - The export root directory path.
108+
* @return {void}
109+
*/
110+
const scaffold = (projectDir, sourceDir, publicDir, exportDir) => wrapAsync(function* () {
111+
/**
112+
* Create mandatory files structure
113+
* 1. Create project source directory
114+
* 2. Create project public directory
115+
* 3. Create project export directory
116+
*/
117+
yield Promise.all([
118+
mkdirsAsync(path.resolve(projectDir, path.normalize(sourceDir))), // 1
119+
mkdirsAsync(path.resolve(projectDir, path.normalize(publicDir))), // 2
120+
mkdirsAsync(path.resolve(projectDir, path.normalize(exportDir))), // 3
121+
]);
122+
});
123+
124+
module.exports = {
125+
defaultPatternlabConfig,
126+
replaceConfigPaths,
127+
scaffold
128+
};

0 commit comments

Comments
 (0)