Skip to content

Commit

Permalink
feat: use node-fetch + x-fetch to replace got, follow latest iconfont…
Browse files Browse the repository at this point in the history
… website changes (#53)
  • Loading branch information
JounQin authored Dec 14, 2023
1 parent 55aefdf commit a8bdd72
Show file tree
Hide file tree
Showing 15 changed files with 5,406 additions and 5,283 deletions.
4 changes: 2 additions & 2 deletions .changeset/config.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"$schema": "https://unpkg.com/@changesets/config@1.6.0/schema.json",
"$schema": "https://unpkg.com/@changesets/config/schema.json",
"changelog": [
"@changesets/changelog-github",
{
"repo": "rx-ts/dl-iconfont"
"repo": "un-ts/dl-iconfont"
}
],
"commit": false,
Expand Down
5 changes: 5 additions & 0 deletions .changeset/ninety-ghosts-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"dl-iconfont": minor
---

feat: use `node-fetch` + `x-fetch` to replace `got`, follow latest `iconfont` website changes
2 changes: 1 addition & 1 deletion .codesandbox/ci.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"node": "14",
"node": "18",
"sandboxes": []
}
9 changes: 4 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@ jobs:
strategy:
matrix:
node:
- 14
- 16
- 18
- 20
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@master
uses: actions/checkout@v4

- name: Setup Node.js ${{ matrix.node }}
uses: actions/setup-node@master
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node }}
cache: yarn

- name: Install Dependencies
run: yarn --frozen-lockfile

- name: Build, Lint and test
- name: Build, Lint and Test
run: |
yarn run-p build lint typecov
yarn dev iconfont.js
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
# This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits
fetch-depth: 0

