Skip to content

Commit

Permalink
Merge pull request #3 from stevent-team/feat/installable
Browse files Browse the repository at this point in the history
Make library installable
  • Loading branch information
GRA0007 authored Oct 14, 2022
2 parents 1625820 + 5d455e8 commit c0f956d
Show file tree
Hide file tree
Showing 15 changed files with 1,647 additions and 89 deletions.
8 changes: 8 additions & 0 deletions .changeset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Changesets

Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
with multi-package repos, or single-package repos to help you version and publish your code. You can
find the full documentation for it [in our repository](https://github.com/changesets/changesets)

We have a quick list of common questions to get you started engaging with this project in
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
11 changes: 11 additions & 0 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
"changelog": "@changesets/cli/changelog",
"commit": false,
"fixed": [],
"linked": [],
"access": "public",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": []
}
5 changes: 5 additions & 0 deletions .changeset/mean-houses-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stevent-team/favicon-scout": minor
---

Add changesets for managing NPM releases
5 changes: 5 additions & 0 deletions .changeset/spicy-carrots-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@stevent-team/favicon-scout": major
---

Favicon Scout released as a stable command line application! See the README for instructions on how to start the server and get those favicons.
2 changes: 0 additions & 2 deletions .env.example

This file was deleted.

55 changes: 0 additions & 55 deletions .eslintrc.js

This file was deleted.

55 changes: 55 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"env": {
"node": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"@typescript-eslint/ban-ts-comment": "off",
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
],
"eqeqeq": 2,
"no-return-await": 1,
"no-var": 2,
"prefer-const": 1,
"yoda": 2,
"no-trailing-spaces": 1,
"eol-last": [1, "always"],
"no-unused-vars": [
1,
{
"args": "all",
"argsIgnorePattern": "^_",
"ignoreRestSiblings": true
}
],
"arrow-parens": [
"error",
"as-needed"
]
}
}
30 changes: 30 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Release

on:
push:
branches: ['main']
paths:
- '**/package.json'
- '.changeset/**'
- '.github/workflows/release.yml'

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-node@v3
with:
node-version: 17
cache: 'yarn'
cache-dependency-path: '**/yarn.lock'
- run: yarn install --immutable
- uses: changesets/action@v1
with:
publish: yarn ci:release
commit: 'ci: release'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_AUTH_TOKEN }}
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/node_modules
/dist
.env
4 changes: 4 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
.vscode
/node_modules
/test
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
# 🧭 Favicon Scout

