-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
92 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
packages/codemod/src/helpers/actions/lint-effected-files.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import {tryLintFile} from '../lint'; | ||
import {effectedFiles} from '../store'; | ||
|
||
export async function lintEffectedFiles() { | ||
try { | ||
await tryLintFile(Array.from(effectedFiles)); | ||
} catch (error) { | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import {getStore, writeFileAndUpdateStore} from './store'; | ||
|
||
async function tryImportPackage(packageName: string) { | ||
try { | ||
return await import(packageName); | ||
} catch { | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Try linting a file with ESLint/Prettier | ||
* First try ESLint, if it fails, try Prettier. | ||
*/ | ||
export async function tryLintFile(filePaths: string[]) { | ||
const eslintPkg = await tryImportPackage('eslint'); | ||
|
||
if (eslintPkg) { | ||
const ESLint = eslintPkg.ESLint; | ||
const eslint = new ESLint({ | ||
fix: true | ||
}); | ||
const result = await eslint.lintFiles(filePaths); | ||
|
||
await ESLint.outputFixes(result); | ||
} else { | ||
const prettier = await tryImportPackage('prettier'); | ||
const options = await prettier.resolveConfig(process.cwd()); | ||
|
||
if (prettier) { | ||
await Promise.all( | ||
filePaths.map(async (filePath) => { | ||
const rawContent = getStore(filePath, 'rawContent'); | ||
const formattedContent = await prettier.format(rawContent, { | ||
options, | ||
parser: 'typescript' | ||
}); | ||
|
||
writeFileAndUpdateStore(filePath, 'rawContent', formattedContent); | ||
}) | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters