rigjs 2.1.29 → 3.0.0-alpha

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 +144 -141
  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/demo/yarn.lock +20 -7
  10. package/doc/cicd_cn.md +120 -0
  11. package/doc/cmd_cn.md +0 -0
  12. package/doc/dependencies.md +0 -0
  13. package/doc/dependencies_cn.md +28 -0
  14. package/doc/share_cn.md +8 -0
  15. package/jest/test.rig.json5 +14 -0
  16. package/jest.config.ts +10 -1
  17. package/lib/add/index.ts +37 -0
  18. package/lib/build/build.md +6 -0
  19. package/lib/build/index.ts +64 -59
  20. package/lib/classes/RigConfig.test.ts +14 -0
  21. package/lib/classes/RigConfig.ts +191 -0
  22. package/lib/classes/cicd/CICD.ts +6 -7
  23. package/lib/classes/cicd/CICDCmd.ts +1 -0
  24. package/lib/classes/cicd/DirLevel.ts +1 -1
  25. package/lib/classes/cicd/Endpoint.ts +7 -1
  26. package/lib/classes/dependencies/Dep.ts +56 -0
  27. package/lib/dev/index.ts +44 -0
  28. package/lib/init/index.ts +118 -0
  29. package/lib/install/{index.js → index.ts} +3 -6
  30. package/lib/postinstall/{index.js → index.ts} +12 -20
  31. package/lib/preinstall/index.ts +78 -0
  32. package/lib/print/index.ts +38 -0
  33. package/lib/publish/index.ts +4 -4
  34. package/lib/rig/index.ts +29 -13
  35. package/lib/sync/index.test.ts +4 -0
  36. package/lib/sync/index.ts +32 -0
  37. package/lib/utils/fsHelper.ts +37 -1
  38. package/lib/utils/objectHelper.ts +12 -0
  39. package/lib/utils/regexHelper.test.ts +12 -0
  40. package/lib/utils/{regex.ts → regexHelper.ts} +4 -1
  41. package/package.json +9 -8
  42. package/package.rig.json5 +8 -0
  43. package/tsconfig.json +4 -2
  44. package/demo/cicd.rig.json5 +0 -56
  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
