From 3598cd342ff9f8c459d06230044fe06f675bd138 Mon Sep 17 00:00:00 2001 From: restrry Date: Fri, 17 Apr 2020 11:32:42 +0200 Subject: [PATCH 1/7] convert eslint scripts into TS --- src/dev/eslint/{index.js => index.ts} | 0 src/dev/eslint/{lint_files.js => lint_files.ts} | 5 +++-- .../{pick_files_to_lint.js => pick_files_to_lint.ts} | 8 +++++--- 3 files changed, 8 insertions(+), 5 deletions(-) rename src/dev/eslint/{index.js => index.ts} (100%) rename src/dev/eslint/{lint_files.js => lint_files.ts} (90%) rename src/dev/eslint/{pick_files_to_lint.js => pick_files_to_lint.ts} (88%) diff --git a/src/dev/eslint/index.js b/src/dev/eslint/index.ts similarity index 100% rename from src/dev/eslint/index.js rename to src/dev/eslint/index.ts diff --git a/src/dev/eslint/lint_files.js b/src/dev/eslint/lint_files.ts similarity index 90% rename from src/dev/eslint/lint_files.js rename to src/dev/eslint/lint_files.ts index a76edeb2eb865..80c493233f39a 100644 --- a/src/dev/eslint/lint_files.js +++ b/src/dev/eslint/lint_files.ts @@ -19,7 +19,8 @@ import { CLIEngine } from 'eslint'; -import { createFailError } from '@kbn/dev-utils'; +import { createFailError, ToolingLog } from '@kbn/dev-utils'; +import { File } from '../file'; import { REPO_ROOT } from '../constants'; /** @@ -30,7 +31,7 @@ import { REPO_ROOT } from '../constants'; * @param {Array} files * @return {undefined} */ -export function lintFiles(log, files, { fix } = {}) { +export function lintFiles(log: ToolingLog, files: File[], { fix }: { fix?: boolean } = {}) { const cli = new CLIEngine({ cache: true, cwd: REPO_ROOT, diff --git a/src/dev/eslint/pick_files_to_lint.js b/src/dev/eslint/pick_files_to_lint.ts similarity index 88% rename from src/dev/eslint/pick_files_to_lint.js rename to src/dev/eslint/pick_files_to_lint.ts index e3212c00d9e0d..b96781fc3a611 100644 --- a/src/dev/eslint/pick_files_to_lint.js +++ b/src/dev/eslint/pick_files_to_lint.ts @@ -16,9 +16,11 @@ * specific language governing permissions and limitations * under the License. */ - import { CLIEngine } from 'eslint'; +import { ToolingLog } from '@kbn/dev-utils'; +import { File } from '../file'; + /** * Filters a list of files to only include lintable files. * @@ -26,8 +28,8 @@ import { CLIEngine } from 'eslint'; * @param {Array} files * @return {Array} */ -export function pickFilesToLint(log, files) { - const cli = new CLIEngine(); +export function pickFilesToLint(log: ToolingLog, files: File[]) { + const cli = new CLIEngine({}); return files.filter(file => { if (!file.isJs() && !file.isTypescript()) { From 4019e18f35994f493d181e7bb455d9f32fa4e5fe Mon Sep 17 00:00:00 2001 From: restrry Date: Fri, 17 Apr 2020 11:33:04 +0200 Subject: [PATCH 2/7] update settings for BWC --- .prettierrc | 1 + 1 file changed, 1 insertion(+) diff --git a/.prettierrc b/.prettierrc index d440171380611..d529ffc9859d2 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,6 @@ { "singleQuote": true, "trailingComma": "es5", + "arrowParens": "avoid", "printWidth": 100 } From bb1fcb5d0baa7e23259bd349f9e8e342645d1cb4 Mon Sep 17 00:00:00 2001 From: restrry Date: Fri, 17 Apr 2020 11:47:09 +0200 Subject: [PATCH 3/7] add script updating styles to pretter v2 --- scripts/prettier_on_changed.js | 21 ++++++++++ src/dev/run_prettier_on_changed.ts | 67 ++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 scripts/prettier_on_changed.js create mode 100644 src/dev/run_prettier_on_changed.ts diff --git a/scripts/prettier_on_changed.js b/scripts/prettier_on_changed.js new file mode 100644 index 0000000000000..f9598110f91fd --- /dev/null +++ b/scripts/prettier_on_changed.js @@ -0,0 +1,21 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +require('../src/setup_node_env/babel_register'); +require('../src/dev/run_prettier_on_changed'); diff --git a/src/dev/run_prettier_on_changed.ts b/src/dev/run_prettier_on_changed.ts new file mode 100644 index 0000000000000..d13de99ad77ac --- /dev/null +++ b/src/dev/run_prettier_on_changed.ts @@ -0,0 +1,67 @@ +/* + * Licensed to Elasticsearch B.V. under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch B.V. licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { fromNode as fcb } from 'bluebird'; +import execa from 'execa'; +// @ts-ignore +import SimpleGit from 'simple-git'; + +import { run } from '@kbn/dev-utils'; +import { REPO_ROOT } from './constants'; +import { File } from './file'; +import * as Eslint from './eslint'; + +run(async function getChangedFiles({ log }) { + const simpleGit = new SimpleGit(REPO_ROOT); + const currentBranch = await fcb(cb => simpleGit.revparse(['--abbrev-ref', 'HEAD'], cb)); + const changedFileStatuses: string = await fcb(cb => + simpleGit.diff(['--name-status', `master...${currentBranch}`], cb) + ); + + const changedFiles = changedFileStatuses + .split('\n') + // Ignore blank lines + .filter(line => line.trim().length > 0) + // git diff --name-status outputs lines with two OR three parts + // separated by a tab character + .map(line => line.trim().split('\t')) + .map(([status, ...paths]) => { + // ignore deleted files + if (status === 'D') { + return undefined; + } + + // the status is always in the first column + // .. If the file is edited the line will only have two columns + // .. If the file is renamed it will have three columns + // .. In any case, the last column is the CURRENT path to the file + return new File(paths[paths.length - 1]); + }) + .filter((file): file is File => Boolean(file)); + + const filesToLint = Eslint.pickFilesToLint(log, changedFiles); + + if (filesToLint.length > 0) { + const pathsToLint = filesToLint.map(f => f.getAbsolutePath()); + for (const path of pathsToLint) { + await execa('npx', ['prettier@2.0.4', '--write', path]); + await fcb(cb => simpleGit.add([path], cb)); + } + } +}); From b55794208fac062a7972da1adef5cc20cc41437d Mon Sep 17 00:00:00 2001 From: restrry Date: Mon, 20 Apr 2020 14:38:04 +0200 Subject: [PATCH 4/7] use default prettier config --- .prettierrc | 1 - 1 file changed, 1 deletion(-) diff --git a/.prettierrc b/.prettierrc index d529ffc9859d2..d440171380611 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,6 +1,5 @@ { "singleQuote": true, "trailingComma": "es5", - "arrowParens": "avoid", "printWidth": 100 } From 713f531904b85d1b68b8423666c910d3767ddae3 Mon Sep 17 00:00:00 2001 From: restrry Date: Mon, 20 Apr 2020 14:57:57 +0200 Subject: [PATCH 5/7] run npx once, point to the correct head branch, do not add to git --- src/dev/run_prettier_on_changed.ts | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/dev/run_prettier_on_changed.ts b/src/dev/run_prettier_on_changed.ts index d13de99ad77ac..8f4e8330b788a 100644 --- a/src/dev/run_prettier_on_changed.ts +++ b/src/dev/run_prettier_on_changed.ts @@ -17,22 +17,30 @@ * under the License. */ -import { fromNode as fcb } from 'bluebird'; import execa from 'execa'; // @ts-ignore import SimpleGit from 'simple-git'; - import { run } from '@kbn/dev-utils'; +import Util from 'util'; + +import pkg from '../../package.json'; import { REPO_ROOT } from './constants'; import { File } from './file'; import * as Eslint from './eslint'; run(async function getChangedFiles({ log }) { const simpleGit = new SimpleGit(REPO_ROOT); - const currentBranch = await fcb(cb => simpleGit.revparse(['--abbrev-ref', 'HEAD'], cb)); - const changedFileStatuses: string = await fcb(cb => - simpleGit.diff(['--name-status', `master...${currentBranch}`], cb) - ); + + const revParse = Util.promisify(simpleGit.revparse.bind(simpleGit)); + const currentBranch = await revParse(['--abbrev-ref', 'HEAD']); + const headBranch = pkg.branch; + + const diff = Util.promisify(simpleGit.diff.bind(simpleGit)); + + const changedFileStatuses: string = await diff([ + '--name-status', + `${headBranch}...${currentBranch}`, + ]); const changedFiles = changedFileStatuses .split('\n') @@ -55,13 +63,9 @@ run(async function getChangedFiles({ log }) { }) .filter((file): file is File => Boolean(file)); - const filesToLint = Eslint.pickFilesToLint(log, changedFiles); + const pathsToLint = Eslint.pickFilesToLint(log, changedFiles).map(f => f.getAbsolutePath()); - if (filesToLint.length > 0) { - const pathsToLint = filesToLint.map(f => f.getAbsolutePath()); - for (const path of pathsToLint) { - await execa('npx', ['prettier@2.0.4', '--write', path]); - await fcb(cb => simpleGit.add([path], cb)); - } + if (pathsToLint.length > 0) { + await execa('npx', ['prettier@2.0.4', '--write', ...pathsToLint]); } }); From 887c6c64062f8e2452ccd3ebcdcd336e72c008e3 Mon Sep 17 00:00:00 2001 From: restrry Date: Mon, 20 Apr 2020 15:11:56 +0200 Subject: [PATCH 6/7] throw if run script not on a clear branch --- src/dev/run_prettier_on_changed.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/dev/run_prettier_on_changed.ts b/src/dev/run_prettier_on_changed.ts index 8f4e8330b788a..f132267df2775 100644 --- a/src/dev/run_prettier_on_changed.ts +++ b/src/dev/run_prettier_on_changed.ts @@ -21,6 +21,7 @@ import execa from 'execa'; // @ts-ignore import SimpleGit from 'simple-git'; import { run } from '@kbn/dev-utils'; +import dedent from 'dedent'; import Util from 'util'; import pkg from '../../package.json'; @@ -31,6 +32,17 @@ import * as Eslint from './eslint'; run(async function getChangedFiles({ log }) { const simpleGit = new SimpleGit(REPO_ROOT); + const getStatus = Util.promisify(simpleGit.status.bind(simpleGit)); + const gitStatus = await getStatus(); + + if (gitStatus.files.length > 0) { + throw new Error( + dedent(`You should run prettier formatter on a clean branch. + Found not committed changes to: + ${gitStatus.files.map((f: { path: string }) => f.path).join('\n')}`) + ); + } + const revParse = Util.promisify(simpleGit.revparse.bind(simpleGit)); const currentBranch = await revParse(['--abbrev-ref', 'HEAD']); const headBranch = pkg.branch; From 22ba12e5c125b40138973519da1df33a0405d4d7 Mon Sep 17 00:00:00 2001 From: restrry Date: Tue, 21 Apr 2020 09:52:20 +0200 Subject: [PATCH 7/7] run in a batch, add logging --- src/dev/run_prettier_on_changed.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/dev/run_prettier_on_changed.ts b/src/dev/run_prettier_on_changed.ts index f132267df2775..deca4fa1be3ce 100644 --- a/src/dev/run_prettier_on_changed.ts +++ b/src/dev/run_prettier_on_changed.ts @@ -78,6 +78,10 @@ run(async function getChangedFiles({ log }) { const pathsToLint = Eslint.pickFilesToLint(log, changedFiles).map(f => f.getAbsolutePath()); if (pathsToLint.length > 0) { - await execa('npx', ['prettier@2.0.4', '--write', ...pathsToLint]); + log.debug('[prettier] run on %j files: ', pathsToLint.length, pathsToLint); + } + + while (pathsToLint.length > 0) { + await execa('npx', ['prettier@2.0.4', '--write', ...pathsToLint.splice(0, 100)]); } });