-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.ts
More file actions
109 lines (94 loc) · 2.98 KB
/
Copy pathmod.ts
File metadata and controls
109 lines (94 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import * as cheerio from 'cheerio';
import color from 'picocolors';
import open from '@rdsq/open';
import {
intro,
outro,
log,
confirm,
select,
} from '@clack/prompts';
const rootUrl = 'https://docs.deno.com';
const examplesUrl = `${rootUrl}/examples/`;
interface Example {
title: string;
url: string;
}
// Fetch a bunch of examples from the web
// and extract their titles and urls
const gatherExamples = async () => {
const $ = await cheerio.fromURL(examplesUrl);
return $.extract({
sections: [
{
selector: 'section:has(h2)',
value: {
name:{
selector: 'h2',
value: (el) => {
return $(el).text().replace('Jump to heading#', '').trim();
}
},
links: [{
selector: 'li.learning-list-item>a',
value: (el) => {
const title = $(el).text() || '';
const url = $(el).attr('href') || '';
return { title, url };
}
}]
}
}
]
});
}
// get random items from the examples array
const getRandomExamples = (examples: Example[], n: number): Example[] => {
return examples.sort(() => Math.random() - 0.5).slice(0, n);
}
// rest here a while, my sweet
const sleep = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// Offer a random selection of nuggets to explore
const offerNuggets = async () => {
const data = await gatherExamples();
const options: { value: string; label: string }[] = data.sections.map((el) => ({ value: el.name || '', label: el.name || ''}));
const section = await select({
message: 'There are examples on a range of topics. Pick one to explore:',
options: options
});
// get links from sections object where the label matches the section
// and offer a random selection of 5
const links = data.sections.find((el) => el.name === section)?.links || [];
const nugs : { value: string; label: string }[] = getRandomExamples(links, 5).map(({ url, title }) => ({ value: url, label: title || '' }));
const nugget = await select({
message: 'Here are a few random nuggets from the ' + color.yellow(section as string) + ' section:',
options: nugs
});
log.info(`You can see that here: ${color.cyan(rootUrl + String(nugget))}`);
const shouldVisit = await confirm({
message: 'Shall we take a look?',
});
if(shouldVisit) {
await open(`${rootUrl}${String(nugget)}`);
}
const more = await confirm({
message: 'Want to see some other examples?',
});
if(more) {
offerNuggets();
} else {
outro(color.yellow(`Find lots more examples at ${examplesUrl}`)) ;
}
}
// Show a simple CLI to offer nuggets of useful information from the examples
if (import.meta.main) {
console.log(`
🔥 🌶️ 🌶️ 🌶️ 🌶️ 🔥
`);
intro(color.yellow(color.inverse(' Deno Nuggies ')));
log.info("Let's find you a nugget of info from the Deno examples");
await sleep(1000);
offerNuggets();
}