-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add commander support to override env easily (#38)
- Loading branch information
Showing
8 changed files
with
113 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"dl-iconfont": minor | ||
--- | ||
|
||
feat: add commander support to override env easily |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"dl-iconfont": minor | ||
--- | ||
|
||
chore!: rename `ICONFONT_PROJECT_ID` env to `ICONFONT_PROJECT` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,72 @@ | ||
import './env.js' | ||
|
||
import { download, fetchJsUrl } from './index.js' | ||
import fs from 'node:fs/promises' | ||
import path from 'node:path' | ||
import { fileURLToPath } from 'node:url' | ||
|
||
import { program } from 'commander' | ||
|
||
import { download, fetchJsUrl, FetchOptions } from './index.js' | ||
|
||
const { | ||
ICONFONT_PROJECT_ID, | ||
ICONFONT_PROJECT, | ||
ICONFONT_LOGIN, | ||
ICONFONT_PASSWORD, | ||
ICONFONT_DOWNLOAD_FILE, | ||
PUPPETEER_HEADLESS, | ||
} = process.env | ||
|
||
const DOWNLOAD_FILE = process.argv[2] || ICONFONT_DOWNLOAD_FILE | ||
|
||
const jsUrl = await fetchJsUrl({ | ||
projectId: ICONFONT_PROJECT_ID!, | ||
login: ICONFONT_LOGIN!, | ||
password: ICONFONT_PASSWORD!, | ||
headless: !['0', 'false'].includes(PUPPETEER_HEADLESS!), | ||
}) | ||
if (DOWNLOAD_FILE) { | ||
console.log( | ||
`[dl-iconfont]: downloading \`${jsUrl!}\` into \`${DOWNLOAD_FILE}\``, | ||
await program | ||
.version( | ||
( | ||
JSON.parse( | ||
await fs.readFile( | ||
path.resolve(fileURLToPath(import.meta.url), '../../package.json'), | ||
'utf8', | ||
), | ||
) as { | ||
version: string | ||
} | ||
).version, | ||
) | ||
.requiredOption( | ||
'-p, --project <string>', | ||
'Project ID from iconfont.cn', | ||
ICONFONT_PROJECT, | ||
) | ||
await download(jsUrl!, DOWNLOAD_FILE) | ||
} else { | ||
console.log( | ||
'[dl-iconfont] no `ICONFONT_DOWNLOAD_FILE` provided, you can use the environment variable or `dli ICONFONT_DOWNLOAD_FILE` to download the js file automatically', | ||
.requiredOption( | ||
'-l, --login <string>', | ||
'User login (mobile number) for iconfont.cn', | ||
ICONFONT_LOGIN, | ||
) | ||
console.log(`[dl-iconfont]: ${jsUrl!}`) | ||
} | ||
.requiredOption( | ||
'-p, --password <string>', | ||
'User password for iconfont.cn', | ||
ICONFONT_PASSWORD, | ||
) | ||
.option( | ||
'-h, --headless <boolean>', | ||
'Run in headless mode', | ||
!['0', 'false'].includes(PUPPETEER_HEADLESS!), | ||
) | ||
.argument( | ||
'[downloadFile]', | ||
'Filename path to be downloaded', | ||
ICONFONT_DOWNLOAD_FILE, | ||
) | ||
.action(async (downloadFile: string, options: FetchOptions) => { | ||
const jsUrl = await fetchJsUrl(options) | ||
|
||
if (downloadFile) { | ||
console.log( | ||
`[dl-iconfont]: downloading \`${jsUrl!}\` into \`${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!}`) | ||
} | ||
}) | ||
.parseAsync(process.argv) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters