|
1 | 1 | 'use strict';
|
2 | 2 |
|
| 3 | +const rcs = require('./utils/rcs'); |
| 4 | +const fs = require('fs-extra'); |
| 5 | +const path = require('path'); |
| 6 | +const glob = require('glob'); |
| 7 | +const async = require('async'); |
| 8 | +const _ = require('lodash'); |
| 9 | + |
| 10 | +/** |
| 11 | + * parses through every single document and renames the names |
| 12 | + * |
| 13 | + * @module cli |
| 14 | + */ |
| 15 | +const cli = module.exports = {}; |
| 16 | + |
| 17 | +cli.save = (newFilePath, data, cb) => { |
| 18 | + // @todo check if the filepath has an .ext |
| 19 | + // @todo check if the new filename is the same .ext |
| 20 | + // @todo do not overwrite file! use for that an flag |
| 21 | + fs.mkdirs(path.dirname(newFilePath), (err) => { |
| 22 | + fs.writeFile(newFilePath, data, (err, data) => { |
| 23 | + if (err) return cb(err); |
| 24 | + |
| 25 | + cb(null, "Successfully wrote " + newFilePath); |
| 26 | + }); |
| 27 | + }); |
| 28 | +}; |
| 29 | + |
| 30 | +/** |
| 31 | + * @typedef {Object} processOptions |
| 32 | + * @property {[type]} [propName] [description] |
| 33 | + */ |
3 | 34 | /**
|
4 |
| - * @todo parse files and rename |
| 35 | + * process over all files - set and replace |
| 36 | + * |
| 37 | + * @param {pathString} pathString this pathString can be either an expression for `glob` or a filepath |
| 38 | + * @param {processOptions} options |
| 39 | + * @param {Function} cb the callback |
| 40 | + * @return {Function} cb |
5 | 41 | */
|
6 |
| -const rcs = require('./utils/rcs'); |
| 42 | +cli.process = (pathString, options, cb) => { |
| 43 | + const optionsDefault = { |
| 44 | + collectSelectors: false, |
| 45 | + overwrite: false, |
| 46 | + cwd: '', |
| 47 | + newPath: 'rcs', |
| 48 | + flatten: false |
| 49 | + }; |
| 50 | + |
| 51 | + // set cb if options are not set |
| 52 | + if (typeof cb !== 'function') { |
| 53 | + cb = options; |
| 54 | + options = {}; |
| 55 | + } |
| 56 | + |
| 57 | + options = _.merge(optionsDefault, options); |
| 58 | + |
| 59 | + glob(pathString, { |
| 60 | + cwd: options.cwd |
| 61 | + }, (err, filesArray) => { |
| 62 | + if (err) return cb(err); |
| 63 | + |
| 64 | + // fail if nothing is found |
| 65 | + if (filesArray.length <= 0) { |
| 66 | + return cb({ |
| 67 | + message: 'No files found', |
| 68 | + error: 'ENOENT' |
| 69 | + }); |
| 70 | + } |
| 71 | + |
| 72 | + // call replaceCss if options.collectSelectors is set to true |
| 73 | + if (options.collectSelectors) { |
| 74 | + // call in series |
| 75 | + // not all selectors are stored, maybe some selectors are duplicated in different files |
| 76 | + async.eachSeries(filesArray, (filePath, callback) => { |
| 77 | + rcs.fileReplace.replaceCss(path.join(options.cwd, filePath), (err, data) => { |
| 78 | + let joinedPath; |
| 79 | + |
| 80 | + if (err) callback(err); |
| 81 | + |
| 82 | + joinedPath = path.join(options.newPath, filePath); |
| 83 | + |
| 84 | + cli.save(joinedPath, data.data, (err) => { |
| 85 | + if (err) callback(err); |
| 86 | + |
| 87 | + callback(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }, err => { |
| 91 | + if (err) { |
| 92 | + cb(err); |
| 93 | + } else { |
| 94 | + cb(null, true); |
| 95 | + } |
| 96 | + }); |
| 97 | + } else { |
| 98 | + // can be fired asynchronous |
| 99 | + // all selectors are collected |
| 100 | + // ⚡️ speed it up with async ⚡️ |
| 101 | + async.each(filesArray, (filePath, callback) => { |
| 102 | + rcs.fileReplace.replace(path.join(options.cwd, filePath), (err, data) => { |
| 103 | + let joinedPath; |
| 104 | + |
| 105 | + if (err) callback(err); |
| 106 | + |
| 107 | + joinedPath = path.join(options.newPath, filePath); |
| 108 | + |
| 109 | + cli.save(joinedPath, data.data, (err) => { |
| 110 | + if (err) callback(err); |
7 | 111 |
|
8 |
| -rcs.fileReplace.replaceCss('test/files/results/style.css', (err, data) => { |
9 |
| - console.log(data); |
10 |
| -}); |
| 112 | + callback(); |
| 113 | + }); |
| 114 | + }); |
| 115 | + }, err => { |
| 116 | + if (err) { |
| 117 | + cb(err); |
| 118 | + } else { |
| 119 | + cb(null, true); |
| 120 | + } |
| 121 | + }); |
| 122 | + } |
| 123 | + }); |
| 124 | +}; |
0 commit comments