Skip to content

Commit

Permalink
build: format code before written (#2079)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed May 5, 2020
1 parent 76ee266 commit be438bd
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 31 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Expand Up @@ -4,3 +4,4 @@ test/fixtures
build/
docs/
protos/
src/apis
1 change: 1 addition & 0 deletions .prettierignore
Expand Up @@ -4,3 +4,4 @@ test/fixtures
build/
docs/
protos/
src/apis
2 changes: 2 additions & 0 deletions package.json
Expand Up @@ -58,6 +58,7 @@
"@types/ncp": "^2.0.1",
"@types/node": "^12.0.1",
"@types/nunjucks": "^3.1.1",
"@types/prettier": "^2.0.0",
"@types/qs": "^6.5.3",
"@types/rimraf": "^3.0.0",
"@types/sinon": "^9.0.0",
Expand All @@ -79,6 +80,7 @@
"nunjucks": "^3.2.1",
"open": "^7.0.0",
"p-queue": "^6.0.0",
"prettier": "^2.0.5",
"rimraf": "^3.0.0",
"server-destroy": "^1.0.1",
"sinon": "^9.0.2",
Expand Down
59 changes: 29 additions & 30 deletions src/generator/generator.ts
Expand Up @@ -29,6 +29,7 @@ import * as path from 'path';
import {URL} from 'url';
import * as util from 'util';
import Q from 'p-queue';
import * as prettier from 'prettier';
import {downloadDiscoveryDocs} from './download';

const writeFile = util.promisify(fs.writeFile);
Expand Down Expand Up @@ -274,6 +275,7 @@ export class Generator {
`GenerateAPI call failed with error: ${e}, moving on.`
);
console.error(`Failed to generate API: ${api.id}`);
console.error(e);
console.log(
api.id +
'\n-----------\n' +
Expand Down Expand Up @@ -316,41 +318,26 @@ export class Generator {
const desc = metadata.filter(x => x.name === file)[0].description;
// generate the index.ts
const apiIdxPath = path.join(apisPath, file, 'index.ts');
const result = this.env.render('api-index.njk', {
name: file,
api: apis[file],
});
await writeFile(apiIdxPath, result);
const apiIndexData = {name: file, api: apis[file]};
await this.render('api-index.njk', apiIndexData, apiIdxPath);
// generate the package.json
const pkgPath = path.join(apisPath, file, 'package.json');
const pkgResult = this.env.render('package.json.njk', {
name: file,
desc,
});
await writeFile(pkgPath, pkgResult);
const packageData = {name: file, desc};
await this.render('package.json.njk', packageData, pkgPath);
// generate the README.md
const rdPath = path.join(apisPath, file, 'README.md');
const rdResult = this.env.render('README.md.njk', {name: file, desc});
await writeFile(rdPath, rdResult);
await this.render('README.md.njk', {name: file, desc}, rdPath);
// generate the tsconfig.json
const tsPath = path.join(apisPath, file, 'tsconfig.json');
const tsResult = this.env.render('tsconfig.json.njk');
await writeFile(tsPath, tsResult);
await this.render('tsconfig.json.njk', {}, tsPath);
// generate the webpack.config.js
const wpPath = path.join(apisPath, file, 'webpack.config.js');
const wpResult = this.env.render('webpack.config.js.njk', {
name: file,
});
await writeFile(wpPath, wpResult);
await this.render('webpack.config.js.njk', {name: file}, wpPath);
}
}
}

const result = this.env.render('index.njk', {apis});
await writeFile(indexPath, result, {encoding: 'utf8'});

const res2 = this.env.render('root-index.njk', {apis});
await writeFile(rootIndexPath, res2, {encoding: 'utf8'});
await this.render('index.njk', {apis}, indexPath);
await this.render('root-index.njk', {apis}, rootIndexPath);
}

/**
Expand Down Expand Up @@ -460,17 +447,29 @@ export class Generator {
schema.name,
schema.version + '.ts'
);
this.logResult(apiDiscoveryUrl, 'Generating templates...');
this.logResult(apiDiscoveryUrl, 'Step 1...');
this.logResult(apiDiscoveryUrl, 'Downloading snippets...');
await Promise.all(tasks.map(t => t()));
this.logResult(apiDiscoveryUrl, 'Step 2...');
const contents = this.env.render(API_TEMPLATE, {api: schema});
this.logResult(apiDiscoveryUrl, 'Generating APIs...');
await mkdirp(path.dirname(exportFilename));
this.logResult(apiDiscoveryUrl, 'Step 3...');
await writeFile(exportFilename, contents, {encoding: 'utf8'});
await this.render(API_TEMPLATE, {api: schema}, exportFilename);
this.logResult(apiDiscoveryUrl, 'Template generation complete.');
return exportFilename;
}

/**
* Render a nunjucks template, format it, and write to disk
*/
private async render(templatePath: string, data: {}, outputPath: string) {
let output = this.env.render(templatePath, data);
output = prettier.format(output, {
bracketSpacing: false,
singleQuote: true,
trailingComma: 'es5',
arrowParens: 'avoid',
parser: 'typescript',
});
await writeFile(outputPath, output, {encoding: 'utf8'});
}
}

/**
Expand Down
7 changes: 6 additions & 1 deletion synth.py
Expand Up @@ -28,4 +28,9 @@

common_templates = gcp.CommonTemplates()
templates = common_templates.node_library()
s.copy(templates, excludes=[".github/CONTRIBUTING.md", "README.md"])
s.copy(templates, excludes=[
".github/CONTRIBUTING.md",
"README.md",
".eslintignore",
".prettierignore"
])
5 changes: 5 additions & 0 deletions test/test.clients.ts
Expand Up @@ -13,6 +13,7 @@

import * as assert from 'assert';
import {describe, it, before, beforeEach, after} from 'mocha';
import {execSync} from 'child_process';
import {APIEndpoint} from 'googleapis-common';
import * as nock from 'nock';
import {GoogleApis} from '../src';
Expand Down Expand Up @@ -218,6 +219,10 @@ describe('Clients', () => {
);
});

it('should pass eslint for a given client', () => {
execSync('npx eslint --no-ignore src/apis/youtube/*.ts');
});

after(() => {
nock.cleanAll();
nock.enableNetConnect();
Expand Down

0 comments on commit be438bd

Please sign in to comment.