Skip to content

Commit 267b909

Browse files
authored
Add helpers and improve error logging. (#201)
* Dependency treadmill. * Add media type helper. * Add context to rendering error. * Downgrade @oclif/command due to node engine version mismatch. * Revert oclif upgrades; change @types/node to v10. * Don't bundle test files. * Ignore package bundles. * Add request payload helper. * Add resolutions and `npm audit fix`. * Allow missing request in payload checker. * sort-package-json * Parallelize rendering and improve error logging. * Update copyrights.
1 parent ded646a commit 267b909

File tree

9 files changed

+689
-12891
lines changed

9 files changed

+689
-12891
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ coverage
88
.env
99
reports
1010
.idea
11+
*.tgz

package-lock.json

Lines changed: 518 additions & 12788 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@
4949
"git add"
5050
]
5151
},
52+
"resolutions": {
53+
"**/@oclif/command": "<=1.8.3",
54+
"**/yargs": "<17"
55+
},
5256
"dependencies": {
5357
"@oclif/command": "1.8.0",
5458
"@oclif/config": "1.17.1",
@@ -62,7 +66,7 @@
6266
"lodash": "4.17.21",
6367
"loglevel": "1.8.0",
6468
"make-fetch-happen": "8.0.14",
65-
"snyk": "1.685.0",
69+
"snyk": "1.787.0",
6670
"tmp": "0.2.1",
6771
"ts-node": "8.10.2",
6872
"unzipper": "0.10.11"
@@ -78,9 +82,9 @@
7882
"@types/js-yaml": "^3.12.7",
7983
"@types/jszip": "^3.4.1",
8084
"@types/mocha": "^8.2.3",
81-
"@types/node": "^14.14.6",
85+
"@types/node": "^10.17.60",
8286
"@types/sinon": "^9.0.11",
83-
"@types/tmp": "^0.2.1",
87+
"@types/tmp": "^0.2.2",
8488
"@typescript-eslint/eslint-plugin": "^2.34.0",
8589
"@typescript-eslint/parser": "^2.34.0",
8690
"chai": "^4.3.4",
@@ -89,16 +93,16 @@
8993
"depcheck": "^0.9.2",
9094
"eslint": "^6.8.0",
9195
"eslint-config-prettier": "^6.15.0",
92-
"eslint-plugin-header": "^3.0.0",
93-
"eslint-plugin-prettier": "^3.4.0",
94-
"jszip": "^3.6.0",
96+
"eslint-plugin-header": "^3.1.1",
97+
"eslint-plugin-prettier": "^3.4.1",
98+
"jszip": "^3.7.1",
9599
"lint-staged": "^8.2.1",
96100
"mocha": "^8.4.0",
97101
"nock": "^12.0.3",
98102
"nyc": "^15.1.0",
99-
"prettier": "^2.3.2",
103+
"prettier": "^2.5.1",
100104
"sinon": "^9.2.4",
101-
"sort-package-json": "^1.50.0",
105+
"sort-package-json": "^1.53.1",
102106
"tslib": "^1.14.1",
103107
"typescript": "^3.9.7"
104108
},

