Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gogocode-plugin-prettier): support to format code by config file #168

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ packages/esbuild-import-plugin
packages/transform-project
packages/gogocode-vue-playground
packages/gogocode-element-playground
packages/gogocode-core/umd
packages/gogocode-core/umd
packages/gogocode-plugin-prettier/test/output
23 changes: 21 additions & 2 deletions packages/gogocode-plugin-prettier/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const prettier = require('prettier');
const path = require("path")
const fs = require("fs")

/**
* 转换入口导出一个函数,按照如下函数签名
Expand All @@ -24,12 +26,29 @@ module.exports = function (fileInfo, api, options) {
return sourceCode;
}

return prettier.format(sourceCode, {
let prettierConfig = {
trailingComma: 'es5',
tabWidth: 2,
semi: false,
singleQuote: true,
printWidth: 80,
parser: /\.vue$/.test(fileInfo.path) ? 'vue' : 'typescript',
});
};
const { prettierrc, rootPath } = options

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the command gogocode -s dir/input.js -t gogocode-plugin-prettier -o dir/output.js -p prettierrc=.prettierrc.json, rootPath value here would be dir instead of current working directory.

Probably options.pwdPath makes more sense?

if (prettierrc) {
let prettierConfigFilePath = prettier.resolveConfigFile.sync(
path.resolve(rootPath, prettierrc)
)
if (fs.existsSync(prettierConfigFilePath)) {
const resolvedConfig = prettier.resolveConfig.sync(prettierConfigFilePath)
prettierConfig = {
...prettierConfig,
...resolvedConfig
}
} else {
console.warn(`prettier config file ${prettierConfigFilePath} not found`)
}
}

return prettier.format(sourceCode, prettierConfig);
};
9 changes: 8 additions & 1 deletion packages/gogocode-plugin-prettier/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"version": "0.0.1",
"description": "",
"main": "index.js",
"scripts": {},
"scripts": {
"test:api": "node ./test/run.js",
"test:command": "gogocode -s test/input.js -t index.js -o test/output/cli.js --params=prettierrc=test/.prettierrc.json"
},
"keywords": [
"vue",
"gogocode",
Expand All @@ -13,5 +16,9 @@
"license": "ISC",
"dependencies": {
"prettier": "^2.2.1"
},
"devDependencies": {
"gogocode": "^1.0.53",
"gogocode-cli": "^0.2.27"
}
}
7 changes: 7 additions & 0 deletions packages/gogocode-plugin-prettier/test/.prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"tabWidth": 4,
"useTabs": false,
"singleQuote": true,
"semi": true,
"printWidth": 120
}
21 changes: 21 additions & 0 deletions packages/gogocode-plugin-prettier/test/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function HelloWorld({greeting = "hello", greeted = '"World"', silent = false, onMouseOver,}) {

if(!greeting){return null};

// TODO: Don't use random in render
let num = Math.floor (Math.random() * 1E+7).toString().replace(/\.\d+/ig, "")

return <div className='HelloWorld' title={`You are visitor number ${ num }`} onMouseOver={onMouseOver}>

<strong>{ greeting.slice( 0, 1 ).toUpperCase() + greeting.slice(1).toLowerCase() }</strong>
{greeting.endsWith(",") ? " " : <span style={{color: '\grey'}}>", "</span> }
<em>
{ greeted }
</em>
{ (silent)
? "."
: "!"}

</div>;

}
19 changes: 19 additions & 0 deletions packages/gogocode-plugin-prettier/test/output/cli.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function HelloWorld({ greeting = 'hello', greeted = '"World"', silent = false, onMouseOver }) {
if (!greeting) {
return null;
}

// TODO: Don't use random in render
let num = Math.floor(Math.random() * 1e7)
.toString()
.replace(/\.\d+/gi, '');

return (
<div className="HelloWorld" title={`You are visitor number ${num}`} onMouseOver={onMouseOver}>
<strong>{greeting.slice(0, 1).toUpperCase() + greeting.slice(1).toLowerCase()}</strong>
{greeting.endsWith(',') ? ' ' : <span style={{ color: 'grey' }}>", "</span>}
<em>{greeted}</em>
{silent ? '.' : '!'}
</div>
);
}
19 changes: 19 additions & 0 deletions packages/gogocode-plugin-prettier/test/output/custom-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function HelloWorld({ greeting = 'hello', greeted = '"World"', silent = false, onMouseOver }) {
if (!greeting) {
return null;
}

// TODO: Don't use random in render
let num = Math.floor(Math.random() * 1e7)
.toString()
.replace(/\.\d+/gi, '');

return (
<div className="HelloWorld" title={`You are visitor number ${num}`} onMouseOver={onMouseOver}>
<strong>{greeting.slice(0, 1).toUpperCase() + greeting.slice(1).toLowerCase()}</strong>
{greeting.endsWith(',') ? ' ' : <span style={{ color: 'grey' }}>", "</span>}
<em>{greeted}</em>
{silent ? '.' : '!'}
</div>
);
}
34 changes: 34 additions & 0 deletions packages/gogocode-plugin-prettier/test/output/no-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function HelloWorld({
greeting = 'hello',
greeted = '"World"',
silent = false,
onMouseOver,
}) {
if (!greeting) {
return null
}

// TODO: Don't use random in render
let num = Math.floor(Math.random() * 1e7)
.toString()
.replace(/\.\d+/gi, '')

return (
<div
className="HelloWorld"
title={`You are visitor number ${num}`}
onMouseOver={onMouseOver}
>
<strong>
{greeting.slice(0, 1).toUpperCase() + greeting.slice(1).toLowerCase()}
</strong>
{greeting.endsWith(',') ? (
' '
) : (
<span style={{ color: 'grey' }}>", "</span>
)}
<em>{greeted}</em>
{silent ? '.' : '!'}
</div>
)
}
47 changes: 47 additions & 0 deletions packages/gogocode-plugin-prettier/test/run.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const $ = require('gogocode');
const t = require('../index');
const path = require('path');
const fs = require('fs');

const inputPath = path.resolve(__dirname, './input.js');
const outputPath = path.resolve(__dirname, './output/no-config.js');
const outputPathWithConfig = path.resolve(__dirname, './output/custom-config.js');

fs.readFile(inputPath, function read(err, code) {
if (err) {
throw err;
}
const outputCode = t(
{
source: code.toString(),
path: inputPath
},
{
gogocode: $
},
{
rootPath: __dirname,
outFilePath: outputPath,
outRootPath: __dirname,
}
);
const outputCodeWithConfig = t(
{
source: code.toString(),
path: inputPath
},
{
gogocode: $
},
{
rootPath: __dirname,
outFilePath: outputPath,
outRootPath: __dirname,
prettierrc: "./prettierrc.json"
}
);

fs.writeFileSync(outputPath, outputCode, { encoding: "utf-8" });
fs.writeFileSync(outputPathWithConfig, outputCodeWithConfig, { encoding: "utf-8" });
console.log('The file was saved!');
});