rigjs 2.0.0-alpha.0 → 2.0.0-alpha.11

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.
@@ -1,17 +1,45 @@
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';
7
+ const replaceDefine = (target:string,defines?:Define)=>{
8
+ const dirs = fs.readdirSync(target);
9
+ for (let dir of dirs){
10
+ const stat = fs.statSync(path.join(target, dir));
11
+ if (stat.isDirectory()){
12
+ replaceDefine(path.join(target, dir),defines);
13
+ }else{
14
+ if (defines){
15
+ const namePieces = dir.split('.');
16
+ const fileType = namePieces[namePieces.length - 1];
17
+ if (['js','ts'].indexOf(fileType)>=0){
18
+ let file = fs.readFileSync(path.join(target, dir)).toString();
19
+ const replaceArr = Object.keys(defines);
20
+ for (let replace of replaceArr){
21
+ file = file.replace(new RegExp(replace,'g'),defines[replace] as string);
22
+ }
23
+ fs.writeFileSync(path.join(target, dir),file);
24
+ }
25
+ }
26
+ }
27
+ }
28
+ }
6
29
  export default async (cmd: any) => {
7
30
  //create cicd object
8
31
  const cicd = CICD.createByDefault(cmd);
9
32
  //construct cmd object
10
33
  const cicdCmd = new CICDCmd(cmd, cicd);
34
+ console.log(cicd)
11
35
  //build by cicdCmd and cicdConfig
12
36
  console.log(cicdCmd.endpoints);
37
+
13
38
  for (let i = 0; i < cicdCmd.endpoints.length; i++) {
14
39
  const ep = cicdCmd.endpoints[i];
40
+ ep.build = ep.build.replace('$public_path', ep.publicPath);
41
+ console.log('exec build:', ep.build);
15
42
  shell.exec(ep.build);
43
+ replaceDefine(path.join(cicd.source.root_path, ep.dir), ep.defines);
16
44
  }
17
45
  }
@@ -1,9 +1,6 @@
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 {Dir} from 'fs';
5
- import fsHelper from '@/utils/fsHelper';
6
- import CICDCmd from '@/classes/cicd/CICDCmd';
7
4
 
8
5
  const JSON5 = require('json5');
9
6
  import qs from 'querystring';
@@ -21,13 +18,18 @@ interface DeploySource {
21
18
  /**
22
19
  * Deploy target
23
20
  */
24
- interface DeployTarget {
21
+ export interface DeployTarget {
25
22
  id: string;
26
23
  type: CloudType;
27
24
  bucket: string;
25
+ region: string;
28
26
  access_key: string;
29
27
  access_secret: string;
30
28
  root_path: '/';
29
+ uri_rewrite: {
30
+ original: string;
31
+ final?: string;
32
+ };
31
33
  }
32
34
 
33
35
  /**
@@ -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
  }
@@ -0,0 +1,59 @@
1
+ import aliOSS from 'ali-oss';
2
+ import fs from 'fs';
3
+ import { DeployTarget } from '../CICD';
4
+ class AliOSS {
5
+ ossClient: aliOSS;
6
+ constructor(target: DeployTarget) {
7
+ this.ossClient = new aliOSS({
8
+ region: target.region,
9
+ accessKeyId: target.access_key,
10
+ accessKeySecret: target.access_secret,
11
+ bucket: target.bucket,
12
+ timeout: 600000,
13
+ });
14
+ }
15
+
16
+ private async progress(p: number, filePath: string, ossPath: string) {
17
+ // 上传进度。
18
+ process.stdout.clearLine(1);
19
+ process.stdout.cursorTo(0);
20
+ process.stdout.write(
21
+ `progress: ${p.toFixed(2)}%, Upload '${filePath}' To OSS_PATH:${ossPath}`
22
+ );
23
+ }
24
+
25
+ public async putStreamFiles(
26
+ filesList: string[],
27
+ ossBasePath: string,
28
+ dir: string
29
+ ) {
30
+ for (let i = 0; i < filesList.length; i++) {
31
+ const filePath = filesList[i].split('dist\\')[1];
32
+ const ossPath =
33
+ ossBasePath + filePath.replace(/\\/g, '/').replace(dir, '');
34
+
35
+ //@ts-ignore
36
+ let options: aliOSS.PutStreamOptions = {
37
+ contentLength: fs.statSync(filesList[i]).size,
38
+ };
39
+ if (filesList[i].includes('index.html')) {
40
+ options = Object.assign({ headers: { 'Cache-Control': 'max-age=0' } });
41
+ }
42
+ const fileResult = await this.ossClient.putStream(
43
+ ossPath,
44
+ fs.createReadStream(filesList[i]),
45
+ options
46
+ );
47
+ if (fileResult.res.status !== 200) {
48
+ throw new Error('Upload OSS Error');
49
+ }
50
+ if (fileResult.res.status === 200) {
51
+ const p = ((i + 1) * 100) / filesList.length;
52
+ this.progress(p, filesList[i], ossPath);
53
+ }
54
+ }
55
+ console.log('\n');
56
+ }
57
+ }
58
+
59
+ export default AliOSS;
@@ -0,0 +1,189 @@
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';
7
+
8
+ type TFlag = 'break' | 'enhance_break' | null;
9
+
10
+ class CDN {
11
+ AccessKeySecret: string;
12
+ AccessKeyId: string;
13
+ constructor(target: DeployTarget) {
14
+ this.AccessKeyId = target.access_key;
15
+ this.AccessKeySecret = target.access_secret;
16
+ }
17
+ /**
18
+ * 访问CDN通用接口
19
+ * @param {接口名称} actionName
20
+ * @param {各接口定制化参数} paramObj
21
+ * @returns
22
+ */
23
+ private async getCdnData(actionName: string, paramObj: Object) {
24
+ let config = {
25
+ Action: actionName,
26
+ Format: 'JSON',
27
+ Version: '2018-05-10',
28
+ AccessKeyId: this.AccessKeyId,
29
+ SignatureMethod: 'HMAC-SHA1',
30
+ Timestamp: moment().toDate().toISOString(),
31
+ SignatureVersion: '1.0',
32
+ SignatureNonce: uuid.v1(),
33
+ };
34
+ config = Object.assign(config, paramObj);
35
+ let paramConfig = qs.stringify(config, {
36
+ sort: (a: any, b: any) => {
37
+ return a < b ? -1 : 1;
38
+ },
39
+ charset: 'utf-8',
40
+ });
41
+
42
+ const strSign = `GET&%2F&${encodeURIComponent(paramConfig)}`;
43
+ // console.log(`strSign: ${strSign}\n`);
44
+ const hmacSha1 = crypto.createHmac('sha1', `${this.AccessKeySecret}&`);
45
+ hmacSha1.update(strSign);
46
+ const signature = hmacSha1.digest('base64');
47
+ config = Object.assign(config, {
48
+ Signature: signature,
49
+ });
50
+ paramConfig = qs.stringify(config, {
51
+ sort: (a, b) => {
52
+ return a < b ? -1 : 1;
53
+ },
54
+ charset: 'utf-8',
55
+ format: 'RFC3986',
56
+ });
57
+
58
+ const url = `http://cdn.aliyuncs.com?${paramConfig}`;
59
+
60
+ const res = await axios.create().get(url);
61
+ return res.data;
62
+ }
63
+
64
+ /**
65
+ * 改写回源URI接口
66
+ * @param {加速域名} domainName
67
+ * @param {需要重写的url 数组} sourceUrls
68
+ * @param {重写目标url 数组} targetUrls
69
+ * @param {改写操作执行规则 数组 值为null、break或enhance_break} flags
70
+ * @returns
71
+ */
72
+ public async setRWriteUri(
73
+ domainName: string,
74
+ sourceUrls: string[],
75
+ targetUrls: string[],
76
+ flags: TFlag[]
77
+ ) {
78
+ try {
79
+ if (sourceUrls.length !== targetUrls.length) {
80
+ throw new Error(`sourceUrls's length not equal targetUrls's length`);
81
+ }
82
+ const Functions: Object[] = [];
83
+ sourceUrls.forEach((item, index) => {
84
+ Functions.push({
85
+ functionArgs: [
86
+ {
87
+ argName: 'source_url',
88
+ argValue: item,
89
+ },
90
+ {
91
+ argName: 'target_url',
92
+ argValue: targetUrls[index],
93
+ },
94
+ {
95
+ argName: 'flag',
96
+ argValue: flags[index],
97
+ },
98
+ ],
99
+ functionName: 'back_to_origin_url_rewrite',
100
+ });
101
+ });
102
+
103
+ const data = await this.getCdnData('BatchSetCdnDomainConfig', {
104
+ DomainNames: domainName,
105
+ Functions: JSON.stringify(Functions),
106
+ });
107
+ return data;
108
+ } catch (e) {
109
+ console.error(
110
+ `Error: ${e.response ? JSON.stringify(e.response.data.Message) : e}`
111
+ );
112
+ throw new Error(e.response.data.Message);
113
+ }
114
+ }
115
+
116
+ /**
117
+ * 刷新节点上的文件内容
118
+ * @param {刷新URL, 格式为加速域名或刷新的文件或目录。多个URL之间使用换行符(\n)或(\r\n)分隔} objectPath
119
+ * @param {刷新的类型 File: 文件; Directory: 目录} objectType
120
+ */
121
+ public async refreshCache(objectPath: string, objectType?: string) {
122
+ try {
123
+ let param = {
124
+ ObjectPath: objectPath,
125
+ };
126
+ if (objectType) {
127
+ param = Object.assign(param, { ObjectType: objectType });
128
+ }
129
+ const data = await this.getCdnData('RefreshObjectCaches', param);
130
+ return data;
131
+ } catch (e) {
132
+ console.error('Error:');
133
+ console.error(e.response.data.Message);
134
+ throw new Error(e.response.data.Message);
135
+ }
136
+ }
137
+
138
+ /**
139
+ * 预热源站内容到缓存节点
140
+ * @param {预热URL,格式为加速域名或预热的文件 多个URL之间使用换行符(\n)或(\r\n)分隔 单条长度最长为1024个字符} objectPath
141
+ * @returns
142
+ */
143
+ async pushCache(objectPath: string) {
144
+ const data = await this.getCdnData('PushObjectCache', {
145
+ ObjectPath: objectPath,
146
+ });
147
+ return data;
148
+ }
149
+
150
+ /**
151
+ * 通过任务编号查询刷新预热任务信息
152
+ * @param {支持同时传入多个任务ID,多个任务ID之间用英文逗号(,)分隔,最多支持同时传入10个任务ID} taskIds
153
+ * @returns
154
+ */
155
+ async describeRefreshTaskById(taskIds: string) {
156
+ try {
157
+ const data = await this.getCdnData('DescribeRefreshTaskById', {
158
+ TaskId: taskIds,
159
+ });
160
+ return data;
161
+ } catch (e) {
162
+ console.error('Error:');
163
+ console.error(e.response.data.Message);
164
+ throw new Error(e.response.data.Message);
165
+ }
166
+ }
167
+
168
+ /**
169
+ * 刷新CDN节点
170
+ * @param {加速域名} domainName
171
+ * @param {功能配置ID} configId
172
+ * @returns
173
+ */
174
+ async describeCdnDomainConfigs(domainName: string, configId?: string) {
175
+ try {
176
+ const data = await this.getCdnData('DescribeCdnDomainConfigs', {
177
+ DomainName: domainName,
178
+ ConfigId: configId,
179
+ });
180
+ return data;
181
+ } catch (e) {
182
+ console.error('Error:');
183
+ console.error(e.response.data.Message);
184
+ throw new Error(e.response.data.Message);
185
+ }
186
+ }
187
+ }
188
+
189
+ 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 => {
@@ -13,7 +13,7 @@ const replaceDefine = (target:string,define?:Define)=>{
13
13
  if (define){
14
14
  const namePieces = dir.split('.');
15
15
  const fileType = namePieces[namePieces.length - 1];
16
- if (['js','json','json5','yml','ts'].indexOf(fileType)>=0){
16
+ if (['js','ts'].indexOf(fileType)>=0){
17
17
  let file = fs.readFileSync(path.join(target, dir)).toString();
18
18
  const replaceArr = Object.keys(define);
19
19
  for (let replace of replaceArr){
@@ -1,41 +1,56 @@
1
- import fs from "fs";
2
- import path from "path";
3
- import aliOSS from "ali-oss";
4
- import fsHelper from "../utils/fsHelper";
5
- import CICD from "@/classes/cicd/CICD";
6
- import CICDCmd from "@/classes/cicd/CICDCmd";
7
- //
8
- // const progress = (p: number, filePath: string, ossPath: string) => {
9
- // // 上传进度。
10
- // process.stdout.clearLine(-1);
11
- // process.stdout.cursorTo(0);
12
- // process.stdout.write(
13
- // `progress: ${p.toFixed(2)}%, Upload '${filePath}' To OSS_PATH:${ossPath}`
14
- // );
15
- // };
16
- //
17
- // let filesList: string[] = [];
18
- // const traverseFolder = (url: string) => {
19
- // if (fs.existsSync(url)) {
20
- // const files = fs.readdirSync(url);
21
- // files.forEach((file) => {
22
- // const curPath = path.join(url, file);
23
- // if (fs.statSync(curPath).isDirectory()) {
24
- // traverseFolder(curPath);
25
- // } else {
26
- // filesList.push(curPath);
27
- // }
28
- // });
29
- // }
30
- // };
31
- //
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
+
8
+ let filesList: string[] = [];
9
+ const traverseFolder = (url: string) => {
10
+ if (fs.existsSync(url)) {
11
+ const files = fs.readdirSync(url);
12
+ files.forEach((file) => {
13
+ const curPath = path.join(url, file);
14
+ if (fs.statSync(curPath).isDirectory()) {
15
+ traverseFolder(curPath);
16
+ } else {
17
+ filesList.push(curPath);
18
+ }
19
+ });
20
+ }
21
+ };
22
+
32
23
  export default async (cmd: any) => {
33
- try {
34
- //create cicd object
35
- const cicd = CICD.createByDefault(cmd);
36
- //construct cmd object
37
- const cicdCmd = new CICDCmd(cmd, cicd);
38
- }catch (e) {
39
- throw e;
40
- }
41
- }
24
+ try {
25
+ console.log('Start Deploy-----');
26
+ //create cicd object
27
+ const cicd = CICD.createByDefault(cmd);
28
+ //construct cmd object
29
+ const cicdCmd = new CICDCmd(cmd, cicd);
30
+
31
+ const target = Array.isArray(cicdCmd.cicd.target)
32
+ ? cicdCmd.cicd.target[0]
33
+ : cicdCmd.cicd.target;
34
+
35
+ const aliOss = new AliOSS(target);
36
+ console.log('Please Wait for Upload OSS...');
37
+ for (let i = 0; i < cicdCmd.endpoints.length; i++) {
38
+ const distPath = path.join(
39
+ process.cwd(),
40
+ cicd.source.root_path,
41
+ cicdCmd.endpoints[i].dir
42
+ );
43
+ traverseFolder(distPath);
44
+ await aliOss.putStreamFiles(
45
+ filesList,
46
+ cicdCmd.endpoints[i].deployDir.replace(/\\/g, '/'),
47
+ cicdCmd.endpoints[i].dir
48
+ );
49
+ filesList = [];
50
+ }
51
+ console.log('Upload OSS Done');
52
+ console.log('Deploy Done-----');
53
+ } catch (e) {
54
+ throw e;
55
+ }
56
+ };