pixuireactcomponents 1.3.0 → 1.3.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pixuireactcomponents",
3
- "version": "1.3.0",
3
+ "version": "1.3.2",
4
4
  "description": "pixui react components",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -0,0 +1,184 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ // 自动生成图集, 生成一个Images.ts文件,引用目标目录下的所有图片, 方便直接使用
4
+ /*
5
+ commands:
6
+ create imgDirectoryPath
7
+ update imgDirectoryPath
8
+ */
9
+ var fs = require('fs');
10
+ var path = require('path');
11
+ // 支持的命令
12
+ var Commands = {
13
+ create: 'create',
14
+ update: 'update', //更新图集
15
+ };
16
+ var args = process.argv;
17
+ console.log('makeImage args:', args);
18
+ if (args.length != 4) {
19
+ console.error('the num of params is wrong!');
20
+ process.exit();
21
+ }
22
+ var command = args[2];
23
+ var imgsDirectoryPath = args[3];
24
+ var _unifiedPath = preProcessPath(imgsDirectoryPath);
25
+ switch (command) {
26
+ case Commands.create:
27
+ createImages(_unifiedPath);
28
+ break;
29
+ case Commands.update:
30
+ updateImages(_unifiedPath);
31
+ break;
32
+ default:
33
+ console.log('unsupport this command!!!');
34
+ }
35
+ function preProcessPath(directoryPath) {
36
+ var _path = directoryPath;
37
+ if (isStartWithLetter(_path)) {
38
+ _path = './' + _path;
39
+ }
40
+ else if (_path.startsWith('../')) {
41
+ _path = './' + _path;
42
+ }
43
+ if (_path.endsWith('/'))
44
+ _path = _path.substring(0, _path.length - 1);
45
+ return _path;
46
+ }
47
+ function isStartWithLetter(path) {
48
+ if (path.length == 0)
49
+ return false;
50
+ var firstChar = path.charAt(0);
51
+ if ((firstChar >= 'a' && firstChar <= 'z') || (firstChar >= 'A' && firstChar <= 'Z'))
52
+ return true;
53
+ return false;
54
+ }
55
+ function createImages(directoryPath) {
56
+ if (!isDirectoryExist(directoryPath)) {
57
+ console.error('images directory is wrong!');
58
+ return;
59
+ }
60
+ var imgUrls = [];
61
+ collectImageUrls(directoryPath, imgUrls);
62
+ console.log(imgUrls);
63
+ generateImage(directoryPath, imgUrls);
64
+ }
65
+ function isDirectoryExist(directory) {
66
+ var stat;
67
+ try {
68
+ stat = fs.lstatSync(directory);
69
+ }
70
+ catch (e) {
71
+ return false;
72
+ }
73
+ return stat.isDirectory();
74
+ }
75
+ function collectImageUrls(directoryPath, imgUrls) {
76
+ var files = fs.readdirSync(directoryPath);
77
+ for (var i = 0; i < files.length; i++) {
78
+ var current = path.join(directoryPath, files[i]).replaceAll('\\', '/');
79
+ if (isDirectoryExist(current)) { // if it's a directory ,recursive
80
+ collectImageUrls(current, imgUrls);
81
+ }
82
+ else {
83
+ if (!isAnImg(current))
84
+ continue;
85
+ if (isStartWithLetter(current) || current.startsWith('../'))
86
+ current = './' + current;
87
+ imgUrls.push(current);
88
+ }
89
+ }
90
+ }
91
+ function isAnImg(imgUrl) {
92
+ if (imgUrl.endsWith('.png') || imgUrl.endsWith('.jpg') || imgUrl.endsWith('.jpeg') || imgUrl.endsWith('.gif'))
93
+ return true;
94
+ return false;
95
+ }
96
+ function generateImage(directoryPath, imgUrls, keyMap) {
97
+ var out = 'Images.ts';
98
+ fs.writeFileSync(out, '');
99
+ // write import
100
+ for (var i = 0; i < imgUrls.length; i++) {
101
+ var current = imgUrls[i];
102
+ var imgKey = generateImgKey(directoryPath, current);
103
+ if (current.startsWith('../'))
104
+ current = './' + current;
105
+ // console.warn(directoryPath, current, imgKey)
106
+ fs.writeFileSync(out, "import ".concat(imgKey, " from \"").concat(current, "\";\n"), { flag: 'a+' });
107
+ }
108
+ fs.writeFileSync(out, '\n\n', { flag: 'a+' });
109
+ //write export
110
+ fs.writeFileSync(out, '\nexport const Images = {\n', { flag: 'a+' });
111
+ for (var i = 0; i < imgUrls.length; i++) {
112
+ var current = imgUrls[i];
113
+ var imgKey = generateImgKey(directoryPath, current);
114
+ var imgNewname = keyMap == null || keyMap.get(imgKey) == undefined ? imgKey : keyMap.get(imgKey);
115
+ fs.writeFileSync(out, " ".concat(imgNewname, ": ").concat(imgKey, ",\n"), { flag: 'a+' });
116
+ }
117
+ fs.writeFileSync(out, '}\n\n', { flag: 'a+' });
118
+ console.log(keyMap == null ? 'generate' : 'update', ' Images.ts successfully!');
119
+ }
120
+ function generateImgKey(directoryPath, imgUrl) {
121
+ var key = imgUrl.substring(directoryPath.length);
122
+ key = key.replace(/\//g, '_');
123
+ if (key.startsWith('_'))
124
+ key = key.substring(1);
125
+ var extname = path.extname(key);
126
+ key = key.substring(0, key.length - extname.length);
127
+ return key;
128
+ }
129
+ function updateImages(directoryPath) {
130
+ if (!isDirectoryExist(directoryPath)) {
131
+ console.error('images directory is wrong!');
132
+ return;
133
+ }
134
+ var imagePath = 'Images.ts';
135
+ if (!isFileExist(imagePath)) {
136
+ console.error("there is no Images.ts in current path, can't match update commmand!");
137
+ return;
138
+ }
139
+ var keyMaps = readOldImagesInfo(imagePath);
140
+ var imgUrls = [];
141
+ collectImageUrls(directoryPath, imgUrls);
142
+ console.log(imgUrls);
143
+ generateImage(directoryPath, imgUrls, keyMaps);
144
+ }
145
+ function isFileExist(path) {
146
+ var stat;
147
+ try {
148
+ stat = fs.lstatSync(path);
149
+ }
150
+ catch (e) {
151
+ return false;
152
+ }
153
+ return stat.isFile();
154
+ }
155
+ function readOldImagesInfo(imagePath) {
156
+ var imageContent = fs.readFileSync(imagePath, { encoding: 'utf8' });
157
+ var lines = imageContent.split('\n');
158
+ var importNames = [];
159
+ for (var i = 0; i < lines.length; i++) {
160
+ var currentLine = lines[i];
161
+ if (currentLine.startsWith('import')) {
162
+ var result = currentLine.match(/import (\w+) from/);
163
+ var importName = result[1];
164
+ importNames.push(importName);
165
+ }
166
+ }
167
+ console.log(importNames);
168
+ var importName2UseName = new Map();
169
+ for (var i = 0; i < lines.length; i++) {
170
+ var currentLine = lines[i];
171
+ var result = currentLine.match(/(\w+): (\w+)/);
172
+ if (result == null)
173
+ continue;
174
+ var useName = result[1];
175
+ var importName = result[2];
176
+ importName2UseName.set(importName, useName);
177
+ }
178
+ if (importName2UseName.size != importNames.length) {
179
+ console.log('the Images.ts’s format is not correct!');
180
+ console.log('importNames len:', importNames.length, ' useNames len:', importName2UseName.size);
181
+ return;
182
+ }
183
+ return importName2UseName;
184
+ }
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file