-
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.
feat: add format effected files (#134)
* feat: add format effected files * fix: typo * feat: change to use eslint * feat: add format to use prettier through option
- Loading branch information
Showing
12 changed files
with
141 additions
and
8 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
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-affected-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 {affectedFiles} from '../store'; | ||
|
||
export async function lintAffectedFiles() { | ||
try { | ||
await tryLintFile(Array.from(affectedFiles)); | ||
} 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,58 @@ | ||
import {getOptionsValue} from './options'; | ||
import {getStore, writeFileAndUpdateStore} from './store'; | ||
|
||
async function tryImportPackage(packageName: string) { | ||
try { | ||
return await import(packageName); | ||
} catch { | ||
return null; | ||
} | ||
} | ||
|
||
export async function lintWithESLint(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); | ||
} | ||
} | ||
|
||
export async function lintWithPrettier(filePaths: string[]) { | ||
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); | ||
}) | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Try linting a file with ESLint or Prettier | ||
*/ | ||
export async function tryLintFile(filePaths: string[]) { | ||
try { | ||
if (getOptionsValue('format')) { | ||
await lintWithPrettier(filePaths); | ||
} else { | ||
await lintWithESLint(filePaths); | ||
} | ||
} catch { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
export const CODEMOD_OPTIONS = { | ||
format: false | ||
}; | ||
|
||
export function initOptions(options: {format: boolean}) { | ||
const {format} = options; | ||
|
||
setOptionsValue('format', format); | ||
} | ||
|
||
export function setOptionsValue(key: keyof typeof CODEMOD_OPTIONS, value: boolean) { | ||
CODEMOD_OPTIONS[key] = value; | ||
} | ||
|
||
export function getOptionsValue<T extends keyof typeof CODEMOD_OPTIONS>(key: T) { | ||
return CODEMOD_OPTIONS[key]; | ||
} |
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