Skip to content

Commit

Permalink
Allow multiple --css flags in cli (fixes #514) (#546)
Browse files Browse the repository at this point in the history
  • Loading branch information
bezoerb authored Oct 3, 2022
1 parent 6651b7c commit 5077dc0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 3 deletions.
1 change: 1 addition & 0 deletions cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const meowOpts = {
css: {
type: 'string',
alias: 'c',
isMultiple: true,
},
width: {
alias: 'w',
Expand Down
8 changes: 5 additions & 3 deletions src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions test/file.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {FileNotFoundError} from '../src/errors.js';
import {
BASE_WARNING,
isRemote,
checkCssOption,
fileExists,
joinPath,
urlParse,
Expand Down Expand Up @@ -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'});
Expand Down

0 comments on commit 5077dc0

Please sign in to comment.