Skip to content

Commit

Permalink
fix(TDOPS-5707): Move lint-merge-report to scripts-core (#4996)
Browse files Browse the repository at this point in the history
  • Loading branch information
smouillour authored Nov 21, 2023
1 parent f43609c commit e77b32c
Show file tree
Hide file tree
Showing 10 changed files with 261 additions and 146 deletions.
5 changes: 5 additions & 0 deletions .changeset/five-games-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@talend/scripts-core': minor
---

Add option lint-merge-report
5 changes: 5 additions & 0 deletions .changeset/rotten-scissors-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@talend/scripts-yarn-workspace': major
---

[BREAKING CHANGE]Remove option lint-merge-report
131 changes: 66 additions & 65 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
{
"devDependencies": {
"@changesets/cli": "^2.26.2",
"@talend/scripts-config-babel": "^13.2.0",
"@talend/scripts-config-prettier": "^12.1.0",
"@talend/scripts-yarn-workspace": "^1.3.0",
"cross-env": "^7.0.3",
"cross-spawn": "^7.0.3",
"eslint": "^8.53.0",
"husky": "^8.0.3",
"i18next-scanner": "^4.4.0",
"lint-staged": "^13.3.0",
"prettier": "^3.1.0",
"rimraf": "^3.0.2",
"typescript": "^5.2.2"
},
"scripts": {
"postinstall": "talend-yarn-workspace run build:lib",
"pre-release": "talend-yarn-workspace run pre-release",
"start": "yarn workspace @talend/ui-playground run start",
"start-storybook": "yarn workspace @talend/ui-storybook-one run start",
"release": "yarn pre-release && yarn changeset publish",
"lint-staged": "lint-staged",
"lint": "cross-env WORKSPACE_RUN_FAIL=no-bail talend-yarn-workspace run lint",
"lint-merge-report": "talend-yarn-workspace lint-merge-report",
"test": "cross-env TZ=UTC talend-yarn-workspace run test --silent",
"test:update": "cross-env TZ=UTC yarn workspaces run test --silent -u",
"test:cov": "cross-env TZ=UTC talend-yarn-workspace run test:cov",
"test:demo": "talend-yarn-workspace run test:demo",
"test:cron": "talend-yarn-workspace run test:cron",
"start-components": "yarn workspace @talend/react-components run start",
"start-containers": "yarn workspace @talend/react-containers run start",
"start-stepper": "yarn workspace @talend/react-stepper run start",
"start-forms": "yarn workspace @talend/react-forms run start",
"start-theme": "yarn workspace @talend/bootstrap-theme run start",
"changelog": "git log --pretty=\"format:%C(bold green)%ad%C(reset) %s\" --date=short --color",
"prepare": "husky install"
},
"resolutions": {
"**/cypress": "^12.17.4",
"**/@types/react": "^18.2.7",
"**/@types/react-dom": "^18.2.7",
"**/i18next-scanner-typescript/typescript": "^5.0.4",
"**/browser-sync-client/typescript": "^5.0.4",
"**/vinyl-fs/glob-parent": "^5.1.2",
"**/fast-glob/glob-parent": "^5.1.2",
"**/trim": "^1.0.1",
"**/x-default-browser": "^0.5.2",
"**/reactour/prop-types": "^15.7.2"
},
"repository": {
"type": "git",
"url": "https://github.com/Talend/ui.git"
},
"version": "0.0.0",
"private": true,
"workspaces": [
"packages/*",
"tools/*",
"fork/*"
],
"lint-staged": {
"*.{json,md,mdx,html,js,jsx,ts,tsx}": [
"prettier --write"
]
}
"devDependencies": {
"@changesets/cli": "^2.26.2",
"@talend/scripts-config-babel": "^13.2.0",
"@talend/scripts-config-prettier": "^12.1.0",
"@talend/scripts-core": "^16.2.0",
"@talend/scripts-yarn-workspace": "^1.3.0",
"cross-env": "^7.0.3",
"cross-spawn": "^7.0.3",
"eslint": "^8.53.0",
"husky": "^8.0.3",
"i18next-scanner": "^4.4.0",
"lint-staged": "^13.3.0",
"prettier": "^3.1.0",
"rimraf": "^3.0.2",
"typescript": "^5.2.2"
},
"scripts": {
"postinstall": "talend-yarn-workspace run build:lib",
"pre-release": "talend-yarn-workspace run pre-release",
"start": "yarn workspace @talend/ui-playground run start",
"start-storybook": "yarn workspace @talend/ui-storybook-one run start",
"release": "yarn pre-release && yarn changeset publish",
"lint-staged": "lint-staged",
"lint": "cross-env WORKSPACE_RUN_FAIL=no-bail talend-yarn-workspace run lint",
"lint-merge-report": "talend-scripts lint-merge-report",
"test": "cross-env TZ=UTC talend-yarn-workspace run test --silent",
"test:update": "cross-env TZ=UTC yarn workspaces run test --silent -u",
"test:cov": "cross-env TZ=UTC talend-yarn-workspace run test:cov",
"test:demo": "talend-yarn-workspace run test:demo",
"test:cron": "talend-yarn-workspace run test:cron",
"start-components": "yarn workspace @talend/react-components run start",
"start-containers": "yarn workspace @talend/react-containers run start",
"start-stepper": "yarn workspace @talend/react-stepper run start",
"start-forms": "yarn workspace @talend/react-forms run start",
"start-theme": "yarn workspace @talend/bootstrap-theme run start",
"changelog": "git log --pretty=\"format:%C(bold green)%ad%C(reset) %s\" --date=short --color",
"prepare": "husky install"
},
"resolutions": {
"**/cypress": "^12.17.4",
"**/@types/react": "^18.2.7",
"**/@types/react-dom": "^18.2.7",
"**/i18next-scanner-typescript/typescript": "^5.0.4",
"**/browser-sync-client/typescript": "^5.0.4",
"**/vinyl-fs/glob-parent": "^5.1.2",
"**/fast-glob/glob-parent": "^5.1.2",
"**/trim": "^1.0.1",
"**/x-default-browser": "^0.5.2",
"**/reactour/prop-types": "^15.7.2"
},
"repository": {
"type": "git",
"url": "https://github.com/Talend/ui.git"
},
"version": "0.0.0",
"private": true,
"workspaces": [
"packages/*",
"tools/*",
"fork/*"
],
"lint-staged": {
"*.{json,md,mdx,html,js,jsx,ts,tsx}": [
"prettier --write"
]
}
}
5 changes: 5 additions & 0 deletions talend-scripts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"lintMergeReport": {
"packageDirs": ["fork", "packages", "tools"]
}
}
3 changes: 2 additions & 1 deletion tools/scripts-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ It will expose basic scripts:
- test
- build
- lint
- lint-merge-report

