Skip to content

Commit

Permalink
Merge branch 'main' into attribute-vs-property-docu
Browse files Browse the repository at this point in the history
  • Loading branch information
smahati authored Oct 25, 2024
2 parents c46d39b + 0caebbb commit 8c5fbcd
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 116 deletions.
42 changes: 22 additions & 20 deletions code_quality.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const gitChangedFiles = require('git-changed-files');
const prettier = require('prettier');
const prettier = require('@prettier/sync');
const prettierConfig = require('./prettier_config.json');
const codeQualityConfig = require('./package.json').codeQuality || {};
const path = require('path');
Expand All @@ -23,21 +23,21 @@ const getAllFiles = () => {
}
const sourcePaths = getSourcePaths(options.sourcePaths);
let results = [];
sourcePaths.forEach(sourcePath => {
sourcePaths.forEach((sourcePath) => {
const files = getAllFilesRecoursive(sourcePath);
results = results.concat(files);
});
return results;
};

const getSourcePaths = sourcePaths => {
return sourcePaths.split(',').map(sourcePath => path.resolve(__dirname, ...sourcePath.split('/')));
const getSourcePaths = (sourcePaths) => {
return sourcePaths.split(',').map((sourcePath) => path.resolve(__dirname, ...sourcePath.split('/')));
};

const getAllFilesRecoursive = dir => {
const getAllFilesRecoursive = (dir) => {
let results = [];
const list = fs.readdirSync(dir);
list.forEach(function(file) {
list.forEach(function (file) {
file = dir + '/' + file;
const stat = fs.statSync(file);
if (fileToExclude(file, stat)) {
Expand Down Expand Up @@ -73,15 +73,17 @@ const fileToExclude = (file, stat) => {
*/
const getChangedFiles = async () => {
const committedGitFiles = await gitChangedFiles({ baseBranch: 'main' });
return committedGitFiles.unCommittedFiles.filter(file => fs.existsSync(file) && !file.endsWith('package-lock.json'));
return committedGitFiles.unCommittedFiles.filter(
(file) => fs.existsSync(file) && !file.endsWith('package-lock.json')
);
};

/**
* Group list of file paths by extension.
* @param files: array of file absolute paths
* @returns {key: 'File extension:, value: Array of file absolute paths}
*/
const groupFilesByExtension = files => {
const groupFilesByExtension = (files) => {
return files.reduce((map, file) => {
try {
if (file.startsWith('./') || file.indexOf('.') === -1) {
Expand Down Expand Up @@ -124,12 +126,12 @@ const prettifyFile = (file, config) => {
* Applying prettier on several files. We have a specific configuration for file extension on file prettier_config.json.
* @param filesByExtension: we pass a Map json object where the key is the extension, value is an Array with absolute file paths
*/
const prettifyFiles = filesByExtension => {
const prettifyFiles = (filesByExtension) => {
let filesChanged = 0;
if (!codeQualityConfig.usePrettier) {
return; // no need to use pretty;
}
Object.keys(filesByExtension).forEach(extension => {
Object.keys(filesByExtension).forEach((extension) => {
const files = filesByExtension[extension];
const config = prettierConfig[extension];
if (!config) {
Expand All @@ -138,7 +140,7 @@ const prettifyFiles = filesByExtension => {
);
return;
}
files.forEach(file => {
files.forEach((file) => {
if (prettifyFile(file, config)) {
filesChanged++;
}
Expand All @@ -152,7 +154,7 @@ const prettifyFiles = filesByExtension => {
* @param files: array with absolute file paths
* @returns {Promise<{}|{report: string, error: boolean}>}
*/
const eslintFiles = async files => {
const eslintFiles = async (files) => {
if (!codeQualityConfig.useEslint) {
return {};
}
Expand All @@ -163,7 +165,7 @@ const eslintFiles = async files => {
for (const file of files) {
try {
const result = await eslint.lintFiles(file);
error = error || result.some(res => res.errorCount > 0);
error = error || result.some((res) => res.errorCount > 0);
await ESLint.outputFixes(result);
const resultText = formatter.format(result);
if (!!resultText && resultText.trim().length > 0) {
Expand All @@ -182,7 +184,7 @@ const eslintFiles = async files => {
* Run `prettier` on changed files before commit;
* You can also call this function using: npm run code-quality-prettier
*/
const preCommitPrettier = async filesByExtension => {
const preCommitPrettier = async (filesByExtension) => {
if (!filesByExtension) {
const files = await getChangedFiles();
if (!files) {
Expand All @@ -200,7 +202,7 @@ const preCommitPrettier = async filesByExtension => {
* Run `eslint` on changed files before commit;
* You can also call this function using: npm run code-quality-eslint
*/
const preCommitEslint = async filesByExtension => {
const preCommitEslint = async (filesByExtension) => {
if (!filesByExtension) {
const files = await getChangedFiles();
if (!files) {
Expand Down Expand Up @@ -265,12 +267,12 @@ const preCommit = async () => {
* @param filesByExtension: we pass a Map json object where the key is the extension, value is an Array with absolute file paths
* @returns {Promise<{report: string, numberFiles: number, error: boolean}>}
*/
const eslintFilesByExtension = async filesByExtension => {
const eslintFilesByExtension = async (filesByExtension) => {
let error = false;
let report = '';
let numberFiles = 0;
const extensions = Object.keys(filesByExtension);
for (const extension of extensions.filter(extension => extension === 'ts' || extension === 'js')) {
for (const extension of extensions.filter((extension) => extension === 'ts' || extension === 'js')) {
const esLintFiles = filesByExtension[extension];
numberFiles += esLintFiles.length;
const eslintResult = await eslintFiles(esLintFiles);
Expand Down Expand Up @@ -299,7 +301,7 @@ const full = async () => {
* Run `prettier` on all project files;
* You can also call this function using: npm run full-code-quality
*/
const fullPrettier = async filesByExtension => {
const fullPrettier = async (filesByExtension) => {
if (!filesByExtension) {
const files = getAllFiles();
filesByExtension = groupFilesByExtension(files);
Expand All @@ -311,7 +313,7 @@ const fullPrettier = async filesByExtension => {
* Run `eslint` on all project files;
* You can also call this function using: npm run full-code-quality
*/
const fullEslint = async filesByExtension => {
const fullEslint = async (filesByExtension) => {
if (!filesByExtension) {
const files = getAllFiles();
filesByExtension = groupFilesByExtension(files);
Expand Down Expand Up @@ -376,7 +378,7 @@ const getOptions = () => {
console.error(
'You need to pass application parameter -- mode=pre_commit|pre_commit-prettier|pre_commit-eslint|full|full-prettier|full-eslint'
);
})().catch(err => {
})().catch((err) => {
console.log(err);
process.exit(1);
});
Loading

0 comments on commit 8c5fbcd

Please sign in to comment.