2
* Copyright 2020 The Backstage Authors
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
* A collection of codemods for Backstage projects
20
* @packageDocumentation
23
import { program } from 'commander';
24
import chalk from 'chalk';
25
import { codemods } from './codemods';
26
import { exitWithError } from './errors';
27
import { createCodemodAction } from './action';
28
import { version } from '../package.json';
30
async function main(argv: string[]) {
31
program.name('backstage-codemods').version(version);
33
const applyCommand = program
34
.command('apply <codemod> [target-dirs...]')
36
'Apply a codemod to target directories, defaulting to the current directory',
39
for (const codemod of codemods) {
41
.command(`${codemod.name} [target-dirs...]`)
42
.description(codemod.description)
43
.option('-d, --dry', 'Dry run, no changes written to files')
44
.action(createCodemodAction(codemod.name));
49
.description('List available codemods')
51
const maxNameLength = Math.max(...codemods.map(m => m.name.length));
52
for (const codemod of codemods) {
53
const paddedName = codemod.name.padEnd(maxNameLength, ' ');
54
console.log(`${paddedName} - ${codemod.description}`);
58
program.on('command:*', () => {
60
console.log(chalk.red(`Invalid command: ${program.args.join(' ')}`));
69
process.on('unhandledRejection', (rejection: unknown) => {
70
if (rejection instanceof Error) {
71
exitWithError(rejection);
73
exitWithError(new Error(`Unknown rejection: '${rejection}'`));
77
main(process.argv).catch(exitWithError);