@@ -0,0 +1,191 @@
1
+ import {Dep} from '@/classes/dependencies/Dep';
2
+ import fsHelper from '@/utils/fsHelper';
3
+ import objectHelper from '@/utils/objectHelper';
4
+ import print from '@/print';
5
+ import shell from 'shelljs';
6
+ import semver from 'semver';
7
+ import compareVersions from 'compare-versions'
8
+
9
+ const JSON5 = require('json5');
10
+
11
+ interface RigConfigJSON5 {
12
+ dependencies: { [name: string]: Dep; };
13
+ share:{ [name: string]: string | string[]; };
14
+ }
15
+
16
+ class RigConfig {
17
+ dependencies: { [name: string]: Dep; } = {};
18
+ isLegacy = false;
19
+ share: { [name: string]: string | string[]; } = {};
20
+
21
+
22
+ constructor(rigConfig: RigConfigJSON5 | Dep[]) {
23
+ if (Array.isArray(rigConfig)) {
24
+ //旧版配置
25
+ this.isLegacy = true;
26
+ const depArrLegacy = rigConfig as Dep[];
27
+ for (let depLegacy of depArrLegacy) {
28
+ const dep = new Dep(depLegacy);
29
+ this.dependencies[dep.name] = dep;
30
+ }
31
+ } else {
32
+ for (let rigName in rigConfig.dependencies) {
33
+ this.dependencies[rigName] = new Dep({...rigConfig.dependencies[rigName], name: rigName})
34
+ }
35
+ this.share = rigConfig.share;
36
+ }
37
+ console.log(this);
38
+ this.validate();
39
+ }
40
+
41
+ /**
42
+ * createFromCWD 从当前工作目录创建config对象
43
+ * @returns {RigConfig}
44
+ */
45
+ static createFromCWD() {
46
+ const configJSON5 = fsHelper.readConfig();
47
+ return new RigConfig(configJSON5);
48
+ }
49
+
50
+ /**
51
+ * createFromPath 从absolute path中创建config对象
52
+ * @param {string} path
53
+ * @returns {RigConfig}
54
+ */
55
+ static createFromPath(path: string) {
56
+ const configJSON5 = fsHelper.readConfig(path);
57
+ return new RigConfig(configJSON5);
58
+ }
59
+
60
+ validate() {
61
+ print.info(`rig validating rig-dependencies...`);
62
+ for (let key in this.dependencies) {
63
+ const ret = this.dependencies[key].validate();
64
+ if (!ret) {
65
+ throw new Error(`rig validating:${this.dependencies[key]} is invalid`);
66
+ }
67
+ }
68
+ }
69
+
70
+ validateDeps() {
71
+ print.info(`rig validating dependencies' requirements`);
72
+ let valid = true;
73
+ for (let rigName in this.dependencies) {
74
+ const rigDep = this.dependencies[rigName];
75
+ try {
76
+ let pkgStr: string;
77
+ if (rigDep.dev) {
78
+ pkgStr = fsHelper.readPkgStrInRepo(rigName);
79
+ print.info(`${rigName} is in deleloping.Validating ${rigName}'s dependencies...`);
80
+ } else {
81
+ const cmd = `git fetch ${rigDep.source} refs/tags/${rigDep.version} && git show FETCH_HEAD:package.json`;
82
+ print.info(`validateDeps:${cmd}`);
83
+ let showPackageProcess = shell.exec(cmd,
84
+ {silent: true}
85
+ );
86
+ pkgStr = showPackageProcess.stdout.trim();
87
+ }
88
+ const pkg = JSON.parse(pkgStr);
89
+ //获取rig依赖
90
+ if (pkg.rig) {
91
+ const pkgRig = pkg.rig;
92
+ //遍历rig依赖
93
+ Object.keys(pkgRig).forEach(key => {
94
+ //获取该rig依赖的要求的版本范围
95
+ let max = '';
96
+ let min = '';
97
+ if (Array.isArray(pkgRig[key])) {
98
+ //使用数组的情况
99
+ if (pkgRig[key].length == 0) {
100
+ //依赖该库,但是无版本要求
101
+ } else if (pkgRig[key].length == 1) {
102
+ min = semver.valid(pkgRig[key][0]) || '';
103
+ } else if (pkgRig[key].length == 2) {
104
+ min = semver.valid(pkgRig[key][0]) || '';
105
+ max = semver.valid(pkgRig[key][1]) || '';
106
+ } else {
107
+ print.error(`invalid array ${JSON.stringify([key])}`);
108
+ valid = false;
109
+ }
110
+ } else {
111
+ //使用字典的情况
112
+ min = pkgRig[key]['min'] || '';
113
+ max = pkgRig[key]['max'] || '';
114
+ }
115
+ //从rigJson5获取该依赖的版本号
116
+ const dep = this.dependencies[key]
117
+ print.info(`checking requirements of ${rigName}: ${key}[${min},${max}]`);
118
+ if (dep) {
119
+ if (dep.dev) {
120
+ valid = true;
121
+ } else {
122
+ if (min) {
123
+ if (!dep.version) throw new Error(`${dep.name}'s version is invalid:${dep.version}`)
124
+ valid = compareVersions(dep.version, min) >= 0;
125
+ }
126
+ if (max) {
127
+ if (!dep.version) throw new Error(`${dep.name}'s version is invalid:${dep.version}`)
128
+ valid = compareVersions(dep.version, max) <= 0
129
+ }
130
+ if (!valid) {
131
+ throw new Error(`${rigName} requires ${key}[${min},${max}],but ${key} is ${dep.version}!`);
132
+ }
133
+ }
134
+ } else {
135
+ print.error(`${key}[${min},${max}] is required`);
136
+ valid = false;
137
+ }
138
+ })
139
+ }
140
+ } catch (e) {
141
+ throw new Error(`rig validateDeps failed:${e.message}`);
142
+ }
143
+ }
144
+ }
145
+
146
+ toString() {
147
+ return JSON5.stringify(this, null, 2);
148
+ }
149
+
150
+ overwrite(overwritePath: string = '') {
151
+ overwritePath ? fsHelper.writeConfig(this, overwritePath) :
152
+ fsHelper.writeConfig({
153
+ dependencies: this.dependencies
154
+ });
155
+ }
156
+
157
+ checkDepExists(name: string) {
158
+ return Object.keys(this.dependencies).find((key) => {
159
+ return name === key;
160
+ }) == name;
161
+ }
162
+
163
+ setDependencyToDev(name: string) {
164
+ this.setDependenciesToDev(name);
165
+ }
166
+
167
+ setDependenciesToDev(names: string | string[]) {
168
+ let nameArr = [];
169
+ if (Array.isArray(names)) {
170
+ nameArr.push(...names);
171
+ } else {
172
+ nameArr.push(names);
173
+ }
174
+ nameArr.forEach(name => {
175
+ this.dependencies[name].dev = true;
176
+ this.dependencies[name] = objectHelper.sortKeys(this.dependencies[name]);
177
+
178
+ });
179
+ }
180
+
181
+ findOrUpsertDep(name: string, dep: Dep) {
182
+ if (this.checkDepExists(name)) {
183
+ this.dependencies[name].dev = true;
184
+ } else {
185
+ this.dependencies[name] = dep;
186
+ }
187
+ this.dependencies[name] = objectHelper.sortKeys(this.dependencies[name]);
188
+ }
189
+ }
190
+
191
+ export default RigConfig;
@@ -1,10 +1,9 @@
1
1
  import DirLevel from '@/classes/cicd/DirLevel';
