|
| 1 | +/* eslint-disable no-param-reassign, operator-assignment */ |
| 2 | +require('colors'); |
| 3 | + |
| 4 | +const fs = require('fs'); |
| 5 | +const path = require('path'); |
| 6 | +const postcss = require('postcss'); |
| 7 | +const postcssScss = require('postcss-scss'); |
| 8 | + |
| 9 | +const ScssValidator = require('./ScssValidator'); |
| 10 | +const cssCompiler = require('./css/compile'); |
| 11 | + |
| 12 | +const CONDITIONAL_IMPORT = 'conditional-import'; |
| 13 | + |
| 14 | +class NodeSassAutoFixer { |
| 15 | + /** |
| 16 | + * |
| 17 | + * @param themePath |
| 18 | + * @param themeConfig |
| 19 | + * @param cliOptions |
| 20 | + */ |
| 21 | + constructor(themePath, themeConfig, cliOptions) { |
| 22 | + this.themePath = themePath; |
| 23 | + this.themeConfig = themeConfig; |
| 24 | + this.cliOptions = cliOptions; |
| 25 | + |
| 26 | + this.validator = new ScssValidator(themePath, themeConfig); |
| 27 | + } |
| 28 | + |
| 29 | + async run() { |
| 30 | + const assetsPath = path.join(this.themePath, 'assets'); |
| 31 | + const rawConfig = await this.themeConfig.getConfig(); |
| 32 | + const cssFiles = await this.validator.getCssFiles(); |
| 33 | + |
| 34 | + let issuesDetected = false; |
| 35 | + /* eslint-disable-next-line no-useless-catch */ |
| 36 | + try { |
| 37 | + for await (const file of cssFiles) { |
| 38 | + try { |
| 39 | + /* eslint-disable-next-line no-await-in-loop */ |
| 40 | + await cssCompiler.compile( |
| 41 | + rawConfig, |
| 42 | + assetsPath, |
| 43 | + file, |
| 44 | + cssCompiler.SASS_ENGINE_NAME, |
| 45 | + ); |
| 46 | + } catch (e) { |
| 47 | + issuesDetected = true; |
| 48 | + await this.tryToFix(e, file); |
| 49 | + } |
| 50 | + } |
| 51 | + if (!issuesDetected) { |
| 52 | + console.log('No issues deteted'); |
| 53 | + } |
| 54 | + } catch (e) { |
| 55 | + throw e; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + async tryToFix(err, file) { |
| 60 | + const problem = this.detectProblem(err); |
| 61 | + if (problem) { |
| 62 | + const dirname = path.join(this.themePath, 'assets/scss'); |
| 63 | + const filePath = this.resolveScssFileName(dirname, err.file); |
| 64 | + if (problem === CONDITIONAL_IMPORT) { |
| 65 | + await this.fixConditionalImport(filePath); |
| 66 | + } |
| 67 | + } else { |
| 68 | + const filePath = path.join(this.themePath, 'assets/scss', file + '.scss'); |
| 69 | + console.log("Couldn't determine and autofix the problem. Please fix it manually.".red); |
| 70 | + console.log('Found trying compile file:'.red, filePath); |
| 71 | + throw new Error(err); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + detectProblem(err) { |
| 76 | + if (err.formatted) { |
| 77 | + if ( |
| 78 | + err.formatted.includes( |
| 79 | + 'Error: Import directives may not be used within control directives or mixins', |
| 80 | + ) |
| 81 | + ) { |
| 82 | + return CONDITIONAL_IMPORT; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + async fixConditionalImport(filePath) { |
| 90 | + const scss = fs.readFileSync(filePath, 'utf8'); |
| 91 | + const condImportFile = await this.processCss(scss, this.transformConditionalImport()); |
| 92 | + |
| 93 | + this.overrideFile(filePath, condImportFile.css); |
| 94 | + for await (const message of condImportFile.messages) { |
| 95 | + if (message.type === 'import') { |
| 96 | + const importFile = this.findImportedFile(message.filename, filePath); |
| 97 | + const importFileScss = fs.readFileSync(importFile); |
| 98 | + const mixinFile = await this.processCss( |
| 99 | + importFileScss, |
| 100 | + this.transformRootToMixin(message), |
| 101 | + ); |
| 102 | + this.overrideFile(importFile, mixinFile.css); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + transformConditionalImport() { |
| 108 | + return { |
| 109 | + postcssPlugin: 'Transform Conditional Import', |
| 110 | + AtRule: (rule, { AtRule, result }) => { |
| 111 | + if (rule.name === 'if') { |
| 112 | + rule.walkAtRules('import', (decl) => { |
| 113 | + const newRule = new AtRule({ |
| 114 | + name: 'import', |
| 115 | + params: decl.params, |
| 116 | + source: decl.source, |
| 117 | + }); |
| 118 | + const root = decl.root(); |
| 119 | + root.prepend(newRule); |
| 120 | + decl.name = 'include'; |
| 121 | + decl.params = decl.params.replace(/['"]+/g, ''); |
| 122 | + result.messages.push({ |
| 123 | + type: 'import', |
| 124 | + filename: decl.params, |
| 125 | + }); |
| 126 | + }); |
| 127 | + } |
| 128 | + }, |
| 129 | + }; |
| 130 | + } |
| 131 | + |
| 132 | + transformRootToMixin(data) { |
| 133 | + const self = this; |
| 134 | + return { |
| 135 | + postcssPlugin: 'Transform Root to Mixin', |
| 136 | + Once(root, { AtRule, result }) { |
| 137 | + // already wrapped in mixin |
| 138 | + if ( |
| 139 | + root.nodes.length === 1 && |
| 140 | + root.nodes[0].type === 'atrule' && |
| 141 | + root.nodes[0].name === 'mixin' |
| 142 | + ) { |
| 143 | + return; |
| 144 | + } |
| 145 | + const nodes = root.nodes.map((node) => { |
| 146 | + const cloned = node.clone(); |
| 147 | + cloned.raws.before = '\n'; |
| 148 | + return cloned; |
| 149 | + }); |
| 150 | + self.formatNodes(nodes); |
| 151 | + const newRoot = new AtRule({ |
| 152 | + name: 'mixin', |
| 153 | + params: data.filename, |
| 154 | + source: root.source, |
| 155 | + nodes, |
| 156 | + }); |
| 157 | + result.root.nodes = [newRoot]; |
| 158 | + }, |
| 159 | + }; |
| 160 | + } |
| 161 | + |
| 162 | + formatNodes(nodes) { |
| 163 | + const spacer = this.getSpacer(nodes[0]); |
| 164 | + this.addTabsToNodes(nodes, spacer); |
| 165 | + } |
| 166 | + |
| 167 | + addTabsToNodes(nodes, spacer) { |
| 168 | + nodes.forEach((node) => { |
| 169 | + if (node.nodes) { |
| 170 | + node.raws.before = node.raws.before + spacer; |
| 171 | + node.raws.after = node.raws.after + spacer; |
| 172 | + this.addTabsToNodes(node.nodes, spacer); |
| 173 | + } else { |
| 174 | + if (node.raws.before) { |
| 175 | + node.raws.before = node.raws.before + spacer; |
| 176 | + } |
| 177 | + if (node.prop === 'src') { |
| 178 | + node.value = node.value.replace(/\n/g, '\n' + spacer); |
| 179 | + } |
| 180 | + } |
| 181 | + }); |
| 182 | + } |
| 183 | + |
| 184 | + getSpacer(node) { |
| 185 | + const hasTabSpace = this.hasTabSpace(node.raws.before); |
| 186 | + if (hasTabSpace) { |
| 187 | + return '\t'; |
| 188 | + } |
| 189 | + |
| 190 | + return ' '; |
| 191 | + } |
| 192 | + |
| 193 | + hasTabSpace(string) { |
| 194 | + return /\t/g.test(string); |
| 195 | + } |
| 196 | + |
| 197 | + async processCss(data, plugin) { |
| 198 | + const processor = postcss([plugin]); |
| 199 | + return processor.process(data, { from: undefined, parser: postcssScss }); |
| 200 | + } |
| 201 | + |
| 202 | + findImportedFile(file, originalFilePath) { |
| 203 | + const originalDirname = path.dirname(originalFilePath); |
| 204 | + return this.resolveScssFileName(originalDirname, file); |
| 205 | + } |
| 206 | + |
| 207 | + resolveScssFileName(dirname, fileName) { |
| 208 | + if (!fileName.includes('.scss')) { |
| 209 | + fileName += '.scss'; |
| 210 | + } |
| 211 | + const filePath = path.join(dirname, fileName); |
| 212 | + if (!fs.existsSync(filePath)) { |
| 213 | + // try with underscore |
| 214 | + const withUnderscoreFileName = this.getFileNameWithUnderscore(fileName); |
| 215 | + const filePathWithUnderscore = path.join(dirname, withUnderscoreFileName); |
| 216 | + if (!fs.existsSync(filePathWithUnderscore)) { |
| 217 | + throw new Error( |
| 218 | + `Import ${fileName} wasn't resolved in ${filePath} or ${filePathWithUnderscore}`, |
| 219 | + ); |
| 220 | + } |
| 221 | + return filePathWithUnderscore; |
| 222 | + } |
| 223 | + |
| 224 | + return filePath; |
| 225 | + } |
| 226 | + |
| 227 | + getFileNameWithUnderscore(fileName) { |
| 228 | + const fileNameParts = fileName.split('/'); |
| 229 | + const withUnderscoreFileName = fileNameParts |
| 230 | + .map((part, i) => { |
| 231 | + if (i === fileNameParts.length - 1) { |
| 232 | + return '_' + part; |
| 233 | + } |
| 234 | + return part; |
| 235 | + }) |
| 236 | + .join('/'); |
| 237 | + return withUnderscoreFileName; |
| 238 | + } |
| 239 | + |
| 240 | + overrideFile(filePath, data) { |
| 241 | + const phrase = this.cliOptions.dry ? 'Would override' : 'Overriding'; |
| 242 | + console.log(phrase.green + ' file:'.green, filePath); |
| 243 | + if (this.cliOptions.dry) { |
| 244 | + console.log('---Content---'.yellow); |
| 245 | + console.log(data); |
| 246 | + console.log('---END of Content---'.yellow); |
| 247 | + } else { |
| 248 | + fs.writeFileSync(filePath, data); |
| 249 | + } |
| 250 | + } |
| 251 | +} |
| 252 | + |
| 253 | +module.exports = NodeSassAutoFixer; |
0 commit comments