Skip to content

Commit a3087e9

Browse files
checkpoint
1 parent 11ba45e commit a3087e9

File tree

7 files changed

+68
-41
lines changed

7 files changed

+68
-41
lines changed

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ If you are submitting a change, please add your name or the name of the organiza
77
## Humans
88

99
* [Brandon Bjelland](https://github.com/brandonjbjelland) \<brandon at atscale.run\>
10+
* [Orrin Fiandaca](https://github.com/orrin) \<ofiandaca at gmail.com\>
1011

1112
## Organizations
1213

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file. See [standa
1515
- Snippets updated to match latest documentation. Created snippets 5385 from 111 providers.
1616
- providers now sourced from the public registry
1717
- upgraded all libaries
18+
- refactored provider fetch workflow to remove the need for the hard-coded number of total snippets
1819

1920
## [0.4.0](https://github.com/run-at-scale/vscode-terraform-doc-snippets/compare/v0.3.0...v0.4.0) (2019-03-27)
2021

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ npm run build:snippets
4242
## Known Issues
4343

4444
- Issue #1 is probably that this is my first node project and I'm not to be trusted with the language. Help and review wanted! Tests needed.
45-
- The async nature of node means we need to provide a hard stop at the number of snippets collected before cleanup can happen. The stop-gap implementation has this count specified by hand in `config.json`. If a run of `build:snippets` produces more than the value of `total_snippets`, adjust the number up to match.
4645

4746
## Contributing
4847

src/config.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"total_snippets": 5385,
32
"override_snippets": [
43
{
54
"tf-conditional": {

src/downloadGitRepo.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class SnippetClass {
2222
}
2323
}
2424

25-
export function downloadGitRepo(
25+
export async function downloadGitRepo(
2626
workspace,
2727
providerRepo,
2828
providerName,
@@ -31,7 +31,7 @@ export function downloadGitRepo(
3131
) {
3232
console.log(`Downloading git repo for ${providerName} at ${providerRepo}`);
3333
const providerDir = `${workspace}/${providerName}`;
34-
git.Clone.clone(providerRepo, providerDir).then(() => {
34+
await git.Clone.clone(providerRepo, providerDir).then(() => {
3535
async.each(Object.keys(tfTypesMap), (tfType) => {
3636
const providerTypeDir = `${providerDir}/website/docs/${tfType}`;
3737
const resourceType = tfTypesMap[tfType];

src/getAvailableProviders.ts

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,40 @@
11
import request = require('request');
2-
import async = require("async");
3-
import { iterateOnDocFiles } from "./iterateOnDocFiles";
2+
43
const urlBase = 'https://registry.terraform.io'
54

6-
// handle errors and do pagination
7-
export function getAvailableProviders(tmpDir, snips, downloadGitRepo) {
5+
export async function getAvailableProviders() {
86
const urlSuffix = '/v1/providers?offset=0'
9-
fetchProviderBatch(urlBase + urlSuffix, snips, tmpDir, downloadGitRepo)
10-
}
7+
let result;
8+
let url;
9+
const list = [];
1110

12-
function fetchProviderBatch(url, snips, tmpDir, downloadGitRepo) {
13-
console.log(url)
14-
request(url, { json: true }, (err, _, body) => {
15-
if (err) {
16-
return console.log(err);
11+
do {
12+
// API paging
13+
if (result.hasOwnProperty('meta')) {
14+
//repeat API call for page 2+
15+
url = urlBase + result.meta.next_url;
16+
result = await request(url, { json: true }, (err, _, body) => {
17+
if (err) {
18+
return console.log(err);
19+
}
20+
return body
21+
});
1722
}
18-
let nextUrl = urlBase + body.meta.next_url
19-
if (nextUrl != url) {
20-
fetchProviderBatch(nextUrl, snips, tmpDir, downloadGitRepo)
23+
else {
24+
//first API call - get page 1
25+
url = urlBase + urlSuffix;
26+
result = await request(url, { json: true }, (err, _, body) => {
27+
if (err) {
28+
return console.log(err);
29+
}
30+
return body
31+
});
2132
}
22-
async.forEach(body.providers, (element) => {
23-
downloadGitRepo(tmpDir, element.source, element.name, snips, iterateOnDocFiles)
33+
console.log(result)
34+
result.providers.forEach((element) => {
35+
list.push(element.source);
2436
});
25-
});
37+
} while (urlBase + result.meta.next_url != url);
38+
39+
return list;
2640
}

src/main.ts

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,49 @@
11
import { writeFileSync } from "fs";
22
import { cleanup } from "./cleanup";
33
import { downloadGitRepo } from "./downloadGitRepo";
4+
import { iterateOnDocFiles } from "./iterateOnDocFiles";
45
import { getAvailableProviders } from "./getAvailableProviders";
56
import { setOverrides } from "./setOverrides";
67
import setup = require("./setup");
78
import { sortObject } from "./sortObject";
9+
import { exit } from "process";
10+
811
const configuration = require("./config.json");
912

1013
async function main() {
11-
const cap = configuration.total_snippets;
1214
const tmpDir = setup.setupWorkspace();
1315
const snips = {};
14-
getAvailableProviders(tmpDir, snips, downloadGitRepo);
15-
// TODO: find a better way to count up front the number of processed markdown files OR providers
16-
await sleep(600);
17-
while (Object.keys(snips).length < cap) {
18-
console.log(`${Object.keys(snips).length} of ${cap}`);
19-
await sleep(20);
20-
}
21-
console.log(Object.keys(snips).length);
22-
const overrideSnipsIncluded = setOverrides(snips);
23-
const sortedSnips = sortObject(overrideSnipsIncluded);
24-
writeFileSync(
25-
"snippets/terraform.json",
26-
JSON.stringify(sortedSnips, null, 2),
27-
"utf-8",
28-
);
29-
cleanup(tmpDir);
30-
}
16+
const providersList = await getAvailableProviders();
17+
console.log(providersList);
18+
exit
19+
20+
// for(let i = 0; i < providersList.length; i++) {
21+
// const name = providersList[i];
22+
// let subtotal = Object.keys(snips).length;
23+
24+
// await downloadGitRepo(tmpDir, name, snips, iterateOnDocFiles);
25+
26+
// subtotal = Object.keys(snips).length - subtotal;
27+
// console.log(`provider ${i + 1} of ${providersList.length}: ${subtotal} snips\n`)
28+
29+
3130

32-
function sleep(ms) {
33-
return new Promise((resolve) => setTimeout(resolve, ms * 100));
31+
// getAvailableProviders(tmpDir, snips, downloadGitRepo);
32+
// // TODO: find a better way to count up front the number of processed markdown files OR providers
33+
// await sleep(600);
34+
// while (Object.keys(snips).length < cap) {
35+
// console.log(`${Object.keys(snips).length} of ${cap}`);
36+
// await sleep(20);
37+
// }
38+
// console.log(Object.keys(snips).length);
39+
// const overrideSnipsIncluded = setOverrides(snips);
40+
// const sortedSnips = sortObject(overrideSnipsIncluded);
41+
// writeFileSync(
42+
// "snippets/terraform.json",
43+
// JSON.stringify(sortedSnips, null, 2),
44+
// "utf-8",
45+
// );
46+
// cleanup(tmpDir);
3447
}
3548

3649
main();

0 commit comments

Comments
 (0)