From b9744b57d608b6423a63c5dd3893d956594f922e Mon Sep 17 00:00:00 2001 From: nju33 Date: Sat, 3 Mar 2018 21:01:02 +0900 Subject: [PATCH] fix: better type of the result --- .circleci/config.yml | 19 ++++++++++++ .coveralls.yml | 1 + README.md | 34 ++++++++++++++++++++- package.json | 2 ++ src/check-deadlink.ts | 71 +++++++++++++++++++++++++++++-------------- yarn.lock | 22 ++++++++++++-- 6 files changed, 124 insertions(+), 25 deletions(-) create mode 100644 .circleci/config.yml create mode 100644 .coveralls.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..9a53c65 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,19 @@ +version: 2 +jobs: + build: + docker: + - image: circleci/node:8 + working_directory: ~/repo + steps: + - checkout + - restore_cache: + keys: + - v1-dependencies-{{ checksum "package.json" }} + - v1-dependencies- + - run: yarn install + - save_cache: + paths: + - node_modules + key: v1-dependencies-{{ checksum "package.json" }} + - run: yarn test --coverage + - run: yarn coveralls diff --git a/.coveralls.yml b/.coveralls.yml new file mode 100644 index 0000000..88f9522 --- /dev/null +++ b/.coveralls.yml @@ -0,0 +1 @@ +repo_token: OZoQb1WxqH1jHAKdj3C6IiT6tVMNIKesy diff --git a/README.md b/README.md index e712304..b2fa2fa 100644 --- a/README.md +++ b/README.md @@ -1 +1,33 @@ -wip +# check-deadlink + +[![npm: check-deadlink](https://img.shields.io/npm/v/check-deadlink.svg)](https://www.npmjs.com/package/check-deadlink) +[![CircleCI](https://circleci.com/gh/nju33/check-deadlink.svg?style=svg&circle-token=a28ff5af8b1e0a0e3f4ec38d619681fc4886f63c)](https://circleci.com/gh/nju33/check-deadlink) +[![Coverage Status](https://coveralls.io/repos/github/nju33/check-deadlink/badge.svg?branch=master)](https://coveralls.io/github/nju33/check-deadlink?branch=master) +[![tested with jest](https://img.shields.io/badge/tested_with-jest-99424f.svg)](https://github.com/facebook/jest) +[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) +![license: mit](https://img.shields.io/packagist/l/doctrine/orm.svg) +[![TypeScript](https://badges.frapsoft.com/typescript/code/typescript.svg?v=101)](https://github.com/ellerbrock/typescript-badges/) + +## Install + +```bash +yarn add [-D] check-deadlink +``` + +## Example + +```ts +const checkDeadlink = require('../src/check-deadlink'); + +(async () => { + const result = await checkDeadlink('https://example.com', { + verbose: true + }); + + console.log(result); +})() + .catch(err => { + console.error(err); + }); + +``` diff --git a/package.json b/package.json index cceb99f..5ef967a 100644 --- a/package.json +++ b/package.json @@ -15,12 +15,14 @@ ], "scripts": { "test": "jest", + "coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js", "prepare": "yarn build", "build": "yarn build:cjs", "build:cjs": "tsc -p tsconfig.cjs.json" }, "devDependencies": { "@geekcojp/tslint-config": "^0.1.2", + "coveralls": "^3.0.0", "microbundle": "^0.4.4", "ts-node": "^5.0.0", "tslint": "^5.9.1", diff --git a/src/check-deadlink.ts b/src/check-deadlink.ts index 048c26c..2439a2d 100644 --- a/src/check-deadlink.ts +++ b/src/check-deadlink.ts @@ -8,10 +8,17 @@ import * as ipath from './helpers/ipath'; declare namespace checkDeadlink { export interface Result { - status: number; url: string; parentUrl?: string; - error?: got.GotError + response: got.Response; + readonly status: number; + } + + export interface Error { + url: string; + parentUrl?: string; + error: got.GotError; + readonly status: -1; } export interface Config { @@ -21,7 +28,7 @@ declare namespace checkDeadlink { export interface Data { result: { - [url: string]: Result | {}; + [url: string]: Result | Error; }; baseUrl?: string; } @@ -32,12 +39,14 @@ const defaultConfig = { verbose: false }; - const initialData: checkDeadlink.Data = { result: {} }; -const checked: (url: string, data: checkDeadlink.Data) => boolean = (url, data) => { +const checked: (url: string, data: checkDeadlink.Data) => boolean = ( + url, + data +) => { return Object.keys(data.result).includes(url); }; @@ -50,7 +59,7 @@ const checkDeadlink = async ( ) => { const normalizedUrl = ipath.normalize(url); if (data.baseUrl === undefined) { - data.baseUrl = normalizedUrl + data.baseUrl = normalizedUrl; } if (config.verbose) { @@ -64,15 +73,19 @@ const checkDeadlink = async ( try { const res = await got(url, {timeout: 20000}); data.result[url] = { - status: res.statusCode as number, url, - parentUrl + parentUrl, + response: res, + get status() { + return (this as checkDeadlink.Result).response.statusCode as number; + } }; if (normalizedUrl.startsWith(data.baseUrl)) { const doc = new JSDOM(res.body).window.document; const html = doc.body.innerHTML; - const urls = dom.getLinks(normalizedUrl, html) + const urls = dom + .getLinks(normalizedUrl, html) .filter(thisUrl => !checked(thisUrl, data)); await Promise.all( @@ -89,8 +102,12 @@ const checkDeadlink = async ( return; } + /** + * レスポンスが来る前に再度同じURLで実行されない為に + * とりあえず undefined 以外の値を入れる + */ if (data.result[normalizedThisUrl] === undefined) { - data.result[normalizedThisUrl] = {}; + data.result[normalizedThisUrl] = {} as any; } await checkDeadlink( @@ -107,29 +124,39 @@ const checkDeadlink = async ( const res: got.Response | undefined = err.response; if (res === undefined) { data.result[url] = { - status: -1, url, parentUrl, error: err, + get status() { + return -1 as -1; + } + }; + } else { + data.result[url] = { + url, + parentUrl, + error: err, + get status() { + return -1 as -1; + } }; - - return; } - data.result[url] = { - status: res.statusCode as number, - url, - parentUrl, - error: err - }; - return; } const groupedByParentUrl = groupBy(data.result, 'parentUrl'); Object.keys(groupedByParentUrl).forEach(thisUrl => { - const deadlinks = (groupedByParentUrl[thisUrl] as checkDeadlink.Result[]).filter(result => { - return result.status === -1 || result.status === 404 || result.status === 500; + const deadlinks = (groupedByParentUrl[ + thisUrl + ] as checkDeadlink.Result[]).filter(result => { + return ( + result.status === -1 || + result.status === 403 || + result.status === 404 || + result.status === 500 || + result.status === 503 + ); }); if (deadlinks.length === 0) { diff --git a/yarn.lock b/yarn.lock index 6f8984c..b9aa3a9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -808,6 +808,16 @@ cosmiconfig@^2.1.0, cosmiconfig@^2.1.1: parse-json "^2.2.0" require-from-string "^1.1.0" +coveralls@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/coveralls/-/coveralls-3.0.0.tgz#22ef730330538080d29b8c151dc9146afde88a99" + dependencies: + js-yaml "^3.6.1" + lcov-parse "^0.0.10" + log-driver "^1.2.5" + minimist "^1.2.0" + request "^2.79.0" + cpx@^1.5.0: version "1.5.0" resolved "https://registry.yarnpkg.com/cpx/-/cpx-1.5.0.tgz#185be018511d87270dedccc293171e37655ab88f" @@ -2191,7 +2201,7 @@ js-tokens@^3.0.0, js-tokens@^3.0.2: version "3.0.2" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b" -js-yaml@^3.4.3, js-yaml@^3.7.0: +js-yaml@^3.4.3, js-yaml@^3.6.1, js-yaml@^3.7.0: version "3.10.0" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.10.0.tgz#2e78441646bd4682e963f22b6e92823c309c62dc" dependencies: @@ -2321,6 +2331,10 @@ lcid@^1.0.0: dependencies: invert-kv "^1.0.0" +lcov-parse@^0.0.10: + version "0.0.10" + resolved "https://registry.yarnpkg.com/lcov-parse/-/lcov-parse-0.0.10.tgz#1b0b8ff9ac9c7889250582b70b71315d9da6d9a3" + left-pad@^1.2.0: version "1.2.0" resolved "https://registry.yarnpkg.com/left-pad/-/left-pad-1.2.0.tgz#d30a73c6b8201d8f7d8e7956ba9616087a68e0ee" @@ -2386,6 +2400,10 @@ lodash@^4.13.1, lodash@^4.14.0, lodash@^4.17.4, lodash@^4.17.5: version "4.17.5" resolved "https://registry.yarnpkg.com/lodash/-/lodash-4.17.5.tgz#99a92d65c0272debe8c96b6057bc8fbfa3bed511" +log-driver@^1.2.5: + version "1.2.7" + resolved "https://registry.yarnpkg.com/log-driver/-/log-driver-1.2.7.tgz#63b95021f0702fedfa2c9bb0a24e7797d71871d8" + longest@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097" @@ -3421,7 +3439,7 @@ request@2.81.0: tunnel-agent "^0.6.0" uuid "^3.0.0" -request@^2.83.0: +request@^2.79.0, request@^2.83.0: version "2.83.0" resolved "https://registry.yarnpkg.com/request/-/request-2.83.0.tgz#ca0b65da02ed62935887808e6f510381034e3356" dependencies: