Skip to content

Commit

Permalink
converted scraping script to use ts
Browse files Browse the repository at this point in the history
  • Loading branch information
nfriend committed Jun 16, 2018
1 parent f24029f commit b361ed2
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 76 deletions.
1 change: 0 additions & 1 deletion Key.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -959,5 +959,4 @@ export enum Key {

/** The numeric keypad's places separator character (in the United States, this is a comma, but elsewhere it is frequently a period). */
Separator = 'Separator',

}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ To build this module yourself, first install its dependencies using
npm install
```

Next, run the scraper script ([`scrapeMDNForKeys.js`](./scrapeMDNForKeys.js)) using
Next, run the scraper script ([`scrapeMDNForKeys.ts`](./scrapeMDNForKeys.ts)) using

```bash
npm run scrape
Expand Down
96 changes: 51 additions & 45 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
{
"name": "ts-key-enum",
"version": "2.0.0",
"description": "A TypeScript string enum for compile-time safety when working with event.key",
"main": "dist/js/Key.enum.js",
"files": [
"dist/"
],
"types": "dist/declarations/Key.enum.d.ts",
"scripts": {
"build": "tsc",
"scrape": "node scrapeMDNForKeys.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nfriend/ts-key-enum.git"
},
"keywords": [
"typescript",
"javascript",
"keycodes",
"key",
"enum",
"ts",
"event.which",
"event.key",
"event.keycode"
],
"author": {
"name": "Nathan Friend",
"email": "hello@nathanfriend.io",
"url": "https://nathanfriend.io"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/nfriend/ts-key-enum/issues"
},
"homepage": "https://github.com/nfriend/ts-key-enum#readme",
"devDependencies": {
"bluebird": "^3.5.1",
"chalk": "^2.4.1",
"cheerio": "^1.0.0-rc.2",
"request": "^2.87.0",
"request-promise": "^4.2.2",
"typescript": "^2.9.1"
}
"name": "ts-key-enum",
"version": "2.0.0",
"description": "A TypeScript string enum for compile-time safety when working with event.key",
"main": "dist/js/Key.enum.js",
"files": [
"dist/"
],
"types": "dist/declarations/Key.enum.d.ts",
"scripts": {
"build": "tsc",
"scrape": "./node_modules/.bin/ts-node scrapeMDNForKeys.ts"
},
"repository": {
"type": "git",
"url": "git+https://github.com/nfriend/ts-key-enum.git"
},
"keywords": [
"typescript",
"javascript",
"keycodes",
"key",
"enum",
"ts",
"event.which",
"event.key",
"event.keycode"
],
"author": {
"name": "Nathan Friend",
"email": "hello@nathanfriend.io",
"url": "https://nathanfriend.io"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/nfriend/ts-key-enum/issues"
},
"homepage": "https://github.com/nfriend/ts-key-enum#readme",
"devDependencies": {
"@types/bluebird": "^3.5.20",
"@types/cheerio": "^0.22.7",
"@types/lodash": "^4.14.109",
"@types/request": "^2.47.1",
"@types/request-promise": "^4.1.41",
"bluebird": "^3.5.1",
"chalk": "^2.4.1",
"cheerio": "^1.0.0-rc.2",
"request": "^2.87.0",
"request-promise": "^4.2.2",
"ts-node": "^6.1.1",
"typescript": "^2.9.1"
}
}
65 changes: 37 additions & 28 deletions scrapeMDNForKeys.js → scrapeMDNForKeys.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
const rp = require('request-promise');
const cheerio = require('cheerio');
const chalk = require('chalk');
const bluebird = require('bluebird');
const writeFile = bluebird.promisify(require('fs').writeFile);
const _ = require('lodash');
import * as rp from 'request-promise';
import * as cheerio from 'cheerio';
import chalk from 'chalk';
import * as bluebird from 'bluebird';
import { writeFile } from 'fs';
import * as _ from 'lodash';

const writeFileAsync = bluebird.promisify(writeFile);

(async () => {
const mdnUrl = `https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values#Speech_recognition_keys`;
const mdnUrl =
'https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values#Speech_recognition_keys';

console.info(chalk.gray('Making GET request to MDN...'));

const pageHtml = await rp(mdnUrl);
const pageHtml: string = await rp(mdnUrl);

console.info(chalk.gray('Successfully received response from MDN.'));

Expand All @@ -19,8 +22,10 @@ const _ = require('lodash');
const $ = cheerio.load(pageHtml);

// the list of of all keys found in the page.
// Each entry contains a "value" and "description" property
let keys = [];
let keys: {
value: string;
description: string;
}[] = [];

// extracts the key name (i.e. LaunchCalculator) i.e.
// from a string like "LaunchCalculator" [5].
Expand Down Expand Up @@ -54,28 +59,32 @@ const _ = require('lodash');

console.info(chalk.gray('Eliminating duplicate keys...'));

// elimination of duplicate key is necessary because
// (at the time of writing) there is one duplicate entry on
// the page - "Clear".
keys = _.uniqBy(keys, k => k.value);

console.info(chalk.gray('Generating .ts file...'));

let enumFile = `/**\n`;
enumFile += ` * An enum that includes all non-printable string values one can expect from $event.key.\n`;
enumFile += ` * For example, this enum includes values like "CapsLock", "Backspace", and "AudioVolumeMute",\n`;
enumFile += ` * but does not include values like "a", "A", "#", "é", or "¿".\n`;
enumFile += ` * Auto generated from MDN: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values#Speech_recognition_keys\n`;
enumFile += ` */\n`;
enumFile += `export enum Key {\n\n`;
enumFile += keys
.map(k => {
// prettier-ignore
return ` /** ${k.description} */\n ${k.value} = '${k.value}',\n\n`;
})
.join('');
enumFile += '}';

console.info(chalk.gray('Writing file to Key.enum.ts...'));

await writeFile('./Key.enum.ts', enumFile);
const enumFile =
`/**\n` +
` * An enum that includes all non-printable string values one can expect from $event.key.\n` +
` * For example, this enum includes values like "CapsLock", "Backspace", and "AudioVolumeMute",\n` +
` * but does not include values like "a", "A", "#", "é", or "¿".\n` +
` * Auto generated from MDN: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values#Speech_recognition_keys\n` +
` */\n` +
`export enum Key {\n\n` +
keys
.map(k => {
// prettier-ignore
return ` /** ${k.description} */\n ${k.value} = '${k.value}',`;
})
.join('\n\n') +
'\n}';

console.info(chalk.gray('Writing result to Key.enum.ts...'));

await writeFileAsync('./Key.enum.ts', enumFile);

console.info(chalk.green('✓ All done! Successfully updated Key.enum.ts.'));
})();
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"declarationDir": "dist/declarations",
"module": "commonjs",
"target": "es5",
"outDir": "dist/js"
"outDir": "dist/js",
"lib": ["es5", "es6"]
},
"files": ["Key.enum.ts"]
}

0 comments on commit b361ed2

Please sign in to comment.