Skip to content

Commit

Permalink
feat(autocommit): implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
paveltobias authored and vmasek committed Jun 28, 2018
1 parent 895d428 commit a9d5fd0
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 4 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
"fs-extra": "^6.0.1",
"mustache": "^2.3.0",
"optimist": "^0.6.1",
"swagger-parser": "^5.0.0"
"swagger-parser": "^5.0.0",
"which": "^1.3.1"
},
"devDependencies": {
"@types/fs-extra": "^5.0.3",
Expand All @@ -69,6 +70,7 @@
"@types/rimraf": "^2.0.2",
"@types/swagger-parser": "^4.0.2",
"@types/swagger-schema-official": "^2.0.10",
"@types/which": "^1.3.1",
"dir-compare": "^1.4.0",
"rimraf": "^2.5.2",
"standard-version": "^4.4.0",
Expand Down
52 changes: 52 additions & 0 deletions src/git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { sync as whichSync } from 'which';
import { spawnSync } from 'child_process';
import { version } from '../package.json';

type AsyncProcedure = (...args: any[]) => Promise<void>; // tslint:disable-line no-any

interface CommitOptions {
addPath: string;
}

const COMMIT_MESSAGE = `chore(api-client): generated using version ${version}`;
const COMMIT_AUTHOR = 'api-client-generator <[email protected]>';

export function commitAfter(func: AsyncProcedure, {addPath}: CommitOptions): AsyncProcedure {
return async (...args: any[]) => { // tslint:disable-line no-any
if (whichSync('git', {nothrow: true}) == null) {
throw new Error('Git not installed on system');
}

if (stagedChanges()) {
throw new Error('There are staged changes in your repository');
}

await func(...args);
addFiles(addPath);
commitChanges();
};
}

function stagedChanges(): boolean {
const {status, stdout} = spawnSync('git', ['status', '--porcelain'], {encoding: 'utf8'});
if (status !== 0) {
throw new Error('Not inside a Git repository');
}

return stdout.split('\n').some(line => line.startsWith('A'));
}

function addFiles(addPath: string): void {
const {status, stderr} = spawnSync('git', ['add', addPath]);
if (status !== 0) {
throw new Error(`Error adding "${addPath}":\n${stderr}`);
}
}

function commitChanges(): void {
const commitArgs = ['--allow-empty', '--author', COMMIT_AUTHOR, '-m', COMMIT_MESSAGE];
const {status, stderr} = spawnSync('git', ['commit', ...commitArgs], {encoding: 'utf8'});
if (status !== 0) {
throw new Error(`Error committing:\n${stderr}`);
}
}
18 changes: 15 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import { generateAPIClient } from './generator';
import * as opt from 'optimist';
import { commitAfter } from './git';

const optimist = opt
.usage('Usage: api-client-generator -s path/to/swagger.[json|yaml]')
.alias('h', 'help')
.alias('s', 'source')
.alias('o', 'output')
.alias('C', 'commit')
.alias('v', 'verbose')
.describe('s', 'Path to the swagger file')
.describe('o', 'Path where generated files should be emitted');
.describe('o', 'Path where generated files should be emitted')
.describe('C', 'Autocommit changes')
.describe('v', 'Print error stack traces');

const argv = optimist.argv;

Expand All @@ -25,7 +30,14 @@ if (typeof argv.source === 'undefined' && argv.source !== true) {

const outputDir = argv.output || './output';
const sourceFile = argv.source;
const generate: typeof generateAPIClient = argv.commit ?
commitAfter(generateAPIClient, {addPath: outputDir}) :
generateAPIClient;

generateAPIClient(sourceFile, outputDir)
generate(sourceFile, outputDir)
.then(() => console.info('Angular API client generated successfully'))
.catch((error: Error) => console.error('Error encored during generating', error));
.catch((error: Error) => console.error(
...argv.verbose ?
['Error encountered during generating:', error] :
[error.message]
));
4 changes: 4 additions & 0 deletions src/typings.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
declare module '*package.json' {
export const version: string;
}

declare module 'dir-compare' {
export function compare(path1: string, path2: string, config: { [key: string]: string | boolean }): Promise<Result>;

Expand Down

0 comments on commit a9d5fd0

Please sign in to comment.