kgr v1.0.20
kgr
项目修剪工具 , 输出项目前执行修剪工作 , 文件增删改 , 内容正则替换
install
npm install kgr
Usage: kgr [options]
Options:
-V, --version output the version number
-c, --config [config] config file ',' separated, glob mode
-o, --output [output] output path (default: "output")
-d, --dest [dest] dest path (default: "dest")
-s, --source [source] source path (default: ".source")
-m, --mode [mode] run project mode , default dev (default: "dev")
-n, --name [name] run project name , default find first name in config file (default: "")
--init [init] init project (default: "") 是否需要重新init
--copy [copy] copy project (default: "") 是否需要重新拷贝项目
-h, --help output usage information
说明
执行流程
1. 检出配置文件中 git
对应的 remote
version(这里可以是branch , tag , commitid)
, 同时执行初始化操作 如 npm install
2. 复制到指定目录 [下文中使用.source
代替]
3. 删除配置文件中remove
匹配的文件
4. 替换配置文件中replace
参数内的文件到.source
, replace
规则如下:
1. source
,target
可使用绝对路径 , 或相对路径
2. source
相对路径 , 使用当前配置文件作为依据 , target
相对路径使用的是.source
目录作为依据
3. source
文件存在 , target
文件存在 --> 执行替换操作
4. source
文件存在 , target
文件不存在 --> 执行添加操作
5. 使用配置文件中的changeByLine
参数按文件路径和行号更改的文件 , 返回新的行内容
6. 使用配置文件中的pipe
参数过滤.source
内部的所有文件 , 默认会忽略掉一些大文件 , 规则
1. 使用glob['./**/*','!./{bower_components,node_modules,dist,build}{,/**}','!./**/*.{tar.gz,swf,mp4,webm,ogg,mp3,wav,flac,aac,png,jpg,gif,svg,eot,woff,woff2,ttf,otf,swf}']
2. 可在配置文件中添加glob
数组, 追加已经忽略的文件 如 "glob":["./node_modules/kpc/src/components/*.js"]
7. dev
模式下会监听配置文件,replace
参数内的源文件改动 , 重新执行2
,3
,4
步骤 ; build
模式下将会导出tar.gz
的文件到指定的output
目录下,如output
不存在,只做提示不做任何处理
配置文件示例
module.exports = function () {
return [
/**
* 参数说明
* [name] <string> 必填
* [remote] <string> 必填
* [version] <string> (tag|branch|commitid) 必填
* [glob] [tag|branch|commitid] <string> 必填
* [pipe] <array[<function(content<string>,filename<string>,file<File Object>):content<string>>]> | <function(content<string>,filename<string>,file<File Object>):content<string>> 选填
* [replace] <array[<object>[source,target]]> 选填
* [start] <string> 选填 启动命令
* [restart] <string> 选填 更改文件后触发重启命令 , true 时代表重新运行start , 其他命令时 代表停止start 重启一个restart 的命令
* [build] <string> 选填 构建命令
*/
{
//项目名 , 如没有output参数时,做导出目录的目录名
name: 'components',
//需要执行的初始化命令 , 支持数组 , 多命令并行 , 数组内部 第二个 为传递给shell 的参数 可用来指定执行目录等, 目录会添加输出目录作为根目录
bash: [
'npm install lodash --verbose',
[
'npm install lodash --verbose',{cwd:'v1'}
]
],
//启动命令 ,mode dev时执行 支持数组 , 多命令并行 , 数组内部 第二个 为传递给shell 的参数 可用来指定执行目录等, 目录会添加输出目录作为根目录
start: 'ls',
//文件改变后命令 , 空代表改变文件后不触发操作 , mode dev 时执行 支持数组 , 多命令并行 , 数组内部 第二个 为传递给shell 的参数 可用来指定执行目录等, 目录会添加输出目录作为根目录
restart: 'ls -l',
//构建命令 ,mode build时执行 支持数组 , 多命令并行 , 数组内部 第二个 为传递给shell 的参数 可用来指定执行目录等, 目录会添加输出目录作为根目录
build:'',
//git 地址
remote: 'http://xxxx/xxxx/xxx.git',
//分支
version: 'yinhe-20190921-pbc-v1',
glob: [
'!./output/**/*',
'./output/test/*.js'
],
changeByLine: {
"v2/node_modules/ksc-vue-ui2/index.js:200": function(number,text){
return ''
}
},
pipe: [ //请使用 [gulp-replace语法](https://www.npmjs.com/package/gulp-replace)
['console.ksyun.com', '{$common.sss}'],
['ksyun','lkkl'],
['com', function () {
return this.file.relative
}]
],
// 新增与替换资源路径
//`source`路径不能为空,相对路径时以配置文件所在位置查找;
//`target`路径不能为空,相对路径时以输出目录作为依据
replace: {
"target":"source",
"target":"source"
},
// 待移除文件 glob模式以输出目录作为依据
remove: []
}
]
}
注意
- 请确保有
git
仓库的clone 权限 - 避免加入大文件
replace
, 可使用golb 忽略 pipe
使用方法, 请参考gulp-replace
, 参数以数组方式传入- 请确保运行环境 存在
git
,tar
命令 - output 目录在copy时会被清空