Skip to content

Commit

Permalink
feat: 获取配置文件信息
Browse files Browse the repository at this point in the history
  • Loading branch information
ahwgs committed Apr 9, 2021
1 parent f08ce3c commit 41f2bbd
Show file tree
Hide file tree
Showing 32 changed files with 1,827 additions and 61 deletions.
10 changes: 10 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
module.exports = {
extends: '@osdoc-dev/eslint-config-preset-ts',
rules: {
'@typescript-eslint/no-var-requires': 0,
'brace-style': 0,
'comma-dangle': 0,
'arrow-parens': 0,
'unicorn/prevent-abbreviations': 0,
'space-before-function-paren': 0,
'global-require': 0,
'import/no-dynamic-require': 0,
},
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
`ECMAScript Module` 使用 `import export` 管理依赖
,可以被依赖分析以及 `Tree-Shaking`。支持动态加载 `import()`

## 开发

```
lerna add module-1 --scope=@avenger/cli
```


2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"packages/*"
],
"scripts": {
"clean": "lerna clean --y",
"clean": "lerna clean --y && yarn clean:lib",
"bootstrap": "lerna bootstrap",
"commit": "git cz",
"build": "tsc -b -w packages/**",
Expand Down
14 changes: 9 additions & 5 deletions packages/cli/bin/avenger.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
#!/usr/bin/env node
const updater = require('update-notifier')
const { checkNodeVersion, registerCommand } = require('../lib')
const package = require('../package.json')

const { checkNodeVersion } = require('../lib')
const pkg = require('../package.json')
// 检查 node 版本
checkNodeVersion(package.engines.node, package.name)

// check node version
checkNodeVersion(pkg.engines.node, pkg.name)
// 检查更新
updater({ pkg: package }).notify({ defer: true })

console.log('avenger', pkg.engines.node, pkg.name)
// 注册命令
registerCommand()
8 changes: 7 additions & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"homepage": "https://github.com/osdoc-dev/avenger#readme",
"license": "MIT",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
"files": [
"lib"
],
Expand All @@ -26,7 +27,12 @@
"node": "^12.0.0 || >= 14.0.0"
},
"dependencies": {
"@avenger/core": "^0.0.1",
"@avenger/shared": "^0.0.1",
"@avenger/utils": "^0.0.1",
"commander": "^7.2.0",
"@avenger/utils": "*"
"envinfo": "^7.7.4",
"minimist": "^1.2.5",
"update-notifier": "^5.1.0"
}
}
55 changes: 55 additions & 0 deletions packages/cli/src/command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* @Author: ahwgs
* @Date: 2021-04-02 00:22:55
* @Last Modified by: ahwgs
* @Last Modified time: 2021-04-08 21:26:22
*/

import path from 'path'
import program from 'commander'
import { getPackageJson, chalk } from '@avenger/utils'
import { build } from '@avenger/core'
import envinfo from 'envinfo'
import { getBuildArguments } from './common'
// 注册命令
export const registerCommand = () => {
const packageJson = getPackageJson(path.join(__dirname, '..'))
// version
program.version(`${packageJson.name} v${packageJson.version}`)

program
.command('build')
.description('打包')
.option('--file', '打包输出文件名')
.option('--entry', '打包主入口')
.option('-w, --watch', 'watch 模式')

.allowUnknownOption()
.action(() => build(getBuildArguments()))

// debug info
program
.command('info')
.description('环境信息')
.action(() => {
console.log(chalk.bold('\nEnvironment Info:'))
envinfo
.run(
{
System: ['OS', 'CPU'],
Binaries: ['Node', 'Yarn', 'npm'],
Browsers: ['Chrome', 'Edge', 'Firefox', 'Safari'],
npmPackages: '/**/{typescript,*@avenger*,@avenger/*/}',
npmGlobalPackages: [packageJson.name],
},
{
showNotFound: true,
duplicates: true,
fullTree: true,
}
)
.then(console.log)
})

program.parse(process.argv)
}
21 changes: 18 additions & 3 deletions packages/cli/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,32 @@
* @Author: ahwgs
* @Date: 2021-04-01 00:06:20
* @Last Modified by: ahwgs
* @Last Modified time: 2021-04-01 01:04:15
* @Last Modified time: 2021-04-09 14:54:28
*/
import { semver, error } from '@avenger/utils'

import minimist, { ParsedArgs } from 'minimist'
import { ICliOpt } from '@avenger/shared'
// 检查node版本,避免某些 api 不适配
export const checkNodeVersion = (needVerison: string, packageName: string) => {
if (!semver.satisfies(process.version, needVerison, { includePrerelease: true })) {
error(
`You are using Node ${process.version}, but ${packageName} ` +
`requires Node ${needVerison}.\nPlease upgrade your Node version.`,
`requires Node ${needVerison}.\nPlease upgrade your Node version.`
)
process.exit(1)
}
}

export const getBuildArguments = () => {
const opt = minimist(process.argv.slice(3))
const { file, entry, watch, w } = opt as ParsedArgs
const isWatch = !!watch || !!w
return {
cwd: process.cwd(),
watch: isWatch,
inlineConfig: {
outFile: file,
entry,
},
} as ICliOpt
}
1 change: 1 addition & 0 deletions packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './common'
export * from './command'
21 changes: 20 additions & 1 deletion packages/config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"homepage": "https://github.com/osdoc-dev/avenger#readme",
"license": "ISC",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
"files": [
"lib"
],
Expand All @@ -19,5 +20,23 @@
"bugs": {
"url": "https://github.com/osdoc-dev/avenger/issues"
},
"devDependencies": {}
"dependencies": {
"@avenger/shared": "^0.0.1",
"@avenger/utils": "^0.0.1",
"@babel/preset-typescript": "^7.13.0",
"@babel/plugin-proposal-class-properties": "7.12.1",
"@babel/plugin-proposal-decorators": "7.12.1",
"@babel/plugin-proposal-do-expressions": "7.12.1",
"@babel/plugin-proposal-export-default-from": "7.12.1",
"@babel/plugin-proposal-export-namespace-from": "7.12.1",
"@babel/plugin-proposal-nullish-coalescing-operator": "7.12.1",
"@babel/plugin-proposal-optional-chaining": "7.12.1",
"@babel/plugin-syntax-dynamic-import": "7.8.3",
"@babel/plugin-transform-modules-commonjs": "7.12.1",
"@babel/plugin-transform-runtime": "7.12.1",
"@babel/preset-env": "7.12.1",
"@babel/preset-react": "7.12.1",
"babel-plugin-istanbul": "^5.2.0",
"babel-plugin-react-require": "3.1.1"
}
}
46 changes: 43 additions & 3 deletions packages/config/src/babel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,47 @@
/*
* babel 配置
* 获取babel 配置
* @Author: ahwgs
* @Date: 2021-04-01 00:29:48
* @Date: 2021-04-08 22:02:11
* @Last Modified by: ahwgs
* @Last Modified time: 2021-04-01 00:31:28
* @Last Modified time: 2021-04-09 15:02:28
*/
import { IGetBabelConfigProps } from '@avenger/shared'

export const getBabelConfig = (opt: IGetBabelConfigProps) => {
const { target, nodeVersion = 6, type, typescript } = opt || {}

const isBrowser = target === 'browser'

// 默认兼容node 6
const targets = isBrowser ? { browsers: ['last 2 versions', 'IE 10'] } : { node: nodeVersion }

const presets = [
...(typescript ? [require.resolve('@babel/preset-typescript')] : []),
[
require.resolve('@babel/preset-env'),
{
targets,
modules: type === 'esm' ? false : 'auto',
},
],
]

const plugins = [
require.resolve('babel-plugin-react-require'),
require.resolve('@babel/plugin-syntax-dynamic-import'),
require.resolve('@babel/plugin-proposal-export-default-from'),
require.resolve('@babel/plugin-proposal-export-namespace-from'),
require.resolve('@babel/plugin-proposal-do-expressions'),
require.resolve('@babel/plugin-proposal-nullish-coalescing-operator'),
require.resolve('@babel/plugin-proposal-optional-chaining'),
[require.resolve('@babel/plugin-proposal-decorators'), { legacy: true }],
[require.resolve('@babel/plugin-proposal-class-properties'), { loose: true }],
]

return {
config: {
presets,
plugins,
},
}
}
42 changes: 41 additions & 1 deletion packages/config/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,45 @@
* @Author: ahwgs
* @Date: 2021-04-01 00:19:05
* @Last Modified by: ahwgs
* @Last Modified time: 2021-04-01 00:29:33
* @Last Modified time: 2021-04-09 15:18:42
*/