[![npm version](https://img.shields.io/npm/v/@stevent-team/favicon-scout)](https://www.npmjs.com/package/@stevent-team/favicon-scout)
[![minzip size](https://img.shields.io/bundlephobia/minzip/@stevent-team/favicon-scout)](https://bundlephobia.com/package/@stevent-team/favicon-scout)

A web service to fetch the favicon of any website. Includes fallback options.

## Usage

Install globally

```bash
yarn global add @stevent-team/favicon-scout
```

```bash
npm i -g @stevent-team/favicon-scout
```

Then you can invoke the command line utility to start the server

```bash
favicon-scout # start with default options

favicon-scout --port 8080 # run on port 8080

favicon-scout --host 12.34.56.78 # specify a host

favicon-scout --origins https://example.com # only allow requests from example.com

favicon-scout -p 1234 -h 0.0.0.0 -o https://example1.com /\.example2\.com$/ # all options

favicon-scout --help # show help information
```

### Web server

```h
/{site url}/{size}
```
Expand All @@ -14,10 +45,20 @@ A web service to fetch the favicon of any website. Includes fallback options.
### CORS
By default, any origin is allowed to request from this API. To lock it down, set `ALLOWED_ORIGINS` in a `.env` file to a comma separated list of origins. If an origin starts and ends with `/` it will be treated as a regexp. For example `ALLOWED_ORIGINS="http://example1.com, /\.example2\.com$/"` will accept any request from "http://example1.com" or from a subdomain of "example2.com". See the [fastify-cors options](https://github.com/fastify/fastify-cors#options) for more details.
By default, any origin is allowed to request from this API. To lock it down, use the `--origins` command line options to specify a list of origins. If an origin starts and ends with `/` it will be treated as a regexp. For example `favicon-scout -o http://example1.com /\.example2\.com$/` will accept any request from "http://example1.com" or from a subdomain of "example2.com". See the [fastify-cors options](https://github.com/fastify/fastify-cors#options) for more details.
## Development
Run `yarn` to install dependencies. Run `yarn dev` to watch for changes and rerun.
Run `yarn build` to build to `/dist/index.js`.
## Contributing
If you have any feedback or find a website that favicon scout can't correctly find the favicon for, [create an issue](https://github.com/stevent-team/favicon-scout/issues/new/choose). Contributions are welcome.
This library uses [changesets](https://github.com/changesets/changesets). If the changes you've made would constitute a version bump, run `yarn changeset` and follow the prompts to document the changes you've made. Changesets are consumed on releases, and used to generate a changelog and bump version number.
## License
Created by Stevent (2022) and licensed under MIT
22 changes: 17 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,38 @@
"name": "@stevent-team/favicon-scout",
"version": "0.0.1",
"description": "A web service to fetch the favicon of any website",
"main": "dist/index.js",
"keywords": [
"favicon",
"scraper",
"icon"
],
"repository": "https://github.com/stevent-team/favicon-scout",
"author": "Stevent",
"license": "MIT",
"private": true,
"main": "dist/index.js",
"type": "module",
"bin": {
"favicon-scout": "./dist/index.js"
},
"scripts": {
"start": "node dist/index.js",
"build": "esbuild src/index.ts --platform=node --target=node16 --format=cjs --bundle --external:./node_modules/* --minify --outfile=dist/index.js",
"build": "esbuild src/index.ts --platform=node --target=node16 --format=esm --bundle --external:./node_modules/* --minify --outfile=dist/index.js",
"dev": "nodemon --exec \"yarn build && yarn start\" --watch src --ext ts,js",
"lint": "eslint --ext .ts src",
"test": "vitest"
"test": "vitest",
"ci:release": "yarn build && yarn changeset publish"
},
"dependencies": {
"@fastify/cors": "^8.1.0",
"dotenv": "^16.0.3",
"commander": "^9.4.1",
"fastify": "^4.8.1",
"node-fetch": "2.x.x",
"node-html-parser": "^6.1.1",
"sharp": "^0.31.1",
"sharp-ico": "^0.1.5"
},
"devDependencies": {
"@changesets/cli": "^2.25.0",
"@types/node": "^18.8.4",
"@types/node-fetch": "^2.6.2",
"@types/sharp": "^0.31.0",
Expand Down
41 changes: 28 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import dotenv from 'dotenv'
#!/usr/bin/env node
import { InvalidArgumentError, program } from 'commander'
import createApp from './server'
import { version } from '../package.json'

dotenv.config()
const startServer = ({ port, host, origins }) => {
// If origins is an array, transform regexes
const origin: (string | RegExp)[] | string = Array.isArray(origins) ? origins.map(o => (o.startsWith('/') && o.endsWith('/')) ? new RegExp(o.slice(1,-1)) : o) : origins

// Pull list of allowed origins from .env, default to * if not set
const origin: (string | RegExp)[] | string = process.env.ALLOWED_ORIGINS?.split(',').map(o => o.trim()).map(o => (o.startsWith('/') && o.endsWith('/')) ? new RegExp(o.slice(1,-1)) : o) ?? '*'
const port: number = parseInt(process.env.PORT ?? '3000')
const app = createApp(origin)

const app = createApp(origin)
app.listen({ port, host }).then(() => {
console.log(`🧭 Favicon Scout v${version} running at http://${host}:${port}`)
console.log(`Allowing requests from: ${origin}`)
}).catch(e => {
app.log.error(e)
process.exit(1)
})
}

app.listen({ port }).then(() => {
console.log(`🧭 Favicon Scout v${version} running at http://localhost:${port}`)
console.log(`Allowing requests from: ${origin}`)
}).catch(e => {
app.log.error(e)
process.exit(1)
})
program
.name('favicon-scout')
.description('Start a favicon scout web server')
.version(version)
.option('-p, --port <port>', 'port to use for http server', v => {
const parsed = parseInt(v)
if (isNaN(parsed)) throw new InvalidArgumentError('Not a valid number')
return parsed
}, 3000)
.option('-h, --host <url>', 'host to use for http server', 'localhost')
.option('-o, --origins <urls...>', 'urls or regexes allowed by CORS', '*')
.action(startServer)

program.parse()
File renamed without changes.
Loading

0 comments on commit c0f956d

Please sign in to comment.