+10
-10
@@ -13,3 +13,3 @@ #!/usr/bin/env node | ||
| function printHelp() { | ||
| console.log(` | ||
| console.log(` | ||
| ${description} | ||
@@ -25,9 +25,9 @@ | ||
| if (argv.v || argv.version) { | ||
| console.log(version); | ||
| process.exit(0); | ||
| console.log(version); | ||
| process.exit(0); | ||
| } | ||
| if (argv.h || argv.help || argv._.length === 0) { | ||
| printHelp(); | ||
| process.exit(0); | ||
| printHelp(); | ||
| process.exit(0); | ||
| } | ||
@@ -38,6 +38,6 @@ | ||
| fs.promises.readFile(file) | ||
| .then(data => console.log(oust(data, type).join('\n'))) | ||
| .catch(error => { | ||
| console.error('Error opening file:', error.message); | ||
| process.exit(1); | ||
| }); | ||
| .then(data => console.log(oust(data, type).join('\n'))) | ||
| .catch(error => { | ||
| console.error(`Error opening file: ${error.message}`); | ||
| process.exit(1); | ||
| }); |
+58
-59
@@ -24,72 +24,71 @@ /*! | ||
| const types = { | ||
| stylesheets: { | ||
| selector: 'link[rel*="stylesheet"]', | ||
| attribute: 'href', | ||
| }, | ||
| scripts: { | ||
| selector: 'script', | ||
| attribute: 'src', | ||
| }, | ||
| imports: { | ||
| selector: 'link[rel="import"]', | ||
| attribute: 'href', | ||
| }, | ||
| preload: { | ||
| selector: 'link[rel*="preload"][as="style"]', | ||
| attribute: 'href', | ||
| }, | ||
| links: { | ||
| selector: 'a', | ||
| attribute: 'href', | ||
| }, | ||
| images: { | ||
| selector: 'img', | ||
| attribute: 'src', | ||
| }, | ||
| styles: { | ||
| selector: 'style', | ||
| method: 'text', | ||
| }, | ||
| stylesheets: { | ||
| selector: 'link[rel*="stylesheet"]', | ||
| attribute: 'href', | ||
| }, | ||
| scripts: { | ||
| selector: 'script', | ||
| attribute: 'src', | ||
| }, | ||
| imports: { | ||
| selector: 'link[rel="import"]', | ||
| attribute: 'href', | ||
| }, | ||
| preload: { | ||
| selector: 'link[rel*="preload"][as="style"]', | ||
| attribute: 'href', | ||
| }, | ||
| links: { | ||
| selector: 'a', | ||
| attribute: 'href', | ||
| }, | ||
| images: { | ||
| selector: 'img', | ||
| attribute: 'src', | ||
| }, | ||
| styles: { | ||
| selector: 'style', | ||
| method: 'text', | ||
| }, | ||
| }; | ||
| function oust(src, type, raw) { | ||
| if (typeof src !== 'string' || !type) { | ||
| throw new Error('`src` and `type` required'); | ||
| } | ||
| if (typeof src !== 'string' || !type) { | ||
| throw new Error('`src` and `type` required'); | ||
| } | ||
| const validTypes = Object.keys(types); | ||
| const typeArray = Array.isArray(type) ? type : [type]; | ||
| const validTypes = Object.keys(types); | ||
| const typeArray = Array.isArray(type) ? type : [type]; | ||
| for (const type of typeArray) { | ||
| if (!validTypes.includes(type)) { | ||
| throw new Error(`Invalid \`type\` value "${type}". Choose one of: ${validTypes.join(', ')}`); | ||
| } | ||
| for (const type of typeArray) { | ||
| if (!validTypes.includes(type)) { | ||
| throw new Error(`Invalid \`type\` value "${type}". Choose one of: ${validTypes.join(', ')}`); | ||
| } | ||
| } | ||
| const chosenTypes = typeArray.map(type => ({...types[type], type})); | ||
| const selector = chosenTypes.map(type => type.selector).join(', '); | ||
| const $ = cheerio.load(src); | ||
| const chosenTypes = typeArray.map(type => ({...types[type], type})); | ||
| const selector = chosenTypes.map(type => type.selector).join(', '); | ||
| const $ = cheerio.load(src); | ||
| return Array.prototype.map.call($(selector), element => { | ||
| const $element = $(element); | ||
| return Array.prototype.map.call($(selector), element => { | ||
| const $element = $(element); | ||
| const chosenType = chosenTypes.find(type => $element.is(type.selector)); | ||
| const chosenType = chosenTypes.find(type => $element.is(type.selector)); | ||
| let value = ''; | ||
| if (chosenType.method && $element[chosenType.method]) { | ||
| value = $element[chosenType.method](); | ||
| } else if (chosenType.attribute) { | ||
| value = $element.attr(chosenType.attribute); | ||
| } | ||
| let value = ''; | ||
| if (chosenType.method && $element[chosenType.method]) { | ||
| value = $element[chosenType.method](); | ||
| } else if (chosenType.attribute) { | ||
| value = $element.attr(chosenType.attribute); | ||
| } | ||
| if (raw) { | ||
| return { | ||
| $el: $element, | ||
| type: chosenType.type, | ||
| value, | ||
| }; | ||
| } | ||
| if (raw) { | ||
| return { | ||
| $el: $element, | ||
| type: chosenType.type, | ||
| value, | ||
| }; | ||
| } | ||
| return value; | ||
| }); | ||
| return value; | ||
| }); | ||
| } | ||
@@ -96,0 +95,0 @@ |
+11
-8
| { | ||
| "name": "oust", | ||
| "version": "2.0.0", | ||
| "version": "2.0.1", | ||
| "author": "Addy Osmani <addyosmani@gmail.com> (https://addyosmani.com/)", | ||
@@ -24,4 +24,6 @@ "license": "Apache-2.0", | ||
| "scripts": { | ||
| "mocha": "mocha", | ||
| "test": "npm run xo && npm run mocha", | ||
| "lint": "xo", | ||
| "fix": "xo --fix", | ||
| "uvu": "uvu test --ignore sample", | ||
| "test": "npm run lint && npm run uvu", | ||
| "xo": "xo" | ||
@@ -31,6 +33,6 @@ }, | ||
| "cheerio": "^1.0.0-rc.12", | ||
| "minimist": "^1.2.5" | ||
| "minimist": "^1.2.8" | ||
| }, | ||
| "devDependencies": { | ||
| "mocha": "^7.2.0", | ||
| "uvu": "^0.5.6", | ||
| "xo": "^0.52.3" | ||
@@ -49,8 +51,9 @@ }, | ||
| "xo": { | ||
| "space": 4, | ||
| "space": 2, | ||
| "rules": { | ||
| "unicorn/prefer-module": "off", | ||
| "n/prefer-global/process": "off" | ||
| "capitalized-comments": "off", | ||
| "n/prefer-global/process": "off", | ||
| "unicorn/prefer-module": "off" | ||
| } | ||
| } | ||
| } |
+24
-28
@@ -1,13 +0,11 @@ | ||
| # oust [](https://github.com/addyosmani/oust/actions?workflow=Tests) [](https://david-dm.org/addyosmani/oust) [](https://david-dm.org/addyosmani/oust?type=dev) | ||
| # oust [](https://github.com/addyosmani/oust/actions?workflow=Tests) | ||
| > Extract URLs to stylesheets, scripts, links, images or HTML imports from HTML | ||
| ## Install | ||
| ```sh | ||
| npm install --save-dev oust | ||
| npm install oust -D | ||
| ``` | ||
| ## Usage | ||
@@ -23,3 +21,3 @@ | ||
| #### Extract stylesheets references `<link rel="stylesheet">` | ||
| ### Extract stylesheets references `<link rel="stylesheet">` | ||
@@ -30,11 +28,11 @@ ```js | ||
| #### Extract stylesheets references with media print `<link rel="stylesheet" media="print">` | ||
| ### Extract stylesheets references with media print `<link rel="stylesheet" media="print">` | ||
| ```js | ||
| const hrefs = oust(htmlString, 'stylesheets', (i, $el) => { | ||
| return $el.attr('media') === 'print'; | ||
| return $el.attr('media') === 'print'; | ||
| }); | ||
| ``` | ||
| #### Extract script references `<script src>` | ||
| ### Extract script references `<script src>` | ||
@@ -45,3 +43,3 @@ ```js | ||
| #### Extract HTML imports `<link rel="import">` | ||
| ### Extract HTML imports `<link rel="import">` | ||
@@ -52,3 +50,3 @@ ```js | ||
| #### Extract style preload references `<link rel="preload" as="style">` | ||
| ### Extract style preload references `<link rel="preload" as="style">` | ||
@@ -59,3 +57,3 @@ ```js | ||
| #### Extract URL references `<a href>` | ||
| ### Extract URL references `<a href>` | ||
@@ -66,3 +64,3 @@ ```js | ||
| #### Extract image source references `<img src>` | ||
| ### Extract image source references `<img src>` | ||
@@ -73,3 +71,3 @@ ```js | ||
| #### Extract inline styles `<style>...</style>` | ||
| ### Extract inline styles `<style>...</style>` | ||
@@ -80,3 +78,3 @@ ```js | ||
| #### Extract preload and stylesheet references combined | ||
| ### Extract preload and stylesheet references combined | ||
@@ -87,5 +85,5 @@ ```js | ||
| #### Extract cheerio elements alongside the value | ||
| ### Extract cheerio elements alongside the value | ||
| Usefull for post processing/filtering as you get an array of matched elements | ||
| Useful for post processing/filtering as you get an array of matched elements | ||
| with cheerio convenience syntax (e.g. `$el.attr()`) | ||
@@ -97,12 +95,11 @@ | ||
| -> [ | ||
| {value: '...', $el: '...'}, | ||
| {value: '...', $el: '...'}, | ||
| ... | ||
| {value: '...', $el: '...'}, | ||
| {value: '...', $el: '...'}, | ||
| ... | ||
| ] | ||
| ``` | ||
| ## API | ||
| #### Options | ||
| ### Options | ||
@@ -124,6 +121,6 @@ Attribute | Default | Description | ||
| Usage: | ||
| $ oust <filename> <type> | ||
| $ oust <filename> <type> | ||
| ``` | ||
| #### Extract stylesheets references `<link rel="stylesheet">` | ||
| ### Extract stylesheets references `<link rel="stylesheet">` | ||
@@ -134,3 +131,3 @@ ```sh | ||
| #### Extract script references `<script src>` | ||
| ### Extract script references `<script src>` | ||
@@ -141,3 +138,3 @@ ```sh | ||
| #### Extract HTML imports `<link rel="import">` | ||
| ### Extract HTML imports `<link rel="import">` | ||
@@ -148,3 +145,3 @@ ```sh | ||
| #### Extract URL references `<a href>` | ||
| ### Extract URL references `<a href>` | ||
@@ -155,3 +152,3 @@ ```sh | ||
| #### Extract image source references `<img src>` | ||
| ### Extract image source references `<img src>` | ||
@@ -162,5 +159,4 @@ ```sh | ||
| ## License | ||
| Released under the [Apache 2 license. © Google 2014](LICENSE). |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
18414
-2.22%147
-2.65%Updated