@shijiu/jsview 1.9.909 → 1.9.914
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/dom/bin/jsview-dom-browser.min.js +1 -1
- package/dom/bin/jsview-dom-native.min.js +1 -1
- package/dom/bin/jsview-engine-js-browser.min.js +1 -1
- package/package.json +1 -1
- package/tools/jsview-batch-upgrade.mjs +382 -0
- package/tools/jsview-common.mjs +11 -0
- package/tools/jsview-upgrade.mjs +0 -353
package/package.json
CHANGED
|
@@ -0,0 +1,382 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
import fs from 'fs';
|
|
5
|
+
import path from 'path';
|
|
6
|
+
import url from 'url';
|
|
7
|
+
import {
|
|
8
|
+
askAndAnswer,
|
|
9
|
+
checkNodeVersion,
|
|
10
|
+
execCommand,
|
|
11
|
+
parseArguments
|
|
12
|
+
} from './jsview-common.mjs';
|
|
13
|
+
|
|
14
|
+
function getOptions(argv)
|
|
15
|
+
{
|
|
16
|
+
const options = {
|
|
17
|
+
stagePixels: '1',
|
|
18
|
+
stageAnimation: '2',
|
|
19
|
+
stageHashHistory: '3',
|
|
20
|
+
stageAudiotrack: '4',
|
|
21
|
+
stageImportPostfix: '5',
|
|
22
|
+
stageAppConfig: '6',
|
|
23
|
+
stageMainEntry: '7',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const scriptPath = path.resolve(process.cwd(), process.argv[1]);
|
|
27
|
+
options.rootDir = scriptPath.replaceAll(/node_modules\/@shijiu\/jsview.*/g, '');
|
|
28
|
+
|
|
29
|
+
options.stages = argv.stages?.split(',') ?? [];
|
|
30
|
+
|
|
31
|
+
const defaultUpgradeDir = path.resolve(options.rootDir, 'src');
|
|
32
|
+
options.upgradeDir = path.resolve(process.cwd(), argv.dir ?? defaultUpgradeDir);
|
|
33
|
+
if (fs.existsSync(options.upgradeDir) == false) {
|
|
34
|
+
console.error('JsView Error: Upgrade dir [' + options.upgradeDir + '] is not exists.');
|
|
35
|
+
process.exit(1);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
console.info();
|
|
39
|
+
console.info('Upgrade JsView source: ');
|
|
40
|
+
console.info(' Project folder: ' + options.rootDir);
|
|
41
|
+
console.info('=====================================');
|
|
42
|
+
console.info(' Upgrade folder: ' + path.relative(options.rootDir, options.upgradeDir));
|
|
43
|
+
console.info(' Stages: [ ' + options.stages.join(', ') + ' ]');
|
|
44
|
+
console.info('=====================================');
|
|
45
|
+
|
|
46
|
+
console.info();
|
|
47
|
+
console.info('Are you sure to upgrade it?');
|
|
48
|
+
const answer = askAndAnswer('(y/N) ');
|
|
49
|
+
if (answer.substring(0, 1) != 'y' && answer.substring(0, 3) != 'yes') {
|
|
50
|
+
console.error('JsView Info: User cancelled.')
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
options.sourcePathList = getFilesRecursive(options.upgradeDir);
|
|
55
|
+
|
|
56
|
+
return options;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function getFilesRecursive(dirPath) {
|
|
60
|
+
const ignoreFileList = [
|
|
61
|
+
'.git',
|
|
62
|
+
'.gitignore',
|
|
63
|
+
'node_modules',
|
|
64
|
+
'package.json',
|
|
65
|
+
'package-lock.json'
|
|
66
|
+
];
|
|
67
|
+
const fileExtList = [
|
|
68
|
+
'!.d.ts',
|
|
69
|
+
'!.min.js',
|
|
70
|
+
'.css',
|
|
71
|
+
'.js',
|
|
72
|
+
'.json',
|
|
73
|
+
'.mjs',
|
|
74
|
+
'.ts',
|
|
75
|
+
'.tsx',
|
|
76
|
+
'.vue',
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
let filePathList = [];
|
|
80
|
+
|
|
81
|
+
const fileNameList = fs.readdirSync(dirPath);
|
|
82
|
+
for (const fileName of fileNameList) {
|
|
83
|
+
if (ignoreFileList.includes(fileName)) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const filePath = path.join(dirPath, fileName);
|
|
88
|
+
if (fs.statSync(filePath).isDirectory()) {
|
|
89
|
+
const subFilePathList = getFilesRecursive(filePath);
|
|
90
|
+
filePathList = [...filePathList, ...subFilePathList];
|
|
91
|
+
} else {
|
|
92
|
+
let needUpgrade = false;
|
|
93
|
+
for (let fileExtName of fileExtList) {
|
|
94
|
+
if (fileExtName.startsWith('!')) { // 强制忽略该文件
|
|
95
|
+
fileExtName = fileExtName.substring(1);
|
|
96
|
+
if (filePath.endsWith(fileExtName)) {
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
if (filePath.endsWith(fileExtName)) {
|
|
101
|
+
needUpgrade = true;
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
if (needUpgrade == false) {
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const fileExtName = path.extname(filePath);
|
|
111
|
+
if (fileExtList.includes(fileExtName) == false) {
|
|
112
|
+
continue;
|
|
113
|
+
} else if (fileExtList.includes('!' + fileExtName)) {
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
filePathList.push(filePath);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return filePathList;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function replaceStageContent(options, sourcePathList, msgPrefix,
|
|
124
|
+
repleceRegExp, replaceto) {
|
|
125
|
+
for (const filePath of sourcePathList) {
|
|
126
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
127
|
+
|
|
128
|
+
const fixedContent = content.replaceAll(repleceRegExp, replaceto);
|
|
129
|
+
if (fixedContent === content) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
fs.writeFileSync(filePath, fixedContent, 'utf8');
|
|
134
|
+
console.info(msgPrefix + ': Upgraded ' + path.relative(options.rootDir, filePath));
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return 0;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function removeStageFile(options, sourcePathList, msgPrefix,
|
|
141
|
+
fromExtNameList, toExtName)
|
|
142
|
+
{
|
|
143
|
+
for (const filePath of sourcePathList) {
|
|
144
|
+
let fromExtName = null;
|
|
145
|
+
for (const extName of fromExtNameList) {
|
|
146
|
+
if (filePath.endsWith(extName)) {
|
|
147
|
+
fromExtName = extName;
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
if (fromExtName == null) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
const toFilePath = filePath.replace(new RegExp(fromExtName + '$'), toExtName);
|
|
156
|
+
fs.renameSync(filePath, toFilePath)
|
|
157
|
+
|
|
158
|
+
// json => mjs
|
|
159
|
+
if (fromExtName == '.json') {
|
|
160
|
+
const content = fs.readFileSync(toFilePath, 'utf8');
|
|
161
|
+
const fixedContent = 'export default ' + content;
|
|
162
|
+
fs.writeFileSync(toFilePath, fixedContent, 'utf8');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
console.info(msgPrefix + ': Upgraded ' + path.relative(options.rootDir, filePath));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return 0;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function upgradeStagePixel(options) {
|
|
172
|
+
const msgPrefix = 'Stage ' + options.stagePixels;
|
|
173
|
+
console.info(msgPrefix + ': Upgrading pixels...');
|
|
174
|
+
|
|
175
|
+
const ret = replaceStageContent(options, options.sourcePathList, msgPrefix,
|
|
176
|
+
/([0-9}])px/g, '$1');
|
|
177
|
+
return ret;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function upgradeStageAnimation(options)
|
|
181
|
+
{
|
|
182
|
+
const msgPrefix = 'Stage ' + options.stageAnimation;
|
|
183
|
+
console.info(msgPrefix + ': Upgrading animation...');
|
|
184
|
+
|
|
185
|
+
const ret = replaceStageContent(options, options.sourcePathList, msgPrefix,
|
|
186
|
+
':onAnimationEnd', '@animationend');
|
|
187
|
+
|
|
188
|
+
return ret;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
function upgradeStageHashHistory(options)
|
|
192
|
+
{
|
|
193
|
+
const msgPrefix = 'Stage ' + options.stageHashHistory;
|
|
194
|
+
console.info(msgPrefix + ': Upgrading hash history...');
|
|
195
|
+
|
|
196
|
+
const ret = replaceStageContent(options, options.sourcePathList, msgPrefix,
|
|
197
|
+
'createJsvHashHistory', 'jsvCreateHashHistory');
|
|
198
|
+
|
|
199
|
+
return ret;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
function upgradeStageAudiotrack(options)
|
|
203
|
+
{
|
|
204
|
+
const msgPrefix = 'Stage ' + options.stageAudiotrack;
|
|
205
|
+
console.info(msgPrefix + ': Upgrading audiotrack...');
|
|
206
|
+
|
|
207
|
+
const ret = replaceStageContent(options, options.sourcePathList, msgPrefix,
|
|
208
|
+
'<audiotrack', '<jsv-audiotrack');
|
|
209
|
+
|
|
210
|
+
return ret;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function upgradeStageImportPostfix(options)
|
|
214
|
+
{
|
|
215
|
+
const msgPrefix = 'Stage ' + options.stageImportPostfix;
|
|
216
|
+
console.info(msgPrefix + ': upgrading import postfix...');
|
|
217
|
+
|
|
218
|
+
const amendImportPostfix = function(currentDir, content) {
|
|
219
|
+
if (content.trim().startsWith('import') == false) {
|
|
220
|
+
return content;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
let importFromFile = content.replaceAll(/.*'(.*)'.*/g, '$1');
|
|
224
|
+
if (importFromFile === content) {
|
|
225
|
+
importFromFile = content.replaceAll(/.*"\(\)".*/g, "$1");
|
|
226
|
+
}
|
|
227
|
+
if (importFromFile === content) {
|
|
228
|
+
return content;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
let fixedContent = content;
|
|
232
|
+
|
|
233
|
+
const importFilePath = path.resolve(currentDir, importFromFile);
|
|
234
|
+
if (fs.existsSync(importFilePath + '.js')) {
|
|
235
|
+
// 执行到此处,则需要给给import的文件加上js尾缀。当同时存在.js和.vue时,.js优先。
|
|
236
|
+
fixedContent = content.replace(importFromFile, importFromFile + '.js');
|
|
237
|
+
} else if (fs.existsSync(importFilePath + '.vue')) {
|
|
238
|
+
// 执行到此处,则需要给给import的文件加上vue尾缀。
|
|
239
|
+
fixedContent = content.replace(importFromFile, importFromFile + '.vue');
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return fixedContent;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
for (const filePath of options.sourcePathList) {
|
|
246
|
+
const currentDir = path.dirname(filePath);
|
|
247
|
+
const content = fs.readFileSync(filePath, 'utf8');
|
|
248
|
+
|
|
249
|
+
const lineContentList = content.split('\n');
|
|
250
|
+
for (let idx = 0; idx < lineContentList.length; idx++) {
|
|
251
|
+
let lineContent = lineContentList[idx];
|
|
252
|
+
lineContent = amendImportPostfix(currentDir, lineContent);
|
|
253
|
+
lineContentList[idx] = lineContent;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
const fixedContent = lineContentList.join('\n');
|
|
257
|
+
if (fixedContent == content) {
|
|
258
|
+
continue;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
fs.writeFileSync(filePath, fixedContent, 'utf8');
|
|
262
|
+
console.info(msgPrefix + ': Upgraded ' + path.relative(options.rootDir, filePath));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return 0;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
function upgradeStageAppConfig(options)
|
|
269
|
+
{
|
|
270
|
+
const msgPrefix = 'Stage ' + options.stageAppConfig;
|
|
271
|
+
console.info(msgPrefix + ': Upgrading appConfig...');
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
const appConfigDir = path.resolve(options.upgradeDir, 'appConfig');
|
|
275
|
+
if (fs.existsSync(appConfigDir) == false) {
|
|
276
|
+
console.error(msgPrefix + ': Failed to upgrading appConfig, appConfig folder is not found.');
|
|
277
|
+
process.exit(1);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const appConfigFilePath = path.resolve(appConfigDir, 'app_config.json');
|
|
281
|
+
|
|
282
|
+
let ret = removeStageFile(options, [ appConfigFilePath ], msgPrefix,
|
|
283
|
+
[ 'app_config.json' ], 'app.config.json');
|
|
284
|
+
if (ret < 0) {
|
|
285
|
+
console.error(msgPrefix + ': Failed to upgrading app config file, is not found.');
|
|
286
|
+
process.exit(1);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
const sourcePathList = getFilesRecursive(appConfigDir);
|
|
290
|
+
|
|
291
|
+
ret = removeStageFile(options, sourcePathList, msgPrefix,
|
|
292
|
+
[ '.js', '.json' ], '.mjs');
|
|
293
|
+
|
|
294
|
+
return ret;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function upgradeStageMainEntry(options)
|
|
298
|
+
{
|
|
299
|
+
const msgPrefix = 'Stage ' + options.stageMainEntry;
|
|
300
|
+
console.info(msgPrefix + ': Upgrading main entry...');
|
|
301
|
+
|
|
302
|
+
const mainEntryFilePath = path.resolve(options.upgradeDir, 'main.ts');
|
|
303
|
+
const fixedMainEntryFilePath = path.resolve(options.upgradeDir, 'main.tsx');
|
|
304
|
+
if (fs.existsSync(mainEntryFilePath) == false) {
|
|
305
|
+
if (fs.existsSync(fixedMainEntryFilePath)) {
|
|
306
|
+
return 0;
|
|
307
|
+
}
|
|
308
|
+
console.error(msgPrefix + ': Failed to upgrading main entry, main.ts is not found.');
|
|
309
|
+
process.exit(1);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const ret = removeStageFile(options, [ mainEntryFilePath ], msgPrefix,
|
|
313
|
+
[ 'main.ts' ], 'main.tsx');
|
|
314
|
+
|
|
315
|
+
return ret;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function main(argv)
|
|
319
|
+
{
|
|
320
|
+
checkNodeVersion();
|
|
321
|
+
|
|
322
|
+
try {
|
|
323
|
+
const options = getOptions(argv);
|
|
324
|
+
|
|
325
|
+
// Stage 1
|
|
326
|
+
if (options.stages.includes(options.stagePixels)) {
|
|
327
|
+
upgradeStagePixel(options);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Stage 2
|
|
331
|
+
if (options.stages.includes(options.stageAnimation)) {
|
|
332
|
+
upgradeStageAnimation(options);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// Stage 3
|
|
336
|
+
if (options.stages.includes(options.stageHashHistory)) {
|
|
337
|
+
upgradeStageHashHistory(options);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Stage 4
|
|
341
|
+
if (options.stages.includes(options.stageAudiotrack)) {
|
|
342
|
+
upgradeStageAudiotrack(options);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Stage 5
|
|
346
|
+
if (options.stages.includes(options.stageImportPostfix)) {
|
|
347
|
+
upgradeStageImportPostfix(options);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// Stage 6
|
|
351
|
+
if (options.stages.includes(options.stageAppConfig)) {
|
|
352
|
+
upgradeStageAppConfig(options);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Stage 7
|
|
356
|
+
if (options.stages.includes(options.stageMainEntry)) {
|
|
357
|
+
upgradeStageMainEntry(options);
|
|
358
|
+
}
|
|
359
|
+
} catch (ex) {
|
|
360
|
+
console.error('JsView Error: Failed to update jsview source!');
|
|
361
|
+
console.error(ex);
|
|
362
|
+
process.exit(1);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
console.error('JsView Info: Done!');
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
const requiredUsages = {
|
|
369
|
+
'--stages': `Upgrade stages, separate by comma(,). The value means the following:
|
|
370
|
+
1. Fix pixels. "[0-9}]px => [0-9}]"
|
|
371
|
+
2. Fix animation. ":onAnimationEnd => @animationend"
|
|
372
|
+
3. Fix hash history. "createJsvHashHistory => jsvCreateHashHistory"
|
|
373
|
+
4. Fix audiotrack. "<audiotrack => <jsv-audiotrack"
|
|
374
|
+
5. Fix import postfix. "import xxx from 'xxx' => import xxx from 'xxx.vue'"
|
|
375
|
+
6. Fix appConfig. "xxx.config.js => xxx.config.mjs"
|
|
376
|
+
7. Fix main.ts. "main.ts => main.tsx"`,
|
|
377
|
+
};
|
|
378
|
+
const optionalUsages = {
|
|
379
|
+
'--dir': 'Upgrade directory, default is [src].',
|
|
380
|
+
};
|
|
381
|
+
const argv = parseArguments(requiredUsages, optionalUsages, false);
|
|
382
|
+
main(argv)
|
package/tools/jsview-common.mjs
CHANGED
|
@@ -320,7 +320,18 @@ function checkNodeVersion(minMajorVersion = 16)
|
|
|
320
320
|
}
|
|
321
321
|
}
|
|
322
322
|
|
|
323
|
+
function askAndAnswer(question)
|
|
324
|
+
{
|
|
325
|
+
process.stdout.write(question)
|
|
326
|
+
let buffer = Buffer.alloc(100)
|
|
327
|
+
fs.readSync(0, buffer, 0, 100)
|
|
328
|
+
let answer = buffer.toString().toLowerCase().replace('\n', '');
|
|
329
|
+
answer = answer.substring(0, answer.indexOf('\0'));
|
|
330
|
+
return answer;
|
|
331
|
+
}
|
|
332
|
+
|
|
323
333
|
export {
|
|
334
|
+
askAndAnswer,
|
|
324
335
|
checkNodeVersion,
|
|
325
336
|
cpSync,
|
|
326
337
|
execCommand,
|