- name: Setup Node.js 16
uses: actions/setup-node@v3
- name: Setup Node.js LTS
uses: actions/setup-node@v4
with:
node-version: 16
node-version: lts/*
cache: yarn

- name: Install Dependencies
Expand Down
12 changes: 7 additions & 5 deletions .github/workflows/size-limit.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
name: Size Limit

on:
pull_request_target:
pull_request:
branches:
- main

jobs:
size-limit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup Node.js 16
uses: actions/setup-node@v3
- name: Setup Node.js LTS
uses: actions/setup-node@v4
with:
node-version: 16
# node-version: lts/*
# workaround for eslint-plugin-jsdoc@39 on main branch
node-version: 18
cache: yarn

- name: Install Dependencies
Expand Down
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lts/*
2 changes: 1 addition & 1 deletion .remarkrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"plugins": [
"@1stg/remark-config"
"@1stg/remark-preset"
]
}
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@

### Patch Changes

- [#5](https://github.com/rx-ts/dl-iconfont/pull/5) [`e343e2a`](https://github.com/rx-ts/dl-iconfont/commit/e343e2ad980f0282d0d63b2bb65f1376d10f87f4) Thanks [@JounQin](https://github.com/JounQin)! - fix: should use latest node after refreshing
- [#5](https://github.com/un-ts/dl-iconfont/pull/5) [`e343e2a`](https://github.com/un-ts/dl-iconfont/commit/e343e2ad980f0282d0d63b2bb65f1376d10f87f4) Thanks [@JounQin](https://github.com/JounQin)! - fix: should use latest node after refreshing

## 0.1.0

### Minor Changes

- [#2](https://github.com/rx-ts/dl-iconfont/pull/2) [`ba04593`](https://github.com/rx-ts/dl-iconfont/commit/ba0459398688e1a35f81bcec48e183ca6669dc8e) Thanks [@JounQin](https://github.com/JounQin)! - feat: make this project a npm module, follow latest login process
- [#2](https://github.com/un-ts/dl-iconfont/pull/2) [`ba04593`](https://github.com/un-ts/dl-iconfont/commit/ba0459398688e1a35f81bcec48e183ca6669dc8e) Thanks [@JounQin](https://github.com/JounQin)! - feat: make this project a npm module, follow latest login process
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ npm i -g dl-iconfont

#### Command

`dli`, `dl-iconfont` and `iconfont-dl` are all available.

```sh
Usage: dli [options] [downloadFile]

Expand Down
66 changes: 18 additions & 48 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,9 @@
"contributors": [
"JounQin (https://www.1stG.me) <[email protected]>"
],
"donate": {
"recipients": [
{
"name": "unts",
"platform": "opencollective",
"address": "https://opencollective.com/unts",
"weight": 60
},
{
"name": "rxts",
"platform": "opencollective",
"address": "https://opencollective.com/rxts",
"weight": 20
},
{
"name": "1stG",
"email": "[email protected]",
"weight": 20,
"platforms": [
{
"platform": "opencollective",
"address": "https://opencollective.com/1stG"
},
{
"platform": "patreon",
"address": "https://www.patreon.com/1stG"
}
]
}
]
},
"funding": "https://opencollective.com/unts",
"license": "MIT",
"packageManager": "[email protected]",
"engines": {
"node": "^14.18.0 || >=16.0.0"
},
Expand Down Expand Up @@ -71,7 +41,7 @@
"build": "run-p build:*",
"build:r": "r -f cjs",
"build:tsc": "tsc -b",
"dev": "tsx ./src/cli.ts",
"dev": "tsx ./src/cli",
"lint": "run-p lint:*",
"lint:es": "eslint . -f friendly",
"lint:tsc": "tsc --noEmit",
Expand All @@ -81,24 +51,24 @@
"typecov": "type-coverage"
},
"dependencies": {
"dotenv": "^16.0.1",
"got": "^12.3.1",
"puppeteer": "^16.1.0",
"tslib": "^2.4.0"
"commander": "^11.1.0",
"dotenv": "^16.3.1",
"node-fetch": "^3.3.2",
"puppeteer": "^21.6.1",
"tslib": "^2.6.2",
"x-fetch": "^0.1.3"
},
"devDependencies": {
"@1stg/lib-config": "^9.0.2",
"@changesets/changelog-github": "^0.4.6",
"@changesets/cli": "^2.24.2",
"@types/puppeteer": "^5.4.6",
"@types/web": "^0.0.71",
"commander": "^9.4.0",
"size-limit": "^8.0.0",
"size-limit-preset-node-lib": "^0.1.0",
"tsx": "^3.8.1",
"type-coverage": "^2.22.0",
"typescript": "^4.7.4",
"yarn-deduplicate": "^5.0.0"
"@1stg/lib-config": "^12.0.1",
"@changesets/changelog-github": "^0.5.0",
"@changesets/cli": "^2.27.1",
"@types/web": "^0.0.127",
"size-limit": "^11.0.1",
"size-limit-preset-node-lib": "^0.3.0",
"tsx": "^4.6.2",
"type-coverage": "^2.27.0",
"typescript": "^5.3.3",
"yarn-deduplicate": "^6.0.2"
},
"publishConfig": {
"access": "public"
Expand Down
6 changes: 3 additions & 3 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ await program

if (downloadFile) {
console.log(
`[dl-iconfont]: downloading \`${jsUrl!}\` into \`${downloadFile}\``,
`[dl-iconfont]: downloading \`${jsUrl}\` into \`${downloadFile}\``,
)
await download(jsUrl!, downloadFile)
await download(jsUrl, downloadFile)
} else {
console.log(
'[dl-iconfont] no `downloadFile` provided, you can use the environment variable or `dli ICONFONT_DOWNLOAD_FILE` to download the js file automatically',
)
console.log(`[dl-iconfont]: ${jsUrl!}`)
console.log(`[dl-iconfont]: ${jsUrl}`)
}
})
.parseAsync(process.argv)
9 changes: 9 additions & 0 deletions src/env.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { config } from 'dotenv'
import fetch, { Headers } from 'node-fetch'

if (typeof fetch !== 'function') {
globalThis.fetch = fetch
}

if (typeof Headers !== 'function') {
globalThis.Headers = Headers
}

config()
35 changes: 25 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'node:fs/promises'

import got from 'got'
import puppeteer from 'puppeteer'
import { fetchApi } from 'x-fetch'

const ICONFONT_URL = 'https://www.iconfont.cn/'

Expand All @@ -25,7 +25,7 @@ export const fetchJsUrl = async ({
}

const browser = await puppeteer.launch({
headless,
headless: headless && 'new',
defaultViewport: VIEWPORT,
})

Expand Down Expand Up @@ -57,16 +57,26 @@ export const fetchJsUrl = async ({
{ waitUntil: 'networkidle0' },
)

if (await page.$('.project-code-mask')) {
const anchor = await page.waitForSelector('a#J_cdn_type_svgsymbol')

const copy = await page.$('.project-code-top .cover-btn:not(:last-child)')

if (copy) {
const refresh = await page.$('.project-code-top .cover-btn:last-child')
if (refresh) {
await refresh.click()
await page.waitForSelector('.project-code-mask', { hidden: true })
}
await refresh!.click()
const button = await page.waitForSelector(
'.mx-modal-footer button.mp-e2e-button',
)
await button!.click()
await page.waitForSelector(
'.project-code-top .cover-btn:not(:last-child)',
{
hidden: true,
},
)
}

const element = await page.waitForSelector('#J_cdn_type_svgsymbol')
const jsUrl = await element!.evaluate(el => el.textContent)
const jsUrl = await anchor!.evaluate(el => el.href)

await browser.close()

Expand All @@ -81,5 +91,10 @@ export const download = async (url: string, file: string) => {
throw new Error('invalid url for downloading')
}
}
return fs.writeFile(file, got.stream(url))
return fs.writeFile(
file,
await fetchApi(url, {
type: 'text',
}),
)
}
Loading

0 comments on commit a8bdd72

Please sign in to comment.