Skip to content

Commit

Permalink
Fix lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Mamaev committed Apr 2, 2019
1 parent 9851cbf commit 40320b9
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 167 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Expand Up @@ -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);
};
});

Expand Down
48 changes: 24 additions & 24 deletions src/lib/decl-processor.js
Expand Up @@ -95,20 +95,21 @@ const replaceUrl = (url, dir, options, result, decl) => {
return wrappedUrlProcessor(asset, dir, option);
};

const resultPromise;
let resultPromise;

if (Array.isArray(matchedOptions)) {
resultPromise = Promise.resolve();
matchedOptions.forEach((option) => {
resultPromise = resultPromise.then(() => {
return process(option);
});
resultPromise = resultPromise.then(() => {
return process(option);
});
});
} else {
resultPromise = process(matchedOptions);
resultPromise = process(matchedOptions);
}

return resultPromise.then((url) => {
asset.url = url;
return resultPromise.then((newUrl) => {
asset.url = newUrl;
});
};

Expand All @@ -130,27 +131,26 @@ const declProcessor = (from, to, options, result, decl) => {
const promises = [];

decl.value = decl.value
.replace(pattern, (matched, before, url, after) => {
const newUrlPromise = replaceUrl(url, dir, options, result, decl);;

const marker = `::id${id++}`;
.replace(pattern, (matched, before, url, after) => {
const newUrlPromise = replaceUrl(url, dir, options, result, decl);
const marker = `::id${id++}`;

promises.push(
newUrlPromise
.then((newUrl) => {
if (!newUrl) return matched;
promises.push(
newUrlPromise
.then((newUrl) => {
if (!newUrl) return matched;

if (WITH_QUOTES.test(newUrl) && WITH_QUOTES.test(after)) {
before = before.slice(0, -1);
after = after.slice(1);
}
if (WITH_QUOTES.test(newUrl) && WITH_QUOTES.test(after)) {
before = before.slice(0, -1);
after = after.slice(1);
}

decl.value = decl.value.replace(marker, `${before}${newUrl}${after}`);
})
);
decl.value = decl.value.replace(marker, `${before}${newUrl}${after}`);
})
);

return marker;
});
return marker;
});

return Promise.all(promises);
};
Expand Down
77 changes: 39 additions & 38 deletions src/lib/get-file.js
Expand Up @@ -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)
);
}
};

/**
*
Expand All @@ -48,21 +48,22 @@ const oneSuccess = (promises) => {
* @returns {Promise<PostcssUrl~File | Undefined>}
*/
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;
Expand Down
117 changes: 58 additions & 59 deletions src/type/copy.js
Expand Up @@ -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();
});
});
});
})
});
};

/**
Expand All @@ -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}`;
});
}
);
);
};

0 comments on commit 40320b9

Please sign in to comment.