Skip to content

Commit

Permalink
feat: 修改create 增加--template
Browse files Browse the repository at this point in the history
  • Loading branch information
ahwgs committed May 28, 2021
1 parent 9c4a957 commit 912aa54
Show file tree
Hide file tree
Showing 30 changed files with 511 additions and 262 deletions.
11 changes: 6 additions & 5 deletions packages/cli/src/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
* @Author: ahwgs
* @Date: 2021-04-02 00:22:55
* @Last Modified by: ahwgs
* @Last Modified time: 2021-05-25 11:43:11
* @Last Modified time: 2021-05-28 14:31:14
*/

import path from 'path'
import program from 'commander'
import { getPackageJson, chalk } from '@osdoc-dev/avenger-utils'
import { build, create } from '@osdoc-dev/avenger-core'
import envinfo from 'envinfo'
import { getBuildArguments } from './common'
import { CreateProjectType } from '@osdoc-dev/avenger-shared'
import { getBuildArguments, getCreateArguments } from './common'

// 注册命令
export const registerCommand = () => {
const packageJson = getPackageJson(path.join(__dirname, '..'))
Expand All @@ -32,9 +34,8 @@ export const registerCommand = () => {
.command('create [name]')
.description('创建一个新项目')
.option('--force', '强制覆盖已存在文件夹')
.action(async (name, options) => {
create({ name, options })
})
.option('--template', `创建模版项目,可选项:[ ${Object.keys(CreateProjectType).join(', ')} ]`)
.action(() => create(getCreateArguments()))

// debug info
program
Expand Down
30 changes: 27 additions & 3 deletions packages/cli/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
* @Author: ahwgs
* @Date: 2021-04-01 00:06:20
* @Last Modified by: ahwgs
* @Last Modified time: 2021-05-14 14:02:18
* @Last Modified time: 2021-05-28 14:24:24
*/
import { semver, error } from '@osdoc-dev/avenger-utils'
import { semver, error, log } from '@osdoc-dev/avenger-utils'
import minimist, { ParsedArgs } from 'minimist'
import { ICliOpt } from '@osdoc-dev/avenger-shared'
import { ICliOpt, ICreateOpt, CreateProjectType } from '@osdoc-dev/avenger-shared'

// 检查node版本,避免某些 api 不适配
export const checkNodeVersion = (needVerison: string, packageName: string) => {
Expand Down Expand Up @@ -34,3 +34,27 @@ export const getBuildArguments = () => {
},
} as ICliOpt
}

const getTemplate = (template: any) => {
// 没有输入template
if (!template) return ''
const temps = Object.keys(CreateProjectType).map(v => v)
let ret = template
if (!temps.includes(template)) {
log(`当前 template 不在预设范围内,将默认使用 ${CreateProjectType.basic}`)
ret = CreateProjectType.basic
}
return ret
}

export const getCreateArguments = () => {
const opt = minimist(process.argv.slice(3))
const { template, force, _ } = opt as ParsedArgs
return {
name: _[0] || '',
options: {
force: force || false,
template: getTemplate(template),
},
} as ICreateOpt
}
9 changes: 5 additions & 4 deletions packages/config/src/rollup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @Author: ahwgs
* @Date: 2021-04-02 21:35:08
* @Last Modified by: ahwgs
* @Last Modified time: 2021-05-26 23:08:07
* @Last Modified time: 2021-05-28 11:52:28
*/
import path from 'path'
import { readFileSync, existsSync } from 'fs'
Expand All @@ -18,7 +18,7 @@ import {
IPackageJson,
} from '@osdoc-dev/avenger-shared'
import { RollupOptions, OutputOptions } from 'rollup'
import { terser } from 'rollup-plugin-terser'
import { terser, Options } from 'rollup-plugin-terser'
import url from '@rollup/plugin-url'
import svgr from '@svgr/rollup'
import typescript2 from 'rollup-plugin-typescript2'
Expand Down Expand Up @@ -109,13 +109,14 @@ export const getRollupConfig = (opt: IRollupBuildOpt): RollupOptions[] => {
const input = path.join(cwd, entry)

// rollup-plugin-terser 插件配置
const terserOpts = {
const terserOpts: Options = {
compress: {
pure_getters: true,
unsafe: true,
unsafe_comps: true,
warnings: false,
},
output: { comments: false },
ecma: 5,
}

/** 获取插件配置 */
Expand Down
9 changes: 2 additions & 7 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
"files": [
"lib"
"lib",
"templates"
],
"publishConfig": {
"access": "public"
Expand All @@ -29,13 +30,7 @@
"@osdoc-dev/avenger-config": "^0.1.0",
"@osdoc-dev/avenger-shared": "^0.1.0",
"@osdoc-dev/avenger-utils": "^0.1.0",
"fs-extra": "^10.0.0",
"lodash": "^4.17.21",
"rollup": "^2.45.2",
"validate-npm-package-name": "^3.0.0"
},
"devDependencies": {
"@types/fs-extra": "^9.0.11",
"@types/lodash": "^4.14.168"
}
}
33 changes: 33 additions & 0 deletions packages/core/src/author.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* @Description: 获取项目作者
* @Author: ahwgs
* @Date: 2021-05-28 20:17:27
* @Last Modified by: ahwgs
* @Last Modified time: 2021-05-28 20:19:36
*/
import { shelljs } from '@osdoc-dev/avenger-utils'

export const setAuthorName = (author: string) => {
shelljs.exec(`npm config set init-author-name "${author}"`, { silent: true })
}

export const getAuthorName = (): string => {
let author = ''

author = shelljs.exec('npm config get init-author-name', { silent: true }).stdout.trim()
if (author) return author

author = shelljs.exec('git config --global user.name', { silent: true }).stdout.trim()
if (author) {
setAuthorName(author)
return author
}

author = shelljs.exec('npm config get init-author-email', { silent: true }).stdout.trim()
if (author) return author

author = shelljs.exec('git config --global user.email', { silent: true }).stdout.trim()
if (author) return author

return author
}
Loading

0 comments on commit 912aa54

Please sign in to comment.