Skip to content

Commit

Permalink
fix: better type of the result
Browse files Browse the repository at this point in the history
  • Loading branch information
nju33 committed Mar 3, 2018
1 parent 9dedde3 commit b9744b5
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 25 deletions.
19 changes: 19 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .coveralls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repo_token: OZoQb1WxqH1jHAKdj3C6IiT6tVMNIKesy
34 changes: 33 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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);
});

```
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
71 changes: 49 additions & 22 deletions src/check-deadlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
readonly status: number;
}

export interface Error {
url: string;
parentUrl?: string;
error: got.GotError;
readonly status: -1;
}

export interface Config {
Expand All @@ -21,7 +28,7 @@ declare namespace checkDeadlink {

export interface Data {
result: {
[url: string]: Result | {};
[url: string]: Result | Error;
};
baseUrl?: string;
}
Expand All @@ -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);
};

Expand All @@ -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) {
Expand All @@ -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(
Expand 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(
Expand All @@ -107,29 +124,39 @@ const checkDeadlink = async (
const res: got.Response<string> | 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) {
Expand Down
22 changes: 20 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit b9744b5

Please sign in to comment.