Skip to content

Commit

Permalink
refactor(ng-dev/format): use command module syntax for defining comma…
Browse files Browse the repository at this point in the history
…nds in ng-dev format commands (#762)

Migrating to the command module syntax to align with the other command sets.

PR Close #762
  • Loading branch information
josephperrott committed Aug 10, 2022
1 parent 30f1061 commit e238b8f
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 57 deletions.
42 changes: 42 additions & 0 deletions ng-dev/format/all.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Argv, Arguments, CommandModule} from 'yargs';

import {GitClient} from '../utils/git/git-client.js';
import {checkFiles, formatFiles} from './format.js';

/** Command line options. */
export interface Options {
check: boolean;
}

/** Yargs command builder for the command. */
function builder(argv: Argv): Argv<Options> {
return argv.option('check', {
type: 'boolean',
default: process.env['CI'] ? true : false,
description: 'Run the formatter to check formatting rather than updating code format',
});
}

/** Yargs command handler for the command. */
async function handler({check}: Arguments<Options>) {
const git = await GitClient.get();
const executionCmd = check ? checkFiles : formatFiles;
const allFiles = git.allFiles();
process.exitCode = await executionCmd(allFiles);
}

/** CLI command module. */
export const AllFilesModule: CommandModule<{}, Options> = {
builder,
handler,
command: 'all',
describe: 'Run the formatter on all files in the repository',
};
46 changes: 46 additions & 0 deletions ng-dev/format/changed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Argv, Arguments, CommandModule} from 'yargs';

import {GitClient} from '../utils/git/git-client.js';
import {checkFiles, formatFiles} from './format.js';

/** Command line options. */
export interface Options {
shaOrRef?: string;
check: boolean;
}

/** Yargs command builder for the command. */
function builder(argv: Argv): Argv<Options> {
return argv
.option('check', {
type: 'boolean',
default: process.env['CI'] ? true : false,
description: 'Run the formatter to check formatting rather than updating code format',
})
.positional('shaOrRef', {type: 'string'});
}

/** Yargs command handler for the command. */
async function handler({shaOrRef, check}: Arguments<Options>) {
const git = await GitClient.get();
const sha = shaOrRef || git.mainBranchName;
const executionCmd = check ? checkFiles : formatFiles;
const allChangedFilesSince = git.allChangesFilesSince(sha);
process.exitCode = await executionCmd(allChangedFilesSince);
}

/** CLI command module. */
export const ChangedModule: CommandModule<{}, Options> = {
builder,
handler,
command: 'changed [shaOrRef]',
describe: 'Run the formatter on files changed since the provided sha/ref',
};
65 changes: 8 additions & 57 deletions ng-dev/format/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,16 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Argv} from 'yargs';
import {GitClient} from '../utils/git/git-client.js';

import {checkFiles, formatFiles} from './format.js';
import {AllFilesModule} from './all.js';
import {ChangedModule} from './changed.js';
import {FilesModule} from './files.js';
import {StagedModule} from './staged.js';

/** Build the parser for the format commands. */
export function buildFormatParser(localYargs: Argv) {
return localYargs
.help()
.strict()
.demandCommand()
.option('check', {
type: 'boolean',
default: process.env['CI'] ? true : false,
description: 'Run the formatter to check formatting rather than updating code format',
})
.command(
'all',
'Run the formatter on all files in the repository',
(args) => args,
async ({check}) => {
const git = await GitClient.get();
const executionCmd = check ? checkFiles : formatFiles;
const allFiles = git.allFiles();
process.exitCode = await executionCmd(allFiles);
},
)
.command(
'changed [shaOrRef]',
'Run the formatter on files changed since the provided sha/ref',
(args) => args.positional('shaOrRef', {type: 'string'}),
async ({shaOrRef, check}) => {
const git = await GitClient.get();
const sha = shaOrRef || git.mainBranchName;
const executionCmd = check ? checkFiles : formatFiles;
const allChangedFilesSince = git.allChangesFilesSince(sha);
process.exitCode = await executionCmd(allChangedFilesSince);
},
)
.command(
'staged',
'Run the formatter on all staged files',
(args) => args,
async ({check}) => {
const git = await GitClient.get();
const executionCmd = check ? checkFiles : formatFiles;
const allStagedFiles = git.allStagedFiles();
process.exitCode = await executionCmd(allStagedFiles);
if (!check && process.exitCode === 0) {
git.runGraceful(['add', ...allStagedFiles]);
}
},
)
.command(
'files <files..>',
'Run the formatter on provided files',
(args) => args.positional('files', {array: true, type: 'string'}),
async ({check, files}) => {
const executionCmd = check ? checkFiles : formatFiles;
process.exitCode = await executionCmd(files!);
},
);
.command(AllFilesModule)
.command(StagedModule)
.command(ChangedModule)
.command(FilesModule);
}
42 changes: 42 additions & 0 deletions ng-dev/format/files.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Argv, Arguments, CommandModule} from 'yargs';

import {checkFiles, formatFiles} from './format.js';

/** Command line options. */
export interface Options {
files: string[];
check: boolean;
}

/** Yargs command builder for the command. */
function builder(argv: Argv): Argv<Options> {
return argv
.option('check', {
type: 'boolean',
default: process.env['CI'] ? true : false,
description: 'Run the formatter to check formatting rather than updating code format',
})
.positional('files', {array: true, type: 'string', demandOption: true});
}

/** Yargs command handler for the command. */
async function handler({files, check}: Arguments<Options>) {
const executionCmd = check ? checkFiles : formatFiles;
process.exitCode = await executionCmd(files);
}

/** CLI command module. */
export const FilesModule: CommandModule<{}, Options> = {
builder,
handler,
command: 'files <files..>',
describe: 'Run the formatter on provided files',
};
45 changes: 45 additions & 0 deletions ng-dev/format/staged.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {Argv, Arguments, CommandModule} from 'yargs';

import {GitClient} from '../utils/git/git-client.js';
import {checkFiles, formatFiles} from './format.js';

/** Command line options. */
export interface Options {
check: boolean;
}

/** Yargs command builder for the command. */
function builder(argv: Argv): Argv<Options> {
return argv.option('check', {
type: 'boolean',
default: process.env['CI'] ? true : false,
description: 'Run the formatter to check formatting rather than updating code format',
});
}

/** Yargs command handler for the command. */
async function handler({check}: Arguments<Options>) {
const git = await GitClient.get();
const executionCmd = check ? checkFiles : formatFiles;
const allStagedFiles = git.allStagedFiles();
process.exitCode = await executionCmd(allStagedFiles);
if (!check && process.exitCode === 0) {
git.runGraceful(['add', ...allStagedFiles]);
}
}

/** CLI command module. */
export const StagedModule: CommandModule<{}, Options> = {
builder,
handler,
command: 'staged',
describe: 'Run the formatter on all staged files',
};

0 comments on commit e238b8f

Please sign in to comment.