Skip to content

Commit

Permalink
Check for Prettier (closes #15)
Browse files Browse the repository at this point in the history
  • Loading branch information
knutkirkhorn committed Jan 24, 2024
1 parent 1bb44d4 commit 2e39833
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
46 changes: 46 additions & 0 deletions checkers/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,49 @@ async function checkNpmPackage(directoryPath) {
return npmPackageErrors;
}

async function checkPrettierConfig(directoryPath) {
const packageJson = JSON.parse(
await fs.readFile(path.join(directoryPath, 'package.json'), 'utf8'),
);

const prettierErrors = [];
const hasDevelopmentDependencies = packageJson.devDependencies !== undefined;

const isUsingPrettier = hasDevelopmentDependencies
? Object.keys(packageJson.devDependencies).includes('prettier')
: false;

if (!isUsingPrettier) {
return ['package.json: missing `prettier` in `devDependencies`'];
}

const hasInstalledSortImportsPrettierPlugin = hasDevelopmentDependencies
? Object.keys(packageJson.devDependencies).includes(
'@ianvs/prettier-plugin-sort-imports',
)
: false;

if (!hasInstalledSortImportsPrettierPlugin) {
prettierErrors.push(
'package.json: missing `@ianvs/prettier-plugin-sort-imports` in `devDependencies`',
);
}

const hasInstalledPrettierEslintConfig = hasDevelopmentDependencies
? Object.keys(packageJson.devDependencies).includes(
'eslint-config-prettier',
)
: false;

if (!hasInstalledPrettierEslintConfig) {
prettierErrors.push(
'package.json: missing `eslint-config-prettier` in `devDependencies`',
);
}

return prettierErrors;
}

export async function checkJavascriptErrors(directoryPath) {
const errors = [];

Expand All @@ -138,5 +181,8 @@ export async function checkJavascriptErrors(directoryPath) {
const eslintErrors = await checkEslintConfig(directoryPath);
errors.push(...eslintErrors);

const prettierErrors = await checkPrettierConfig(directoryPath);
errors.push(...prettierErrors);

return errors;
}
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ async function checkGithubActions(directoryPath) {
async function checkDirectoryFiles(directoryPath) {
const filesToCheck = [...defaultFilesToCheck];
const isJavascriptDirectory = await hasPackageJson(directoryPath);
if (isJavascriptDirectory) filesToCheck.push('.eslintrc.json');
if (isJavascriptDirectory) {
filesToCheck.push('.eslintrc.json', 'prettier.config.js');
}

const isDirectoryGitRepo = await isGitRepo(directoryPath);
if (isDirectoryGitRepo) filesToCheck.push('.gitignore', '.gitattributes');
Expand Down

0 comments on commit 2e39833

Please sign in to comment.