From 20f07083ad1811555bc5224e63cdff03e7fca0ec Mon Sep 17 00:00:00 2001 From: Konstantin Mamaev Date: Tue, 2 Apr 2019 19:24:23 +0300 Subject: [PATCH] Fix lint issues --- src/index.js | 4 +- src/lib/decl-processor.js | 1 + src/lib/get-file.js | 77 ++++++++++++------------- src/type/copy.js | 117 +++++++++++++++++++------------------- src/type/inline.js | 77 +++++++++++-------------- 5 files changed, 133 insertions(+), 143 deletions(-) diff --git a/src/index.js b/src/index.js index 5eeaf1c..402705e 100644 --- a/src/index.js +++ b/src/index.js @@ -19,10 +19,10 @@ module.exports = postcss.plugin('postcss-url', (options) => { const to = opts.to ? path.dirname(opts.to) : from; styles.walkDecls((decl) => - promises.push(declProcessor(from, to, options, result, decl)) + promises.push(declProcessor(from, to, options, result, decl)) ); - return Promise.all(promises); + return Promise.all(promises); }; }); diff --git a/src/lib/decl-processor.js b/src/lib/decl-processor.js index 80dce9b..6e57746 100644 --- a/src/lib/decl-processor.js +++ b/src/lib/decl-processor.js @@ -96,6 +96,7 @@ const replaceUrl = (url, dir, options, result, decl) => { }; const resultPromise; + if (Array.isArray(matchedOptions)) { resultPromise = Promise.resolve(); matchedOptions.forEach((option) => { diff --git a/src/lib/get-file.js b/src/lib/get-file.js index bcadaa7..bd08562 100644 --- a/src/lib/get-file.js +++ b/src/lib/get-file.js @@ -6,38 +6,38 @@ const mime = require('mime'); const getPathByBasePath = require('./paths').getPathByBasePath; const readFileAsync = (filePath) => { - return new Promise((resolse, reject) => { - fs.readFile(filePath, (err, data) => { - if (err) { - reject(err); - } - resolve(data); + return new Promise((resolve, reject) => { + fs.readFile(filePath, (err, data) => { + if (err) { + reject(err); + } + resolve(data); + }); }); - }); }; const existFileAsync = (filePath) => { - new Promise((resolve, reject) => - fs.access(filePath, (err) => { - if (err) { - reject(); - } - resolve(path); - }) - ) + new Promise((resolve, reject) => + fs.access(filePath, (err) => { + if (err) { + reject(); + } + resolve(filePath); + }) + ); }; const oneSuccess = (promises) => { - return Promise.all(promises.map(p => { - return p.then( - val => Promise.reject(val), - err => Promise.resolve(err) + return Promise.all(promises.map((p) => { + return p.then( + (val) => Promise.reject(val), + (err) => Promise.resolve(err) + ); + })).then( + (errors) => Promise.reject(errors), + (val) => Promise.resolve(val) ); - })).then( - errors => Promise.reject(errors), - val => Promise.resolve(val) - ); -} +}; /** * @@ -48,21 +48,22 @@ const oneSuccess = (promises) => { * @returns {Promise} */ const getFile = (asset, options, dir, warn) => { - const paths = options.basePath ? - getPathByBasePath(options.basePath, dir.from, asset.pathname) : - [asset.absolutePath]; + const paths = options.basePath + ? getPathByBasePath(options.basePath, dir.from, asset.pathname) + : [asset.absolutePath]; - return oneSuccess(paths.map(path => existFileAsync(path))) - .then(path => readFileAsync(path)) - .then(contents => ({ - path: filePath, - contents: contents, - mimeType: mime.getType(filePath) - })) - .catch(() => { - warn(`Can't read file '${paths.join()}', ignoring`); - return; - }); + return oneSuccess(paths.map((path) => existFileAsync(path))) + .then((path) => readFileAsync(path) + .then((contents) => ({ + path, + contents, + mimeType: mime.getType(path) + }))) + .catch(() => { + warn(`Can't read file '${paths.join()}', ignoring`); + + return; + }); }; module.exports = getFile; diff --git a/src/type/copy.js b/src/type/copy.js index dd8a1d4..0f8fa6a 100644 --- a/src/type/copy.js +++ b/src/type/copy.js @@ -13,45 +13,45 @@ const getAssetsPath = paths.getAssetsPath; const normalize = paths.normalize; const getHashName = (file, options) => - (options && options.append ? (`${path.basename(file.path, path.extname(file.path))}_`) : '') + - calcHash(file.contents, options) + - path.extname(file.path); - -const createDirAsync = (path) => { - return Promise((resolve, reject) => { - mkdirp(path, (err) => { - if (err) reject(err); - resolve(); - }) - }) + (options && options.append ? (`${path.basename(file.path, path.extname(file.path))}_`) : '') + + calcHash(file.contents, options) + + path.extname(file.path); + +const createDirAsync = (dirPath) => { + return new Promise((resolve, reject) => { + mkdirp(dirPath, (err) => { + if (err) reject(err); + resolve(); + }); + }); }; -const writeFileAsync = (file, src) => { - return new Promise((resolve, reject) => { - fs.open(dest, 'wx', (err, fd) => { - if (err) { - if (err.code === 'EEXIST') { - resolve(); - } +const writeFileAsync = (file, dest) => { + return new Promise((resolve, reject) => { + fs.open(dest, 'wx', (err, fd) => { + if (err) { + if (err.code === 'EEXIST') { + resolve(); + } - reject(err); - } + reject(err); + } - resolve(fd); - }) + resolve(fd); + }); }) - .then(fd => { - if (!fd) return; - - return new Promise((resolve, reject) => { - fs.writeFile(newAssetPath, file.contents, (err) => { - if (err) { - reject(err); - } - resolve(); + .then((fd) => { + if (!fd) return; + + return new Promise((resolve, reject) => { + fs.writeFile(dest, file.contents, (err) => { + if (err) { + reject(err); + } + resolve(); + }); + }); }); - }) - }); }; /** @@ -74,31 +74,30 @@ const writeFileAsync = (file, src) => { */ module.exports = function processCopy(asset, dir, options, decl, warn, addDependency) { - if (!options.assetsPath && dir.from === dir.to) { - warn('Option `to` of postcss is required, ignoring'); - - return Promise.resolve(); - } - - const newRelativeAssetPath = generateNewPath(file, dir, options); - - return getFile(asset, options, dir, warn) - .then(file => { - const assetRelativePath = options.useHash ? - getHashName(file, options.hashOptions) : - asset.relativePath; - - const targetDir = getTargetDir(dir); - const newAssetBaseDir = getAssetsPath(targetDir, options.assetsPath); - const newAssetPath = path.join(newAssetBaseDir, assetRelativePath); - const newRelativeAssetPath = normalize(path.relative(targetDir, newAssetPath)); - - return createDirAsync(path.dirname(newAssetPath)) - .then(() => writeFileAsync(file, newAssetPath)) - .then(() => { - addDependency(file.path); - return `${newRelativeAssetPath}${asset.search}${asset.hash}`; - }); + if (!options.assetsPath && dir.from === dir.to) { + warn('Option `to` of postcss is required, ignoring'); + + return Promise.resolve(); + } + + return getFile(asset, options, dir, warn) + .then((file) => { + const assetRelativePath = options.useHash + ? getHashName(file, options.hashOptions) + : asset.relativePath; + + const targetDir = getTargetDir(dir); + const newAssetBaseDir = getAssetsPath(targetDir, options.assetsPath); + const newAssetPath = path.join(newAssetBaseDir, assetRelativePath); + const newRelativeAssetPath = normalize(path.relative(targetDir, newAssetPath)); + + return createDirAsync(path.dirname(newAssetPath)) + .then(() => writeFileAsync(file, newAssetPath)) + .then(() => { + addDependency(file.path); + + return `${newRelativeAssetPath}${asset.search}${asset.hash}`; + }); } - ); + ); }; diff --git a/src/type/inline.js b/src/type/inline.js index c7f405d..f100e07 100644 --- a/src/type/inline.js +++ b/src/type/inline.js @@ -1,7 +1,5 @@ 'use strict'; -const fs = require('fs'); - const processCopy = require('./copy'); const processRebase = require('./rebase'); @@ -27,32 +25,31 @@ const processFallback = (originUrl, dir, options) => { default: return Promise.resolve(); } -} +}; -const inlineProcess = (file, asset, warn, options) => { - const isSvg = file.mimeType === 'image/svg+xml'; - const defaultEncodeType = isSvg ? 'encodeURIComponent' : 'base64'; - const encodeType = options.encodeType || defaultEncodeType; +const inlineProcess = (file, asset, warn, addDependency, options) => { + const isSvg = file.mimeType === 'image/svg+xml'; + const defaultEncodeType = isSvg ? 'encodeURIComponent' : 'base64'; + const encodeType = options.encodeType || defaultEncodeType; - // Warn for svg with hashes/fragments - if (isSvg && asset.hash && !options.ignoreFragmentWarning) { - // eslint-disable-next-line max-len - warn(`Image type is svg and link contains #. Postcss-url cant handle svg fragments. SVG file fully inlined. ${file.path}`); - } + // Warn for svg with hashes/fragments + if (isSvg && asset.hash && !options.ignoreFragmentWarning) { + // eslint-disable-next-line max-len + warn(`Image type is svg and link contains #. Postcss-url cant handle svg fragments. SVG file fully inlined. ${file.path}`); + } - addDependency(file.path); + addDependency(file.path); - const optimizeSvgEncode = isSvg && options.optimizeSvgEncode; - const encodedStr = encodeFile(file, encodeType, optimizeSvgEncode); - const resultValue = options.includeUriFragment && asset.hash - ? encodedStr + asset.hash - : encodedStr; + const optimizeSvgEncode = isSvg && options.optimizeSvgEncode; + const encodedStr = encodeFile(file, encodeType, optimizeSvgEncode); + const resultValue = options.includeUriFragment && asset.hash + ? encodedStr + asset.hash + : encodedStr; - // wrap url by quotes if percent-encoded svg - return isSvg && encodeType !== 'base64' ? `"${resultValue}"` : resultValue; + // wrap url by quotes if percent-encoded svg + return isSvg && encodeType !== 'base64' ? `"${resultValue}"` : resultValue; }; - /** * Inline image in url() * @@ -70,33 +67,25 @@ const inlineProcess = (file, asset, warn, options) => { // eslint-disable-next-line complexity module.exports = function(asset, dir, options, decl, warn, addDependency) { return getFile(asset, options, dir, warn) - .then(file => { - if (!file) return; - - if (!file.mimeType) { - warn(`Unable to find asset mime-type for ${file.path}`); - - return; - } - - const maxSize = (options.maxSize || 0) * 1024; - - if (maxSize) { - const size = Buffer.byteLength(file.contents); - - if (stats.size >= maxSize) { - return processFallback.apply(this, arguments); - } - } - - return inlineProcess(file, asset, warn, options); - }); - - + .then((file) => { + if (!file) return; + if (!file.mimeType) { + warn(`Unable to find asset mime-type for ${file.path}`); + return; + } + const maxSize = (options.maxSize || 0) * 1024; + if (maxSize) { + const size = Buffer.byteLength(file.contents); + if (size >= maxSize) { + return processFallback.apply(this, arguments); + } + } + return inlineProcess(file, asset, warn, addDependency, options); + }); };