Skip to content

Commit eeebefe

Browse files
committed
Build prettier options only once
1 parent abbbb57 commit eeebefe

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

src/cli.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@ program
2626
if (!globPattern) {
2727
throw new Error('You must provide a file name or glob pattern to transform');
2828
}
29+
const prettierOptions: prettier.Options = {
30+
arrowParens: program.arrowParens,
31+
bracketSpacing: !program.noBracketSpacing,
32+
jsxBracketSameLine: !!program.jsxBracketSameLine,
33+
printWidth: parseInt(program.printWidth, 10),
34+
proseWrap: program.proseWrap,
35+
semi: !program.noSemi,
36+
singleQuote: !!program.singleQuote,
37+
tabWidth: parseInt(program.tabWidth, 10),
38+
trailingComma: program.trailingComma,
39+
useTabs: !!program.useTabs,
40+
};
2941
const files = glob.sync(globPattern, {});
3042
for (const file of files) {
3143
const filePath = path.resolve(file);
3244
const newPath = filePath.replace(/\.jsx?$/, '.tsx');
3345

3446
try {
3547
fs.renameSync(filePath, newPath);
36-
const prettierOptions: prettier.Options = {
37-
arrowParens: program.arrowParens,
38-
bracketSpacing: !program.noBracketSpacing,
39-
jsxBracketSameLine: !!program.jsxBracketSameLine,
40-
printWidth: parseInt(program.printWidth, 10),
41-
proseWrap: program.proseWrap,
42-
semi: !program.noSemi,
43-
singleQuote: !!program.singleQuote,
44-
tabWidth: parseInt(program.tabWidth, 10),
45-
trailingComma: program.trailingComma,
46-
useTabs: !!program.useTabs,
47-
};
4848
const result = run(newPath, prettierOptions);
4949
fs.writeFileSync(newPath, result);
5050
} catch (error) {

src/compiler.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { TransformFactoryFactory } from '.';
1515
export function compile(
1616
filePath: string,
1717
factoryFactories: TransformFactoryFactory[],
18-
incomingPrettierOptions: prettier.Options = {},
18+
incomingPrettierOptions: prettier.Options = {}
1919
) {
2020
const compilerOptions: ts.CompilerOptions = {
2121
target: ts.ScriptTarget.ES2017,
@@ -56,7 +56,7 @@ export function compile(
5656
const inputSource = fs.readFileSync(filePath, 'utf-8');
5757
const prettierOptions = getPrettierOptions(filePath, inputSource, incomingPrettierOptions);
5858

59-
return prettier.format(printed, incomingPrettierOptions);
59+
return prettier.format(printed, prettierOptions);
6060
}
6161

6262
/**
@@ -76,7 +76,7 @@ export function getPrettierOptions(filePath: string, source: string, options: pr
7676
const semi = getUseOfSemi(source);
7777
const quotations = getQuotation(source);
7878

79-
_.defaults(options, {
79+
_.defaults(Object.assign({}, options), {
8080
tabWidth: indentAmount,
8181
useTabs: indentType && indentType === 'tab',
8282
printWidth: sourceWidth,

0 commit comments

Comments
 (0)