From 5077dc048e77ef74952a94f318391c8f5d42f3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ben=20Z=C3=B6rb?= Date: Mon, 3 Oct 2022 23:28:38 +0200 Subject: [PATCH] Allow multiple --css flags in cli (fixes #514) (#546) --- cli.js | 1 + src/file.js | 8 +++++--- test/file.test.js | 10 ++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/cli.js b/cli.js index f87fc11a..f5baa2b4 100755 --- a/cli.js +++ b/cli.js @@ -44,6 +44,7 @@ const meowOpts = { css: { type: 'string', alias: 'c', + isMultiple: true, }, width: { alias: 'w', diff --git a/src/file.js b/src/file.js index 33a28d20..6a65c132 100644 --- a/src/file.js +++ b/src/file.js @@ -35,6 +35,8 @@ const unlinkAsync = promisify(fs.unlink); const readFileAsync = promisify(fs.readFile); const writeFileAsync = promisify(fs.writeFile); +export const checkCssOption = (css) => Boolean((!Array.isArray(css) && css) || (Array.isArray(css) && css.length > 0)); + export async function outputFileAsync(file, data) { const dir = path.dirname(file); @@ -793,7 +795,7 @@ export async function getStylesheet(document, filepath, options = {}) { // Create absolute file paths for local files passed via css option // to prevent document relative stylesheet paths if they are not relative specified - if (!Buffer.isBuffer(originalPath) && !isVinyl(filepath) && !isRemote(filepath) && css) { + if (!Buffer.isBuffer(originalPath) && !isVinyl(filepath) && !isRemote(filepath) && checkCssOption(css)) { filepath = path.resolve(filepath); } @@ -803,7 +805,7 @@ export async function getStylesheet(document, filepath, options = {}) { } // Restore original path for local files referenced from document and not from options - if (!Buffer.isBuffer(originalPath) && !isRemote(originalPath) && !css) { + if (!Buffer.isBuffer(originalPath) && !isRemote(originalPath) && !checkCssOption(css)) { file.path = originalPath; } @@ -868,7 +870,7 @@ async function getCss(document, options = {}) { const {css} = options; let stylesheets = []; - if (css) { + if (checkCssOption(css)) { const files = await glob(css, options); stylesheets = await mapAsync(files, (file) => getStylesheet(document, file, options)); debug('(getCss) css option set', files, stylesheets); diff --git a/test/file.test.js b/test/file.test.js index 56c3c60e..99950ee4 100644 --- a/test/file.test.js +++ b/test/file.test.js @@ -16,6 +16,7 @@ import {FileNotFoundError} from '../src/errors.js'; import { BASE_WARNING, isRemote, + checkCssOption, fileExists, joinPath, urlParse, @@ -61,6 +62,15 @@ afterEach(() => { stderr.mockRestore(); }); +test('checkCssOption', () => { + expect(checkCssOption(undefined)).toEqual(false); + expect(checkCssOption('')).toEqual(false); + expect(checkCssOption(false)).toEqual(false); + expect(checkCssOption([])).toEqual(false); + expect(checkCssOption(['abc'])).toEqual(true); + expect(checkCssOption('abc')).toEqual(true); +}); + test('Normalize paths', () => { const plattform = process.platform; Object.defineProperty(process, 'platform', {value: 'win32'});