@shijiu/jsview 1.9.888 → 1.9.912

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,335 @@
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
+ stageAudiotrack: '3',
20
+ stageImportPostfix: '4',
21
+ stageAppConfig: '5',
22
+ stageMainEntry: '6',
23
+ };
24
+
25
+ const scriptPath = path.resolve(process.cwd(), process.argv[1]);
26
+ options.rootDir = scriptPath.replaceAll(/node_modules\/@shijiu\/jsview.*/g, '');
27
+
28
+ options.stages = argv.stages?.split(',') ?? [];
29
+
30
+ const defaultUpgradeDir = path.resolve(options.rootDir, 'src');
31
+ options.upgradeDir = path.resolve(process.cwd(), argv.dir ?? defaultUpgradeDir);
32
+ if (fs.existsSync(options.upgradeDir) == false) {
33
+ console.error('JsView Error: Upgrade dir [' + options.upgradeDir + '] is not exists.');
34
+ process.exit(1);
35
+ }
36
+
37
+ console.info();
38
+ console.info('Upgrade JsView source: ');
39
+ console.info(' Project folder: ' + options.rootDir);
40
+ console.info('=====================================');
41
+ console.info(' Upgrade folder: ' + path.relative(options.rootDir, options.upgradeDir));
42
+ console.info(' Stages: [ ' + options.stages.join(', ') + ' ]');
43
+ console.info('=====================================');
44
+
45
+ console.info();
46
+ console.info('Are you sure to upgrade it?');
47
+ const answer = askAndAnswer('(y/N) ');
48
+ if (answer.substring(0, 1) != 'y' && answer.substring(0, 3) != 'yes') {
49
+ console.error('JsView Info: User cancelled.')
50
+ process.exit(1);
51
+ }
52
+
53
+ options.sourcePathList = getFilesRecursive(options.upgradeDir);
54
+
55
+ return options;
56
+ }
57
+
58
+ function getFilesRecursive(dirPath) {
59
+ const ignoreFileList = [
60
+ '.git',
61
+ '.gitignore',
62
+ 'node_modules',
63
+ 'package.json',
64
+ 'package-lock.json'
65
+ ];
66
+ const fileExtList = [
67
+ '!.d.ts',
68
+ '!.min.js',
69
+ '.css',
70
+ '.js',
71
+ '.json',
72
+ '.mjs',
73
+ '.ts',
74
+ '.tsx',
75
+ '.vue',
76
+ ];
77
+
78
+ let filePathList = [];
79
+
80
+ const fileNameList = fs.readdirSync(dirPath);
81
+ for (const fileName of fileNameList) {
82
+ if (ignoreFileList.includes(fileName)) {
83
+ continue;
84
+ }
85
+
86
+ const filePath = path.join(dirPath, fileName);
87
+ if (fs.statSync(filePath).isDirectory()) {
88
+ const subFilePathList = getFilesRecursive(filePath);
89
+ filePathList = [...filePathList, ...subFilePathList];
90
+ } else {
91
+ let needUpgrade = false;
92
+ for (let fileExtName of fileExtList) {
93
+ if (fileExtName.startsWith('!')) { // 强制忽略该文件
94
+ fileExtName = fileExtName.substring(1);
95
+ if (filePath.endsWith(fileExtName)) {
96
+ break;
97
+ }
98
+ } else {
99
+ if (filePath.endsWith(fileExtName)) {
100
+ needUpgrade = true;
101
+ break;
102
+ }
103
+ }
104
+ }
105
+ if (needUpgrade == false) {
106
+ continue;
107
+ }
108
+
109
+ const fileExtName = path.extname(filePath);
110
+ if (fileExtList.includes(fileExtName) == false) {
111
+ continue;
112
+ } else if (fileExtList.includes('!' + fileExtName)) {
113
+ continue;
114
+ }
115
+ filePathList.push(filePath);
116
+ }
117
+ }
118
+
119
+ return filePathList;
120
+ }
121
+
122
+ function replaceStageContent(options, sourcePathList, msgPrefix,
123
+ repleceRegExp, replaceto) {
124
+ for (const filePath of sourcePathList) {
125
+ const content = fs.readFileSync(filePath, 'utf8');
126
+
127
+ const fixedContent = content.replaceAll(repleceRegExp, replaceto);
128
+ if (fixedContent === content) {
129
+ continue;
130
+ }
131
+
132
+ fs.writeFileSync(filePath, fixedContent, 'utf8');
133
+ console.info(msgPrefix + ': Upgraded ' + path.relative(options.rootDir, filePath) + '.');
134
+ }
135
+
136
+ return 0;
137
+ }
138
+
139
+ function removeStageFile(options, sourcePathList, msgPrefix,
140
+ fromExtName, toExtName)
141
+ {
142
+ for (const filePath of sourcePathList) {
143
+ if (filePath.endsWith(fromExtName) == false) {
144
+ continue;
145
+ }
146
+
147
+ const toFilePath = filePath.replace(new RegExp(fromExtName + '$'), toExtName);
148
+ fs.renameSync(filePath, toFilePath)
149
+ console.info(msgPrefix + ': Upgraded ' + path.relative(options.rootDir, filePath) + '.');
150
+ }
151
+
152
+ return 0;
153
+ }
154
+
155
+ function upgradeStagePixel(options) {
156
+ const msgPrefix = 'Stage ' + options.stagePixels;
157
+ console.info(msgPrefix + ': Upgrading pixels...');
158
+
159
+ const ret = replaceStageContent(options, options.sourcePathList, msgPrefix,
160
+ /([0-9])px/g, '$1');
161
+ return ret;
162
+ }
163
+
164
+ function upgradeStageAnimation(options)
165
+ {
166
+ const msgPrefix = 'Stage ' + options.stageAnimation;
167
+ console.info(msgPrefix + ': Upgrading animation...');
168
+
169
+ const ret = replaceStageContent(options, options.sourcePathList, msgPrefix,
170
+ ':onAnimationEnd', '@animationend');
171
+
172
+ return ret;
173
+ }
174
+
175
+ function upgradeStageAudiotrack(options)
176
+ {
177
+ const msgPrefix = 'Stage ' + options.stageAudiotrack;
178
+ console.info(msgPrefix + ': Upgrading audiotrack...');
179
+
180
+ const ret = replaceStageContent(options, options.sourcePathList, msgPrefix,
181
+ '<audiotrack', '<jsv-audiotrack');
182
+
183
+ return ret;
184
+ }
185
+
186
+ function upgradeStageImportPostfix(options)
187
+ {
188
+ const msgPrefix = 'Stage ' + options.stageImportPostfix;
189
+ console.info(msgPrefix + ': upgrading import postfix...');
190
+
191
+ const amendImportPostfix = function(currentDir, content) {
192
+ if (content.trim().startsWith('import') == false) {
193
+ return content;
194
+ }
195
+
196
+ let importFromFile = content.replaceAll(/.*'(.*)'.*/g, '$1');
197
+ if (importFromFile === content) {
198
+ importFromFile = content.replaceAll(/.*"\(\)".*/g, "$1");
199
+ }
200
+ if (importFromFile === content) {
201
+ return content;
202
+ }
203
+ let importFilePath = path.resolve(currentDir, importFromFile);
204
+ importFilePath += '.vue';
205
+ if (fs.existsSync(importFilePath) == false) {
206
+ return content;
207
+ }
208
+
209
+ // 执行到此处,则需要给给import的文件加上vue尾缀。
210
+ const fixedContent = content.replace(importFromFile, importFromFile + '.vue');
211
+
212
+ return fixedContent;
213
+ }
214
+
215
+ for (const filePath of options.sourcePathList) {
216
+ const currentDir = path.dirname(filePath);
217
+ const content = fs.readFileSync(filePath, 'utf8');
218
+
219
+ const lineContentList = content.split('\n');
220
+ for (let idx = 0; idx < lineContentList.length; idx++) {
221
+ let lineContent = lineContentList[idx];
222
+ lineContent = amendImportPostfix(currentDir, lineContent);
223
+ lineContentList[idx] = lineContent;
224
+ }
225
+
226
+ const fixedContent = lineContentList.join('\n');
227
+ if (fixedContent == content) {
228
+ continue;
229
+ }
230
+
231
+ fs.writeFileSync(filePath, fixedContent, 'utf8');
232
+ console.info(msgPrefix + ': Upgraded ' + path.relative(options.rootDir, filePath) + '.');
233
+ }
234
+
235
+ return 0;
236
+ }
237
+
238
+ function upgradeStageAppConfig(options)
239
+ {
240
+ const msgPrefix = 'Stage ' + options.stageAppConfig;
241
+ console.info(msgPrefix + ': Upgrading appConfig...');
242
+
243
+
244
+ const appConfigDir = path.resolve(options.upgradeDir, 'appConfig');
245
+ if (fs.existsSync(appConfigDir) == false) {
246
+ console.error(msgPrefix + ': Failed to upgrading appConfig, appConfig folder is not found.');
247
+ process.exit(1);
248
+ }
249
+
250
+ const sourcePathList = getFilesRecursive(appConfigDir);
251
+
252
+ const ret = removeStageFile(options, sourcePathList, msgPrefix,
253
+ '.js', '.mjs');
254
+
255
+ return ret;
256
+ }
257
+
258
+ function upgradeStageMainEntry(options)
259
+ {
260
+ const msgPrefix = 'Stage ' + options.stageMainEntry;
261
+ console.info(msgPrefix + ': Upgrading main entry...');
262
+
263
+ const mainEntryFilePath = path.resolve(options.upgradeDir, 'main.ts');
264
+ const fixedMainEntryFilePath = path.resolve(options.upgradeDir, 'main.tsx');
265
+ if (fs.existsSync(mainEntryFilePath) == false
266
+ && fs.existsSync(fixedMainEntryFilePath) == false) {
267
+ console.error(msgPrefix + ': Failed to upgrading main entry, main.ts is not found.');
268
+ process.exit(1);
269
+ }
270
+
271
+ const ret = removeStageFile(options, [ mainEntryFilePath ], msgPrefix,
272
+ 'main.ts', 'main.tsx');
273
+
274
+ return ret;
275
+ }
276
+
277
+ function main(argv)
278
+ {
279
+ checkNodeVersion();
280
+
281
+ try {
282
+ const options = getOptions(argv);
283
+
284
+ // Stage 1
285
+ if (options.stages.includes(options.stagePixels)) {
286
+ upgradeStagePixel(options);
287
+ }
288
+
289
+ // Stage 2
290
+ if (options.stages.includes(options.stageAnimation)) {
291
+ upgradeStageAnimation(options);
292
+ }
293
+
294
+ // Stage 3
295
+ if (options.stages.includes(options.stageAudiotrack)) {
296
+ upgradeStageAudiotrack(options);
297
+ }
298
+
299
+ // Stage 4
300
+ if (options.stages.includes(options.stageImportPostfix)) {
301
+ upgradeStageImportPostfix(options);
302
+ }
303
+
304
+ // Stage 5
305
+ if (options.stages.includes(options.stageAppConfig)) {
306
+ upgradeStageAppConfig(options);
307
+ }
308
+
309
+ // Stage 6
310
+ if (options.stages.includes(options.stageMainEntry)) {
311
+ upgradeStageMainEntry(options);
312
+ }
313
+ } catch (ex) {
314
+ console.error('JsView Error: Failed to update jsview source!');
315
+ console.error(ex);
316
+ process.exit(1);
317
+ }
318
+
319
+ console.error('JsView Info: Done!');
320
+ }
321
+
322
+ const requiredUsages = {
323
+ '--stages': `Upgrade stages, separate by comma(,). The value means the following:
324
+ 1. Fix pixels.
325
+ 2. Fix animation.
326
+ 3. Fix audiotrack.
327
+ 4. Fix import postfix.
328
+ 5. Fix appConfig.
329
+ 6. Rename main.ts to main.tsx.`,
330
+ };
331
+ const optionalUsages = {
332
+ '--dir': 'Upgrade directory, default is [src].',
333
+ };
334
+ const argv = parseArguments(requiredUsages, optionalUsages, false);
335
+ main(argv)
@@ -1,9 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict';
3
3
 
