Skip to content

Commit

Permalink
Add support for specifying output directory
Browse files Browse the repository at this point in the history
  • Loading branch information
jakhog committed Mar 27, 2021
1 parent 27c624e commit 2960989
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
1 change: 1 addition & 0 deletions Source/JavaScript/GenerateOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { GenerationTarget } from './GenerationTarget';

export type GenerateOptions = {
readonly target: GenerationTarget;
readonly output: string;
readonly paths: readonly string[];
readonly includes: readonly string[];
readonly rewrites: readonly {readonly from: string, readonly to: string, readonly package: boolean}[];
Expand Down
29 changes: 16 additions & 13 deletions Source/JavaScript/Generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

import os from 'os';
import { dirname as pathDirname, join as pathJoin, relative as pathRelative } from 'path';
import process from 'process';
import { dirname as pathDirname, join as pathJoin, relative as pathRelative, normalize as pathNormalize, resolve as pathResolve } from 'path';
import { mkdir, mkdtemp, readdir, readFile, rmdir, stat, writeFile } from 'fs/promises';

import { protoc, protocTS } from './Compilers';
import { GenerateOptions } from './GenerateOptions';
import { GenerationTarget } from './GenerationTarget';

export class Generator {
constructor(
readonly outputDirectory: string
) {}

async generate(options: GenerateOptions): Promise<void> {
try {
console.log('Generating code for', options.target);
console.log('With includes', options.includes.join(' '));
console.log('With rewrites', options.rewrites.map(_ => `${_.from}:${_.to}`).join(' '));
console.log('To directory', this.outputDirectory);
console.log('To directory', options.output);

await this.ensureCleanOutputDirectory();
await this.ensureCleanOutputDirectory(options);
const protoFiles = await this.findAllProtoFilesIn(...options.paths);

const tmpDir = await this.createTemporaryBuildDirectory();
Expand Down Expand Up @@ -67,7 +64,7 @@ export class Generator {

if (!shouldInclude) continue;

const movedFilePath = pathJoin(this.outputDirectory, rewrittenPath);
const movedFilePath = pathJoin(options.output, rewrittenPath);
const movedFileDirectory = pathDirname(movedFilePath);
await mkdir(movedFileDirectory, { recursive: true });
await writeFile(movedFilePath, rewrittenContents);
Expand Down Expand Up @@ -111,19 +108,25 @@ export class Generator {
return [contents, true];
}

private async ensureCleanOutputDirectory(): Promise<void> {
private async ensureCleanOutputDirectory(options: GenerateOptions): Promise<void> {
const currentDirectory = pathResolve(process.cwd());
const outputDirectory = pathResolve(options.output);
if (currentDirectory.startsWith(outputDirectory)) {
console.log('Output directory includes current directory, not cleaning');
return;
}
try {
const info = await stat(this.outputDirectory);
const info = await stat(outputDirectory);
if (info.isDirectory()) {
await rmdir(this.outputDirectory, { recursive: true });
await rmdir(outputDirectory, { recursive: true });
} else {
throw new Error(`Output directory '${this.outputDirectory}' is not a directory`);
throw new Error(`Output directory '${options.output}' is not a directory`);
}
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}

await mkdir(this.outputDirectory, { recursive: true });
await mkdir(outputDirectory, { recursive: true });
}

private async createTemporaryBuildDirectory(): Promise<string> {
Expand Down
2 changes: 2 additions & 0 deletions Source/JavaScript/generateAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { GenerationTarget } from './GenerationTarget';
type GenerateActionCallback = (options: GenerateOptions) => Promise<void> | void;

type Options = {
O: string,
I: string[],
R: string[],
skipEmptyFiles?: boolean,
Expand All @@ -16,6 +17,7 @@ export const generateAction = (target: GenerationTarget, action: GenerateActionC
return (paths: string[], options: Options) => {
action({
target,
output: options.O,
paths,
includes: options.I,
rewrites: options.R.map(_ => {
Expand Down
5 changes: 4 additions & 1 deletion Source/JavaScript/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ import { generateAction } from './generateAction';
import { GenerationTarget } from './GenerationTarget';
import { Generator } from './Generator';

const generator = new Generator('./build');
const generator = new Generator();

const program = new Command('dolittle_proto_build');

const output = new Option('-O <path>', 'Output path').default('./build');
const includes = new Option('-I <path>', 'Include path (multiple allowed)');
const rewrite = new Option('-R <rewrite>', 'Rewrite file paths (multiple allowed)');
const skipEmptyFiles = new Option('--skip-empty-files', 'Remove files generated without any content');

program
.command('grpc-node <paths...>')
.addOption(output)
.addOption(repeated(includes))
.addOption(repeated(rewrite))
.addOption(skipEmptyFiles)
Expand All @@ -27,6 +29,7 @@ program

program
.command('grpc-web <paths...>')
.addOption(output)
.addOption(repeated(includes))
.addOption(repeated(rewrite))
.addOption(skipEmptyFiles)
Expand Down

0 comments on commit 2960989

Please sign in to comment.