Skip to content

Commit

Permalink
feat: support config file using process.cwd() as directory
Browse files Browse the repository at this point in the history
  • Loading branch information
mjeanroy committed Nov 12, 2018
1 parent 6159456 commit b8ae8f8
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 7 deletions.
33 changes: 32 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,18 @@ module.exports = (options) => {

if (newOptions && hasSourceMap(newOptions)) {
sourcemap = isSourceMapEnabled(newOptions);
newOptions = omitSourceMap(newOptions);
}

// Try to resolve config file it it exists
const cwd = newOptions && hasCwd(newOptions) ? newOptions.cwd : process.cwd();
const configOptions = prettier.resolveConfig.sync(cwd);
if (configOptions != null) {
newOptions = Object.assign(configOptions, newOptions || {});
}

newOptions = omitSourceMap(newOptions);
newOptions = omitCwd(newOptions);

// Do not send an empty option object.
if (isEmpty(newOptions)) {
newOptions = undefined;
Expand Down Expand Up @@ -130,6 +139,28 @@ const SOURCE_MAPS_OPTS = [
'sourceMap', // Name of the property with rollup < 0.48.
];

/**
* Check if given option object has a `cwd` entry.
*
* @param {Object} opts Option object.
* @return {boolean} `true` if `opts` has `cwd` entry, `false` otherwise.
*/
function hasCwd(opts) {
return 'cwd' in opts;
}

/**
* Create option object from given one removing `cwd` key if present.
*
* @param {Object} opts Option object.
* @return {Object} New option object.
*/
function omitCwd(opts) {
return omitBy(opts, (v, k) => (
k === 'cwd'
));
}

/**
* Check if `sourcemap` option is defined on option object.
*
Expand Down
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions test/fixtures/prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2017 Mickael Jeanroy
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

module.exports = {
parser: 'babylon',
singleQuote: true,
tabWidth: 2,
};
19 changes: 19 additions & 0 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

'use strict';

const path = require('path');
const prettier = require('prettier');
const plugin = require('../dist/index.js');

Expand Down Expand Up @@ -316,4 +317,22 @@ describe('rollup-plugin-prettier', () => {
singleQuote: true,
});
});

it('should run prettier using config file from given current working directory', () => {
const cwd = path.join(__dirname, 'fixtures');
const options = {cwd};

spyOn(prettier, 'format').and.callThrough();

const code = 'var foo = 0;';
const instance = plugin(options);
instance.transformBundle(code);

expect(options).toEqual({cwd});
expect(prettier.format).toHaveBeenCalledWith(code, {
parser: 'babylon',
singleQuote: true,
tabWidth: 2,
});
});
});
21 changes: 15 additions & 6 deletions test/it/it.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('rollup-plugin-prettier', () => {
it('should run prettier on final bundle', (done) => {
const output = path.join(tmpDir.name, 'bundle.js');
const config = {
input: path.join(__dirname, 'fixtures', 'bundle.js'),
input: getBundlePath(),
output: {
file: output,
format: 'es',
Expand Down Expand Up @@ -90,7 +90,7 @@ describe('rollup-plugin-prettier', () => {
it('should run prettier with @babel/parser instead of babylon', (done) => {
const output = path.join(tmpDir.name, 'bundle.js');
const config = {
input: path.join(__dirname, 'fixtures', 'bundle.js'),
input: getBundlePath(),
output: {
file: output,
format: 'es',
Expand Down Expand Up @@ -132,7 +132,7 @@ describe('rollup-plugin-prettier', () => {
it('should run prettier on final bundle with sourcemap set in output option', (done) => {
const output = path.join(tmpDir.name, 'bundle.js');
const config = {
input: path.join(__dirname, 'fixtures', 'bundle.js'),
input: getBundlePath(),

output: {
file: output,
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('rollup-plugin-prettier', () => {
it('should run prettier on final bundle with sourcemap set in output array option', (done) => {
const output = path.join(tmpDir.name, 'bundle.js');
const config = {
input: path.join(__dirname, 'fixtures', 'bundle.js'),
input: getBundlePath(),

output: [
{file: output, format: 'es', sourcemap: 'inline'},
Expand Down Expand Up @@ -212,7 +212,7 @@ describe('rollup-plugin-prettier', () => {
it('should enable sourcemap (lowercase) on plugin', (done) => {
const output = path.join(tmpDir.name, 'bundle.js');
const config = {
input: path.join(__dirname, 'fixtures', 'bundle.js'),
input: getBundlePath(),

output: {
file: output,
Expand Down Expand Up @@ -250,7 +250,7 @@ describe('rollup-plugin-prettier', () => {
it('should enable sourcemap (camelcase) on plugin', (done) => {
const output = path.join(tmpDir.name, 'bundle.js');
const config = {
input: path.join(__dirname, 'fixtures', 'bundle.js'),
input: getBundlePath(),

output: {
file: output,
Expand Down Expand Up @@ -284,4 +284,13 @@ describe('rollup-plugin-prettier', () => {
done.fail(err);
});
});

/**
* Get the output bundle absolute path.
*
* @return {string} Bundle absolute path.
*/
function getBundlePath() {
return path.join(__dirname, '..', 'fixtures', 'bundle.js');
}
});

0 comments on commit b8ae8f8

Please sign in to comment.