4
- const childProcess = require('child_process');
5
- const fs = require('fs');
6
- const path = require('path');
4
+ import childProcess from 'child_process';
5
+ import fs from 'fs';
6
+ import path from 'path';
7
7
 
8
8
  function printArgumentsUsages(requiredUsages, optionalUsages, helpUsages)
9
9
  {
@@ -41,6 +41,7 @@ function parseArguments(requiredUsages = {},
41
41
  optionalUsages = {},
42
42
  withoutUnparsed = true)
43
43
  {
44
+ const keySeparator = '|';
44
45
  const formatKey = (key) => {
45
46
  let formattedKey = key.trim().replace(/^-*/, '');
46
47
  formattedKey = formattedKey.replace(/-(.)/g, (x) => x[1].toUpperCase())
@@ -57,9 +58,25 @@ function parseArguments(requiredUsages = {},
57
58
  }
58
59
  return formattedValue;
59
60
  }
60
- const checkAliases = (aliases, options) => {
61
+ const hasKey = (usages, key) => {
62
+ const fmtKey = formatKey(key);
63
+ for (const checker in usages) {
64
+ const checkerList = checker.split(keySeparator);
65
+ for (const alias of checkerList) {
66
+ const fmtAlias = formatKey(alias);
67
+ if (fmtAlias === fmtKey) {
68
+ return true;
69
+ }
70
+ }
71
+ }
72
+
73
+ return false;
74
+ }
75
+ const checkAliases = (checker, options) => {
76
+ const checkerList = checker.split(keySeparator);
77
+
61
78
  const formattedAliases = [];
62
- for (const alias of aliases) {
79
+ for (const alias of checkerList) {
63
80
  const fmtAlias = formatKey(alias);
64
81
  formattedAliases.push(fmtAlias);
65
82
  }
@@ -112,31 +129,35 @@ function parseArguments(requiredUsages = {},
112
129
  }
113
130
  }
114
131
 
132
+ if (hasKey(helpUsages, key) == false
133
+ && hasKey(requiredUsages, key) == false
134
+ && hasKey(optionalUsages, key) == false) {
135
+ console.error('JsView Error: Failed to parse usage: ' + argu);
136
+ process.exit(1);
137
+ }
138
+
115
139
  let formattedKey = formatKey(key);
116
140
  let formattedValue = formatValue(value);
117
141
  options[formattedKey] = formattedValue;
118
142
  }
119
143
 
120
144
  for (const checker in helpUsages) {
121
- const checkerList = checker.split('|');
122
- const aliasedOptions = checkAliases(checkerList, options);
145
+ const aliasedOptions = checkAliases(checker, options);
123
146
  if (aliasedOptions) {
124
147
  printArgumentsUsages(requiredUsages, optionalUsages, helpUsages);
125
148
  process.exit(0);
126
149
  }
127
150
  }
128
151
  for (const checker in requiredUsages) {
129
- const checkerList = checker.split('|');
130
- const aliasedOptions = checkAliases(checkerList, options);
152
+ const aliasedOptions = checkAliases(checker, options);
131
153
  if (!aliasedOptions) {
132
- console.log('JsView Error: Failed to find required option: ' + checkerList);
154
+ console.log('JsView Error: Failed to find required option: ' + checker);
133
155
  process.exit(-1);
134
156
  }
135
157
  options = { ...options, ...aliasedOptions };
136
158
  }
137
159
  for (const checker in optionalUsages) {
138
- const checkerList = checker.split('|');
139
- const aliasedOptions = checkAliases(checkerList, options);
160
+ const aliasedOptions = checkAliases(checker, options);
140
161
  options = { ...options, ...aliasedOptions };
141
162
  }
142
163
 
@@ -208,7 +229,8 @@ function getPackageObject(modulePath)
208
229
  console.error('JsView Error: Failed to get ' + modulePath + ', file is not exists.');
209
230
  process.exit(1);
210
231
  }
211
- const pkgObj = require(pkgFullFile);
232
+ let pkgContent = fs.readFileSync(pkgFullFile, 'utf8');
233
+ const pkgObj = JSON.parse(pkgContent);
212
234
 
213
235
  return pkgObj;
214
236
  }
@@ -298,7 +320,18 @@ function checkNodeVersion(minMajorVersion = 16)
298
320
  }