resources/.npmignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/*.test.ts

src/generate/apiMetadata.test.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, salesforce.com, inc.
2+
* Copyright (c) 2021, salesforce.com, inc.
33
* All rights reserved.
44
* SPDX-License-Identifier: BSD-3-Clause
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
@@ -193,35 +193,35 @@ describe("ApiMetaData render tests", () => {
193193
});
194194

195195
it("does nothing without templates for the entire tree", async () => {
196-
const ensureDirSyncSpy = sandbox.spy(fs, "ensureDirSync");
197-
const writeFileSyncSpy = sandbox.spy(fs, "writeFileSync");
196+
const ensureDirSpy = sandbox.spy(fs, "ensureDir");
197+
const writeFileSpy = sandbox.spy(fs, "writeFile");
198198
await apiMetadata.render();
199199

200-
expect(ensureDirSyncSpy.notCalled).to.be.true;
201-
expect(writeFileSyncSpy.notCalled).to.be.true;
200+
expect(ensureDirSpy.notCalled).to.be.true;
201+
expect(writeFileSpy.notCalled).to.be.true;
202202
});
203203

204204
it("renders root template only", async () => {
205-
const ensureDirSyncSpy = sandbox.spy(fs, "ensureDirSync");
206-
const writeFileSyncSpy = sandbox.spy(fs, "writeFileSync");
205+
const ensureDirSpy = sandbox.spy(fs, "ensureDir");
206+
const writeFileSpy = sandbox.spy(fs, "writeFile");
207207
apiMetadata.addTemplate(
208208
handlebarTemplate,
209209
path.join(tmpDir.name, "apis.txt")
210210
);
211211

212212
await apiMetadata.render();
213213

214-
expect(ensureDirSyncSpy.calledOnce).to.be.true;
215-
expect(writeFileSyncSpy.calledOnce).to.be.true;
214+
expect(ensureDirSpy.calledOnce).to.be.true;
215+
expect(writeFileSpy.calledOnce).to.be.true;
216216

217217
expect(path.join(tmpDir.name, "apis.txt"))
218218
.to.be.a.file()
219219
.with.content("apis");
220220
});
221221

222222
it("renders child templates only", async () => {
223-
const ensureDirSyncSpy = sandbox.spy(fs, "ensureDirSync");
224-
const writeFileSyncSpy = sandbox.spy(fs, "writeFileSync");
223+
const ensureDirSpy = sandbox.spy(fs, "ensureDir");
224+
const writeFileSpy = sandbox.spy(fs, "writeFile");
225225

226226
apiMetadata.children[0].addTemplate(
227227
handlebarTemplate,
@@ -235,8 +235,8 @@ describe("ApiMetaData render tests", () => {
235235

236236
await apiMetadata.render();
237237

238-
expect(ensureDirSyncSpy.calledTwice).to.be.true;
239-
expect(writeFileSyncSpy.calledTwice).to.be.true;
238+
expect(ensureDirSpy.calledTwice).to.be.true;
239+
expect(writeFileSpy.calledTwice).to.be.true;
240240

241241
expect(path.join(tmpDir.name, "child1.txt"))
242242
.to.be.a.file()
@@ -248,8 +248,8 @@ describe("ApiMetaData render tests", () => {
248248
});
249249

250250
it("renders all templates", async () => {
251-
const ensureDirSyncSpy = sandbox.spy(fs, "ensureDirSync");
252-
const writeFileSyncSpy = sandbox.spy(fs, "writeFileSync");
251+
const ensureDirSpy = sandbox.spy(fs, "ensureDir");
252+
const writeFileSpy = sandbox.spy(fs, "writeFile");
253253

254254
apiMetadata.addTemplate(
255255
handlebarTemplate,
@@ -268,8 +268,8 @@ describe("ApiMetaData render tests", () => {
268268

269269
await apiMetadata.render();
270270

271-
expect(ensureDirSyncSpy.calledThrice).to.be.true;
272-
expect(writeFileSyncSpy.calledThrice).to.be.true;
271+
expect(ensureDirSpy.calledThrice).to.be.true;
272+
expect(writeFileSpy.calledThrice).to.be.true;
273273

274274
expect(path.join(tmpDir.name, "apis.txt"))
275275
.to.be.a.file()

src/generate/apiMetadata.ts

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, salesforce.com, inc.
2+
* Copyright (c) 2021, salesforce.com, inc.
33
* All rights reserved.
44
* SPDX-License-Identifier: BSD-3-Clause
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
@@ -75,10 +75,25 @@ export class ApiMetadata {
7575
*/
7676
public async renderThis(): Promise<void> {
7777
ramlToolLogger.info(`Rendering templates for ${this.name.original}`);
78-
this.templates.forEach((template) => {
79-
fs.ensureDirSync(path.dirname(template.outputFile));
80-
fs.writeFileSync(template.outputFile, template.handlebarTemplate(this));
78+
// Render templates in parallel
79+
const promises = this.templates.map(async (template) => {
80+
try {
81+
await fs.ensureDir(path.dirname(template.outputFile));
82+
await fs.writeFile(
83+
template.outputFile,
84+
template.handlebarTemplate(this, {
85+
allowProtoPropertiesByDefault: true,
86+
allowProtoMethodsByDefault: true,
87+
})
88+
);
89+
} catch (err) {
90+
ramlToolLogger.error(
91+
`Error while rendering ${template.outputFile}:`,
92+
err
93+
);
94+
}
8195
});
96+
await Promise.all(promises);
8297
}
8398

8499
/**
@@ -90,11 +105,12 @@ export class ApiMetadata {
90105
try {
91106
await this.renderThis();
92107
} catch (err) {
93-
ramlToolLogger.error(err);
108+
ramlToolLogger.error(`Failed to render ${this.name}:`, err);
94109
}
95-
return this.children.forEach(async (child) => {
96-
await child.render();
97-
});
110+
111+
// Render children in parallel
112+
const promises = this.children.map(async (child) => await child.render());
113+
await Promise.all(promises);
98114
}
99115

100116
/**

src/generate/apiModel.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, salesforce.com, inc.
2+
* Copyright (c) 2021, salesforce.com, inc.
33
* All rights reserved.
44
* SPDX-License-Identifier: BSD-3-Clause
55
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
@@ -84,6 +84,7 @@ export class ApiModel extends ApiMetadata {
8484
* @memberof ApiModel
8585
*/
8686
public async loadModel(updateName = true): Promise<void> {
87+
ramlToolLogger.info(`Loading model for ${this.name.original}`);
8788
const tmpModel = await parseRamlFile(path.join(this.path, this.main));
8889

8990
// The model needs to be cloned because resolveApiModel messes with the dataTypes.
@@ -120,15 +121,24 @@ export class ApiModel extends ApiMetadata {
120121
}
121122

122123
ramlToolLogger.info(`Rendering templates for ${this.name.original}`);
123-
this.templates.forEach((template) => {
124-
fs.ensureDirSync(path.dirname(template.outputFile));
125-
fs.writeFileSync(
126-
template.outputFile,
127-
template.handlebarTemplate(this, {
128-
allowProtoPropertiesByDefault: true,
129-
allowProtoMethodsByDefault: true,
130-
})
131-
);
124+
// Render templates in parallel
125+
const promises = this.templates.map(async (template) => {
126+
try {
127+
await fs.ensureDir(path.dirname(template.outputFile));
128+
await fs.writeFile(
129+
template.outputFile,
130+
template.handlebarTemplate(this, {
131+
allowProtoPropertiesByDefault: true,
132+
allowProtoMethodsByDefault: true,
133+
})
134+
);
135+
} catch (err) {
136+
ramlToolLogger.error(
137+
`Error while rendering ${template.outputFile}:`,
138+
err
139+
);
140+
}
132141
});
142+
await Promise.all(promises);
133143
}
134144
}

0 commit comments

Comments
 (0)