Skip to content

Commit 6ecba2c

Browse files
authored
Run local prettier script (#63959)
* convert eslint scripts into TS * update settings for BWC * add script updating styles to pretter v2 * use default prettier config * run npx once, point to the correct head branch, do not add to git * throw if run script not on a clear branch * run in a batch, add logging
1 parent 6987171 commit 6ecba2c

File tree

5 files changed

+116
-5
lines changed

5 files changed

+116
-5
lines changed

scripts/prettier_on_changed.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
require('../src/setup_node_env/babel_register');
21+
require('../src/dev/run_prettier_on_changed');
File renamed without changes.

src/dev/eslint/lint_files.js renamed to src/dev/eslint/lint_files.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919

2020
import { CLIEngine } from 'eslint';
2121

22-
import { createFailError } from '@kbn/dev-utils';
22+
import { createFailError, ToolingLog } from '@kbn/dev-utils';
23+
import { File } from '../file';
2324
import { REPO_ROOT } from '../constants';
2425

2526
/**
@@ -30,7 +31,7 @@ import { REPO_ROOT } from '../constants';
3031
* @param {Array<File>} files
3132
* @return {undefined}
3233
*/
33-
export function lintFiles(log, files, { fix } = {}) {
34+
export function lintFiles(log: ToolingLog, files: File[], { fix }: { fix?: boolean } = {}) {
3435
const cli = new CLIEngine({
3536
cache: true,
3637
cwd: REPO_ROOT,

src/dev/eslint/pick_files_to_lint.js renamed to src/dev/eslint/pick_files_to_lint.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,20 @@
1616
* specific language governing permissions and limitations
1717
* under the License.
1818
*/
19-
2019
import { CLIEngine } from 'eslint';
2120

21+
import { ToolingLog } from '@kbn/dev-utils';
22+
import { File } from '../file';
23+
2224
/**
2325
* Filters a list of files to only include lintable files.
2426
*
2527
* @param {ToolingLog} log
2628
* @param {Array<File>} files
2729
* @return {Array<File>}
2830
*/
29-
export function pickFilesToLint(log, files) {
30-
const cli = new CLIEngine();
31+
export function pickFilesToLint(log: ToolingLog, files: File[]) {
32+
const cli = new CLIEngine({});
3133

3234
return files.filter(file => {
3335
if (!file.isJs() && !file.isTypescript()) {

src/dev/run_prettier_on_changed.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import execa from 'execa';
21+
// @ts-ignore
22+
import SimpleGit from 'simple-git';
23+
import { run } from '@kbn/dev-utils';
24+
import dedent from 'dedent';
25+
import Util from 'util';
26+
27+
import pkg from '../../package.json';
28+
import { REPO_ROOT } from './constants';
29+
import { File } from './file';
30+
import * as Eslint from './eslint';
31+
32+
run(async function getChangedFiles({ log }) {
33+
const simpleGit = new SimpleGit(REPO_ROOT);
34+
35+
const getStatus = Util.promisify(simpleGit.status.bind(simpleGit));
36+
const gitStatus = await getStatus();
37+
38+
if (gitStatus.files.length > 0) {
39+
throw new Error(
40+
dedent(`You should run prettier formatter on a clean branch.
41+
Found not committed changes to:
42+
${gitStatus.files.map((f: { path: string }) => f.path).join('\n')}`)
43+
);
44+
}
45+
46+
const revParse = Util.promisify(simpleGit.revparse.bind(simpleGit));
47+
const currentBranch = await revParse(['--abbrev-ref', 'HEAD']);
48+
const headBranch = pkg.branch;
49+
50+
const diff = Util.promisify(simpleGit.diff.bind(simpleGit));
51+
52+
const changedFileStatuses: string = await diff([
53+
'--name-status',
54+
`${headBranch}...${currentBranch}`,
55+
]);
56+
57+
const changedFiles = changedFileStatuses
58+
.split('\n')
59+
// Ignore blank lines
60+
.filter(line => line.trim().length > 0)
61+
// git diff --name-status outputs lines with two OR three parts
62+
// separated by a tab character
63+
.map(line => line.trim().split('\t'))
64+
.map(([status, ...paths]) => {
65+
// ignore deleted files
66+
if (status === 'D') {
67+
return undefined;
68+
}
69+
70+
// the status is always in the first column
71+
// .. If the file is edited the line will only have two columns
72+
// .. If the file is renamed it will have three columns
73+
// .. In any case, the last column is the CURRENT path to the file
74+
return new File(paths[paths.length - 1]);
75+
})
76+
.filter((file): file is File => Boolean(file));
77+
78+
const pathsToLint = Eslint.pickFilesToLint(log, changedFiles).map(f => f.getAbsolutePath());
79+
80+
if (pathsToLint.length > 0) {
81+
log.debug('[prettier] run on %j files: ', pathsToLint.length, pathsToLint);
82+
}
83+
84+
while (pathsToLint.length > 0) {
85+
await execa('npx', ['prettier@2.0.4', '--write', ...pathsToLint.splice(0, 100)]);
86+
}
87+
});

0 commit comments

Comments
 (0)