import { IBuildConfigOpt, ICliOpt, DEFAULT_FILES, CLI_CONFIG_FILES } from '@avenger/shared'
import { getExistFile, info } from '@avenger/utils'

const getConfigModule = (filePath: string) => {
const module = require(filePath)
return module.default || module
}

// 获取配置文件配置
const getConfigByFile = ({ cwd }): IBuildConfigOpt => {
try {
const configFile = getExistFile({
cwd,
files: CLI_CONFIG_FILES,
returnRelative: false,
})
if (!configFile) return {}
const config = getConfigModule(configFile)
info(JSON.stringify(config))
// 校验配置文件格式

return config
} catch (error) {
console.log(error)
return {}
}
}

// 获取用户配置
export const getBundleOpts = (opt: ICliOpt) => {
const { cwd, inlineConfig } = opt
// 获取默认入口
const entry = getExistFile({ cwd, files: DEFAULT_FILES, returnRelative: true })
console.log('entry', entry)

const config = getConfigByFile({ cwd })
console.log('config', config, inlineConfig)
// 将配置文件与行内配置合并
}
3 changes: 3 additions & 0 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './rollup'
export * from './cli'
export * from './babel'
7 changes: 7 additions & 0 deletions packages/config/src/rollup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/*
* @Author: ahwgs
* @Date: 2021-04-02 21:35:08
* @Last Modified by: ahwgs
* @Last Modified time: 2021-04-02 21:35:08
*/
export const getRollupConfig = () => {}
14 changes: 13 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"homepage": "https://github.com/osdoc-dev/avenger#readme",
"license": "MIT",
"main": "./lib/index.js",
"typings": "./lib/index.d.ts",
"files": [
"lib"
],
Expand All @@ -19,5 +20,16 @@
"bugs": {
"url": "https://github.com/osdoc-dev/avenger/issues"
},
"devDependencies": {}
"dependencies": {
"@avenger/config": "^0.0.1",
"@avenger/shared": "0.0.1",
"@avenger/utils": "^0.0.1",
"@babel/core": "7.12.3",
"@babel/register": "7.12.1",
"lodash": "^4.17.21",
"rollup": "^2.44.0"
},
"devDependencies": {
"@types/lodash": "^4.14.168"
}
}
20 changes: 20 additions & 0 deletions packages/core/src/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* build fils
* @Author: ahwgs
* @Date: 2021-04-02 09:45:43
* @Last Modified by: ahwgs
* @Last Modified time: 2021-04-09 15:08:55
*/
import { ICliOpt, CLI_CONFIG_FILES } from '@avenger/shared'
import { getBundleOpts } from '@avenger/config'
import registerBabel from './register-babel'

export const build = (opt?: ICliOpt) => {
const { cwd } = opt

// 配置文件 babel 转一下,不然export default 报错
registerBabel({ cwd, only: CLI_CONFIG_FILES })

// 获取打包配置
getBundleOpts(opt)
}
5 changes: 5 additions & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import rollup from 'rollup'

export * from './build'

export { rollup }
28 changes: 28 additions & 0 deletions packages/core/src/register-babel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* @Author: ahwgs
* @Date: 2021-04-08 20:11:52
* @Last Modified by: ahwgs
* @Last Modified time: 2021-04-09 15:04:02
*/
import path from 'path'
import { slash } from '@avenger/utils'
import { getBabelConfig } from '@avenger/config'

interface IRegisterBabelProps {
cwd: string
only: string[]
}

export default ({ cwd, only }: IRegisterBabelProps) => {
const { config } = getBabelConfig({
target: 'node',
typescript: true,
})
require('@babel/register')({
...config,
extensions: ['.es6', '.es', '.jsx', '.js', '.mjs', '.ts', '.tsx'],
only: only.map(file => slash(path.join(cwd, file))),
babelrc: false,
cache: false,
})
}
Loading

0 comments on commit 41f2bbd

Please sign in to comment.