|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const path = require('path'); |
| 4 | + |
| 5 | +const Pattern = require('./object_factory').Pattern; |
| 6 | +const mp = require('./markdown_parser'); |
| 7 | +const logger = require('./log'); |
| 8 | +const patternEngines = require('./pattern_engines'); |
| 9 | +const ch = require('./changes_hunter'); |
| 10 | +const da = require('./data_loader'); |
| 11 | +const addPattern = require('./addPattern'); |
| 12 | +const buildListItems = require('./buildListItems'); |
| 13 | +const readDocumentation = require('./readDocumentation'); |
| 14 | + |
| 15 | +const markdown_parser = new mp(); |
| 16 | +const changes_hunter = new ch(); |
| 17 | +const dataLoader = new da(); |
| 18 | + |
| 19 | +//this is mocked in unit tests |
| 20 | +let fs = require('fs-extra'); //eslint-disable-line prefer-const |
| 21 | + |
| 22 | + // loads a pattern from disk, creates a Pattern object from it and |
| 23 | + // all its associated files, and records it in patternlab.patterns[] |
| 24 | +module.exports = function (relPath, patternlab) { |
| 25 | + |
| 26 | + var relativeDepth = (relPath.match(/\w(?=\\)|\w(?=\/)/g) || []).length; |
| 27 | + if (relativeDepth > 2) { |
| 28 | + logger.warning(''); |
| 29 | + logger.warning('Warning:'); |
| 30 | + logger.warning('A pattern file: ' + relPath + ' was found greater than 2 levels deep from ' + patternlab.config.paths.source.patterns + '.'); |
| 31 | + logger.warning('It\'s strongly suggested to not deviate from the following structure under _patterns/'); |
| 32 | + logger.warning('[patternType]/[patternSubtype]/[patternName].[patternExtension]'); |
| 33 | + logger.warning(''); |
| 34 | + logger.warning('While Pattern Lab may still function, assets may 404 and frontend links may break. Consider yourself warned. '); |
| 35 | + logger.warning('Read More: http://patternlab.io/docs/pattern-organization.html'); |
| 36 | + logger.warning(''); |
| 37 | + } |
| 38 | + |
| 39 | + //check if the found file is a top-level markdown file |
| 40 | + var fileObject = path.parse(relPath); |
| 41 | + if (fileObject.ext === '.md') { |
| 42 | + try { |
| 43 | + var proposedDirectory = path.resolve(patternlab.config.paths.source.patterns, fileObject.dir, fileObject.name); |
| 44 | + var proposedDirectoryStats = fs.statSync(proposedDirectory); |
| 45 | + if (proposedDirectoryStats.isDirectory()) { |
| 46 | + var subTypeMarkdownFileContents = fs.readFileSync(proposedDirectory + '.md', 'utf8'); |
| 47 | + var subTypeMarkdown = markdown_parser.parse(subTypeMarkdownFileContents); |
| 48 | + var subTypePattern = new Pattern(relPath, null, patternlab); |
| 49 | + subTypePattern.patternSectionSubtype = true; |
| 50 | + subTypePattern.patternLink = subTypePattern.name + '/index.html'; |
| 51 | + subTypePattern.patternDesc = subTypeMarkdown.markdown; |
| 52 | + subTypePattern.flatPatternPath = subTypePattern.flatPatternPath + '-' + subTypePattern.fileName; |
| 53 | + subTypePattern.isPattern = false; |
| 54 | + subTypePattern.engine = null; |
| 55 | + |
| 56 | + patternlab.subtypePatterns[subTypePattern.patternPartial] = subTypePattern; |
| 57 | + |
| 58 | + return subTypePattern; |
| 59 | + } |
| 60 | + } catch (err) { |
| 61 | + // no file exists, meaning it's a pattern markdown file |
| 62 | + if (err.code !== 'ENOENT') { |
| 63 | + logger.warning(err); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + //extract some information |
| 70 | + var filename = fileObject.base; |
| 71 | + var ext = fileObject.ext; |
| 72 | + var patternsPath = patternlab.config.paths.source.patterns; |
| 73 | + |
| 74 | + // skip non-pattern files |
| 75 | + if (!patternEngines.isPatternFile(filename, patternlab)) { return null; } |
| 76 | + |
| 77 | + //make a new Pattern Object |
| 78 | + var currentPattern = new Pattern(relPath, null, patternlab); |
| 79 | + |
| 80 | + //if file is named in the syntax for variants |
| 81 | + if (patternEngines.isPseudoPatternJSON(filename)) { |
| 82 | + return currentPattern; |
| 83 | + } |
| 84 | + |
| 85 | + //can ignore all non-supported files at this point |
| 86 | + if (patternEngines.isFileExtensionSupported(ext) === false) { |
| 87 | + return currentPattern; |
| 88 | + } |
| 89 | + |
| 90 | + //look for a json file for this template |
| 91 | + try { |
| 92 | + var jsonFilename = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName); |
| 93 | + const patternData = dataLoader.loadDataFromFile(jsonFilename, fs); |
| 94 | + |
| 95 | + if (patternData) { |
| 96 | + currentPattern.jsonFileData = patternData; |
| 97 | + logger.debug(`found pattern-specific data for ${currentPattern.patternPartial}`); |
| 98 | + } |
| 99 | + } |
| 100 | + catch (err) { |
| 101 | + logger.warning(`There was an error parsing sibling JSON for ${currentPattern.relPath}`); |
| 102 | + logger.warning(err); |
| 103 | + } |
| 104 | + |
| 105 | + //look for a listitems.json file for this template |
| 106 | + try { |
| 107 | + var listJsonFileName = path.resolve(patternsPath, currentPattern.subdir, currentPattern.fileName + ".listitems"); |
| 108 | + const listItemsData = dataLoader.loadDataFromFile(listJsonFileName, fs); |
| 109 | + |
| 110 | + if (listItemsData) { |
| 111 | + logger.debug(`found pattern-specific listitems data for ${currentPattern.patternPartial}`); |
| 112 | + currentPattern.listitems = listItemsData; |
| 113 | + buildListItems(currentPattern); |
| 114 | + } |
| 115 | + } |
| 116 | + catch (err) { |
| 117 | + logger.warning(`There was an error parsing sibling listitem JSON for ${currentPattern.relPath}`); |
| 118 | + logger.warning(err); |
| 119 | + } |
| 120 | + |
| 121 | + //look for a markdown file for this template |
| 122 | + readDocumentation(currentPattern, patternlab); |
| 123 | + |
| 124 | + //add the raw template to memory |
| 125 | + var templatePath = path.resolve(patternsPath, currentPattern.relPath); |
| 126 | + |
| 127 | + currentPattern.template = fs.readFileSync(templatePath, 'utf8'); |
| 128 | + |
| 129 | + //find any stylemodifiers that may be in the current pattern |
| 130 | + currentPattern.stylePartials = currentPattern.findPartialsWithStyleModifiers(); |
| 131 | + |
| 132 | + //find any pattern parameters that may be in the current pattern |
| 133 | + currentPattern.parameteredPartials = currentPattern.findPartialsWithPatternParameters(); |
| 134 | + |
| 135 | + [templatePath, jsonFilename, listJsonFileName].forEach(file => { |
| 136 | + changes_hunter.checkLastModified(currentPattern, file); |
| 137 | + }); |
| 138 | + |
| 139 | + changes_hunter.checkBuildState(currentPattern, patternlab); |
| 140 | + |
| 141 | + //add currentPattern to patternlab.patterns array |
| 142 | + addPattern(currentPattern, patternlab); |
| 143 | + |
| 144 | + return currentPattern; |
| 145 | +}; |
| 146 | + |
0 commit comments