Skip to content

Commit

Permalink
chore(create-printer): support select channel
Browse files Browse the repository at this point in the history
  • Loading branch information
ourongxing committed Dec 20, 2022
1 parent 68e3525 commit 4e6f338
Show file tree
Hide file tree
Showing 24 changed files with 223 additions and 79 deletions.
26 changes: 24 additions & 2 deletions packages/create-printer/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
# create-printer

a easy way to create printer.
a easy way to create [printer](https://github.com/busiyiworld/web-printer).

## Usage
```bash
pnpm create printer@latest

# or complete in one step
pnpm create printer@latest web-printer -p vitepress
pnpm create printer@latest web-printer -t vitepress -c chrome
```

`-p` means plugin, you can select from:
- vitepress
- mdbook
- juejin
- zhihu
- javascript-info
- xiaobot
- zhubai
- ruanyifeng
- wikipedia

`-c` means Chromium channel, or version, you can select from:
- chromium
- chrome
- chrome-beta
- chrome-dev
- chrome-canary
- msedge
- msedge-beta
- msedge-dev
- msedge-canary

## License

<a href="https://github.com/busiyiworld/web-printer/blob/main/LICENSE">MIT</a> <span>©</span> <a href="https://github.com/ourongxing"><img width=15 src="https://avatars.githubusercontent.com/u/48356807?v=4"></a>
7 changes: 4 additions & 3 deletions packages/create-printer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
"@types/prompts": "^2.4.2"
},
"dependencies": {
"minimist": "^1.2.7",
"prompts": "^2.4.2",
"clipboardy": "^3.0.0",
"fs-extra": "^11.1.0",
"kolorist": "^1.6.0"
"kolorist": "^1.6.0",
"minimist": "^1.2.7",
"prompts": "^2.4.2"
}
}
102 changes: 94 additions & 8 deletions packages/create-printer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import fs from "fs-extra"
import path from "path"
import minimist from "minimist"
import prompts from "prompts"
import { red, reset } from "kolorist"
import { green, red, reset } from "kolorist"

type ColorFunc = (str: string | number) => string

type PluginOption = {
type Option = {
name: string
display: string
color?: ColorFunc
Expand All @@ -19,7 +19,7 @@ const argv = minimist<{
template?: string
}>(process.argv.slice(2), { string: ["_"] })

const plugins: PluginOption[] = [
const plugins: Option[] = [
{
name: "vitepress",
display: "Vitepress"
Expand Down Expand Up @@ -52,19 +52,65 @@ const plugins: PluginOption[] = [
name: "ruanyifeng",
display: "阮一峰的网络日志"
},
{
name: "wikipedia",
display: "Wikipedia"
},
{
name: "manual",
display: "Manual"
}
]

const channels: Option[] = [
{
name: "chromium",
display: "Chromium"
},
{
name: "chrome",
display: "Chrome"
},
{
name: "chrome-beta",
display: "Chrome Beta"
},
{
name: "chrome-dev",
display: "Chrome Dev"
},
{
name: "chrome-canary",
display: "Chrome Canary"
},
{
name: "msedge",
display: "Microsoft Edge"
},
{
name: "msedge-beta",
display: "Microsoft Edge Beta"
},
{
name: "msedge-dev",
display: "Microsoft Edge Dev"
},
{
name: "msedge-canary",
display: "Microsoft Edge Canary"
}
]

const defaultTargetDir = "web-printer"
async function init() {
const argTargetDir = argv._[0]
const argPlugin = argv.plugin || argv.p
const argChannel = argv.channel || argv.c
let targetDir = argTargetDir || defaultTargetDir

let result: prompts.Answers<"projectName" | "overwrite" | "plugin">
let targetChannelName = ""
let result: prompts.Answers<
"projectName" | "overwrite" | "plugin" | "channel" | "haveInstalled"
>

try {
result = await prompts(
Expand Down Expand Up @@ -103,7 +149,7 @@ async function init() {
? null
: "select",
name: "plugin",
message: reset("Select a plugin template:"),
message: reset("Select a priner template:"),
initial: 0,
choices: plugins.map(k => {
return {
Expand All @@ -113,6 +159,36 @@ async function init() {
value: k.name
}
})
},
{
type:
argChannel && channels.find(k => k.name === argChannel)
? null
: "select",
name: "channel",
message: reset(
"Select a Chromium channel you have installed or want to install:"
),
initial: 0,
choices: channels.map(k => {
return {
title: k.color
? k.color(k.display || k.name)
: reset(k.display || k.name),
value: k.name
}
}),
onState: state => {
targetChannelName = channels.find(
k => k.name === state.value
)!.display
}
},
{
type: () => (targetChannelName ? "confirm" : null),
name: "haveInstalled",
message: () => reset(`Do you have installed ${targetChannelName} ?`),
initial: true
}
],
{
Expand All @@ -125,7 +201,7 @@ async function init() {
console.log(cancelled.message)
return
}
const { plugin, overwrite } = result
const { plugin, overwrite, channel, haveInstalled } = result
const root = path.join(cwd, targetDir)
if (overwrite) {
await fs.remove(targetDir)
Expand All @@ -138,10 +214,20 @@ async function init() {
)

await fs.copy(templateDir, root)
const indexFilePath = path.resolve(root, "src/index.ts")
await fs.writeFile(
indexFilePath,
(await fs.readFile(indexFilePath))
.toString("utf8")
.replace(/channel: "chrome"/g, `channel: "${channel || argChannel}"`)
)
console.log()
console.log("Done. Now Run:")
console.log()
console.log(red(`cd ${targetDir} && pnpm i && pnpm pw && code .`))
if (channel && !haveInstalled)
console.log(green(`> pnpm dlx playwright install ${channel}`))
console.log(red(`> cd ${targetDir} && pnpm i && code .`))
console.log()
}

function isEmpty(path: string) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"scripts": {
"print": "tsx src/index.ts",
"pw": "playwright install chromium"
"print": "tsx src/index.ts"
},
"devDependencies": {
"@web-printer/core": "^0.2.5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Printer } from "@web-printer/core"
import javascriptInfo from "@web-printer/javascript-info"

new Printer({
threads: 5
threads: 5,
channel: "chrome"
})
.use(
javascriptInfo({
Expand Down
3 changes: 1 addition & 2 deletions packages/create-printer/template/juejin/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"scripts": {
"print": "tsx src/index.ts",
"pw": "playwright install chromium"
"print": "tsx src/index.ts"
},
"devDependencies": {
"@web-printer/core": "^0.2.5",
Expand Down
3 changes: 2 additions & 1 deletion packages/create-printer/template/juejin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { Printer } from "@web-printer/core"
import juejin from "@web-printer/juejin"

new Printer({
threads: 5
threads: 5,
channel: "chrome"
})
.use(
juejin({
Expand Down
Loading

0 comments on commit 4e6f338

Please sign in to comment.