rigjs 2.0.0-alpha.2 → 2.0.0-alpha.22

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.
@@ -12,26 +12,27 @@
12
12
  access_secret: '${as}',
13
13
  root_path: '/',
14
14
  uri_rewrite: {
15
- original: 'view-fp',
15
+ original: '/',
16
16
  }
17
17
  },
18
18
  endpoints: {
19
19
  'ykp/test/oem1': {
20
- build: 'yarn build:oem1',
20
+ build: 'cross-env PUBLIC_PATH=$public_path npx vue-cli-service build --mode oem1',
21
21
  defines: {
22
22
  NGINX_REPLACE_B: 'http://',
23
23
  NGINX_REPLACE_A: '替换了',
24
+ RIG_REPLACE_PUBLIC: '$public_path',
24
25
  },
25
- domain: 'ci-test.rys.com',
26
+ domain: 'cdn.rys.com',
26
27
  },
27
28
  'ykp/test/oem2': {
28
29
  // build: 'yarn build:test:oem2',
29
- build: 'yarn build:oem2',
30
+ build: 'cross-env PUBLIC_PATH=$public_path npx vue-cli-service build --mode oem2',
30
31
  defines: {
31
32
  NGINX_REPLACE_B: 'http://',
32
33
  NGINX_REPLACE_A: '替换了',
33
34
  },
34
- domain: 'ci-test.rys.com'
35
+ domain: 'cdn.rys.com'
35
36
  },
36
37
  },
37
38
  groups: [
package/demo/package.json CHANGED
@@ -13,7 +13,9 @@
13
13
  },
14
14
  "dependencies": {
15
15
  "core-js": "^3.8.3",
16
- "vue": "^2.6.14"
16
+ "vue": "^2.6.14",
17
+ "rig-test-1": "git+ssh://git@github.com:FlashHand/rig-test-1.git#1.0.1",
18
+ "rig-test-2": "git+ssh://git@github.com:FlashHand/rig-test-2.git#1.0.1"
17
19
  },