2
2
  import Endpoint, {EndpointDict} from '@/classes/cicd/Endpoint';
3
3
  import fs from 'fs';
4
+ import qs from 'querystring';
4
5
 
5
6
  const JSON5 = require('json5');
6
- import qs from 'querystring';
7
- import util from 'util';
8
7
 
9
8
  export enum CloudType {
10
9
  alicloud = 'alicloud',
@@ -71,6 +70,7 @@ export interface CICDConfig {
71
70
  * fafafafa
72
71
  */
73
72
  tree_schema: string;
73
+ path_schema: string;
74
74
  web_type: 'hash'|'history'|'mpa';
75
75
  source: DeploySource;
76
76
  target: DeployTarget | DeployTarget[];
@@ -101,7 +101,7 @@ class CICD {
101
101
  groups: DirGroup[];
102
102
 
103
103
  constructor(config: CICDConfig) {
104
- this.treeSchema = config.tree_schema;
104
+ this.treeSchema = config.tree_schema || config.path_schema;
105
105
  this.web_type = config.web_type || 'hash';
106
106
  this.schema = DirLevel.createSchema(this.treeSchema);
107
107
  this.endpoints = Endpoint.createEndpointArr(config, this.schema);
@@ -112,17 +112,16 @@ class CICD {
112
112
 
113
113
  static createByDefault(cmd: any) {
114
114
  //replace params
115
- let cicdStr = fs.readFileSync(`${process.cwd()}/cicd.rig.json5`).toString();
115
+ let pkgStr = fs.readFileSync(`${process.cwd()}/package.rig.json5`).toString();
116
116
  const paramsStr = cmd.params;
117
117
  const params = qs.parse(paramsStr);
118
118
  //替换动态变量
119
119
  Object.keys(params).forEach(key => {
120
120
  const regStr = `\\$\\{${key}\\}`;
121
121
  const regex = new RegExp(regStr, 'g');
122
- cicdStr = cicdStr.replace(regex, params[key] as string);
122
+ pkgStr = pkgStr.replace(regex, params[key] as string);
123
123
  });
124
- const cicd = new CICD(JSON5.parse(cicdStr))
125
- return cicd;
124
+ return new CICD(JSON5.parse(pkgStr).cicd);
126
125
  }
127
126
 
128
127
  matchEndpoints(cmdDirStrArr: string[]) {
@@ -57,6 +57,7 @@ class CICDCmd {
57
57
  if (this.dirStrArr.length > this.dirInSchemaStrArr.length) {
58
58
  const sufDir = this.dirStrArr.slice(this.dirInSchemaStrArr.length, this.dirStrArr.length).join('/');
59
59
  this.endpoints = this.endpoints.map(ep => {
60
+ //TODO:需要重构
60
61
  ep.deployDir = path.join(ep.deployDir, sufDir);
61
62
  ep.publicPath = ep.deployDir;
62
63
  return ep;
@@ -1,4 +1,4 @@
1
- import regex from '../../utils/regex';
1
+ import regex from '../../utils/regexHelper';
2
2
 
3
3
  /**
4
4
  * contain attributes of every level in the directory schema
@@ -1,7 +1,7 @@
1
1
  import { CICDConfig, Define, DefineDict, DeployTarget, DirGroup } from './CICD';
2
2
  import { mkdirSync } from 'fs';
3
3
  import DirLevel from '@/classes/cicd/DirLevel';
4
- //Mapping endpoints in cicd.rig.json5
4
+ //Mapping endpoints in package.rig.json5->cicd
5
5
  interface EndpointInfo {
6
6
  build: string;
7
7
  target: string;
@@ -68,6 +68,12 @@ class Endpoint {
68
68
 
69
69
  }
70
70
 
71
+ /**
72
+ *
73
+ * @param {string[]} dirSchemaStrArr split tree_schema string into array
74
+ * @param {DirGroup[]} groups
75
+ * @returns {boolean}
76
+ */
71
77
  matchCmd(dirSchemaStrArr: string[], groups: DirGroup[]) {
72
78
  if (this.dirStrArr.length < dirSchemaStrArr.length) {
73
79
  return false;
@@ -0,0 +1,56 @@
1
+ import print from '@/print';
2
+ import semver from 'semver';
3
+ let gitUrlReg = /(?:git|ssh|https?|git@[-\w.]+):(\/\/)?(.*?)(\.git)(\/?|\#[-\d\w._]+?)$/;
4
+
5
+ export class Dep {
6
+ version?: string
7
+ name: string;
8
+ source: string;
9
+ dev = false;
10
+
11
+ constructor(props: Partial<Dep>) {
12
+ if (!props.name) {
13
+ throw new Error(`Dep's name should not be ${props.name}.`);
14
+ }
15
+ if (!props.source) {
16
+ throw new Error(`Dep's source should not be ${props.source}.'`);
17
+ }
18
+ this.name = props.name!;
19
+ this.source = props.source!;
20
+ this.version = props.version;
21
+ this.dev = props.dev || false;
22
+ }
23
+ validateName(){
24
+ if (this.name) {
25
+ return true;
26
+ } else {
27
+ print.error(`name value(${this.name}) invalid`);
28
+ return false;
29
+ }
30
+ }
31
+ validateSource(){
32
+ let isValid;
33
+ try {
34
+ isValid = gitUrlReg.test(this.source);
35
+ } catch (e) {
36
+ isValid = false;
37
+ print.error(e.message);
38
+ }
39
+ if (!isValid) {
40
+ print.error(`source value(${this.source}) invalid!`);
41
+ }
42
+ return isValid;
43
+ }
44
+ validateVersion(){
45
+ let isValid = this.dev?true:!!semver.valid(this.version);
46
+ if (!isValid) {
47
+ print.error(`version field must follow Semantic Versioning 2.0.0(https://semver.org/)`);
48
+ }
49
+ return isValid;
50
+ }
51
+ validate(){
52
+ return this.validateName() && this.validateSource() && this.validateVersion();
53
+ }
54
+
55
+
56
+ }
@@ -0,0 +1,44 @@
1
+ import regexHelper from '../utils/regexHelper';
2
+ import {Dep} from '@/classes/dependencies/Dep';
3
+ import RigConfig from '@/classes/RigConfig';
4
+ import install from '../install';
5
+ import print from '../print';
6
+
7
+ /**
8
+ * @desc dev
9
+ * 模式1: 激活某个rig库的dev模式:修改package.rig.json5->执行install
10
+ * 模式2: 安装rig库的git ssh url,并设为dev模式:修改package.rig.json5->执行install
11
+ * @returns {Promise<void>}
12
+ */
13
+ export default async (cmd: any) => {
14
+ try {
15
+ print.info(`rig dev`);
16
+ const rigConfig = RigConfig.createFromCWD();
17
+ console.log(rigConfig);
18
+ if (rigConfig.isLegacy) throw new Error(`rig dev' does not support legacy config template`);
19
+ const cmdArgs = cmd.args;
20
+ if (regexHelper.gitURL.test(cmdArgs[0])) {
21
+ //传入git url,模式2处理
22
+ const source = cmdArgs[0];
23
+ const gitName = source.match(regexHelper.matchGitName)[2];
24
+ print.info(`add ${gitName}(${source}) to dev`);
25
+ const dep = new Dep({name: gitName, source, dev: true});
26
+ rigConfig.findOrUpsertDep(gitName, dep);
27
+ } else {
28
+ const gitName = cmdArgs[0];
29
+ print.info(`set ${gitName} to dev`);
30
+ //传入git名称,模式1处理
31
+ if (rigConfig.checkDepExists(gitName)) {
32
+ rigConfig.setDependencyToDev(gitName);
33
+ } else {
34
+ throw new Error(`Cannot find ${gitName} in rig-dependencies.See '' to add a rig-dependency.`);
35
+ }
36
+ }
37
+ console.log('do overwritting');
38
+ rigConfig.overwrite();
39
+ console.log('do installing');
40
+ await install()
41
+ } catch (e) {
42
+ throw new Error(`rig dev failed:${e.message}`);
43
+ }
44
+ }
@@ -0,0 +1,118 @@
1
+ /**
2
+ * @ignore
3
+ * @Description nothing
4
+ * @author Wang Bo (ralwayne@163.com)
5
+ * @date 2020/10/9 6:14 PM
6
+ */
7
+ import print from '../print';
8
+ import fs from 'fs';
9
+ import axios from 'axios';
10
+
11
+ export default async () => {
12
+ try {
13
+ print.start('init rig...');
14
+ //检查当前目录是否存在package.json
15
+ if (!(fs.existsSync('package.json') && fs.lstatSync('package.json').isFile())) {
16
+ throw new Error('Please run this in the root directory of project!Must have a validate package.json');
17
+ }
18
+ //检查是否存在package.rig.json5
19
+ if (fs.existsSync('package.rig.json5')) {
20
+ print.info('package.rig.json5 already exists~');
21
+ } else {
22
+ //创建package.rig.json5
23
+ print.info('create package.rig.json5');
24
+ const resPackageRigJSON5 = await axios.get('https://gist.githubusercontent.com/FlashHand/ea156ac4930b05832ad7c568f7f00cdd/raw/48a4321ccf1b0339a15f6454336f289b8b5bad58/package.rig.json5');
25
+ const packageRigJSON5 = resPackageRigJSON5.data;
26
+ fs.writeFileSync('./package.rig.json5', packageRigJSON5);
27
+ }
28
+ //检查是否存在rig_helper.js
29
+ if (fs.existsSync(`${process.cwd()}/rig_helper.js`)) {
30
+ print.info('rig_helper.js already exists~');
31
+ } else {
32
+ print.info('create rig_helper.js');
33
+ const resRigHelper = await axios.get('https://gist.githubusercontent.com/FlashHand/c13135e8b9cd088221fbbfdb51f104ca/raw/baa02b804ee5b1186b06ca172f80b55003317bcc/rigHelper.js');
34
+ const rigHelper = resRigHelper.data;
35
+ fs.writeFileSync(`${process.cwd()}/rig_helper.js`, rigHelper);
36
+ }
37
+ //检查rig_dev是否存在
38
+ if (fs.existsSync('./rig_dev') && fs.lstatSync('./rig_dev').isDirectory()) {
39
+ print.info('folder rig_dev already exists~');
40
+ } else {
41
+ print.info('create folder rig_dev');
42
+ fs.mkdirSync('rig_dev');
43
+ fs.writeFileSync('rig_dev/.gitkeep', '')
44
+ }
45
+
46
+ //填充gitignore
47
+ let rigIgnoreStrArr = [
48
+ 'rigs/*',
49
+ 'rig_dev/*',
50
+ '.env.rig',
51
+ '!rigs/.gitkeep',
52
+ '!rig_dev/.gitkeep'
53
+ ];
54
+ let gitignoreStr = ''
55
+ if (fs.existsSync('.gitignore')) {
56
+ gitignoreStr = fs.readFileSync('.gitignore').toString();
57
+ }
58
+ for (let i of rigIgnoreStrArr) {
59
+ if (gitignoreStr.indexOf(i) > -1) {
60
+ print.info(`${i} already in .gitignore`);
61
+ } else {
62
+ print.info(`append ${i} to .gitignore`);
63
+ gitignoreStr += '\n' + i;
64
+ }
65
+ }
66
+ fs.writeFileSync('.gitignore', gitignoreStr);
67
+ //modify package.json
68
+ let pkgJSONStr = fs.readFileSync('./package.json').toString();
69
+ let pkgJSON = JSON.parse(pkgJSONStr);
70
+ let inserted = {
71
+ private: true,
72
+ workspaces: [
73
+ "rigs/*",
74
+ "rig_dev/*"
75
+ ],
76
+ scripts: {
77
+ preinstall: "rig preinstall",
78
+ postinstall: "rig postinstall",
79
+ },
80
+ devDependencies: {
81
+ json5: '2.2.1'
82
+ }
83
+ }
84
+ pkgJSON.private = inserted.private;
85
+ //初始化workspaces
86
+ if (pkgJSON.workspaces && pkgJSON.workspaces instanceof Array) {
87
+ if (pkgJSON.workspaces.indexOf('rig_dev/*') === -1) {
88
+ pkgJSON.workspaces.push('rig_dev/*')
89
+ }
90
+ } else {
91
+ pkgJSON.workspaces = inserted.workspaces
92
+ }
93
+ //初始化pre/post-install
94
+ if (pkgJSON.scripts && pkgJSON.scripts instanceof Object) {
95
+ if (pkgJSON.scripts.preinstall && pkgJSON.scripts.preinstall.indexOf(inserted.scripts.preinstall) < 0) {
96
+ pkgJSON.scripts.preinstall = `${pkgJSON.scripts.preinstall} && ${inserted.scripts.preinstall}`
97
+ } else {
98
+ pkgJSON.scripts.preinstall = inserted.scripts.preinstall;
99
+ }
100
+ if (pkgJSON.scripts.postinstall && pkgJSON.scripts.postinstall.indexOf(inserted.scripts.postinstall) < 0) {
101
+ pkgJSON.scripts.postinstall = `${pkgJSON.scripts.postinstall} && ${inserted.scripts.postinstall}`
102
+ } else {
103
+ pkgJSON.scripts.postinstall = inserted.scripts.postinstall;
104
+ }
105
+ } else {
106
+ pkgJSON.scripts = inserted.scripts
107
+ }
108
+ if (pkgJSON.devDependencies) {
109
+ pkgJSON.devDependencies.json5 = inserted.devDependencies.json5;
110
+ } else {
111
+ pkgJSON.devDependencies = inserted.devDependencies;
112
+ }
113
+ fs.writeFileSync('package.json', JSON.stringify(pkgJSON, null, 2));
114
+ print.succeed('rig init succeed');
115
+ } catch (e) {
116
+ print.error(e.message);
117
+ }
118
+ };
@@ -5,16 +5,16 @@
5
5
  * @date 2020/10/9 6:14 PM
6
6
  */
7
7
  const shell = require('shelljs');
8
- const print = require('../print')
8
+ import print from '../print';
9
9
  //加载命令控制器
10
- const load = async () => {
10
+ export default async () => {
11
11
  print.info('start installing\n');
12
12
  try {
13
13
  let yarnProcess = shell.exec('yarn install',{silent:true});
14
14
  let stderrStr = yarnProcess.stderr.toString();
15
15
  if (stderrStr && stderrStr.indexOf('postinstall SUCCEED!') >-1) {
16
16
  }else{
17
- throw new Error(stderrStr);
17
+ throw new Error(`rig install failed:${stderrStr}`);
18
18
  }
19
19
  console.log(stderrStr);
20
20
  print.succeed(`rig install SUCCEED!`);
@@ -23,6 +23,3 @@ const load = async () => {
23
23
  print.error(e.message);
24
24
  }
25
25
  }
26
- module.exports = {
27
- load
28
- }
@@ -4,26 +4,24 @@
4
4
  * @author Wang Bo (ralwayne@163.com)
5
5
  * @date 2020/10/9 6:14 PM
6
6
  */
7
- const shell = require('shelljs');
8
- const fs = require('fs');
9
- const json5 = require('json5');
10
- const path = require('path');
11
- const print = require('../print');
12
- const info = require('../info');
7
+ import shell from 'shelljs';
8
+ import fs from 'fs';
9
+ import path from 'path';
10
+ import print from '../print';
11
+ import RigConfig from '@/classes/RigConfig';
13
12
 
14
13
  //link开发库
15
- const load = async (cmd) => {
16
- print.info('start linking dependencies')
14
+ export default async () => {
15
+ print.info('rig postinstall start linking dependencies')
17
16
  try {
18
17
  if (!fs.existsSync('node_modules')) {
19
18
  shell.mkdir('node_modules');
20
19
  }
20
+ const rigConfig = RigConfig.createFromCWD();
21
21
  let rootPath = shell.pwd().stdout;
22
- let rigJson5 = json5.parse(fs.readFileSync('package.rig.json5'));
23
- console.log(rigJson5);
24
22
  let devDeps = [];
25
- for (let dep of rigJson5) {
26
-
23
+ for (let rigName in rigConfig.dependencies) {
24
+ const dep = rigConfig.dependencies[rigName];
27
25
  if (dep.dev) {
28
26
  print.warn(`developing:${dep.name}`);
29
27
  devDeps.push(dep.name);
@@ -31,23 +29,17 @@ const load = async (cmd) => {
31
29
  shell.rm('-rf', `node_modules/${dep.name}`);
32
30
  }
33
31
  fs.symlinkSync(
34
- path.resolve(rootPath, `./rigs/${dep.name}`),
32
+ path.resolve(rootPath, `./rig_dev/${dep.name}`),
35
33
  path.resolve(rootPath, `./node_modules/${dep.name}`),
36
34
  'dir'
37
35
  );
38
36
  }
39
- //TODO:检查依赖间的版本号要求
40
37
  }
41
- info.info();
42
38
  print.succeed(`postinstall SUCCEED!`);
43
39
  if (devDeps.length>0){
44
- print.warn(`developing ${devDeps.length} modules: ${devDeps}.Installed in rigs/.Already linked.`)
40
+ print.warn(`developing ${devDeps.length} modules: ${devDeps}.Installed in rig_dev/.Already linked.`)
45
41
  }
46
42
  } catch (e) {
47
43
  print.error(e.message);
48
44
  }
49
45
  }
50
- module.exports = {
51
- name: 'install',
52
- load
53
- }
@@ -0,0 +1,78 @@
1
+ /**
2
+ * @ignore
3
+ * @Description nothing
4
+ * @author Wang Bo (ralwayne@163.com)
5
+ * @date 2020/10/9 6:14 PM
6
+ */
7
+ import shell from 'shelljs';
8
+ import fs from 'fs';
9
+ import print from '../print';
10
+ import RigConfig from '@/classes/RigConfig';
11
+ import {Dep} from '@/classes/dependencies/Dep';
12
+
13
+ // let semverReg = /^([0-9]+)\.([0-9]+)\.([0-9]+)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+)?$/;
14
+ const clone = (target:string, dep:Dep) => {
15
+ print.info(`cloning ${dep.name}`);
16
+ let cloneProcess = shell.exec(`git clone ${dep.source} ${target}/${dep.name}`,
17
+ {silent: true}
18
+ );
19
+ if (cloneProcess.stderr && !fs.existsSync(`${target}/${dep.name}`)) {
20
+ print.error(`clone failed:${dep.source}`);
21
+ print.error(cloneProcess.stderr);
22
+ process.exit(1);
23
+ }
24
+ }
25
+ //加载命令控制器
26
+ export default async (cmd:any) => {
27
+ print.info('start rig preinstall');
28
+ try {
29
+ //读取package.rig.json5
30
+ const rigConfig = RigConfig.createFromCWD();
31
+ rigConfig.validate();
32
+ rigConfig.validateDeps();
33
+ if (!(fs.existsSync('./rig_dev') && fs.lstatSync('./rig_dev').isDirectory())) {
34
+ print.info('create folder rig_dev');
35
+ fs.mkdirSync('rig_dev');
36
+ fs.writeFileSync('rig_dev/.gitkeep', '')
37
+ }
38
+ shell.chmod('777', 'rig_dev');
39
+ let target = 'rig_dev';
40
+ let pkgJson = JSON.parse(fs.readFileSync('package.json').toString());
41
+ let dependencies = pkgJson['dependencies'];
42
+ /**
43
+ * 核心原则
44
+ * 1. install 不应该覆盖或删除已经clone到rig_dev下的模块,由开发者自己选择要不要删
45
+ * 2. rig_dev下的模块更新,由开发者自己git操作解决。
46
+ */
47
+ for (let rigName in rigConfig.dependencies) {
48
+ const dep = rigConfig.dependencies[rigName];
49
+ if (dep.dev) {
50
+ //不去覆盖已下载的库
51
+ if (fs.existsSync(`${target}/${dep.name}`)) {
52
+ print.info(`${dep.name} already exists.`);
53
+ } else {
54
+ clone(target, dep);
55
+ }
56
+ } else {
57
+ //不是开发中状态,不处理,不去删除已下载的模块
58
+ //预安装时在node_modules中要先清除json5里的库
59
+ if (fs.existsSync(`node_modules/${dep.name}`)) {
60
+ shell.rm('-rf', `node_modules/${dep.name}`);
61
+ }
62
+ if (fs.existsSync(`node_modules/.yarn-integrity`)) {
63
+ shell.rm('-rf', `node_modules/.yarn-integrity`);
64
+ }
65
+ }
66
+ dependencies[dep.name] = `git+ssh://${dep.source}#${dep.version}`
67
+ }
68
+ //覆盖package.json
69
+ pkgJson.dependencies = dependencies;
70
+ fs.writeFileSync('package.json', JSON.stringify(pkgJson, null, 2));
71
+ //收尾
72
+ print.succeed(`preinstall SUCCEED! Will do "yarn install"`);
73
+ //远程链接设置完成,开发库设置完成,准备执行yarn install
74
+ } catch (e) {
75
+ print.error(e.message);
76
+ process.exit(1);
77
+ }
78
+ }