-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up formatting check and local formatting script (#5530)
- Loading branch information
Showing
7 changed files
with
261 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
name: Formatting Check (Run yarn format locally if this fails) | ||
|
||
on: pull_request | ||
|
||
env: | ||
GITHUB_PULL_REQUEST_HEAD_SHA: ${{ github.event.pull_request.head.sha }} | ||
GITHUB_PULL_REQUEST_BASE_SHA: ${{ github.event.pull_request.base.sha }} | ||
|
||
jobs: | ||
format: | ||
name: Run license and prettier formatting tasks | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout Repo | ||
uses: actions/checkout@master | ||
with: | ||
# get all history for the diff | ||
fetch-depth: 0 | ||
- name: Set up Node (14) | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: 14.x | ||
- name: Yarn install | ||
run: yarn | ||
- name: Run formatting script | ||
run: yarn format | ||
- name: Check for changes (fail if so) | ||
run: git diff --exit-code | ||
- name: Formatting needs to be updated. See message below. | ||
if: ${{ failure() }} | ||
run: echo "Something was changed by formatting. Run \`yarn format\` locally to do a prettier/license pass. Use \`yarn format --help\` to see options." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC | ||
* | ||
* Licensed 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 { doPrettier } from './prettier'; | ||
import { doLicense } from './license'; | ||
import { resolve } from 'path'; | ||
import simpleGit from 'simple-git/promise'; | ||
import chalk from 'chalk'; | ||
import glob from 'glob'; | ||
import { join } from 'path'; | ||
import yargs from 'yargs/yargs'; | ||
import { hideBin } from 'yargs/helpers'; | ||
|
||
// Computed Deps | ||
const root = resolve(__dirname, '../..'); | ||
const git = simpleGit(root); | ||
|
||
const { path: targetPath, all: runOnAll } = yargs(hideBin(process.argv)) | ||
.option('all', { | ||
describe: 'Run on all js/ts files in repo', | ||
type: 'boolean' | ||
}) | ||
.option('path', { | ||
describe: 'Specific directory to run on', | ||
type: 'string' | ||
}) | ||
.help() | ||
.usage( | ||
`Runs prettier formatting and updates license headers. ` + | ||
`If no arguments are provided it will run formatting on any ` + | ||
`files changed since master.` | ||
) | ||
.parseSync(); | ||
|
||
const format = async () => { | ||
let changedFiles: string[] | undefined; | ||
try { | ||
if (!runOnAll) { | ||
// If a file pattern is provided, get the individual files. | ||
if (targetPath) { | ||
changedFiles = await new Promise(resolve => { | ||
glob(join(targetPath, '/**/*'), (err, res) => resolve(res)); | ||
}); | ||
} else { | ||
// Otherwise get all files changed since master. | ||
const baseSha = process.env.GITHUB_PULL_REQUEST_BASE_SHA || 'master'; | ||
const diff = await git.diff([ | ||
'--name-only', | ||
'--diff-filter=d', | ||
baseSha | ||
]); | ||
changedFiles = diff.split('\n'); | ||
|
||
if (changedFiles.length === 0) { | ||
console.log(chalk`{green No files changed since ${baseSha}.}`); | ||
return; | ||
} | ||
} | ||
|
||
// Only run on .js or .ts files. | ||
changedFiles = changedFiles!.filter(line => line.match(/\.(js|ts)$/)); | ||
|
||
if (changedFiles.length === 0) { | ||
console.log(chalk`{green No .js or .ts files found in list.`); | ||
return; | ||
} | ||
} | ||
|
||
await doPrettier(changedFiles); | ||
|
||
await doLicense(changedFiles); | ||
|
||
process.exit(); | ||
} catch (err) { | ||
console.error(chalk` | ||
{red Formatting failed, error body below} | ||
`); | ||
console.error(err); | ||
return process.exit(1); | ||
} | ||
}; | ||
|
||
format(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/** | ||
* @license | ||
* Copyright 2017 Google LLC | ||
* | ||
* Licensed 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 { resolve } from 'path'; | ||
import { exec, spawn } from 'child-process-promise'; | ||
import chalk from 'chalk'; | ||
|
||
const root = resolve(__dirname, '../..'); | ||
const packageJson = require(root + '/package.json'); | ||
|
||
async function checkVersion() { | ||
const { stdout } = await exec('yarn prettier --version', { | ||
cwd: root | ||
}); | ||
const lines = stdout.split('\n'); | ||
let runtimeVersion; | ||
for (const line of lines) { | ||
if (line.match(/^\d+\.\d+\.\d+$/)) { | ||
runtimeVersion = line; | ||
break; | ||
} | ||
} | ||
if (!runtimeVersion) { | ||
console.warn('Was not able to find runtime version of prettier.'); | ||
return; | ||
} | ||
const packageVersion = packageJson.devDependencies.prettier; | ||
if (packageVersion !== runtimeVersion) { | ||
const mismatchText = | ||
`Installed version of prettier (${runtimeVersion}) does not match ` + | ||
`required version (${packageVersion}).`; | ||
const versionMismatchMessage = chalk` | ||
{red ${mismatchText}} | ||
{yellow Please re-run {reset 'yarn'} from the root of the repo and try again.} | ||
`; | ||
throw new Error(versionMismatchMessage); | ||
} | ||
} | ||
|
||
export async function doPrettier(changedFiles?: string[]) { | ||
try { | ||
await checkVersion(); | ||
} catch (e) { | ||
console.error(e); | ||
return process.exit(1); | ||
} | ||
|
||
let prettierArgs = [ | ||
'prettier', | ||
'--config', | ||
`${resolve(root, '.prettierrc')}`, | ||
'--write' | ||
]; | ||
|
||
if (changedFiles) { | ||
prettierArgs = [...prettierArgs, ...changedFiles]; | ||
console.log( | ||
chalk`{green Validating ${changedFiles.length} files with Prettier}` | ||
); | ||
} else { | ||
prettierArgs.push('{,!(node_modules)/}**/*.{js,ts}'); | ||
console.log(chalk`{green Validating all .js and .ts files with Prettier}`); | ||
} | ||
|
||
try { | ||
await spawn('yarn', prettierArgs, { | ||
stdio: 'inherit', | ||
cwd: root | ||
}); | ||
} catch (e) { | ||
if ((e as NodeJS.ErrnoException).code === 'E2BIG') { | ||
console.error( | ||
chalk`{red Too many files, use a smaller pattern or use the --all flag.}` | ||
); | ||
process.exit(); | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} |
Oops, something went wrong.