rigjs 2.1.28 → 3.0.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/README.md +146 -183
  2. package/README_CN.md +151 -137
  3. package/built/index.js +143 -173
  4. package/demo/package.json +8 -6
  5. package/demo/package.rig.json5 +64 -10
  6. package/demo/rig_dev/.gitkeep +0 -0
  7. package/demo/rig_helper.js +5 -9
  8. package/demo/vue.config.js +1 -1
  9. package/doc/cicd_cn.md +120 -0
  10. package/doc/cmd_cn.md +0 -0
  11. package/doc/dependencies.md +0 -0
  12. package/doc/dependencies_cn.md +28 -0
  13. package/doc/share_cn.md +8 -0
  14. package/jest/test.rig.json5 +14 -0
  15. package/jest.config.ts +10 -1
  16. package/lib/add/index.ts +37 -0
  17. package/lib/build/build.md +6 -0
  18. package/lib/build/index.ts +64 -59
  19. package/lib/classes/RigConfig.test.ts +14 -0
  20. package/lib/classes/RigConfig.ts +191 -0
  21. package/lib/classes/cicd/CICD.ts +6 -7
  22. package/lib/classes/cicd/CICDCmd.ts +1 -0
  23. package/lib/classes/cicd/DirLevel.ts +1 -1
  24. package/lib/classes/cicd/Endpoint.ts +7 -1
  25. package/lib/classes/dependencies/Dep.ts +56 -0
  26. package/lib/dev/index.ts +44 -0
  27. package/lib/init/index.ts +118 -0
  28. package/lib/install/{index.js → index.ts} +3 -6
  29. package/lib/postinstall/{index.js → index.ts} +12 -20
  30. package/lib/preinstall/index.ts +78 -0
  31. package/lib/print/index.ts +38 -0
  32. package/lib/publish/index.ts +4 -4
  33. package/lib/rig/index.ts +29 -13
  34. package/lib/sync/index.test.ts +4 -0
  35. package/lib/sync/index.ts +32 -0
  36. package/lib/utils/fsHelper.ts +37 -1
  37. package/lib/utils/objectHelper.ts +12 -0
  38. package/lib/utils/regexHelper.test.ts +12 -0
  39. package/lib/utils/{regex.ts → regexHelper.ts} +4 -1
  40. package/package.json +10 -8
  41. package/package.rig.json5 +8 -0
  42. package/tsconfig.json +4 -2
  43. package/demo/cicd.rig.json5 +0 -56
  44. package/demo/yarn.lock +0 -6068
  45. package/lib/init/cicd.rig.json5 +0 -11
  46. package/lib/init/index.d.ts +0 -6
  47. package/lib/init/index.js +0 -189
  48. package/lib/init/rig_helper.js +0 -14
  49. package/lib/install/index.d.ts +0 -5
  50. package/lib/postinstall/index.d.ts +0 -6
  51. package/lib/preinstall/index.d.ts +0 -7
  52. package/lib/preinstall/index.js +0 -198
  53. package/lib/preinstall/index.test.js +0 -16
  54. package/lib/print/index.js +0 -33
