|
| 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