# kgr

> project gardener

Latest version **1.0.20** (published 2019-05-06) · MIT license · 0 weekly downloads

## Install

```sh
npm install kgr
pnpm add kgr
yarn add kgr
bun add kgr
```

Provides the command `kgr`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.20 |
| Published | 2019-05-06 |
| First published | 2018-11-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 17 |
| Unpacked size | 283.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | wangjianliang |
| Maintainers | lynn.wang |

## Links

- npm: https://www.npmjs.com/package/kgr
- npm.io page: https://npm.io/package/kgr

## Dependencies (17)

- [del](https://npm.io/package/del.md) ^3.0.0
- [gulp](https://npm.io/package/gulp.md) ^3.9.1
- [pify](https://npm.io/package/pify.md) ^4.0.1
- [chalk](https://npm.io/package/chalk.md) latest
- [debug](https://npm.io/package/debug.md) ^4.1.0
- [execa](https://npm.io/package/execa.md) ^1.0.0
- [vinyl](https://npm.io/package/vinyl.md) ^2.2.0
- [globby](https://npm.io/package/globby.md) ^8.0.1
- [lodash](https://npm.io/package/lodash.md) ^4.17.10
- [mkdirp](https://npm.io/package/mkdirp.md) ^0.5.1
- [moment](https://npm.io/package/moment.md) ^2.22.2
- [through2](https://npm.io/package/through2.md) ^3.0.0
- [commander](https://npm.io/package/commander.md) ^2.19.0
- [gulp-cached](https://npm.io/package/gulp-cached.md) ^1.1.1
- [gulp-replace](https://npm.io/package/gulp-replace.md) ^1.0.0
- [babel-runtime](https://npm.io/package/babel-runtime.md) ^6.26.0
- [detect-import-require](https://npm.io/package/detect-import-require.md) ^2.0.0

## Recent versions

- 1.0.20 (latest) — 2019-05-06
- 1.0.19 — 2019-04-15
- 1.0.18 — 2019-03-06
- 1.0.16 — 2019-03-06
- 1.0.15 — 2019-03-05
- 1.0.14 — 2019-03-05
- 1.0.13 — 2019-03-05
- 1.0.12 — 2019-01-02
- 1.0.11 — 2019-01-02
- 1.0.10 — 2018-12-24
- 1.0.9 — 2018-12-20
- 1.0.8 — 2018-12-20
- 1.0.6 — 2018-11-20
- 1.0.5 — 2018-11-14
- 1.0.4 — 2018-11-13
- … 3 more at https://npm.io/package/kgr/versions

## README

# kgr
> 项目修剪工具 , 输出项目前执行修剪工作 , 文件增删改 , 内容正则替换

### install
```bash
npm install kgr
```
```bash
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`不存在,只做提示不做任何处理



### 配置文件示例
```js
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: []
        }
    ]
}
```


### 注意
1. 请确保有`git`仓库的clone 权限
2. 避免加入大文件 `replace` , 可使用golb 忽略
3. `pipe`使用方法, 请参考 [`gulp-replace`](https://www.npmjs.com/package/gulp-replace) , 参数以数组方式传入
4. 请确保运行环境 存在 `git` , `tar` 命令
5. output 目录在copy时会被清空

---
_Source: https://npm.io/package/kgr · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
