Skip to content

Commit

Permalink
fix: create-karin
Browse files Browse the repository at this point in the history
  • Loading branch information
sj817 committed Jan 16, 2025
1 parent f97e931 commit 758e20f
Show file tree
Hide file tree
Showing 20 changed files with 589 additions and 179 deletions.
19 changes: 13 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ jobs:
manifest-file: .release-please-manifest.json
- name: 检出代码
uses: actions/checkout@v4
if: ${{ steps.release.outputs['packages/core--release_created'] || steps.release.outputs['packages/cli--release_created'] }}
if: ${{ steps.release.outputs['packages/core--release_created'] || steps.release.outputs['packages/cli--release_created'] || steps.release.outputs['packages/create-karin--release_created'] }}

- name: 设置 node
uses: actions/setup-node@v4
with:
node-version: 20
registry-url: "https://registry.npmjs.org"
if: ${{ steps.release.outputs['packages/core--release_created'] || steps.release.outputs['packages/cli--release_created'] }}
registry-url: 'https://registry.npmjs.org'
if: ${{ steps.release.outputs['packages/core--release_created'] || steps.release.outputs['packages/cli--release_created'] || steps.release.outputs['packages/create-karin--release_created'] }}

- name: 设置 pnpm
uses: pnpm/action-setup@v2
with:
version: 8
if: ${{ steps.release.outputs['packages/core--release_created'] }}
if: ${{ steps.release.outputs['packages/core--release_created'] || steps.release.outputs['packages/create-karin--release_created'] }}

# 安装所有依赖
- name: 安装依赖
run: pnpm install
if: ${{ steps.release.outputs['packages/core--release_created'] }}
if: ${{ steps.release.outputs['packages/core--release_created'] || steps.release.outputs['packages/create-karin--release_created'] }}

# 发布 core 包
- name: 发布 core 包
Expand All @@ -57,4 +57,11 @@ jobs:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
working-directory: packages/cli
run: pnpm pub


# 发布 create-karin 包
- name: 发布 create-karin 包
if: ${{ steps.release.outputs['packages/create-karin--release_created'] }}
env:
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
working-directory: packages/create-karin
run: pnpm build && pnpm pub
9 changes: 8 additions & 1 deletion .release-please-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@
"component": "cli",
"tag-separator": "-",
"scope": "cli"
},
"packages/create-karin": {
"release-type": "node",
"package-name": "create-karin",
"component": "create-karin",
"tag-separator": "-",
"scope": "create-karin"
}
},
"commit-search-depth": 50
}
}
7 changes: 4 additions & 3 deletions .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"packages/core": "1.1.1",
"packages/cli": "1.0.0"
}
"packages/core": "1.1.0",
"packages/cli": "1.0.0",
"packages/create-karin": "1.0.0"
}
9 changes: 8 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
"bin": {
"karin": "index.js"
},
"engines": {
"node": ">=18"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"scripts": {
"pub": "npm publish --access public"
},
Expand All @@ -18,4 +25,4 @@
],
"author": "",
"license": "MIT"
}
}
11 changes: 4 additions & 7 deletions packages/core/exports/cli/exec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import {
exec as execCmd,
execSync as execSyncCmd,
} from 'node:child_process'
import { exec as execCmd, execSync as execSyncCmd } from 'node:child_process'

