From 7b8f1e2a0de8aedb5b93751d437eca5c9851f747 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov <34244704+Lightning00Blade@users.noreply.github.com> Date: Tue, 6 Jun 2023 16:59:58 +0200 Subject: [PATCH] ci: make formatter run on changed files (#10328) --- .prettierignore | 1 + tools/update_chrome_revision.mjs | 50 +++++++++++++++++++++++++------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/.prettierignore b/.prettierignore index 7a08fe06..8196eff6 100644 --- a/.prettierignore +++ b/.prettierignore @@ -46,6 +46,7 @@ CHANGELOG.md package-lock.json test/assets/ docs/api +docs/browsers-api versioned_*/ # Ng-schematics diff --git a/tools/update_chrome_revision.mjs b/tools/update_chrome_revision.mjs index 1d722c83..d0a6fde6 100644 --- a/tools/update_chrome_revision.mjs +++ b/tools/update_chrome_revision.mjs @@ -14,8 +14,9 @@ * limitations under the License. */ -import {execSync} from 'child_process'; +import {execSync, exec} from 'child_process'; import {writeFile, readFile} from 'fs/promises'; +import {promisify} from 'util'; import actions from '@actions/core'; import {PUPPETEER_REVISIONS} from 'puppeteer-core/internal/revisions.js'; @@ -24,10 +25,14 @@ import {SemVer} from 'semver'; import packageJson from '../packages/puppeteer-core/package.json' assert {type: 'json'}; import {versionsPerRelease, lastMaintainedChromeVersion} from '../versions.js'; +const execAsync = promisify(exec); + const CHROME_CURRENT_VERSION = PUPPETEER_REVISIONS.chrome; const VERSIONS_PER_RELEASE_COMMENT = '// In Chrome roll patches, use `NEXT` for the Puppeteer version.'; +const touchedFiles = []; + function checkIfNeedsUpdate(oldVersion, newVersion, newRevision) { const oldSemVer = new SemVer(oldVersion, true); const newSemVer = new SemVer(newVersion, true); @@ -47,11 +52,30 @@ function checkIfNeedsUpdate(oldVersion, newVersion, newRevision) { actions.setOutput('commit', message); } +/** + * We cant use `npm run format` as it's too slow + * so we only scope the files we updated + */ +async function formatUpdateFiles() { + await Promise.all( + touchedFiles.map(file => { + return execAsync(`npx eslint --ext js --ext ts --fix ${file}`); + }) + ); + await Promise.all( + touchedFiles.map(file => { + return execAsync(`npx prettier --write ${file}`); + }) + ); +} + async function replaceInFile(filePath, search, replace) { const buffer = await readFile(filePath); const update = buffer.toString().replace(search, replace); await writeFile(filePath, update); + + touchedFiles.push(filePath); } async function getVersionAndRevisionForStable() { @@ -86,17 +110,23 @@ async function updateDevToolsProtocolVersion(revision) { ); } -async function updateVersionFileLastMaintained(updateVersion) { +async function updateVersionFileLastMaintained(currentVersion, updateVersion) { const versions = [...versionsPerRelease.keys()]; - if (version.indexOf(updateVersion) !== -1) { + if (versions.indexOf(updateVersion) !== -1) { return; } - await replaceInFile( - './versions.js', - VERSIONS_PER_RELEASE_COMMENT, - `${VERSIONS_PER_RELEASE_COMMENT}\n ['${version}', 'NEXT'],` - ); + // If we have manually rolled Chrome but not yet released + // We will have NEXT as value in the Map + if (versionsPerRelease.get(currentVersion) === 'NEXT') { + await replaceInFile('./versions.js', currentVersion, updateVersion); + } else { + await replaceInFile( + './versions.js', + VERSIONS_PER_RELEASE_COMMENT, + `${VERSIONS_PER_RELEASE_COMMENT}\n ['${version}', 'NEXT'],` + ); + } const lastMaintainedIndex = versions.indexOf(lastMaintainedChromeVersion); const nextMaintainedVersion = versions[lastMaintainedIndex - 1]; @@ -118,13 +148,13 @@ await replaceInFile( version ); -await updateVersionFileLastMaintained(version); +await updateVersionFileLastMaintained(CHROME_CURRENT_VERSION, version); await updateDevToolsProtocolVersion(revision); // Create new `package-lock.json` as we update devtools-protocol execSync('npm install --ignore-scripts'); // Make sure we pass CI formatter check by running all the new files though it -execSync('npm run format'); +await formatUpdateFiles(); // Keep this as they can be used to debug GitHub Actions if needed actions.setOutput('version', version);