18
20
  "devDependencies": {
19
21
  "@babel/core": "^7.12.16",
@@ -1,7 +1,12 @@
1
1
  [
2
- // {
3
- // name: "your project name",
4
- // source: "git ssh url",
5
- // version: "semver version(like 1.0.0)",
6
- // },
2
+ {
3
+ name: "rig-test-1",
4
+ source: "git@github.com:FlashHand/rig-test-1.git",
5
+ version: "1.0.1",
6
+ },
7
+ {
8
+ name: "rig-test-2",
9
+ source: "git@github.com:FlashHand/rig-test-2.git",
10
+ version: "1.0.1",
11
+ },
7
12
  ]
package/demo/src/App.vue CHANGED
@@ -2,6 +2,7 @@
2
2
  <div id="app">
3
3
  <img alt="Vue logo" src="./assets/logo.png">
4
4
  <HelloWorld msg="Welcome to Your Vue.js App"/>
5
+ <span>test_replace:{{ test_replace}}</span>
5
6
  </div>
6
7
  </template>
7
8
 
@@ -10,6 +11,11 @@ import HelloWorld from './components/HelloWorld.vue'
10
11
 
11
12
  export default {
12
13
  name: 'App',
14
+ data() {
15
+ return {
16
+ test_replace: 'NGINX_REPLACE_A'
17
+ }
18
+ },
13
19
  components: {
14
20
  HelloWorld
15
21
  }
@@ -1,9 +1,34 @@
1
1
  import fsHelper from '../utils/fsHelper';
2
- import CICD from '@/classes/cicd/CICD';
2
+ import CICD, {Define} from '@/classes/cicd/CICD';
3
3
  import CICDCmd from '@/classes/cicd/CICDCmd';
4
4
  import shell from 'shelljs';
5
5
  import path from 'path';
6
+ import fs from 'fs';
6
7
 
8
+ const JSON5 = require('json5');
9
+
10
+ const replaceDefine = (target: string, defines?: Define) => {
11
+ const dirs = fs.readdirSync(target);
12
+ for (let dir of dirs) {
13
+ const stat = fs.statSync(path.join(target, dir));
14
+ if (stat.isDirectory()) {
15
+ replaceDefine(path.join(target, dir), defines);
16
+ } else {
17
+ if (defines) {
18
+ const namePieces = dir.split('.');
19
+ const fileType = namePieces[namePieces.length - 1];
20
+ if (['js', 'ts','html'].indexOf(fileType) >= 0) {
21
+ let file = fs.readFileSync(path.join(target, dir)).toString();
22
+ const replaceArr = Object.keys(defines);
23
+ for (let replace of replaceArr) {
24
+ file = file.replace(new RegExp(replace, 'g'), defines[replace] as string);
25
+ }
26
+ fs.writeFileSync(path.join(target, dir), file);
27
+ }
28
+ }
29
+ }
30
+ }
31
+ }
7
32
  export default async (cmd: any) => {
8
33
  //create cicd object
9
34
  const cicd = CICD.createByDefault(cmd);
@@ -12,10 +37,21 @@ export default async (cmd: any) => {
12
37
  console.log(cicd)
13
38
  //build by cicdCmd and cicdConfig
14
39
  console.log(cicdCmd.endpoints);
40
+ const regexPublicPath = new RegExp('\\$public_path', 'g');
15
41
  for (let i = 0; i < cicdCmd.endpoints.length; i++) {
16
42
  const ep = cicdCmd.endpoints[i];
17
- const cmdStr = `cross-env PUBLIC_PATH=${path.join('/',ep.deployDir)} ${ep.build}`;
18
- console.log(cmdStr);
19
- shell.exec(cmdStr);
43
+
44
+ ep.build = ep.build.replace(regexPublicPath, ep.publicPath);
45
+ try {
46
+ //替换define中的$public_path
47
+ Object.keys(ep.defines).forEach(key=>{
48
+ ep.defines[key] = ep.defines[key].replace(regexPublicPath, ep.publicPath);
49
+ })
50
+ } catch (e) {
51
+ console.log('JSON5 error:', ep.defines,e.message);
52
+ }
53
+ console.log('exec build:', ep, ep.build, ep.defines);
54
+ shell.exec(ep.build);
55
+ replaceDefine(path.join(cicd.source.root_path, ep.dir), ep.defines);
20
56
  }
21
57
  }
@@ -18,7 +18,7 @@ interface DeploySource {
18
18
  /**
19
19
  * Deploy target
20
20
  */
21
- interface DeployTarget {
21
+ export interface DeployTarget {
22
22
  id: string;
23
23
  type: CloudType;
24
24
  bucket: string;
@@ -28,6 +28,7 @@ interface DeployTarget {
28
28
  root_path: '/';
29
29
  uri_rewrite: {
30
30
  original: string;
31
+ original_regexp: string;
31
32
  final?: string;
32
33
  };
33
34
  }
@@ -55,6 +55,7 @@ class CICDCmd {
55
55
  const sufDir = this.dirStrArr.slice(this.dirInSchemaStrArr.length, this.dirStrArr.length).join('/');
56
56
  this.endpoints = this.endpoints.map(ep => {
57
57
  ep.deployDir = path.join(ep.deployDir, sufDir);
58
+ ep.publicPath = ep.deployDir;
58
59
  return ep;
59
60
  });
60
61
  }
@@ -1,20 +1,22 @@
1
- import aliOSS from "ali-oss";
2
- import fs from "fs";
1
+ import aliOSS from 'ali-oss';
2
+ import fs from 'fs';
3
+ import { DeployTarget } from '../CICD';
4
+ import os from 'os';
3
5
  class AliOSS {
4
6
  ossClient: aliOSS;
5
- constructor(accessKeyId: string, accessKeySecret: string, region: string, bucket: string) {
7
+ constructor(target: DeployTarget) {
6
8
  this.ossClient = new aliOSS({
7
- region,
8
- accessKeyId: accessKeyId,
9
- accessKeySecret: accessKeySecret,
10
- bucket,
9
+ region: target.region,
10
+ accessKeyId: target.access_key,
11
+ accessKeySecret: target.access_secret,
12
+ bucket: target.bucket,
11
13
  timeout: 600000,
12
14
  });
13
15
  }
14
16
 
15
17
  private async progress(p: number, filePath: string, ossPath: string) {
16
18
  // 上传进度。
17
- process.stdout.clearLine(-1);
19
+ process.stdout.clearLine(1);
18
20
  process.stdout.cursorTo(0);
19
21
  process.stdout.write(
20
22
  `progress: ${p.toFixed(2)}%, Upload '${filePath}' To OSS_PATH:${ossPath}`
@@ -24,22 +26,40 @@ class AliOSS {
24
26
  public async putStreamFiles(
25
27
  filesList: string[],
26
28
  ossBasePath: string,
27
- dir: string
29
+ dir: string,
30
+ rootPath: string
28
31
  ) {
29
32
  for (let i = 0; i < filesList.length; i++) {
30
- const filePath = filesList[i].split("dist\\")[1];
33
+ let filePath = '';
34
+ if (os.platform() === 'win32') {
35
+ filePath = filesList[i].split(`${rootPath}\\`)[1];
36
+ } else {
37
+ filePath = filesList[i].split(`${rootPath}/`)[1];
38
+ }
31
39
  const ossPath =
32
- ossBasePath + filePath.replace(/\\/g, "/").replace(dir, "");
40
+ ossBasePath + filePath.replace(/\\/g, '/').replace(dir, '');
41
+
42
+ //@ts-ignore
43
+ let options: aliOSS.PutStreamOptions = {
44
+ contentLength: fs.statSync(filesList[i]).size,
45
+ };
46
+ if (filesList[i].includes('index.html')) {
47
+ options = Object.assign({ headers: { 'Cache-Control': 'max-age=0' } });
48
+ }
33
49
  const fileResult = await this.ossClient.putStream(
34
50
  ossPath,
35
- fs.createReadStream(filesList[i])
51
+ fs.createReadStream(filesList[i]),
52
+ options
36
53
  );
54
+ if (fileResult.res.status !== 200) {
55
+ throw new Error('Upload OSS Error');
56
+ }
37
57
  if (fileResult.res.status === 200) {
38
58
  const p = ((i + 1) * 100) / filesList.length;
39
59
  this.progress(p, filesList[i], ossPath);
40
60
  }
41
61
  }
42
- console.log("\n");
62
+ console.log('\n');
43
63
  }
44
64
  }
45
65
 
@@ -1,17 +1,26 @@
1
- import moment from "dayjs";
2
- import qs from "qs";
3
- import crypto from "crypto";
4
- import axios from "axios";
5
- import * as uuid from "uuid";
1
+ import moment from 'dayjs';
2
+ import qs from 'qs';
3
+ import crypto from 'crypto';
4
+ import axios from 'axios';
5
+ import * as uuid from 'uuid';
6
+ import { DeployTarget } from '../CICD';
6
7
 
7
- type TFlag = "break" | "enhance_break" | null;
8
+ type TFlag = 'break' | 'enhance_break' | null;
9
+
10
+ enum CDNInterfaceEnum {
11
+ BatchSetCdnDomainConfig = 'BatchSetCdnDomainConfig', //批量修改域名信息
12
+ RefreshObjectCaches = 'RefreshObjectCaches', //刷新节点上的文件内容
13
+ PushObjectCache = 'PushObjectCache', //预热CDN节点
14
+ DescribeRefreshTaskById = 'DescribeRefreshTaskById', //通过任务编号查询刷新预热任务信息
15
+ DescribeCdnDomainConfigs = 'DescribeCdnDomainConfigs', // 获取加速域名的配置信息
16
+ }
8
17
 
9
18
  class CDN {
10
19
  AccessKeySecret: string;
11
20
  AccessKeyId: string;
12
- constructor(AccessKeyId: string, AccessKeySecret: string) {
13
- this.AccessKeySecret = AccessKeySecret;
14
- this.AccessKeyId = AccessKeyId;
21
+ constructor(target: DeployTarget) {
22
+ this.AccessKeyId = target.access_key;
23
+ this.AccessKeySecret = target.access_secret;
15
24
  }
16
25
  /**
17
26
  * 访问CDN通用接口
@@ -22,31 +31,39 @@ class CDN {
22
31
  private async getCdnData(actionName: string, paramObj: Object) {
23
32
  let config = {
24
33
  Action: actionName,
25
- Format: "JSON",
26
- Version: "2018-05-10",
34
+ Format: 'JSON',
35
+ Version: '2018-05-10',
27
36
  AccessKeyId: this.AccessKeyId,
28
- SignatureMethod: "HMAC-SHA1",
37
+ SignatureMethod: 'HMAC-SHA1',
29
38
  Timestamp: moment().toDate().toISOString(),
30
- SignatureVersion: "1.0",
39
+ SignatureVersion: '1.0',
31
40
  SignatureNonce: uuid.v1(),
32
41
  };
33
42
  config = Object.assign(config, paramObj);
34
43
  let paramConfig = qs.stringify(config, {
35
- sort: (a: number, b: number) => {
44
+ sort: (a: any, b: any) => {
36
45
  return a < b ? -1 : 1;
37
46
  },
38
- charset: "utf-8",
47
+ charset: 'utf-8',
39
48
  });
40
49
 
41
50
  const strSign = `GET&%2F&${encodeURIComponent(paramConfig)}`;
42
- console.log(`strSign: ${strSign}`);
43
- const hmacSha1 = crypto.createHmac("sha1", this.AccessKeySecret);
51
+ // console.log(`strSign: ${strSign}\n`);
52
+ const hmacSha1 = crypto.createHmac('sha1', `${this.AccessKeySecret}&`);
44
53
  hmacSha1.update(strSign);
45
- const signature = hmacSha1.digest("base64");
46
- paramConfig += `&Signature=${signature}`;
54
+ const signature = hmacSha1.digest('base64');
55
+ config = Object.assign(config, {
56
+ Signature: signature,
57
+ });
58
+ paramConfig = qs.stringify(config, {
59
+ sort: (a, b) => {
60
+ return a < b ? -1 : 1;
61
+ },
62
+ charset: 'utf-8',
63
+ format: 'RFC3986',
64
+ });
47
65
 
48
66
  const url = `http://cdn.aliyuncs.com?${paramConfig}`;
49
- console.log(url);
50
67
 
51
68
  const res = await axios.create().get(url);
52
69
  return res.data;
@@ -66,35 +83,42 @@ class CDN {
66
83
  targetUrls: string[],
67
84
  flags: TFlag[]
68
85
  ) {
69
- if (sourceUrls.length !== targetUrls.length) {
70
- throw new Error(`sourceUrls's length not equal targetUrls's length`);
71
- }
72
- const Functions: Object[] = [];
73
- sourceUrls.forEach((item, index) => {
74
- Functions.push({
75
- functionArgs: [
76
- {
77
- argName: "source_url",
78
- argValue: item,
79
- },
80
- {
81
- argName: "target_url",
82
- argValue: targetUrls[index],
83
- },
84
- {
85
- argName: "flag",
86
- argValue: flags[index],
87
- },
88
- ],
89
- functionName: "back_to_origin_url_rewrite",
86
+ try {
87
+ if (sourceUrls.length !== targetUrls.length) {
88
+ throw new Error(`sourceUrls's length not equal targetUrls's length`);
89
+ }
90
+ const Functions: Object[] = [];
91
+ sourceUrls.forEach((item, index) => {
92
+ Functions.push({
93
+ functionArgs: [
94
+ {
95
+ argName: 'source_url',
96
+ argValue: item,
97
+ },
98
+ {
99
+ argName: 'target_url',
100
+ argValue: targetUrls[index],
101
+ },
102
+ {
103
+ argName: 'flag',
104
+ argValue: flags[index],
105
+ },
106
+ ],
107
+ functionName: 'back_to_origin_url_rewrite',
108
+ });
90
109
  });
91
- });
92
110
 
93
- const data = await this.getCdnData("BatchSetCdnDomainConfig", {
94
- DomainNames: domainName,
95
- Functions: JSON.stringify(Functions),
96
- });
97
- return data;
111
+ const data = await this.getCdnData(CDNInterfaceEnum.BatchSetCdnDomainConfig, {
112
+ DomainNames: domainName,
113
+ Functions: JSON.stringify(Functions),
114
+ });
115
+ return data;
116
+ } catch (e) {
117
+ console.error(
118
+ `Error: ${e.response ? JSON.stringify(e.response.data.Message) : e}`
119
+ );
120
+ throw new Error(e.response.data.Message);
121
+ }
98
122
  }
99
123
 
100
124
  /**
@@ -102,12 +126,21 @@ class CDN {
102
126
  * @param {刷新URL, 格式为加速域名或刷新的文件或目录。多个URL之间使用换行符(\n)或(\r\n)分隔} objectPath
103
127
  * @param {刷新的类型 File: 文件; Directory: 目录} objectType
104
128
  */
105
- public async refreshCache(objectPath: string, objectType: string) {
106
- const data = await this.getCdnData("RefreshObjectCaches", {
107
- ObjectPath: objectPath,
108
- ObjectType: objectType ? objectType : undefined,
109
- });
110
- return data;
129
+ public async refreshCache(objectPath: string, objectType?: string) {
130
+ try {
131
+ let param = {
132
+ ObjectPath: objectPath,
133
+ };
134
+ if (objectType) {
135
+ param = Object.assign(param, { ObjectType: objectType });
136
+ }
137
+ const data = await this.getCdnData(CDNInterfaceEnum.RefreshObjectCaches, param);
138
+ return data;
139
+ } catch (e) {
140
+ console.error('Error:');
141
+ console.error(e.response.data.Message);
142
+ throw new Error(e.response.data.Message);
143
+ }
111
144
  }
112
145
 
113
146
  /**
@@ -116,11 +149,49 @@ class CDN {
116
149
  * @returns
117
150
  */
118
151
  async pushCache(objectPath: string) {
119
- const data = await this.getCdnData("PushObjectCache", {
152
+ const data = await this.getCdnData(CDNInterfaceEnum.PushObjectCache, {
120
153
  ObjectPath: objectPath,
121
154
  });
122
155
  return data;
123
156
  }
157
+
158
+ /**
159
+ * 通过任务编号查询刷新预热任务信息
160
+ * @param {支持同时传入多个任务ID,多个任务ID之间用英文逗号(,)分隔,最多支持同时传入10个任务ID} taskIds
161
+ * @returns
162
+ */
163
+ async describeRefreshTaskById(taskIds: string) {
164
+ try {
165
+ const data = await this.getCdnData(CDNInterfaceEnum.DescribeRefreshTaskById, {
166
+ TaskId: taskIds,
167
+ });
168
+ return data;
169
+ } catch (e) {
170
+ console.error('Error:');
171
+ console.error(e.response.data.Message);
172
+ throw new Error(e.response.data.Message);
173
+ }
174
+ }
175
+
176
+ /**
177
+ * 获取加速域名的配置信息
178
+ * @param {加速域名} domainName
179
+ * @param {功能配置ID} configId
180
+ * @returns
181
+ */
182
+ async describeCdnDomainConfigs(domainName: string, configId?: string) {
183
+ try {
184
+ const data = await this.getCdnData(CDNInterfaceEnum.DescribeCdnDomainConfigs, {
185
+ DomainName: domainName,
186
+ ConfigId: configId,
187
+ });
188
+ return data;
189
+ } catch (e) {
190
+ console.error('Error:');
191
+ console.error(e.response.data.Message);
192
+ throw new Error(e.response.data.Message);
193
+ }
194
+ }
124
195
  }
125
196
 
126
197
  export default CDN;
@@ -1,4 +1,4 @@
1
- import {CICDConfig, DefineDict, DirGroup} from './CICD';
1
+ import {CICDConfig, Define, DefineDict, DeployTarget, DirGroup} from './CICD';
2
2
  import {mkdirSync} from 'fs';
3
3
  import DirLevel from '@/classes/cicd/DirLevel';
4
4
 
@@ -6,7 +6,7 @@ interface EndpointInfo {
6
6
  build: string;
7
7
  target: string;
8
8
  domain: string;
9
- define: DefineDict;
9
+ defines: Define;
10
10
  }
11
11
 
12
12
  export interface EndpointDict {
@@ -20,21 +20,25 @@ class Endpoint {
20
20
  target: string;
21
21
  build: string;
22
22
  domain: string;
23
- deployDir: string;
24
- define: DefineDict;
23
+ deployDir: string = '';
24
+
25
+ publicPath: string = '';
26
+ defines: Define;
25
27
 
26
28
 
27
29
  constructor(dir: string, info: EndpointInfo, schema: DirLevel[]) {
28
30
  this.dir = dir;
29
31
  this.deployDir = dir;
32
+ this.publicPath = dir;
30
33
  this.dirStrArr = dir.split('/').filter(d => d.length > 0);
31
34
  this.dirArr = DirLevel.createDirArr(dir, schema);
32
35
  this.target = info.target;
33
36
  this.build = info.build;
34
37
  this.domain = info.domain;
35
- this.define = info.define;
38
+ this.defines = info.defines;
36
39
  }
37
40
 
41
+
38
42
  static createEndpointArr(cicdConfig: CICDConfig, schema: DirLevel[]) {
39
43
  const endpointDict = cicdConfig.endpoints;
40
44
  return Object.keys(endpointDict).map(dir => {
@@ -1,9 +1,9 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import CICD from "@/classes/cicd/CICD";
4
- import CICDCmd from "@/classes/cicd/CICDCmd";
5
- import AliOSS from "@/classes/cicd/Deploy/AliDeploy";
6
- import CDN from "@/classes/cicd/Deploy/CDN";
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import CICD from '@/classes/cicd/CICD';
4
+ import CICDCmd from '@/classes/cicd/CICDCmd';
5
+ import AliOSS from '@/classes/cicd/Deploy/AliDeploy';
6
+ import CDN from '@/classes/cicd/Deploy/CDN';
7
7
 
8
8
  let filesList: string[] = [];
9
9
  const traverseFolder = (url: string) => {
@@ -22,17 +22,19 @@ const traverseFolder = (url: string) => {
22
22
 
23
23
  export default async (cmd: any) => {
24
24
  try {
25
+ console.log('Start Deploy-----');
25
26
  //create cicd object
26
27
  const cicd = CICD.createByDefault(cmd);
27
28
  //construct cmd object
28
29
  const cicdCmd = new CICDCmd(cmd, cicd);
30
+ console.log('cicdCmd:', cicdCmd.endpoints);
29
31
 
30
32
  const target = Array.isArray(cicdCmd.cicd.target)
31
33
  ? cicdCmd.cicd.target[0]
32
34
  : cicdCmd.cicd.target;
33
35
 
34
- const aliOss = new AliOSS(target.access_key, target.access_secret, target.region, target.bucket);
35
- const cdn = new CDN(target.access_key, target.access_secret);
36
+ const aliOss = new AliOSS(target);
37
+ console.log('Please Wait for Upload OSS...');
36
38
  for (let i = 0; i < cicdCmd.endpoints.length; i++) {
37
39
  const distPath = path.join(
38
40
  process.cwd(),
@@ -42,18 +44,14 @@ export default async (cmd: any) => {
42
44
  traverseFolder(distPath);
43
45
  await aliOss.putStreamFiles(
44
46
  filesList,
45
- cicdCmd.endpoints[i].deployDir.replace(/\\/g, "/"),
46
- cicdCmd.endpoints[i].dir
47
+ cicdCmd.endpoints[i].deployDir.replace(/\\/g, '/'),
48
+ cicdCmd.endpoints[i].dir,
49
+ cicd.source.root_path,
47
50
  );
48
-
49
- // await cdn.setRWriteUri(
50
- // cicdCmd.endpoints[i].domain,
51
- // [""],
52
- // [""],
53
- // ["break"]
54
- // );
55
51
  filesList = [];
56
52
  }
53
+ console.log('Upload OSS Done');
54
+ console.log('Deploy Done-----');
57
55
  } catch (e) {
58
56
  throw e;
59
57
  }