/**
* 执行命令
Expand All @@ -10,7 +7,7 @@ import {
*/
const execSync = (
cmd: string,
options: import('node:child_process').ExecOptions = {}
options: import('node:child_process').ExecOptions = {},
): { status: boolean; error: Error | null; stdout: string; stderr: string } => {
try {
const result = execSyncCmd(cmd, options)
Expand All @@ -27,9 +24,9 @@ const execSync = (
*/
const exec = (
cmd: string,
options: import('node:child_process').ExecOptions = {}
options: import('node:child_process').ExecOptions = {},
): Promise<{ status: boolean; error: Error | null; stdout: string; stderr: string }> => {
return new Promise((resolve) => {
return new Promise(resolve => {
execCmd(cmd, options, (error, stdout, stderr) => {
const status = !error
resolve({ status, error, stdout, stderr })
Expand Down
14 changes: 8 additions & 6 deletions packages/core/exports/cli/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const createDir = () => {
path.join(dir, '@karinjs', 'temp', 'html'),
]

isDev && list.push(path.join(dir, 'plugins', 'karin-plugin-example'))
!isDev && list.push(path.join(dir, 'plugins', 'karin-plugin-example'))
list.forEach(item => {
if (!fs.existsSync(item)) fs.mkdirSync(item, { recursive: true })
})
Expand Down Expand Up @@ -206,8 +206,8 @@ const createWorkspace = (dir: string) => {
* 生成一些其他文件
*/
export const createOtherFile = async () => {
isDev && createPnpmFile(dir)
isDev && createWorkspace(dir)
!isDev && createPnpmFile(dir)
!isDev && createWorkspace(dir)

if (!shouldSkipNpmrc()) {
createOrUpdateNpmrc(dir)
Expand Down Expand Up @@ -259,9 +259,11 @@ export const modifyPackageJson = () => {
data.scripts.karin = 'karin'

const list = ['app', 'start', 'pm2', 'stop', 'rs', 'log']
list.forEach(v => {
data.scripts[v] = `karin ${v}`
})
if (!isDev) {
list.forEach(v => {
data.scripts[v] = `karin ${v}`
})
}

fs.writeFileSync(path.join(dir, 'package.json'), JSON.stringify(data, null, 2))
return data
Expand Down
18 changes: 15 additions & 3 deletions packages/create-karin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@
"bin": {
"create-karin": "./lib/index.js"
},
"engines": {
"node": ">=18"
},
"publishConfig": {
"access": "public",
"registry": "https://registry.npmjs.org"
},
"scripts": {
"dev": "tsx src/index.ts",
"build": "tsup --config tsup.config.ts"
"build": "tsup --config tsup.config.ts",
"pub": "npm publish --access public"
},
"keywords": [
"karin",
Expand All @@ -28,5 +36,9 @@
"@types/prompts": "^2.4.9",
"tsup": "^8.0.1",
"typescript": "^5.3.3"
}
}
},
"files": [
"lib",
"templates"
]
}
99 changes: 99 additions & 0 deletions packages/create-karin/src/dev.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import fs from 'node:fs'
import path from 'node:path'
import { green, magenta, red, yellow } from 'kolorist'
import { checkNetwork, installPuppeteer } from './project'
import { getStr } from './utils/tools'

/**
* 创建开发环境项目
* @param name - 项目名称
* @param type - 项目类型
*/
export const copyTemplate = (name: string, type: 'ts' | 'js') => {
console.log('📦 正在创建项目目录结构...')

/** karin目录 */
const dir = path.join(process.cwd(), name)
/** ts模板路径 */
const templatePath = path.join(__dirname, `../templates/${type}-plugin`)

// 直接复制整个模板目录
fs.cpSync(templatePath, dir, { recursive: true })

// TODO: 后续改为远程api拉取 动态更新
const list = [
'node_sqlite3_binary_host_mirror=https://registry.npmmirror.com/-/binary/sqlite3',
'better_sqlite3_binary_host_mirror=https://registry.npmmirror.com/-/binary/better-sqlite3',
'sass_binary_site=https://registry.npmmirror.com/-/binary/node-sass',
'sharp_binary_host=https://registry.npmmirror.com/-/binary/sharp',
'sharp_libvips_binary_host=https://registry.npmmirror.com/-/binary/sharp-libvips',
'canvas_binary_host_mirror=https://registry.npmmirror.com/-/binary/canvas',
'# 19以下版本',
'puppeteer_download_host=https://registry.npmmirror.com/mirrors',
'# 20以上版本',
'PUPPETEER_DOWNLOAD_BASE_URL = https://registry.npmmirror.com/binaries/chrome-for-testing',
]

const npmrc = path.join(dir, '.npmrc')
fs.writeFileSync(npmrc, list.join('\n'))

/** 将.env文件的两个token随机生成 */
const env = path.join(dir, '.env')
const envContent = fs.readFileSync(env, 'utf-8')
const token = getStr(5)
const wsToken = getStr(5)

fs.writeFileSync(
env,
envContent
.replace('HTTP_AUTH_KEY=QtYTbI', `HTTP_AUTH_KEY=${token}`)
.replace('WS_SERVER_AUTH_KEY=QOnAho', `WS_SERVER_AUTH_KEY=${wsToken}`),
)

console.log(green('✨ 项目目录结构创建完成'))
}

/**
* 创建开发环境项目
* @param name - 项目名称
* @param type - 项目类型
* @param puppeteer - 是否安装Puppeteer
*/
export const createDev = async (name: string, type: 'ts' | 'js', puppeteer: boolean) => {
try {
copyTemplate(name, type)

if (puppeteer) {
const isNpmMirror = await checkNetwork()
installPuppeteer(name, isNpmMirror)
}

const list = [
'\n\n--------------------------------',
'\n✨ 项目创建成功!',
yellow('👇 请执行以下命令:\n'),
green(` cd ${name}`),
green(' pnpm dev\n'),
' 快捷指令(上下任选其一):',
magenta(` cd ${name} && pnpm dev\n`),
'🚀 开始愉快的开发吧!',
]

if (puppeteer) {
list.push(
'--------------------------------',
' 如需使用Puppeteer,请执行以下命令:',
green(` cd ${name}-puppeteer && node .`),
'--------------------------------',
)
}

console.log(list.join('\n'))
} catch (error) {
console.log(magenta('Github: https://github.com/Karinjs/Karin/issues'))
console.log(red('发生错误,请将以下信息反馈给开发者:'))
console.log(green('--------------------------------'))
console.log(error)
console.log(green('--------------------------------'))
}
}
Loading

0 comments on commit 758e20f

Please sign in to comment.