|
| 1 | +'use strict'; |
| 2 | +const ask = require('../ask'); |
| 3 | +const scaffold = require('../scaffold'); |
| 4 | +const installEdition = require('../install-edition'); |
| 5 | +const installStarterkit = require('../install-starterkit'); |
| 6 | +const defaultPatternlabConfig = require('../default-config'); |
| 7 | +const replaceConfigPaths = require('../replace-config'); |
| 8 | +const path = require('path'); |
| 9 | +const wrapAsync = require('../utils').wrapAsync; |
| 10 | +const writeJsonAsync = require('../utils').writeJsonAsync; |
| 11 | + |
| 12 | +const init = options => wrapAsync(function*() { |
| 13 | + const sourceDir = 'source'; |
| 14 | + const publicDir = 'public'; |
| 15 | + const exportDir = 'pattern_exports'; |
| 16 | + const answers = options.projectDir ? options : yield ask(options); |
| 17 | + const projectDir = answers.projectDir || './'; |
| 18 | + const edition = answers.edition; |
| 19 | + const starterkit = answers.starterkit; |
| 20 | + |
| 21 | + /** |
| 22 | + * Process the init routines |
| 23 | + * 1 Replace config paths |
| 24 | + * 2. Scaffold the folder structure |
| 25 | + * 3. If `edition` is present: |
| 26 | + * 3.1 Install edition |
| 27 | + * 3.2 Reassign adjustedconfig |
| 28 | + * 3. If `starterkit` is present |
| 29 | + * 3.1 Install it |
| 30 | + * 3.2 Copy over the mandatory starterkit files to sourceDir |
| 31 | + * 4. Check for starterkit and install it |
| 32 | + * 5. Save patternlab-config.json in projectDir |
| 33 | + */ |
| 34 | + let patternlabConfig = replaceConfigPaths(defaultPatternlabConfig, projectDir, sourceDir, publicDir, exportDir); // 1 |
| 35 | + |
| 36 | + yield scaffold(projectDir, sourceDir, publicDir, exportDir); // 2 |
| 37 | + |
| 38 | + if (edition) { |
| 39 | + const newConf = yield installEdition(edition, patternlabConfig); // 3.1 |
| 40 | + patternlabConfig = Object.assign(patternlabConfig, newConf); // 3.2 |
| 41 | + } |
| 42 | + if (starterkit) yield installStarterkit(starterkit, patternlabConfig); // 4 |
| 43 | + yield writeJsonAsync(path.resolve(projectDir, 'patternlab-config.json'), patternlabConfig); // 4.2 |
| 44 | + |
| 45 | + // Finally :> |
| 46 | + if (!edition && !starterkit) { |
| 47 | + 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 |
| 48 | + } else { |
| 49 | + console.log(`patternlab→init: Additional packages installed - ${edition ? 'edition: ' + edition : ''} ${starterkit ? ', starterkit: ' + starterkit.name : ''}`); // eslint-disable-line |
| 50 | + } |
| 51 | + console.log(`patternlab→init: Yay ☺. PatternLab Node was successfully initialised in ${projectDir}`); // eslint-disable-line |
| 52 | + return true; |
| 53 | +}); |
| 54 | + |
| 55 | +module.exports = init; |
0 commit comments