|
| 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