@struggler/cli 1.0.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.
@@ -0,0 +1,29 @@
1
+ //npm run build打包前执行此段代码
2
+ let fs = require('fs');
3
+ let path = require('path')
4
+
5
+ let packagePath = path.resolve(process.cwd(), './package.json')
6
+ //返回package的json数据
7
+ function getPackageJson() {
8
+ let data = fs.readFileSync(packagePath);//fs读取文件
9
+ return JSON.parse(data);//转换为json对象
10
+ }
11
+
12
+
13
+
14
+ function main(){
15
+ let packageData = getPackageJson();//获取package的json
16
+ let arr = packageData.version.split('.');//切割后的版本号数组
17
+ arr[2] = parseInt(arr[2]) + 1;
18
+ packageData.version = arr.join('.');//转换为以"."分割的字符串
19
+ //用packageData覆盖package.json内容
20
+ fs.writeFile(
21
+ packagePath,
22
+ JSON.stringify(packageData, null, "\t"
23
+ ),
24
+ (err) => { }
25
+ )
26
+ }
27
+
28
+
29
+ module.exports = main
@@ -0,0 +1,7 @@
1
+ var init = require('./init');
2
+ var upload = require('./upload');
3
+ var addVersion = require('./addVersion');
4
+
5
+ exports.init = init;
6
+ exports.upload = upload;
7
+ exports.addVersion = addVersion;
@@ -0,0 +1,41 @@
1
+ let { getConfig, getQiniuConfig } = require('../lib/config')
2
+ const chalk = require('chalk');
3
+ let { getJsonData, setJsonData, setSyncJsonData, directoryExists } = require('../lib/files')
4
+ let path = require('path')
5
+
6
+ function formatDate(date) {
7
+ date = date || new Date()
8
+ const year = date.getFullYear()
9
+ const month = date.getMonth() + 1
10
+ const day = date.getDate()
11
+ const hours = date.getHours()
12
+ const minutes = date.getMinutes()
13
+
14
+ return `${year}${month < 10 ? `0${month}` : month}${day < 10 ? `0${day}` : day}${hours < 10 ? `0${hours}` : hours}${minutes < 10 ? `0${minutes}` : minutes}`
15
+ }
16
+
17
+ function init(qiniuConfigPath, configPath){
18
+ if (!directoryExists(qiniuConfigPath)){
19
+ console.log(chalk.red("七牛配置不存在 正在生成模版 请稍后在下面的文件里填写必要的信息!"))
20
+ console.log(chalk.blue(qiniuConfigPath))
21
+ setSyncJsonData(qiniuConfigPath, getJsonData(path.resolve(__dirname, '../def/qiniu.json')))
22
+ }
23
+
24
+ }
25
+
26
+ function main(options){
27
+ let configPath = getConfig(options)
28
+ let qiniuConfigPath = getQiniuConfig(options)
29
+ init(qiniuConfigPath, configPath)
30
+ const qiniuConfig = getJsonData(qiniuConfigPath)
31
+ let domain = qiniuConfig.domain
32
+
33
+ let config = getJsonData(configPath)
34
+ config.publicPath = qiniuConfig.path + '/' + formatDate() + '/'
35
+ config.base = domain + qiniuConfig.path + '/' + formatDate() + '/'
36
+ //用packageData覆盖package.json内容
37
+ console.log(config)
38
+ setJsonData(configPath, config)
39
+ }
40
+
41
+ module.exports = main
@@ -0,0 +1,202 @@
1
+ var fs = require('fs');
2
+
3
+ var path = require('path');
4
+
5
+ var qiniu = require("qiniu");
6
+
7
+ var qiniuPrefix = require("../lib/prefix")
8
+
9
+ let { getQiniuConfig, getDir } = require('../lib/config')
10
+ let { getJsonData, setJsonData } = require('../lib/files')
11
+
12
+
13
+ function main(options) {
14
+ const qiniuConfig = getJsonData(getQiniuConfig(options))
15
+
16
+ //自己七牛云的秘钥
17
+
18
+ var accessKey = qiniuConfig.accessKey
19
+
20
+ var secretKey = qiniuConfig.secretKey;
21
+
22
+ var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
23
+
24
+ var config = new qiniu.conf.Config();
25
+
26
+ // 空间对应的机房,zone_z1代表华北,其他配置参见七牛云文档
27
+
28
+ config.zone = qiniu.zone[qiniuConfig.zone];
29
+
30
+ // 是否使用https域名
31
+
32
+ config.useHttpsDomain = true;
33
+
34
+ // 上传是否使用cdn加速
35
+
36
+ config.useCdnDomain = true;
37
+
38
+ var formUploader = new qiniu.form_up.FormUploader(config);
39
+
40
+ var putExtra = new qiniu.form_up.PutExtra();
41
+
42
+ //文件前缀
43
+
44
+ const prefix = qiniuPrefix.prefix(options);
45
+
46
+ let dir = getDir(options)
47
+
48
+ function upload(key, localFile) {
49
+
50
+ //这里base-html是存储空间名
51
+
52
+ // var Bucket = `cfun:${key}`;
53
+
54
+ var Bucket = qiniuConfig.Bucket;
55
+
56
+ var options = {
57
+
58
+ scope: Bucket,
59
+
60
+ // detectMime:0
61
+
62
+ // MimeType: 'text/html;text/css;text/javascript;application/x-gzip',
63
+
64
+ };
65
+
66
+ var putPolicy = new qiniu.rs.PutPolicy(options);
67
+
68
+ var uploadToken = putPolicy.uploadToken(mac);
69
+
70
+ //windows
71
+
72
+ /* let str = null;
73
+
74
+ if (localFile.indexOf("./dist\\") >= 0) {
75
+
76
+ str = localFile.replace("./dist\\", "");
77
+
78
+ } else if (localFile.indexOf("./dist/") >= 0) {
79
+
80
+ //苹果
81
+
82
+ str = localFile.replace("./dist/", "");
83
+
84
+ } else {
85
+
86
+ str = localFile;
87
+
88
+ } */
89
+
90
+ const str = path.relative(dir, localFile)
91
+
92
+ key = prefix + str;
93
+
94
+ //上传之后的文件名
95
+
96
+ key = key.replace("\\", "/");
97
+
98
+ formUploader.putFile(uploadToken, key, localFile, null, function (respErr,
99
+
100
+ respBody, respInfo) {
101
+
102
+ if (respErr) {
103
+
104
+ // console.log(uploadToken);
105
+
106
+ console.log(localFile + "文件上传失败,正在重新上传-----------");
107
+
108
+ // console.log(respInfo.statusCode);
109
+
110
+ // console.log(respBody);
111
+
112
+ upload('file2', localFile);
113
+
114
+ throw respErr;
115
+
116
+ } else {
117
+
118
+ if (respInfo.statusCode == 200) {
119
+ respBody.key = qiniuConfig.domain + respBody.key
120
+
121
+ console.log(respBody);
122
+
123
+ } else {
124
+
125
+ console.log(respInfo.statusCode);
126
+
127
+ console.log(respBody);
128
+
129
+ if (respBody.error) {
130
+
131
+ console.log(respBody.error)
132
+
133
+ }
134
+
135
+ }
136
+
137
+ }
138
+
139
+ });
140
+
141
+ }
142
+
143
+ //遍历文件夹
144
+
145
+ function displayFile(param) {
146
+
147
+ //转换为绝对路径
148
+
149
+ //var param = path.resolve(param);
150
+
151
+ fs.stat(param, function (err, stats) {
152
+
153
+ //如果是目录的话,遍历目录下的文件信息
154
+
155
+ if (stats.isDirectory()) {
156
+
157
+ fs.readdir(param, function (err, file) {
158
+
159
+ file.forEach((e) => {
160
+
161
+ //遍历之后递归调用查看文件函数
162
+
163
+ //遍历目录得到的文件名称是不含路径的,需要将前面的绝对路径拼接
164
+
165
+ var absolutePath = path.join(param, e);
166
+
167
+ //var absolutePath = path.resolve(path.join(param, e));
168
+
169
+ displayFile(absolutePath)
170
+
171
+ })
172
+
173
+ })
174
+
175
+ } else {
176
+
177
+ //file2/这里是空间里的文件前缀
178
+
179
+ var key = 'file2';
180
+
181
+ var localFile = param;
182
+
183
+ if (!localFile.endsWith(".gz")) {
184
+
185
+ upload(key, localFile);
186
+
187
+ }
188
+
189
+ }
190
+
191
+ })
192
+
193
+ }
194
+
195
+ displayFile(dir);
196
+
197
+ }
198
+
199
+
200
+
201
+
202
+ module.exports = main
@@ -0,0 +1,4 @@
1
+ {
2
+ "publicPath": "v2_enterprise/202306211252/",
3
+ "base": "https://image.platform.smartfacade.com.cn/v2_enterprise/202306211252/"
4
+ }
package/def/qiniu.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "path": "struggler-cli-path",
3
+ "accessKey": "",
4
+ "secretKey": "",
5
+ "Bucket": "",
6
+ "zone": "",
7
+ "domain": "https://struggler-cli.cn/"
8
+ }
package/index.js ADDED
@@ -0,0 +1,41 @@
1
+ #! /usr/bin/env node
2
+ const { magentaBright } = require('chalk');
3
+ const figlet = require('figlet');
4
+ const clear = require('clear');
5
+ const { program } = require('commander')
6
+ const command = require("./command")
7
+
8
+
9
+ // 清除命令行
10
+ clear();
11
+
12
+ // 输出Logo
13
+ console.log(magentaBright(figlet.textSync('struggler-cli', { horizontalLayout: 'full' })),'\n\n');
14
+
15
+
16
+
17
+ program
18
+ .name('struggler-cli')
19
+ .description('CLI to Upload vite packaged files to Qiniu Cloud OSS.')
20
+ .version('0.0.1')
21
+ .option('-c, --config <path>', 'Specify the path to upload configuration file.', './command/qiniu.json')
22
+ .option('-d, --dir <path>', 'Specify the dir to upload.', './dist')
23
+
24
+
25
+ program
26
+ .command('init')
27
+ .description('Create upload configuration for specified path.')
28
+ .action(()=>{command.init(program.opts())})
29
+
30
+ program
31
+ .command('upload')
32
+ .description('Upload files under the specified file to Qiniu Cloud.')
33
+ .action(() => { command.upload(program.opts()) })
34
+
35
+ program
36
+ .command('addVersion')
37
+ .description('Package add version.')
38
+ .action(() => { command.addVersion(program.opts()) })
39
+
40
+ program.parse()
41
+
package/lib/config.js ADDED
@@ -0,0 +1,15 @@
1
+ let fs = require('fs');
2
+ let path = require('path')
3
+
4
+
5
+ module.exports = {
6
+ getConfig: (options) => {
7
+ return path.resolve(process.cwd(), './command/config.json')
8
+ },
9
+ getQiniuConfig: (options) => {
10
+ return path.resolve(process.cwd(), options.config || './command/qiniu.json')
11
+ },
12
+ getDir: (options) => {
13
+ return path.resolve(process.cwd(), options.dir || './dist')
14
+ },
15
+ };
package/lib/files.js ADDED
@@ -0,0 +1,40 @@
1
+ // files.js
2
+
3
+ const fs = require('fs');
4
+ const path = require('path');
5
+
6
+ module.exports = {
7
+ // 获取目录名称
8
+ getCurrentDirectoryBase: () => {
9
+ return path.basename(process.cwd());
10
+ },
11
+
12
+ // 判断目录是否存在
13
+ directoryExists: (filePath) => {
14
+ return fs.existsSync(filePath);
15
+ },
16
+
17
+
18
+ getJsonData: (filePath) => {
19
+ if (!fs.existsSync(filePath)) {return {}}
20
+ let data = fs.readFileSync(filePath);//fs读取文件
21
+ return JSON.parse(data);//转换为json对象
22
+ },
23
+
24
+ setJsonData: (filePath,data) => {
25
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
26
+ fs.writeFile(
27
+ filePath,
28
+ JSON.stringify(data, null, "\t"
29
+ ),
30
+ (err) => { }
31
+ )
32
+ },
33
+ setSyncJsonData: (filePath, data) => {
34
+ fs.mkdirSync(path.dirname(filePath), { recursive: true });
35
+ fs.writeFileSync(
36
+ filePath,
37
+ JSON.stringify(data, null, "\t"),
38
+ )
39
+ }
40
+ };
package/lib/prefix.js ADDED
@@ -0,0 +1,24 @@
1
+ let { getConfig } = require('./config')
2
+ let { getJsonData } = require('./files')
3
+
4
+ //七牛文件上传前缀,使用时间戳作为文件上传前缀
5
+ function formatDate(date) {
6
+ date = date || new Date()
7
+ const year = date.getFullYear()
8
+ const month = date.getMonth() + 1
9
+ const day = date.getDate()
10
+ const hours = date.getHours()
11
+ const minutes = date.getMinutes()
12
+
13
+ return `${year}${month < 10 ? `0${month}` : month}${day < 10 ? `0${day}` : day}${hours < 10 ? `0${hours}` : hours}${minutes < 10 ? `0${minutes}` : minutes}`
14
+ }
15
+
16
+
17
+
18
+ module.exports = {
19
+ prefix: (options)=>{
20
+ const { publicPath } = getJsonData(getConfig(options))
21
+ return publicPath
22
+ }
23
+
24
+ }
package/package.json ADDED
@@ -0,0 +1,29 @@
1
+ {
2
+ "name": "@struggler/cli",
3
+ "version": "1.0.0",
4
+ "description": "CLI to Upload vite packaged files to Qiniu Cloud OSS.",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "bin": {
10
+ "struggler-cli": "./index.js"
11
+ },
12
+ "keywords": [
13
+ "Qiniu",
14
+ "OSS"
15
+ ],
16
+ "author": "moqi(str@li.cm)",
17
+ "license": "ISC",
18
+ "dependencies": {
19
+ "chalk": "^v4.1.2",
20
+ "clear": "^0.1.0",
21
+ "commander": "^11.0.0",
22
+ "figlet": "^1.6.0",
23
+ "qiniu": "^7.8.0"
24
+ },
25
+ "publicConfig": {
26
+ "registry": "http://registry.npmjs.org/"
27
+ }
28
+
29
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "publicPath": "struggler-cli-path/202306211418/",
3
+ "base": "https://struggler-cli.cn/struggler-cli-path/202306211418/"
4
+ }
@@ -0,0 +1,8 @@
1
+ {
2
+ "path": "struggler-cli-path",
3
+ "accessKey": "",
4
+ "secretKey": "",
5
+ "Bucket": "",
6
+ "zone": "",
7
+ "domain": "https://struggler-cli.cn/"
8
+ }
Binary file
Binary file
Binary file
Binary file
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "struggler-cli",
3
+ "version": "1.0.3",
4
+ "description": "",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "bin": {
10
+ "struggler-cli": "./index.js"
11
+ },
12
+ "author": "",
13
+ "license": "ISC",
14
+ "dependencies": {
15
+ "chalk": "^v4.1.2",
16
+ "clear": "^0.1.0",
17
+ "commander": "^11.0.0",
18
+ "figlet": "^1.6.0",
19
+ "qiniu": "^7.8.0"
20
+ }
21
+ }
package/test/test.sh ADDED
@@ -0,0 +1,5 @@
1
+ struggler-cli upload -d ./dist3
2
+
3
+ struggler-cli addVersion
4
+
5
+ struggler-cli init