rigjs 1.0.34 → 2.0.0-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.
- package/bin/rig.js +1 -1
- package/built/index.js +384 -0
- package/demo/.env.oem1 +4 -0
- package/demo/.env.oem2 +4 -0
- package/demo/babel.config.js +5 -0
- package/demo/cicd.rig.json5 +55 -0
- package/demo/jsconfig.json +19 -0
- package/demo/package.json +43 -11
- package/demo/package.rig.json5 +0 -6
- package/demo/public/favicon.ico +0 -0
- package/demo/public/index.html +17 -0
- package/demo/rig_helper.js +7 -7
- package/demo/src/App.vue +28 -0
- package/demo/src/assets/logo.png +0 -0
- package/demo/src/components/HelloWorld.vue +58 -0
- package/demo/src/main.js +8 -0
- package/demo/vue.config.js +8 -0
- package/demo/yarn.lock +6068 -0
- package/lib/build/index.ts +21 -0
- package/lib/check/index.d.ts +6 -0
- package/lib/check/index.js +0 -1
- package/lib/classes/cicd/CICD.ts +112 -0
- package/lib/classes/cicd/CICDCmd.ts +65 -0
- package/lib/classes/cicd/Deploy/AliDeploy.ts +46 -0
- package/lib/classes/cicd/Deploy/CDN.ts +126 -0
- package/lib/classes/cicd/Deploy/HuaweiDeploy.ts +0 -0
- package/lib/classes/cicd/Deploy/index.ts +0 -0
- package/lib/classes/cicd/DirLevel.ts +49 -0
- package/lib/classes/cicd/Endpoint.ts +87 -0
- package/lib/define/index.ts +47 -0
- package/lib/deploy/index.ts +60 -0
- package/lib/deps/index.d.ts +6 -0
- package/lib/env/index.d.ts +4 -0
- package/lib/info/index.d.ts +6 -0
- package/lib/init/cicd.rig.json5 +9 -0
- package/lib/init/index.d.ts +6 -0
- package/lib/init/index.js +49 -34
- package/lib/init/rig_helper.js +14 -0
- package/lib/install/index.d.ts +5 -0
- package/lib/install/index.js +1 -2
- package/lib/postinstall/index.d.ts +6 -0
- package/lib/preinstall/index.d.ts +6 -0
- package/lib/rig/index.ts +49 -0
- package/lib/tag/index.d.ts +6 -0
- package/lib/tag/index.js +0 -3
- package/lib/utils/fsHelper.ts +10 -0
- package/lib/utils/index.js +4 -1
- package/lib/utils/{regex.js → regex.ts} +9 -3
- package/package.json +16 -3
- package/tsconfig.json +50 -0
- package/.github/workflows/npm-publish.yml +0 -44
- package/demo/package.rig2.json5 +0 -8
- package/demo/test.js +0 -18
- package/lib/rig/index.js +0 -17
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import fsHelper from '../utils/fsHelper';
|
|
2
|
+
import CICD from '@/classes/cicd/CICD';
|
|
3
|
+
import CICDCmd from '@/classes/cicd/CICDCmd';
|
|
4
|
+
import shell from 'shelljs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
|
|
7
|
+
export default async (cmd: any) => {
|
|
8
|
+
//create cicd object
|
|
9
|
+
const cicd = CICD.createByDefault(cmd);
|
|
10
|
+
//construct cmd object
|
|
11
|
+
const cicdCmd = new CICDCmd(cmd, cicd);
|
|
12
|
+
console.log(cicd)
|
|
13
|
+
//build by cicdCmd and cicdConfig
|
|
14
|
+
console.log(cicdCmd.endpoints);
|
|
15
|
+
for (let i = 0; i < cicdCmd.endpoints.length; i++) {
|
|
16
|
+
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);
|
|
20
|
+
}
|
|
21
|
+
}
|
package/lib/check/index.js
CHANGED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import DirLevel from '@/classes/cicd/DirLevel';
|
|
2
|
+
import Endpoint, { EndpointDict} from '@/classes/cicd/Endpoint';
|
|
3
|
+
import fs from 'fs';
|
|
4
|
+
|
|
5
|
+
const JSON5 = require('json5');
|
|
6
|
+
import qs from 'querystring';
|
|
7
|
+
export enum CloudType {
|
|
8
|
+
alicloud = 'alicloud',
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Bundle source
|
|
13
|
+
*/
|
|
14
|
+
interface DeploySource {
|
|
15
|
+
root_path: string
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Deploy target
|
|
20
|
+
*/
|
|
21
|
+
interface DeployTarget {
|
|
22
|
+
id: string;
|
|
23
|
+
type: CloudType;
|
|
24
|
+
bucket: string;
|
|
25
|
+
region: string;
|
|
26
|
+
access_key: string;
|
|
27
|
+
access_secret: string;
|
|
28
|
+
root_path: '/';
|
|
29
|
+
uri_rewrite: {
|
|
30
|
+
original: string;
|
|
31
|
+
final?: string;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @interface DirGroup
|
|
37
|
+
* Only dynamic Dirlevel can use DirGroup
|
|
38
|
+
*/
|
|
39
|
+
export interface DirGroup {
|
|
40
|
+
name: string,
|
|
41
|
+
/**
|
|
42
|
+
* Name in DirLevel,
|
|
43
|
+
*/
|
|
44
|
+
level: string,
|
|
45
|
+
includes: string[],
|
|
46
|
+
}
|
|
47
|
+
export interface Define{
|
|
48
|
+
[replace: string]: String;
|
|
49
|
+
}
|
|
50
|
+
export interface DefineDict{
|
|
51
|
+
[group: string]: Define;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Whole CI/CD config
|
|
56
|
+
*/
|
|
57
|
+
export interface CICDConfig {
|
|
58
|
+
tree_schema: string;
|
|
59
|
+
source: DeploySource;
|
|
60
|
+
target: DeployTarget | DeployTarget[];
|
|
61
|
+
endpoints: EndpointDict;
|
|
62
|
+
groups: DirGroup[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class CICD {
|
|
67
|
+
/**
|
|
68
|
+
* schema of the directory tree
|
|
69
|
+
* @type {string}
|
|
70
|
+
*/
|
|
71
|
+
treeSchema: string;
|
|
72
|
+
/**
|
|
73
|
+
* DirLevel shows every level of the directory structure
|
|
74
|
+
* @type {DirLevel[]}
|
|
75
|
+
*/
|
|
76
|
+
schema: DirLevel[];
|
|
77
|
+
/**
|
|
78
|
+
* endpoints contains build info and deploy info.
|
|
79
|
+
* @type {Endpoint[]}
|
|
80
|
+
*/
|
|
81
|
+
endpoints: Endpoint[];
|
|
82
|
+
source: DeploySource;
|
|
83
|
+
target: DeployTarget | DeployTarget[];
|
|
84
|
+
groups: DirGroup[];
|
|
85
|
+
|
|
86
|
+
constructor(config: CICDConfig) {
|
|
87
|
+
this.treeSchema = config.tree_schema;
|
|
88
|
+
this.schema = DirLevel.createSchema(this.treeSchema);
|
|
89
|
+
this.endpoints = Endpoint.createEndpointArr(config,this.schema);
|
|
90
|
+
this.source = config.source;
|
|
91
|
+
this.target = config.target;
|
|
92
|
+
this.groups = config.groups;
|
|
93
|
+
}
|
|
94
|
+
static createByDefault(cmd:any){
|
|
95
|
+
//replace params
|
|
96
|
+
let cicdStr = fs.readFileSync(`${process.cwd()}/cicd.rig.json5`).toString();
|
|
97
|
+
const paramsStr = cmd.params;
|
|
98
|
+
const params = qs.parse(paramsStr);
|
|
99
|
+
Object.keys(params).forEach(key => {
|
|
100
|
+
const regStr = `\\$\\{${key}\\}`;
|
|
101
|
+
const regex = new RegExp(regStr, 'g');
|
|
102
|
+
cicdStr = cicdStr.replace(regex, params[key] as string);
|
|
103
|
+
});
|
|
104
|
+
return new CICD(JSON5.parse(cicdStr));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
matchEndpoints(cmdDirStrArr: string[]) {
|
|
108
|
+
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export default CICD;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import Endpoint from '@/classes/cicd/Endpoint';
|
|
2
|
+
import CICD, {CICDConfig} from '@/classes/cicd/CICD';
|
|
3
|
+
import qs, {ParsedUrlQuery} from 'querystring';
|
|
4
|
+
import path from 'path'
|
|
5
|
+
import fsHelper from '@/utils/fsHelper';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @class CICDCmd
|
|
9
|
+
* @property dir string Original specific dir in cmd.
|
|
10
|
+
* @property dirArr array Splitted dir.
|
|
11
|
+
* @property endpoints array Tageted endpoints used to build,define or deploy
|
|
12
|
+
*/
|
|
13
|
+
class CICDCmd {
|
|
14
|
+
/**
|
|
15
|
+
* @property dir string Whole directory
|
|
16
|
+
* @type {string}
|
|
17
|
+
*/
|
|
18
|
+
dir: string;
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @type {string[]}
|
|
22
|
+
*/
|
|
23
|
+
dirInSchemaStrArr: string[];
|
|
24
|
+
dirStrArr: string[];
|
|
25
|
+
endpoints: Endpoint[] = [];
|
|
26
|
+
cicd: CICD;
|
|
27
|
+
|
|
28
|
+
constructor(cmd: any, cicd: CICD) {
|
|
29
|
+
const cmdArgs = cmd.args;
|
|
30
|
+
this.dir = cmdArgs[0];
|
|
31
|
+
//获取需要被设置的schema变量
|
|
32
|
+
const schemaKeys = this.dir.match(/(?<=\{)[a-zA-Z]+(?=\})/g) || [];
|
|
33
|
+
//解析schema变量
|
|
34
|
+
let schema: ParsedUrlQuery = qs.parse(cmd.schema);
|
|
35
|
+
for (let key of schemaKeys) {
|
|
36
|
+
const val = schema[key];
|
|
37
|
+
if (val) {
|
|
38
|
+
this.dir = this.dir.replace(`{${key}}`, val as string);
|
|
39
|
+
} else {
|
|
40
|
+
throw new Error(`${key} is not set`);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
//whole user's target dir
|
|
44
|
+
this.dirStrArr = this.dir.split('/').filter((dirStr, index) => dirStr.length > 0);
|
|
45
|
+
//dir within the schema
|
|
46
|
+
this.dirInSchemaStrArr = this.dir.split('/').filter((dirStr, index) => dirStr.length > 0 && index < cicd.schema.length);
|
|
47
|
+
this.cicd = cicd;
|
|
48
|
+
this.createTargetEndpoints();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private createTargetEndpoints() {
|
|
52
|
+
const allEndpints = this.cicd.endpoints;
|
|
53
|
+
this.endpoints = allEndpints.filter(endpoint => endpoint.matchCmd(this.dirInSchemaStrArr, this.cicd.groups));
|
|
54
|
+
if (this.dirStrArr.length > this.dirInSchemaStrArr.length) {
|
|
55
|
+
const sufDir = this.dirStrArr.slice(this.dirInSchemaStrArr.length, this.dirStrArr.length).join('/');
|
|
56
|
+
this.endpoints = this.endpoints.map(ep => {
|
|
57
|
+
ep.deployDir = path.join(ep.deployDir, sufDir);
|
|
58
|
+
return ep;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export default CICDCmd;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import aliOSS from "ali-oss";
|
|
2
|
+
import fs from "fs";
|
|
3
|
+
class AliOSS {
|
|
4
|
+
ossClient: aliOSS;
|
|
5
|
+
constructor(accessKeyId: string, accessKeySecret: string, region: string, bucket: string) {
|
|
6
|
+
this.ossClient = new aliOSS({
|
|
7
|
+
region,
|
|
8
|
+
accessKeyId: accessKeyId,
|
|
9
|
+
accessKeySecret: accessKeySecret,
|
|
10
|
+
bucket,
|
|
11
|
+
timeout: 600000,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
private async progress(p: number, filePath: string, ossPath: string) {
|
|
16
|
+
// 上传进度。
|
|
17
|
+
process.stdout.clearLine(-1);
|
|
18
|
+
process.stdout.cursorTo(0);
|
|
19
|
+
process.stdout.write(
|
|
20
|
+
`progress: ${p.toFixed(2)}%, Upload '${filePath}' To OSS_PATH:${ossPath}`
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public async putStreamFiles(
|
|
25
|
+
filesList: string[],
|
|
26
|
+
ossBasePath: string,
|
|
27
|
+
dir: string
|
|
28
|
+
) {
|
|
29
|
+
for (let i = 0; i < filesList.length; i++) {
|
|
30
|
+
const filePath = filesList[i].split("dist\\")[1];
|
|
31
|
+
const ossPath =
|
|
32
|
+
ossBasePath + filePath.replace(/\\/g, "/").replace(dir, "");
|
|
33
|
+
const fileResult = await this.ossClient.putStream(
|
|
34
|
+
ossPath,
|
|
35
|
+
fs.createReadStream(filesList[i])
|
|
36
|
+
);
|
|
37
|
+
if (fileResult.res.status === 200) {
|
|
38
|
+
const p = ((i + 1) * 100) / filesList.length;
|
|
39
|
+
this.progress(p, filesList[i], ossPath);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
console.log("\n");
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export default AliOSS;
|
|
@@ -0,0 +1,126 @@
|
|
|
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
|
+
|
|
7
|
+
type TFlag = "break" | "enhance_break" | null;
|
|
8
|
+
|
|
9
|
+
class CDN {
|
|
10
|
+
AccessKeySecret: string;
|
|
11
|
+
AccessKeyId: string;
|
|
12
|
+
constructor(AccessKeyId: string, AccessKeySecret: string) {
|
|
13
|
+
this.AccessKeySecret = AccessKeySecret;
|
|
14
|
+
this.AccessKeyId = AccessKeyId;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* 访问CDN通用接口
|
|
18
|
+
* @param {接口名称} actionName
|
|
19
|
+
* @param {各接口定制化参数} paramObj
|
|
20
|
+
* @returns
|
|
21
|
+
*/
|
|
22
|
+
private async getCdnData(actionName: string, paramObj: Object) {
|
|
23
|
+
let config = {
|
|
24
|
+
Action: actionName,
|
|
25
|
+
Format: "JSON",
|
|
26
|
+
Version: "2018-05-10",
|
|
27
|
+
AccessKeyId: this.AccessKeyId,
|
|
28
|
+
SignatureMethod: "HMAC-SHA1",
|
|
29
|
+
Timestamp: moment().toDate().toISOString(),
|
|
30
|
+
SignatureVersion: "1.0",
|
|
31
|
+
SignatureNonce: uuid.v1(),
|
|
32
|
+
};
|
|
33
|
+
config = Object.assign(config, paramObj);
|
|
34
|
+
let paramConfig = qs.stringify(config, {
|
|
35
|
+
sort: (a: number, b: number) => {
|
|
36
|
+
return a < b ? -1 : 1;
|
|
37
|
+
},
|
|
38
|
+
charset: "utf-8",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
const strSign = `GET&%2F&${encodeURIComponent(paramConfig)}`;
|
|
42
|
+
console.log(`strSign: ${strSign}`);
|
|
43
|
+
const hmacSha1 = crypto.createHmac("sha1", this.AccessKeySecret);
|
|
44
|
+
hmacSha1.update(strSign);
|
|
45
|
+
const signature = hmacSha1.digest("base64");
|
|
46
|
+
paramConfig += `&Signature=${signature}`;
|
|
47
|
+
|
|
48
|
+
const url = `http://cdn.aliyuncs.com?${paramConfig}`;
|
|
49
|
+
console.log(url);
|
|
50
|
+
|
|
51
|
+
const res = await axios.create().get(url);
|
|
52
|
+
return res.data;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* 改写回源URI接口
|
|
57
|
+
* @param {加速域名} domainName
|
|
58
|
+
* @param {需要重写的url 数组} sourceUrls
|
|
59
|
+
* @param {重写目标url 数组} targetUrls
|
|
60
|
+
* @param {改写操作执行规则 数组 值为null、break或enhance_break} flags
|
|
61
|
+
* @returns
|
|
62
|
+
*/
|
|
63
|
+
public async setRWriteUri(
|
|
64
|
+
domainName: string,
|
|
65
|
+
sourceUrls: string[],
|
|
66
|
+
targetUrls: string[],
|
|
67
|
+
flags: TFlag[]
|
|
68
|
+
) {
|
|
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",
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
const data = await this.getCdnData("BatchSetCdnDomainConfig", {
|
|
94
|
+
DomainNames: domainName,
|
|
95
|
+
Functions: JSON.stringify(Functions),
|
|
96
|
+
});
|
|
97
|
+
return data;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 刷新节点上的文件内容
|
|
102
|
+
* @param {刷新URL, 格式为加速域名或刷新的文件或目录。多个URL之间使用换行符(\n)或(\r\n)分隔} objectPath
|
|
103
|
+
* @param {刷新的类型 File: 文件; Directory: 目录} objectType
|
|
104
|
+
*/
|
|
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;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* 预热源站内容到缓存节点
|
|
115
|
+
* @param {预热URL,格式为加速域名或预热的文件 多个URL之间使用换行符(\n)或(\r\n)分隔 单条长度最长为1024个字符} objectPath
|
|
116
|
+
* @returns
|
|
117
|
+
*/
|
|
118
|
+
async pushCache(objectPath: string) {
|
|
119
|
+
const data = await this.getCdnData("PushObjectCache", {
|
|
120
|
+
ObjectPath: objectPath,
|
|
121
|
+
});
|
|
122
|
+
return data;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export default CDN;
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import regex from '../../utils/regex';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* contain attributes of every level in the directory schema
|
|
5
|
+
*/
|
|
6
|
+
class DirLevel {
|
|
7
|
+
/**
|
|
8
|
+
* Means that is this level's name can be set dynamically.
|
|
9
|
+
* @type {boolean}
|
|
10
|
+
*/
|
|
11
|
+
dynamic: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* The meaning of this level.Can be changed when [this.dynamic] is true.
|
|
14
|
+
* % matches all dir name.
|
|
15
|
+
* @type {string}
|
|
16
|
+
*/
|
|
17
|
+
name: string | '%';
|
|
18
|
+
/**
|
|
19
|
+
* The level in the directory schema
|
|
20
|
+
* @type {number}
|
|
21
|
+
*/
|
|
22
|
+
level: number;
|
|
23
|
+
|
|
24
|
+
constructor(dirName: string, level: number) {
|
|
25
|
+
this.dynamic = regex.dynamicDir.test(dirName);
|
|
26
|
+
this.name = dirName;
|
|
27
|
+
this.level = level;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
static createSchema(treeSchema: string) {
|
|
31
|
+
let dirStrArr = treeSchema.split('/');
|
|
32
|
+
dirStrArr = dirStrArr.filter(dirStr => dirStr.length > 0);
|
|
33
|
+
return dirStrArr.map((dirStr, index) => {
|
|
34
|
+
return new DirLevel(dirStr, index);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static createDirArr(dir: string, schema: DirLevel[]) {
|
|
39
|
+
let dirStrArr = dir.split('/');
|
|
40
|
+
dirStrArr = dirStrArr.filter(dirStr => dirStr.length > 0);
|
|
41
|
+
return dirStrArr.map((dirStr, index) => {
|
|
42
|
+
const level = new DirLevel(dirStr, index);
|
|
43
|
+
level.dynamic = schema[index].dynamic;
|
|
44
|
+
return level;
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default DirLevel;
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {CICDConfig, DefineDict, DirGroup} from './CICD';
|
|
2
|
+
import {mkdirSync} from 'fs';
|
|
3
|
+
import DirLevel from '@/classes/cicd/DirLevel';
|
|
4
|
+
|
|
5
|
+
interface EndpointInfo {
|
|
6
|
+
build: string;
|
|
7
|
+
target: string;
|
|
8
|
+
domain: string;
|
|
9
|
+
define: DefineDict;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface EndpointDict {
|
|
13
|
+
[dir: string]: EndpointInfo;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
class Endpoint {
|
|
17
|
+
dir: string;
|
|
18
|
+
dirStrArr: string[];
|
|
19
|
+
dirArr: DirLevel[];
|
|
20
|
+
target: string;
|
|
21
|
+
build: string;
|
|
22
|
+
domain: string;
|
|
23
|
+
deployDir: string;
|
|
24
|
+
define: DefineDict;
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
constructor(dir: string, info: EndpointInfo, schema: DirLevel[]) {
|
|
28
|
+
this.dir = dir;
|
|
29
|
+
this.deployDir = dir;
|
|
30
|
+
this.dirStrArr = dir.split('/').filter(d => d.length > 0);
|
|
31
|
+
this.dirArr = DirLevel.createDirArr(dir, schema);
|
|
32
|
+
this.target = info.target;
|
|
33
|
+
this.build = info.build;
|
|
34
|
+
this.domain = info.domain;
|
|
35
|
+
this.define = info.define;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
static createEndpointArr(cicdConfig: CICDConfig, schema: DirLevel[]) {
|
|
39
|
+
const endpointDict = cicdConfig.endpoints;
|
|
40
|
+
return Object.keys(endpointDict).map(dir => {
|
|
41
|
+
const info = endpointDict[dir];
|
|
42
|
+
return new Endpoint(dir, info, schema);
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
matchCmd(dirSchemaStrArr: string[], groups: DirGroup[]) {
|
|
47
|
+
if (this.dirStrArr.length < dirSchemaStrArr.length) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
for (let i = 0; i < dirSchemaStrArr.length; i++) {
|
|
51
|
+
const cmdDir = dirSchemaStrArr[i];
|
|
52
|
+
const dir = this.dirStrArr[i];
|
|
53
|
+
const dirLevel = this.dirArr[i];
|
|
54
|
+
//check if dir is wildcard
|
|
55
|
+
if (cmdDir !== '%') {
|
|
56
|
+
//not wildcard
|
|
57
|
+
//check if dir is group alias
|
|
58
|
+
if (cmdDir.indexOf('%') === 0) {
|
|
59
|
+
//dir is group
|
|
60
|
+
//find group
|
|
61
|
+
const group = groups.find(g => g.name === cmdDir);
|
|
62
|
+
if (group) {
|
|
63
|
+
//group found
|
|
64
|
+
//check if group includes this dir
|
|
65
|
+
if (group.includes.indexOf(dir) < 0) {
|
|
66
|
+
//not include
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
} else {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
} else {
|
|
74
|
+
//cmdDir is not group
|
|
75
|
+
if (cmdDir !== dir) {
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
} else {
|
|
80
|
+
if (!dirLevel.dynamic) return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export default Endpoint;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import fsHelper from '../utils/fsHelper';
|
|
2
|
+
import CICD, {Define} from '@/classes/cicd/CICD';
|
|
3
|
+
import CICDCmd from '@/classes/cicd/CICDCmd';
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
const replaceDefine = (target:string,define?:Define)=>{
|
|
7
|
+
const dirs = fs.readdirSync(target);
|
|
8
|
+
for (let dir of dirs){
|
|
9
|
+
const stat = fs.statSync(path.join(target, dir));
|
|
10
|
+
if (stat.isDirectory()){
|
|
11
|
+
replaceDefine(path.join(target, dir),define);
|
|
12
|
+
}else{
|
|
13
|
+
if (define){
|
|
14
|
+
const namePieces = dir.split('.');
|
|
15
|
+
const fileType = namePieces[namePieces.length - 1];
|
|
16
|
+
if (['js','ts'].indexOf(fileType)>=0){
|
|
17
|
+
let file = fs.readFileSync(path.join(target, dir)).toString();
|
|
18
|
+
const replaceArr = Object.keys(define);
|
|
19
|
+
for (let replace of replaceArr){
|
|
20
|
+
file = file.replace(new RegExp(replace,'g'),define[replace] as string);
|
|
21
|
+
}
|
|
22
|
+
fs.writeFileSync(path.join(target, dir),file);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export default async (cmd: any) => {
|
|
29
|
+
// let phase = 'start define';
|
|
30
|
+
// try{
|
|
31
|
+
// const cicdConfigJson = fsHelper.readCICDConfig();
|
|
32
|
+
// const cicdConfig = new CICD(cicdConfigJson);
|
|
33
|
+
// const args: string[] = cmd.args;
|
|
34
|
+
// const define = cicdConfig.defines[args[1]];
|
|
35
|
+
// //construct cmd object
|
|
36
|
+
// const cicdCmd = new CICDCmd(cmd, cicdConfig);
|
|
37
|
+
// console.log(cicdCmd);
|
|
38
|
+
// //define by cicdCmd and cicdConfig
|
|
39
|
+
// phase = 'start replacement';
|
|
40
|
+
// for (let i = 0; i < cicdCmd.endpoints.length; i++) {
|
|
41
|
+
// const ep = cicdCmd.endpoints[i];
|
|
42
|
+
// replaceDefine(path.join(cicdConfig.source.root_path, ep.dir),define);
|
|
43
|
+
// }
|
|
44
|
+
// }catch (e) {
|
|
45
|
+
// throw new Error(`${phase}:${e.message}`)
|
|
46
|
+
// }
|
|
47
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
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
|
+
|
|
23
|
+
export default async (cmd: any) => {
|
|
24
|
+
try {
|
|
25
|
+
//create cicd object
|
|
26
|
+
const cicd = CICD.createByDefault(cmd);
|
|
27
|
+
//construct cmd object
|
|
28
|
+
const cicdCmd = new CICDCmd(cmd, cicd);
|
|
29
|
+
|
|
30
|
+
const target = Array.isArray(cicdCmd.cicd.target)
|
|
31
|
+
? cicdCmd.cicd.target[0]
|
|
32
|
+
: cicdCmd.cicd.target;
|
|
33
|
+
|
|
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
|
+
for (let i = 0; i < cicdCmd.endpoints.length; i++) {
|
|
37
|
+
const distPath = path.join(
|
|
38
|
+
process.cwd(),
|
|
39
|
+
cicd.source.root_path,
|
|
40
|
+
cicdCmd.endpoints[i].dir
|
|
41
|
+
);
|
|
42
|
+
traverseFolder(distPath);
|
|
43
|
+
await aliOss.putStreamFiles(
|
|
44
|
+
filesList,
|
|
45
|
+
cicdCmd.endpoints[i].deployDir.replace(/\\/g, "/"),
|
|
46
|
+
cicdCmd.endpoints[i].dir
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
// await cdn.setRWriteUri(
|
|
50
|
+
// cicdCmd.endpoints[i].domain,
|
|
51
|
+
// [""],
|
|
52
|
+
// [""],
|
|
53
|
+
// ["break"]
|
|
54
|
+
// );
|
|
55
|
+
filesList = [];
|
|
56
|
+
}
|
|
57
|
+
} catch (e) {
|
|
58
|
+
throw e;
|
|
59
|
+
}
|
|
60
|
+
};
|