Skip to content

Commit

Permalink
feat: set manifest name
Browse files Browse the repository at this point in the history
  • Loading branch information
FliPPeDround committed Jul 11, 2023
1 parent 9028629 commit 6196ddb
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 30 deletions.
5 changes: 2 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
printFinish,
renderLint,
renderTemplate,
setPackageName,
replaceProjectName,
} from './utils'
import { question } from './question'
import type { BaseTemplateList } from './question/template/type'
Expand Down Expand Up @@ -77,8 +77,6 @@ async function init() {
return
}

setPackageName(result.projectName!, root)

const templateRoot = resolve(__dirname, 'template')

type Callback = (dataStore: Record<string, any>) => void
Expand Down Expand Up @@ -215,6 +213,7 @@ async function init() {
},
)
}
replaceProjectName(root, result.projectName!)

printFinish(root, cwd, packageManager)
}
Expand Down
4 changes: 0 additions & 4 deletions src/question/template/templateDate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,13 @@ export const templateList: TemplateList[] = [
github: 'https://github.com/uni-helper/vitesse-uni-app.git',
},
callBack(root) {
// 判断当前系统
const isWin = process.platform === 'win32'
// 如果非 Windows 系统,删除目录下的patches文件夹
if (isWin)
return
rmSync(join(root, 'patches'), { recursive: true })

const pkgPath = join(root, 'package.json')
const packageJson = JSON.parse(readFileSync(pkgPath, 'utf8'))

// 删除 package.json 中的 pnpm
delete packageJson.pnpm

writeFileSync(pkgPath, JSON.stringify(packageJson, null, 2))
Expand Down
4 changes: 2 additions & 2 deletions src/utils/cloneRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join } from 'node:path'
import { bold } from 'kolorist'
import type { BaseTemplateList } from '../question/template/type'
import { ora } from './loading'
import { replacePackageName } from './setPackageName'
import { replaceProjectName } from './setPackageName'

async function removeGitFolder(localPath: string): Promise<void> {
const gitFolderPath = join(localPath, '.git')
Expand Down Expand Up @@ -60,7 +60,7 @@ export async function dowloadTemplate(data: BaseTemplateList['value'], name: str
process.exit(1)
}

replacePackageName(root, name)
replaceProjectName(root, name)

loading.succeed(`${bold('模板下载完成!')}`)
data.callBack?.(root)
Expand Down
31 changes: 10 additions & 21 deletions src/utils/setPackageName.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
import { readFileSync, writeFileSync } from 'node:fs'
import { join, resolve } from 'node:path'
import { join } from 'node:path'

function getPackageInfo(name: string) {
return {
name: name.toLocaleLowerCase().replace(/\s/g, '-'),
version: '0.0.0',
}
function replaceNameContent(filePath: string, projectName: string) {
const fileContent = JSON.parse(readFileSync(filePath, 'utf8'))
fileContent.name = projectName
writeFileSync(filePath, JSON.stringify(fileContent, null, 2))
}

export function setPackageName(name: string, root: string) {
if (!name)
return
writeFileSync(
resolve(root, 'package.json'),
JSON.stringify(getPackageInfo(name), null, 2),
)
}

export function replacePackageName(root: string, name: string) {
export function replaceProjectName(root: string, name: string) {
const projectName = name.toLocaleLowerCase().replace(/\s/g, '-')
const pkgPath = join(root, 'package.json')
const manifestPath = join(root, 'src', 'manifest.json')

const packageJson = JSON.parse(readFileSync(pkgPath, 'utf8'))

Object.assign(packageJson, getPackageInfo(name))

writeFileSync(pkgPath, JSON.stringify(packageJson, null, 2))
replaceNameContent(manifestPath, projectName)
replaceNameContent(pkgPath, projectName)
}
2 changes: 2 additions & 0 deletions template/base/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"name": "uni-app",
"version": "0.0.0",
"private": true,
"scripts": {
"dev": "uni",
Expand Down

0 comments on commit 6196ddb

Please sign in to comment.