@@ -1,11 +0,0 @@
1
- {
2
- tree_schema: '{app}/{env}/{oem}',
3
- source: {
4
- root_path: 'dist',
5
- },
6
- target: {},
7
- endpoints: {
8
-
9
- },
10
- groups: [],
11
- }
@@ -1,6 +0,0 @@
1
- declare namespace init {
2
- const name: string;
3
-
4
- function load(): void;
5
- }
6
- export default init;
package/lib/init/index.js DELETED
@@ -1,189 +0,0 @@
1
- /**
2
- * @ignore
3
- * @Description nothing
4
- * @author Wang Bo (ralwayne@163.com)
5
- * @date 2020/10/9 6:14 PM
6
- */
7
- const fs = require('fs');
8
- const json5 = require('json5');
9
- const chalk = require('chalk');
10
- let log = console.log;
11
- let green = chalk.green;
12
- let redBright = chalk.redBright;
13
-
14
- //加载命令控制器
15
-
16
- const load = async () => {
17
- try {
18
- console.log(chalk.blue('exec init'));
19
- const pkg = JSON.parse(fs.readFileSync(`${process.cwd()}/package.json`));
20
-
21
-
22
-
23
- //检查当前目录是否存在package.json
24
- if (!(fs.existsSync('package.json') && fs.lstatSync('package.json').isFile())) {
25
- console.log(chalk.red('Please run this in the root directory of project!Must have a validate package.json'));
26
- return;
27
- }
28
- //检查是否存在package.rig.json5
29
- if (fs.existsSync('package.rig.json5')) {
30
- console.log(chalk.green('package.rig.json5 already exists~'));
31
- } else {
32
- //创建package.rig.json5.json5
33
- let template = `[
34
- // {
35
- // name: "your project name",
36
- // source: "git ssh url",
37
- // version: "semver version(like 1.0.0)",
38
- // },
39
- ]
40
- `
41
- console.log(chalk.green('create package.rig.json5'));
42
- fs.writeFileSync('./package.rig.json5', template);
43
- }
44
- if (fs.existsSync(`${process.cwd()}/rig_helper.js`)) {
45
- console.log(chalk.green('rig_helper.js already exists~'));
46
- } else {
47
- console.log(chalk.green('create rig_helper.js'));
48
- let template = `const json5 = require('json5');
49
- const fs = require('fs');
50
- const getPkgs = () => {
51
- \tlet pkgArr = json5.parse(fs.readFileSync('./package.rig.json5'));
52
- \tlet flatPkgArr = pkgArr.map((item, index) => {
53
- \t\treturn item.name;
54
- \t});
55
- \tconsole.log(flatPkgArr);
56
- \treturn flatPkgArr;
57
- }
58
-
59
- module.exports = {
60
- \tgetPkgs
61
- }
62
- `
63
- fs.writeFileSync(`${process.cwd()}/rig_helper.js`, template);
64
- }
65
- //检查是否存在cicd.rig.json5
66
- if (fs.existsSync(`${process.cwd()}/cicd.rig.json5`)) {
67
- console.log(chalk.green('cicd.rig.json5 already exists~'));
68
- } else {
69
- console.log(chalk.green('create cicd.rig.json5'));
70
- let template = `{
71
- tree_schema: '{app}/{env}/{oem}',
72
- source: {
73
- root_path: 'dist',
74
- },
75
- target: {},
76
- endpoints: {},
77
- groups: [],
78
- }
79
- `;
80
- fs.writeFileSync(`${process.cwd()}/cicd.rig.json5`, template);
81
-
82
- }
83
- //检查是否存在env.rig.json5
84
- if (fs.existsSync(`${process.cwd()}/env.rig.json5`)) {
85
- console.log(chalk.green('env.rig.json5 already exists~'));
86
- } else {
87
- console.log(chalk.green('create env.rig.json5'));
88
- let template = `{}`;
89
- fs.writeFileSync(`${process.cwd()}/cicd.rig.json5`, template);
90
- }
91
- //检查rigs是否存在
92
- if (fs.existsSync('./rigs') && fs.lstatSync('./rigs').isDirectory()) {
93
- console.log(chalk.green('folder rigs already exists~'));
94
- } else {
95
- console.log(chalk.green('create folder rigs'));
96
- fs.mkdirSync('rigs');
97
- fs.writeFileSync('rigs/.gitkeep', '')
98
-
99
- }
100
- //检查rigs_dev是否存在
101
- // if (fs.existsSync('./rigs_dev') && fs.lstatSync('./rigs_dev').isDirectory()) {
102
- // console.log(green('folder rigs_dev already exists~'));
103
- // } else {
104
- // console.log(green('create folder rigs_dev'));
105
- // fs.mkdirSync('rigs_dev');
106
- // fs.writeFileSync('rigs_dev/.gitkeep', '')
107
- // }
108
- //填充gitignore
109
- let rigIgnoreStrArr = [
110
- 'rigs/*',
111
- 'rigs_dev/*',
112
- '.env.rig',
113
- '!rigs/.gitkeep',
114
- '!rigs_dev/.gitkeep'
115
- ];
116
- let gitignoreStr = ''
117
- if (fs.existsSync('.gitignore')) {
118
- gitignoreStr = fs.readFileSync('.gitignore').toString();
119
- }
120
- for (let i of rigIgnoreStrArr) {
121
- if (gitignoreStr.indexOf(i) > -1) {
122
- console.log(green(`${i} already in .gitignore`))
123
- } else {
124
- console.log(green(`append ${i} to .gitignore`));
125
- gitignoreStr += '\n' + i;
126
- }
127
- }
128
- fs.writeFileSync('.gitignore', gitignoreStr);
129
- //modify package.json
130
- let pkgJSONStr = fs.readFileSync('./package.json').toString();
131
- let pkgJSON = JSON.parse(pkgJSONStr);
132
- let inserted = {
133
- private: true,
134
- workspaces: [
135
- "rigs/*",
136
- "rigs_dev/*"
137
- ],
138
- scripts: {
139
- preinstall: "rig preinstall",
140
- postinstall: "rig postinstall",
141
- },
142
- devDependencies: {
143
- json5: '2.1.3'
144
- }
145
- }
146
- pkgJSON.private = inserted.private;
147
- //初始化workspaces
148
- if (pkgJSON.workspaces && pkgJSON.workspaces instanceof Array) {
149
- if (pkgJSON.workspaces.indexOf('rigs/*') === -1) {
150
- pkgJSON.workspaces.push('rigs/*')
151
- }
152
- if (pkgJSON.workspaces.indexOf('rigs_dev/*') === -1) {
153
- pkgJSON.workspaces.push('rigs_dev/*')
154
- }
155
-
156
- } else {
157
- pkgJSON.workspaces = inserted.workspaces
158
- }
159
- //初始化pre/post-install
160
- if (pkgJSON.scripts && pkgJSON.scripts instanceof Object) {
161
- if (pkgJSON.scripts.preinstall && pkgJSON.scripts.preinstall.indexOf(inserted.scripts.preinstall) < 0) {
162
- pkgJSON.scripts.preinstall = `${pkgJSON.scripts.preinstall} && ${inserted.scripts.preinstall}`
163
- } else {
164
- pkgJSON.scripts.preinstall = inserted.scripts.preinstall;
165
- }
166
- if (pkgJSON.scripts.postinstall && pkgJSON.scripts.postinstall.indexOf(inserted.scripts.postinstall) < 0) {
167
- pkgJSON.scripts.postinstall = `${pkgJSON.scripts.postinstall} && ${inserted.scripts.postinstall}`
168
- } else {
169
- pkgJSON.scripts.postinstall = inserted.scripts.postinstall;
170
- }
171
- } else {
172
- pkgJSON.scripts = inserted.scripts
173
- }
174
- if (pkgJSON.devDependencies) {
175
- //TODO:可优化
176
- pkgJSON.devDependencies.json5 = inserted.devDependencies.json5;
177
- } else {
178
- pkgJSON.devDependencies = inserted.devDependencies;
179
- }
180
- fs.writeFileSync('package.json', JSON.stringify(pkgJSON, null, 2));
181
- log(green('package.json updated'))
182
- } catch (e) {
183
- log(redBright(e.message));
184
- }
185
- }
186
- module.exports = {
187
- name: 'init',
188
- load
189
- }
@@ -1,14 +0,0 @@
1
- const json5 = require('json5');
2
- const fs = require('fs');
3
- const getPkgs = () => {
4
- let pkgArr = json5.parse(fs.readFileSync('./package.rig.json5'));
5
- let flatPkgArr = pkgArr.map((item, index) => {
6
- return item.name;
7
- });
8
- console.log(flatPkgArr);
9
- return flatPkgArr;
10
- }
11
-
12
- module.exports = {
13
- getPkgs
14
- }
@@ -1,5 +0,0 @@
1
- declare namespace install {
2
- function load(): void;
3
-
4
- }
5
- export default install;
@@ -1,6 +0,0 @@
1
- declare namespace postinstall {
2
- function load(): void;
3
- const name: string;
4
-
5
- }
6
- export default postinstall;
@@ -1,7 +0,0 @@
1
- declare namespace preinstall {
2
- function load(): void;
3
- const name: string;
4
- const checkDepsValid: (rigJSON5:any) => void
5
-
6
- }
7
- export default preinstall;
@@ -1,198 +0,0 @@
1
- /**
2
- * @ignore
3
- * @Description nothing
4
- * @author Wang Bo (ralwayne@163.com)
5
- * @date 2020/10/9 6:14 PM
6
- */
7
- const shell = require('shelljs');
8
- const fs = require('fs');
9
- const json5 = require('json5');
10
- const print = require('../print');
11
- const path = require('path');
12
- const compareVersions = require('compare-versions');
13
-
14
- // let semverReg = /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/;
15
- let gitUrlReg = /(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\.git)(\/?|\#[-\d\w._]+?)$/;
16
- const validateName = (name) => {
17
- if (name) {
18
- return true;
19
- } else {
20
- print.error(`name value(${name}) invalidate!`);
21
- return false;
22
- }
23
- }
24
- const validateSource = (source) => {
25
- let isValidate;
26
- try {
27
- isValidate = gitUrlReg.test(source);
28
- } catch (e) {
29
- isValidate = false;
30
- print.error(e.message);
31
- }
32
- if (!isValidate) {
33
- print.error(`source value(${source}) invalidate!`);
34
- }
35
- return isValidate;
36
- }
37
- const validateVersion = (version) => {
38
- let isValidate;
39
- try {
40
- isValidate = version.length > 0;
41
- } catch (e) {
42
- isValidate = false;
43
- print.error(e.message);
44
- }
45
- if (!isValidate) {
46
- print.error(`version value(${version}) invalidate!`);
47
- }
48
- return isValidate;
49
- }
50
- const validate = (rigJson5) => {
51
- let isValidate = true;
52
- try {
53
- for (let dep of rigJson5) {
54
- if (!(validateName(dep.name) && validateSource(dep.source) && validateVersion(dep.version))) {
55
- isValidate = false;
56
- print.error(`!INVALID CONFIG!:${JSON.stringify(dep)}`);
57
- break;
58
- }
59
- }
60
- } catch (e) {
61
- print.error(e.message);
62
- isValidate = false;
63
- }
64
- if (!isValidate) {
65
- print.info(`Visit https://github.com/FlashHand/rig for documentation`);
66
- }
67
- return isValidate;
68
- }
69
- const clone = (target, dep) => {
70
- print.info(`cloning ${dep.name}`);
71
- let cloneProcess = shell.exec(`git clone ${dep.source} ${target}/${dep.name}`,
72
- {silent: true}
73
- );
74
- if (cloneProcess.stderr && !fs.existsSync(`${target}/${dep.name}`)) {
75
- print.error(`clone failed:${dep.source}`);
76
- print.error(cloneProcess.stderr);
77
- process.exit(1);
78
- }
79
- }
80
- const checkDepsValid = (rigJson5) => {
81
- print.info(`checkDepsValid`);
82
- let valid = true;
83
- for (let rig of rigJson5) {
84
- try {
85
- const cmd = `git fetch ${rig.source} refs/tags/${rig.version} && git show FETCH_HEAD:package.json`;
86
- console.log(cmd);
87
- let showPackageProcess = shell.exec(cmd,
88
- {silent: true}
89
- );
90
- let pkgStr = showPackageProcess.stdout.trim();
91
- const pkg = JSON.parse(pkgStr);
92
- //获取rig依赖
93
- if (pkg.rig) {
94
- const rigConfig = pkg.rig;
95
- //遍历rig依赖
96
- Object.keys(rigConfig).forEach(key => {
97
- //获取该rig依赖的要求的版本范围
98
- let max = '';
99
- let min = '';
100
- if (Array.isArray(rigConfig[key])) {
101
- //使用数组的情况
102
- if (rigConfig[key].length !== 2) {
103
- print.error('array\'s length must equal to 2!');
104
- valid = false;
105
- } else {
106
- min = rigConfig[key][0]
107
- max = rigConfig[key][1];
108
- }
109
- } else {
110
- //使用字典的情况
111
- min = rigConfig[key]['min'] || '';
112
- max = rigConfig[key]['max'] || '';
113
- }
114
-
115
- //从rigJson5获取该依赖的版本号
116
- const dep = rigJson5.find(r => r.name === key);
117
- print.info(`checking: ${key}[${min},${max}]`);
118
- if (dep) {
119
- if (min) {
120
- valid = compareVersions(dep.version, min) >= 0
121
- }
122
- if (max) {
123
- valid = compareVersions(dep.version, max) <= 0
124
- }
125
- if (!valid){
126
- print.error(`${rig.name} requires ${key}[${min},${max}],but ${key} is ${dep.version}!`);
127
- }
128
- } else {
129
- print.error(`${key}[${min},${max}] is required`);
130
- valid = false;
131
- }
132
- })
133
- }
134
- } catch (e) {
135
- print.error(`checkDepsValid failed:${e.message}`);
136
- valid = false;
137
- }
138
- }
139
- return valid;
140
- }
141
- //加载命令控制器
142
- const load = async (cmd) => {
143
- print.info('start rig preinstall');
144
- try {
145
- //读取package.rig.json5
146
- let rigJson5Str = fs.readFileSync(path.join(process.cwd(), 'package.rig.json5'));
147
- let rigJson5 = json5.parse(rigJson5Str);
148
- if (!validate(rigJson5)) setTimeout(()=>{process.exit(1)},200)
149
- if (!checkDepsValid(rigJson5)) setTimeout(()=>{process.exit(1)},200)
150
- if (!(fs.existsSync('./rigs') && fs.lstatSync('./rigs').isDirectory())) {
151
- print.info('create folder rigs');
152
- fs.mkdirSync('rigs');
153
- fs.writeFileSync('rigs/.gitkeep', '')
154
- }
155
- shell.chmod('777', 'rigs');
156
- let target = 'rigs';
157
- let pkgJson = JSON.parse(fs.readFileSync('package.json').toString());
158
- let dependencies = pkgJson['dependencies'];
159
- /**
160
- * 核心原则
161
- * 1. install 不应该覆盖或删除已经clone到rigs下的模块,由开发者自己选择要不要删
162
- * 2. rigs下的模块更新,由开发者自己git操作解决。
163
- */
164
- for (let dep of rigJson5) {
165
- if (dep.dev) {
166
- //不去覆盖已下载的库
167
- if (fs.existsSync(`${target}/${dep.name}`)) {
168
- print.info(`${dep.name} already exists.`);
169
- } else {
170
- clone(target, dep);
171
- }
172
- } else {
173
- //不是开发中状态,不处理,不去删除已下载的模块
174
- //预安装时在node_modules中要先清除json5里的库
175
- if (fs.existsSync(`node_modules/${dep.name}`)) {
176
- shell.rm('-rf', `node_modules/${dep.name}`);
177
- }
178
- if (fs.existsSync(`node_modules/.yarn-integrity`)) {
179
- shell.rm('-rf', `node_modules/.yarn-integrity`);
180
- }
181
- }
182
- dependencies[dep.name] = `git+ssh://${dep.source}#${dep.version}`
183
- }
184
- //覆盖package.json
185
- pkgJson.dependencies = dependencies;
186
- fs.writeFileSync('package.json', JSON.stringify(pkgJson, null, 2));
187
- //收尾
188
- print.succeed(`preinstall SUCCEED! Will do "yarn install"`);
189
- //远程链接设置完成,开发库设置完成,准备执行yarn install
190
- } catch (e) {
191
- print.error(e.message);
192
- }
193
- }
194
- module.exports = {
195
- name: 'install',
196
- load,
197
- checkDepsValid
198
- }
@@ -1,16 +0,0 @@
1
- const checkDepsValid = require('./index').checkDepsValid;
2
- const testRigPkgArr = [
3
- {
4
- name: "rig-test-1",
5
- source: "git@github.com:FlashHand/rig-test-1.git",
6
- version: "1.0.1",
7
- },
8
- {
9
- name: "rig-test-2",
10
- source: "git@github.com:FlashHand/rig-test-2.git",
11
- version: "1.0.2",
12
- },
13
- ];
14
- test('checkDepsValid', () => {
15
- expect(checkDepsValid(testRigPkgArr)).toBe(true);
16
- });
@@ -1,33 +0,0 @@
1
- /**
2
- * @ignore
3
- * @Description nothing
4
- * @author Wang Bo (ralwayne@163.com)
5
- * @date 2020/10/20 7:32 PM
6
- */
7
- const ora = require('ora');
8
- const chalk = require('chalk');
9
-
10
- let green = chalk.greenBright;
11
-
12
- let red = chalk.redBright;
13
- let yellow = chalk.yellowBright;
14
- let printer = ora();
15
- const info = (str)=>{
16
- printer.info(green(str));
17
- }
18
- const error = (str)=>{
19
- printer.fail(red(str));
20
- }
21
- const warn = (str)=>{
22
- printer.warn(yellow(str));
23
- }
24
- const succeed = (str)=>{
25
- printer.succeed(green(str));
26
- }
27
-
28
- module.exports = {
29
- info,
30
- warn,
31
- error,
32
- succeed
33
- }