@be-link/cos 1.2.0

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/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # `cos`
2
+
3
+ > TODO: description
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ const cos = require('cos');
9
+
10
+ // TODO: DEMONSTRATE API
11
+ ```
package/bin/cos.js ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env node
2
+ 'use strict'
3
+
4
+ const { staticUploadToCos } = require('../lib/upload')
5
+ function main() {
6
+ staticUploadToCos()
7
+ }
8
+
9
+ main()
package/lib/config.js ADDED
@@ -0,0 +1,19 @@
1
+ const BUCKETS_CONFIG = {
2
+ development: {
3
+ name: 'dev-1304510571',
4
+ host: 'dev-1304510571.cos.ap-nanjing.myqcloud.com',
5
+ protocol: 'https',
6
+ },
7
+ trial: {
8
+ name: 'dev-1304510571',
9
+ host: 'dev-1304510571.cos.ap-nanjing.myqcloud.com',
10
+ protocol: 'https',
11
+ },
12
+ production: {
13
+ name: 'release-1304510571',
14
+ host: 'release-1304510571.file.myqcloud.com',
15
+ protocol: 'https',
16
+ },
17
+ }
18
+
19
+ module.exports = BUCKETS_CONFIG
package/lib/upload.js ADDED
@@ -0,0 +1,131 @@
1
+ 'use strict'
2
+ const fs = require('fs')
3
+ const path = require('path')
4
+ const COS = require('cos-nodejs-sdk-v5')
5
+ const BUCKETS_CONFIG = require('./config')
6
+
7
+ console.log('pro', process.env)
8
+ // 获取项目名称
9
+ const baseUrl = 'project/prod/'
10
+ const packageUrl = path.resolve(process.cwd(), './package.json')
11
+ const projectPackage = fs.readFileSync(packageUrl, { encoding: 'utf-8' })
12
+ const projectName = JSON.parse(projectPackage).name
13
+ const projectRemoteUrl = baseUrl + projectName
14
+
15
+ // 构建参数输出的地址
16
+ const distDirUrl = process.cwd() + '/dist'
17
+
18
+ // 创建一个 cos
19
+ const cos = new COS({
20
+ SecretId: 'AKIDzw0tjuPbbhfPvDO2TgGk00mTUEfjkp0v',
21
+ SecretKey: 'xtjX6xZv4QE4lKNK44qyDFacq9MExW0f',
22
+ })
23
+
24
+ function upload(url, filename) {
25
+ if (!url) return
26
+ if (!filename) return
27
+ cos.putObject(
28
+ {
29
+ Bucket: BUCKETS_CONFIG['production'].name,
30
+ Region: 'ap-nanjing',
31
+ Key: projectRemoteUrl + filename /* 必须 */,
32
+ StorageClass: 'STANDARD',
33
+ Body: fs.createReadStream(url), // 上传文件对象
34
+ onProgress: function (progressData) {},
35
+ },
36
+ function (err, data) {
37
+ if (err) {
38
+ console.log('文件上传失败', err)
39
+ }
40
+ console.log('上传成功')
41
+ },
42
+ )
43
+ }
44
+
45
+ function getAllFiles(directoryPath) {
46
+ const files = fs.readdirSync(directoryPath, { withFileTypes: true })
47
+ const fileNamesAndPaths = []
48
+
49
+ files.forEach((file) => {
50
+ const filePath = path.join(directoryPath, file.name)
51
+
52
+ if (file.isFile()) {
53
+ fileNamesAndPaths.push({
54
+ fileName: file.name,
55
+ filePath: filePath,
56
+ })
57
+ } else if (file.isDirectory()) {
58
+ const subDirectoryFiles = getAllFiles(filePath)
59
+ fileNamesAndPaths.push(...subDirectoryFiles)
60
+ }
61
+ })
62
+
63
+ return fileNamesAndPaths
64
+ }
65
+
66
+ function getBucket() {
67
+ return new Promise((resolve, reject) => {
68
+ cos.getBucket(
69
+ {
70
+ Bucket: BUCKETS_CONFIG['production'].name,
71
+ Region: 'ap-nanjing',
72
+ Prefix: 'project/dev/manager' /* 非必须 */,
73
+ },
74
+ function (err, data) {
75
+ console.log(err || data.Contents)
76
+ },
77
+ )
78
+ })
79
+ }
80
+
81
+ // 删除
82
+ function deleteProject() {
83
+ return new Promise((resolve, reject) => {
84
+ cos.deleteObject(
85
+ {
86
+ Bucket: BUCKETS_CONFIG['production'].name,
87
+ Region: 'ap-nanjing',
88
+ Key: projectRemoteUrl /* 必须 */,
89
+ },
90
+ function (err, data) {
91
+ if (err) {
92
+ console.log('删除失败')
93
+ reject(err)
94
+ }
95
+ if (data) {
96
+ console.log('删除成功')
97
+ resolve(data)
98
+ }
99
+ },
100
+ )
101
+ })
102
+ }
103
+
104
+ // 全量
105
+ // 获取文件列表
106
+
107
+ // 删除cos中的文件
108
+
109
+ // 上传文件列表
110
+
111
+ // 增量
112
+ // 获取远程的文件列表
113
+ // 获取本地的文件列表
114
+ // 将远程的文件列表和本地的文件列表进行对比获取 diff 的文件
115
+ // 删除远程不再需要的文件
116
+ // 上传新的文件
117
+
118
+ function staticUploadToCos() {
119
+ const files = getAllFiles(distDirUrl)
120
+ deleteProject().then(() => {
121
+ files.forEach((file) => {
122
+ // 获取文件名称
123
+ const filename = file.filePath.replace(distDirUrl, '')
124
+ upload(file.filePath, filename)
125
+ })
126
+ })
127
+ }
128
+
129
+ module.exports = {
130
+ staticUploadToCos,
131
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@be-link/cos",
3
+ "version": "1.2.0",
4
+ "description": "前端项目产物上传cos",
5
+ "author": "zhuifeng <yangyiboys@163.com>",
6
+ "homepage": "https://github.com/snowmountain-top/be-link#readme",
7
+ "license": "ISC",
8
+ "main": "lib/cos.js",
9
+ "files": [
10
+ "lib",
11
+ "bin"
12
+ ],
13
+ "bin": {
14
+ "cos": "./bin/cos.js"
15
+ },
16
+ "repository": {
17
+ "type": "git",
18
+ "url": "git+https://github.com/snowmountain-top/be-link.git"
19
+ },
20
+ "scripts": {},
21
+ "bugs": {
22
+ "url": "https://github.com/snowmountain-top/be-link/issues"
23
+ },
24
+ "dependencies": {
25
+ "cos-nodejs-sdk-v5": "2.12.1"
26
+ },
27
+ "publishConfig": {
28
+ "access": "public"
29
+ },
30
+ "gitHead": "0d6189ded04d22e70763cadf2700feacb3291528"
31
+ }