By default no configuration is needed but you can at any time create a configuration file and extends from the default config.
By default no configuration is needed (except for lint-merge-report) but you can at any time create a configuration file and extends from the default config.

`talend-scripts` will detect the kind of package you are in.

Expand Down
13 changes: 13 additions & 0 deletions tools/scripts-core/doc/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ With the cli you have multiple commands:
- **build** build a project using webpack (src/app/index.js)
- **start** start your project using webpack-dev-server or storyook
- **lint** execute eslint and stylelint on your project
- **lint-merge-report** merge eslint and stylelint report (made to be used in GHA to display lint issue with a monorepo)
- **extends** generate configuration files which extend the talend-scripts ones

5. Define the npm scripts you need.
Expand Down Expand Up @@ -106,3 +107,15 @@ _--fix_: fix the corresponding issues that can be automatically fixed.
## tests options

This command is a pass through jest / karma. So you can pass anything you want.

## lint-merge-report config

To be able to use lint-merge-report, you will have to add config in talend-scripts.json. As example, for tui, we have this:

```
{
"lintMergeReport": {
"packageDirs": ["fork", "packages", "tools"]
}
}
```
11 changes: 8 additions & 3 deletions tools/scripts-core/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env node

/* eslint-disable no-console */
import mergeReport from './scripts/lint-merge-report.js';
import { getEnv } from './utils/env.js';
import { printSeparator } from './utils/log.js';
import { getPresetApi } from './utils/preset.js';
Expand All @@ -12,11 +13,12 @@ if (command === '--help' || command === '-h' || command === 'help') {
console.log(`Please use one of the following commands:
* start
* build
* build:lib:umd
* build-storybook
* lint
* lint-merge-report
* test
* extends
* start-storybook
* build-storybook
`);
process.exit(0);
}
Expand Down Expand Up @@ -62,7 +64,7 @@ async function runScript() {
console.error(e);
}

