|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2021 Google LLC. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import * as fs from 'fs'; |
| 19 | +import {join} from 'path'; |
| 20 | +import * as shell from 'shelljs'; |
| 21 | + |
| 22 | +// Exit if any commands error. |
| 23 | +shell.set('-e'); |
| 24 | +process.on('unhandledRejection', e => { |
| 25 | + throw e; |
| 26 | +}); |
| 27 | + |
| 28 | +// Read sub dirs that are under the parent dirs below. |
| 29 | +const parentDirs = ['.', 'toolings']; |
| 30 | +const dirs: string[] = []; |
| 31 | +parentDirs.forEach(curParentDir => { |
| 32 | + const curDirs = |
| 33 | + fs.readdirSync(curParentDir) |
| 34 | + .filter(f => fs.statSync(join(curParentDir, f)).isDirectory()) |
| 35 | + .filter(f => !f.startsWith('.') && f !== 'node_modules'); |
| 36 | + dirs.push(...curDirs.map(curDir => join(curParentDir, curDir))); |
| 37 | +}) |
| 38 | + |
| 39 | +dirs.forEach(dir => { |
| 40 | + shell.cd(dir); |
| 41 | + |
| 42 | + if (!fs.existsSync('./package.json')) { |
| 43 | + shell.cd('../'); |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const packageJSON: {'scripts': {[key: string]: string}} = |
| 48 | + JSON.parse(fs.readFileSync('./package.json', {encoding: 'utf-8'})); |
| 49 | + if (packageJSON['scripts']['test-ci'] != null) { |
| 50 | + console.log(`~~~~~~~~~~~~ Testing ${dir} ~~~~~~~~~~~~`); |
| 51 | + shell.exec('yarn'); |
| 52 | + shell.exec('yarn test-ci'); |
| 53 | + console.log('\n'); |
| 54 | + } else if (packageJSON['scripts']['test'] != null) { |
| 55 | + console.log(`~~~~~~~~~~~~ Testing ${dir} ~~~~~~~~~~~~`); |
| 56 | + shell.exec('yarn'); |
| 57 | + shell.exec('yarn test'); |
| 58 | + console.log('\n'); |
| 59 | + } |
| 60 | + |
| 61 | + if (packageJSON['scripts']['lint'] != null) { |
| 62 | + console.log(`~~~~~~~~~~~~ Linting ${dir} ~~~~~~~~~~~~`); |
| 63 | + shell.exec('yarn'); |
| 64 | + shell.exec('yarn lint'); |
| 65 | + console.log('\n'); |
| 66 | + } |
| 67 | + shell.cd('../'); |
| 68 | +}); |
0 commit comments