|
| 1 | +import * as os from 'os'; |
| 2 | +import * as fs from 'fs'; |
1 | 3 | import * as ts from 'typescript';
|
2 | 4 | import * as chalk from 'chalk';
|
| 5 | +import * as _ from 'lodash'; |
| 6 | +import * as prettier from 'prettier'; |
| 7 | +import * as detectIndent from 'detect-indent'; |
3 | 8 |
|
4 | 9 | import { TransformFactoryFactory } from '.';
|
5 | 10 |
|
6 | 11 | /**
|
7 | 12 | * Compile and return result TypeScript
|
8 | 13 | * @param filePath Path to file to compile
|
9 | 14 | */
|
10 |
| -export function compile(filePath: string, factoryFactories: TransformFactoryFactory[]) { |
| 15 | +export function compile( |
| 16 | + filePath: string, |
| 17 | + factoryFactories: TransformFactoryFactory[], |
| 18 | + incomingPrettierOptions: prettier.Options = {}, |
| 19 | +) { |
11 | 20 | const compilerOptions: ts.CompilerOptions = {
|
12 | 21 | target: ts.ScriptTarget.ES2017,
|
13 | 22 | module: ts.ModuleKind.ES2015,
|
@@ -42,5 +51,70 @@ export function compile(filePath: string, factoryFactories: TransformFactoryFact
|
42 | 51 | const printer = ts.createPrinter();
|
43 | 52 |
|
44 | 53 | // TODO: fix the index 0 access... What if program have multiple source files?
|
45 |
| - return printer.printNode(ts.EmitHint.SourceFile, result.transformed[0], sourceFiles[0]); |
| 54 | + const printed = printer.printNode(ts.EmitHint.SourceFile, result.transformed[0], sourceFiles[0]); |
| 55 | + |
| 56 | + const inputSource = fs.readFileSync(filePath, 'utf-8'); |
| 57 | + const prettierOptions = getPrettierOptions(filePath, inputSource, incomingPrettierOptions); |
| 58 | + |
| 59 | + return prettier.format(printed, incomingPrettierOptions); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Get Prettier options based on style of a JavaScript |
| 64 | + * @param filePath Path to source file |
| 65 | + * @param source Body of a JavaScript |
| 66 | + * @param options Existing prettier option |
| 67 | + */ |
| 68 | +export function getPrettierOptions(filePath: string, source: string, options: prettier.Options): prettier.Options { |
| 69 | + const resolvedOptions = prettier.resolveConfig.sync(filePath); |
| 70 | + if (resolvedOptions) { |
| 71 | + _.defaults(resolvedOptions, options); |
| 72 | + return resolvedOptions; |
| 73 | + } |
| 74 | + const { amount: indentAmount, type: indentType } = detectIndent(source); |
| 75 | + const sourceWidth = getCodeWidth(source, 80); |
| 76 | + const semi = getUseOfSemi(source); |
| 77 | + const quotations = getQuotation(source); |
| 78 | + |
| 79 | + _.defaults(options, { |
| 80 | + tabWidth: indentAmount, |
| 81 | + useTabs: indentType && indentType === 'tab', |
| 82 | + printWidth: sourceWidth, |
| 83 | + semi, |
| 84 | + singleQuote: quotations === 'single', |
| 85 | + }); |
| 86 | + |
| 87 | + return options; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Given body of a source file, return its code width |
| 92 | + * @param source |
| 93 | + */ |
| 94 | +function getCodeWidth(source: string, defaultWidth: number): number { |
| 95 | + return source.split(os.EOL).reduce((result, line) => Math.max(result, line.length), defaultWidth); |
| 96 | +} |
| 97 | + |
| 98 | +/** |
| 99 | + * Detect if a source file is using semicolon |
| 100 | + * @todo: use an actual parser. This is not a proper implementation |
| 101 | + * @param source |
| 102 | + * @return true if code is using semicolons |
| 103 | + */ |
| 104 | +function getUseOfSemi(source: string): boolean { |
| 105 | + return source.indexOf(';') !== -1; |
| 106 | +} |
| 107 | + |
| 108 | +/** |
| 109 | + * Detect if a source file is using single quotes or double quotes |
| 110 | + * @todo use an actual parser. This is not a proper implementation |
| 111 | + * @param source |
| 112 | + */ |
| 113 | +function getQuotation(source: string): 'single' | 'double' { |
| 114 | + const numberOfSingleQuotes = (source.match(/\'/g) || []).length; |
| 115 | + const numberOfDoubleQuotes = (source.match(/\"/g) || []).length; |
| 116 | + if (numberOfSingleQuotes > numberOfDoubleQuotes) { |
| 117 | + return 'single'; |
| 118 | + } |
| 119 | + return 'double'; |
46 | 120 | }
|
0 commit comments