299
321
  }
300
322
 
301
- module.exports = {
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
+
333
+ export {
334
+ askAndAnswer,
302
335
  checkNodeVersion,
303
336
  cpSync,
304
337
  execCommand,
@@ -1,15 +1,15 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict';
3
3
 
4
- const crypto = require('crypto');
5
- const fs = require('fs');
6
- const path = require('path');
7
- const url = require('url')
8
- const {
4
+ import crypto from 'crypto';
5
+ import fs from 'fs';
6
+ import path from 'path';
7
+ import url from 'url'
8
+ import {
9
9
  checkNodeVersion,
10
10
  getOptions,
11
11
  parseArguments,
12
- } = require('./jsview-common');
12
+ } from './jsview-common.mjs';
13
13
 
14
14
  // npm run build 时检查jsview-dom是否处于Debug模式。
15
15
  async function checkDomDebugDisabled(options) {
@@ -227,7 +227,8 @@ async function main(argv)
227
227
 
228
228
  const vueConfigFile = path.resolve(options.projectDir, 'vue.config.js');
229
229
  if (fs.existsSync(vueConfigFile)) {
230
- vueCfgObj = require(vueConfigFile);
230
+ let vueCfgContent = fs.readFileSync(vueConfigFile, 'utf8');
231
+ const vueCfgObj = JSON.parse(vueCfgContent);
231
232
  if(vueCfgObj.outputDir) {
232
233
  options.distDir = path.resolve(options.projectDir, vueCfgObj.outputDir);
233
234
  }
@@ -1,10 +1,10 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict';
3
3
 
4
- const fs = require('fs');
5
- const path = require('path');
6
- const url = require('url');
7
- const {
4
+ import fs from 'fs';
5
+ import path from 'path';
6
+ import url from 'url';
7
+ import {
8
8
  checkNodeVersion,
9
9
  cpSync,
10
10
  execCommand,
@@ -14,7 +14,7 @@ const {
14
14
  parseArguments,
15
15
  symlinkSync,
16
16
  rmSync
17
- } = require('./jsview-common');
17
+ } from './jsview-common.mjs';
18
18
 
19
19
  function checkNpmCommand()
20
20
  {
@@ -1,14 +1,13 @@
1
1
  #!/usr/bin/env node
2
2
  'use strict';
3
3
 
4
- const childProcess = require('child_process');
5
- const path = require('path');
6
- const url = require('url');
7
- const {
4
+ import childProcess from 'child_process';
5
+ import url from 'url';
6
+ import {
8
7
  checkNodeVersion,
9
8
  getOptions,
10
9
  parseArguments
11
- } = require('./jsview-common');
10
+ } from './jsview-common.mjs';
12
11
 
13
12
  async function getExtraOptions(argv)
14
13
  {