Skip to content

Commit 319110a

Browse files
committed
Update navigation to Guides page
1 parent 6317a7a commit 319110a

File tree

3 files changed

+16
-27
lines changed

3 files changed

+16
-27
lines changed

package.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
"dependencies": {
1616
"arg": "^4.1.0",
1717
"chalk": "^2.4.2",
18-
"cheerio": "^1.0.0-rc.3",
1918
"esm": "^3.2.25",
20-
"get-hrefs": "^4.0.0",
2119
"hummus": "^1.0.116",
2220
"inquirer": "^6.4.1",
2321
"listr": "^0.14.3",

src/cli.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ function parseArgumentsIntoOptions(rawArgs) {
1010
'--fileName': String,
1111
'-f': '--fileName',
1212
'--language': String,
13-
'-l': '--language',
14-
'--subProduct': String,
15-
'-u': '--subProduct'
13+
'-l': '--language'
1614
},
1715
{
1816
argv: rawArgs.slice(2)
1917
});
2018

2119
return {
2220
product: args._[0],
23-
subProduct: args['--subProduct'] || '',
2421
pathToSave: args['--save'] ,
2522
fileName: args['--fileName'],
2623
language: args['--language']
@@ -46,14 +43,6 @@ async function promptForMissingOptions(options){
4643
});
4744
}
4845

49-
if(!options.subProduct){
50-
questions.push({
51-
name: 'subProduct',
52-
message: 'Enter name of sub-product(optional)',
53-
default: ''
54-
})
55-
}
56-
5746
if(!options.pathToSave){
5847
questions.push({
5948
name: 'pathToSave',
@@ -84,7 +73,6 @@ async function promptForMissingOptions(options){
8473
return{
8574
...options,
8675
product: product,
87-
subProduct: options.subProduct || answers.subProduct,
8876
fileName: options.fileName || `${product}.pdf`,
8977
pathToSave: options.pathToSave || answers.pathToSave,
9078
language: options.language || answers.language

src/main.js

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import puppeteer from 'puppeteer';
22
import fs from 'fs';
3-
import cheerio from 'cheerio';
43
import _ from 'underscore';
5-
import getHrefs from 'get-hrefs';
64
import path from 'path';
75
import { fileURLToPath } from 'url';
86
//import PDFMerge from 'pdf-merge';
@@ -17,17 +15,18 @@ import {exec} from 'child_process';
1715

1816
const readFile = promisify(fs.readFile);
1917
const writeFile = promisify(fs.writeFile);
18+
const dirname = path.dirname(fileURLToPath(import.meta.url));
19+
const puppeteer_options = {headless: "new", env: {LANGUAGE: "en-US"}}
2020

2121
async function saveNavLinks(options) {
22-
const url = `https://cloud.google.com/${options.product}/docs/${options.subProduct}`;
22+
const url = `https://cloud.google.com/${options.product}/docs`;
2323
let browser;
2424
let product = {
2525
'name': options.product
2626
};
27-
const dirname = path.dirname(fileURLToPath(import.meta.url));
2827
const pathToOutputJson = path.join(dirname, '/../output/products.json');
2928

30-
browser = await puppeteer.launch();
29+
browser = await puppeteer.launch(puppeteer_options);
3130
const page = await browser.newPage();
3231
const response = await page.goto(url);
3332
if (!response.ok()) {
@@ -37,12 +36,16 @@ async function saveNavLinks(options) {
3736
const buffer = await response.buffer();
3837
const html = buffer.toString('utf8');
3938

40-
let $ = cheerio.load(html);
41-
let links = _.uniq(_.reject(getHrefs($('ul.devsite-nav-expandable').html()), (o) => {
42-
return o.indexOf('cloud.google.com') < 0 ||
43-
o.indexOf('ref') > 0;
44-
}));
39+
const element = await page.$('[track-metadata-position="nav - guides"]');
40+
const href = await page.evaluate(el => el.getAttribute('href'), element);
41+
await page.goto(href);
42+
console.log(`${chalk.gray.bold('DEBUG')} Navigated to ${href}`);
43+
44+
const all_hrefs = await page.$$eval('.devsite-mobile-nav-bottom a.devsite-nav-title', links => links.map(link => link.href));
45+
const links = Array.from(new Set(all_hrefs.filter(href => href.includes(url))));
46+
4547
console.log(`${chalk.yellow.bold('INFO')} Found ${links.length} links`);
48+
console.log(links)
4649

4750
product.links = links;
4851
product.count = links.length;
@@ -66,7 +69,7 @@ async function saveNavLinks(options) {
6669

6770
async function downloadAndMergePdf(options) {
6871
let browser;
69-
const pathToOutputJson = path.join(__dirname, '/../output/products.json');
72+
const pathToOutputJson = path.join(dirname, '/../output/products.json');
7073
let data = await readFile(pathToOutputJson, 'utf8');
7174
let jsonData = JSON.parse(data);
7275
let existing = _.find(jsonData.products, (a) => {
@@ -81,7 +84,7 @@ async function downloadAndMergePdf(options) {
8184
fs.mkdirSync(`${options.pathToSave}/${options.product}`);
8285
}
8386

84-
browser = await puppeteer.launch();
87+
browser = await puppeteer.launch(puppeteer_options);
8588
let files = [],
8689
indx = 0;
8790

0 commit comments

Comments
 (0)