if (result.then) {
if (result?.then) {
result
.then(() => {
process.exit(0);
Expand All @@ -85,6 +87,9 @@ switch (command) {
case 'test':
runScript(command, options);
break;
case 'lint-merge-report':
mergeReport(options);
break;
case 'build:lib':
case 'build:lib:umd':
console.log('This command do not exists anymore, please use just "build" command');
Expand Down
156 changes: 156 additions & 0 deletions tools/scripts-core/src/scripts/lint-merge-report.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/* eslint-disable no-console */
import { spawn } from 'child_process';

/* eslint-disable no-param-reassign */
import fs from 'fs';

import { getEnv } from '../utils/env.js';
import { getPresetApi } from '../utils/preset.js';

const reports = ['eslint-report.json', 'stylelint-report.json'];

let buff = [];

async function run(cmd, opts = {}) {
if (opts.verbose) {
console.log(`\n#### RUNNER: ${cmd.name} ${cmd.args.join(' ')}`);
}
const start = Date.now();
return new Promise(async (resolve, reject) => {
const out = spawn(cmd.name, cmd.args);
let stdout = '';
let stderr = '';
out.on('error', error => {
console.error(error);
reject(error);
});
out.on('close', () => {
resolve(stdout);
});
out.on('exit', code => {
if (opts.verbose && stderr) {
console.error(`#### RUNNER: Child Process STDERR: ${stderr}`);
}
if (opts.verbose && stdout) {
console.error(`#### RUNNER: Child Process STDOUT: ${stdout}`);
}
if (code > 0) {
run.exitCode += 1;
console.error(`#### RUNNER: ${cmd.name} ${cmd.args.join(' ')} exit code ${code}`);
reject(`STDOUT: ${stdout}\n\nSTDERR: ${stderr}`);
return;
}
const end = Date.now();
console.log(
`#### RUNNER: ${cmd.name} ${cmd.args.join(' ')} exit code ${code} in ${
(end - start) / 1000
} seconds`,
);
resolve(stdout);
});
out.stdout.on('data', data => {
const datastr = data.toString();
if (data && datastr) {
stdout += datastr;
}
});

out.stderr.on('data', data => {
const datastr = data.toString();
if (data && datastr) {
stderr += datastr;
}
});
});
}

function transform(item) {
if (item.source && !item.filePath) {
item.filePath = item.source;
delete item.source;
}
if (item.warnings && !item.messages) {
item.messages = item.warnings.map(w => ({
...w,
severity: 1,
message: w.text,
ruleId: w.rule,
}));
item.warningCount = item.warnings.length;
delete item.warning;
} else if (item.messages) {
item.messages = item.messages.map(w => ({ ...w, severity: 1 }));
item.warningCount += item.errorCount;
item.errorCount = 0;
}
return item;
}

function getPackages(packageDirs = []) {
return packageDirs.flatMap(dir =>
fs.readdirSync(dir).map(subDir => ({
name: subDir,
location: `${dir}/${subDir}`,
})),
);
}

export default function mergeReport(options) {
// current env vars and talend scripts configuration in <project-folder>/talend-scripts.(js/json)
const env = getEnv(options);
env.TALEND_MODE = 'production';
console.log(`Talend scripts mode : ${env.TALEND_MODE}`);
if (env.TALEND_SCRIPTS_CONFIG) {
console.log('Talend scripts configuration file found and loaded');
} else {
console.log('Talend scripts configuration file not found');
}
const presetApi = getPresetApi(env);
const rootPackageDirs = presetApi.getUserConfig('lintMergeReport', {})?.packageDirs || [];
const packages = getPackages(rootPackageDirs);

if (packages.length === 0) {
throw new Error(
'No packages has been retrieved, check if the talend-scripts.json is well configured',
);
}

// https://stackoverflow.com/questions/65944700/how-to-run-git-diff-in-github-actions
const diff = run({
name: 'git',
args: ['diff', '--name-only', `origin/${options[0]}`, `origin/${options[1]}`],
})
.then(out =>
out
.split('\n')
.map(str => str.trim())
.filter(Boolean),
)
.catch(e => console.error(e));

diff.then(files => {
function onlyIfInDiff(lint) {
return !!files.find(f => lint.filePath.endsWith(`/${f}`));
}

packages.forEach(pkg => {
reports.forEach(report => {
const fpath = `${pkg.location}/${report}`;
if (fs.existsSync(fpath)) {
try {
buff = buff.concat(
JSON.parse(fs.readFileSync(fpath)).map(transform).filter(onlyIfInDiff),
);
} catch (e) {
console.error(e);
}
}
});
});
const target = `${process.cwd()}/eslint-report.json`;

// eslint-disable-next-line no-console
console.log(`report merge into ${target}`);
fs.writeFileSync(target, JSON.stringify(buff, null, 2));
});
}
Loading

0 comments on commit e77b32c

Please sign in to comment.