create-jsview 2.1.5

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.
Files changed (40) hide show
  1. package/common.mjs +223 -0
  2. package/index.mjs +25 -0
  3. package/package.json +35 -0
  4. package/project-creator.mjs +54 -0
  5. package/project-upgrader.mjs +193 -0
  6. package/template-react-webpack/README.md +16 -0
  7. package/template-react-webpack/_gitignore +23 -0
  8. package/template-react-webpack/package-lock-json +27673 -0
  9. package/template-react-webpack/package.json +32 -0
  10. package/template-react-webpack/public/index.html +12 -0
  11. package/template-react-webpack/src/App.css +38 -0
  12. package/template-react-webpack/src/App.js +18 -0
  13. package/template-react-webpack/src/appConfig/HOW_TO_CONFIG.md +20 -0
  14. package/template-react-webpack/src/appConfig/app.config.mjs +7 -0
  15. package/template-react-webpack/src/appConfig/app_sign_private_key_sample.crt +28 -0
  16. package/template-react-webpack/src/appConfig/app_sign_public_key_sample.pem +9 -0
  17. package/template-react-webpack/src/appConfig/jsview.config.mjs +41 -0
  18. package/template-react-webpack/src/logo.png +0 -0
  19. package/template-react-webpack/src/main.tsx +19 -0
  20. package/template-react-webpack/tsconfig.json +9 -0
  21. package/template-vue-vite/.vscode/extensions.json +3 -0
  22. package/template-vue-vite/README.md +16 -0
  23. package/template-vue-vite/_gitignore +24 -0
  24. package/template-vue-vite/index.html +13 -0
  25. package/template-vue-vite/package-lock-json +1625 -0
  26. package/template-vue-vite/package.json +34 -0
  27. package/template-vue-vite/src/App.vue +25 -0
  28. package/template-vue-vite/src/appConfig/HOW_TO_CONFIG.md +15 -0
  29. package/template-vue-vite/src/appConfig/app.config.mjs +7 -0
  30. package/template-vue-vite/src/appConfig/app_sign_private_key_sample.crt +28 -0
  31. package/template-vue-vite/src/appConfig/app_sign_public_key_sample.pem +9 -0
  32. package/template-vue-vite/src/appConfig/jsview.config.mjs +43 -0
  33. package/template-vue-vite/src/appConfig/permission.js +1 -0
  34. package/template-vue-vite/src/assets/logo.png +0 -0
  35. package/template-vue-vite/src/components/HelloWorld.vue +19 -0
  36. package/template-vue-vite/src/env.d.ts +8 -0
  37. package/template-vue-vite/src/main.tsx +16 -0
  38. package/template-vue-vite/tsconfig.json +27 -0
  39. package/template-vue-vite/tsconfig.node.json +8 -0
  40. package/template-vue-vite/vite.config.ts +9 -0
package/common.mjs ADDED
@@ -0,0 +1,223 @@
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+ import url from 'url';
4
+ import prompts from 'prompts'
5
+ import {
6
+ bold, cyan, lightCyan, red
7
+ } from 'kolorist';
8
+
9
+ function copyDirOrFile(fromPath, toPath, ignoreFileList = []) {
10
+ const stat = fs.statSync(fromPath)
11
+ if (stat.isDirectory()) {
12
+ const fileList = fs.readdirSync(fromPath);
13
+ for (const file of fileList) {
14
+ const srcFile = path.resolve(fromPath, file);
15
+ const destFile = path.resolve(toPath, file);
16
+ copyDirOrFile(srcFile, destFile, ignoreFileList);
17
+ }
18
+ } else {
19
+ for (const ignoreFile of ignoreFileList) {
20
+ if (fromPath.endsWith(ignoreFile)
21
+ || fromPath.endsWith(ignoreFile.replaceAll('/', '\\'))) {
22
+ console.log('Ignore to copy ' + ignoreFile);
23
+ return;
24
+ }
25
+ }
26
+
27
+ const toParentPath = path.dirname(toPath);
28
+ fs.mkdirSync(toParentPath, { recursive: true })
29
+ fs.copyFileSync(fromPath, toPath);
30
+ }
31
+ }
32
+
33
+ function deepMergeObjects(mainObj, refObj) {
34
+ const mergedObj = {};
35
+
36
+ for (let key in mainObj) {
37
+ if (key in refObj) {
38
+ if (Array.isArray(mainObj[key]) && Array.isArray(refObj[key])) {
39
+ mergedObj[key] = mainObj[key].concat(refObj[key]);
40
+ } else if (typeof mainObj[key] === "object" && typeof refObj[key] === "object") {
41
+ mergedObj[key] = deepMergeObjects(mainObj[key], refObj[key]);
42
+ } else {
43
+ mergedObj[key] = refObj[key];
44
+ }
45
+ } else {
46
+ mergedObj[key] = mainObj[key];
47
+ }
48
+ }
49
+
50
+ for (let key in refObj) {
51
+ if (!(key in mainObj)) {
52
+ mergedObj[key] = refObj[key];
53
+ }
54
+ }
55
+
56
+ return mergedObj;
57
+ }
58
+
59
+ function deleteObjectKeys(obj, keys) {
60
+ if (!obj) {
61
+ return;
62
+ }
63
+
64
+ for (let key of keys) {
65
+ delete obj[key];
66
+ }
67
+ }
68
+
69
+ function dirNotEmpty(dirPath) {
70
+ if (fs.existsSync(dirPath) == false) {
71
+ return false;
72
+ }
73
+
74
+ const files = fs.readdirSync(dirPath)
75
+ if (files.length === 0) {
76
+ return false;
77
+ }
78
+
79
+ return true;
80
+ }
81
+
82
+ function errorAndExit(msg) {
83
+ console.error(red('✖ ') + msg);
84
+ process.exit(1);
85
+ }
86
+
87
+ function getFileFromScriptDir(fileName) {
88
+ const __filename = url.fileURLToPath(import.meta.url);
89
+ const __dirname = path.dirname(__filename);
90
+
91
+ const filePath = path.resolve(__dirname, fileName);
92
+
93
+ return filePath;
94
+ }
95
+
96
+ function getFrameworkPrompt() {
97
+ return [
98
+ { title: 'vue + vite', value: 'vue-vite' },
99
+ { title: 'react + webpack', value: 'react-webpack' },
100
+ ];
101
+ }
102
+
103
+ async function getProjectConfigAsync(operator, defaultName) {
104
+ const config = {};
105
+ config.promptOptions = {
106
+ onCancel: () => {
107
+ errorAndExit('Operation cancelled');
108
+ }
109
+ };
110
+
111
+ const questions = [];
112
+
113
+ questions.push({
114
+ type: 'select',
115
+ name: 'framework',
116
+ message: `Select framework to ${operator}:`,
117
+ initial: 0,
118
+ choices: getFrameworkPrompt(),
119
+ });
120
+
121
+ questions.push({
122
+ type: 'text',
123
+ name: 'projectName',
124
+ message: 'Project name:',
125
+ initial: defaultName,
126
+ onState: (state) => {
127
+ config.targetDirName = state.value.trim() || defaultProjectName;
128
+ }
129
+ });
130
+
131
+ const result = await prompts(questions, config.promptOptions);
132
+ Object.assign(config, result);
133
+ config.targetDirPath = path.resolve(process.cwd(), config.targetDirName)
134
+
135
+ return config;
136
+ }
137
+
138
+ async function generateProjectAsync(framework, targetDirPath, ignoreFileList) {
139
+ const templateDir = getFileFromScriptDir(`template-${framework}`);
140
+ // console.log(`\ntemplateDir=${templateDir}...`)
141
+
142
+ const renameFiles = {
143
+ 'package-lock-json': 'package-lock.json',
144
+ _gitignore: '.gitignore'
145
+ };
146
+
147
+ const fileList = fs.readdirSync(templateDir);
148
+ for (const file of fileList) {
149
+ if (file === 'package.json') { // package.json 单独处理
150
+ continue;
151
+ }
152
+
153
+ const fromFilePath = path.resolve(templateDir, file);
154
+ const toFileName = renameFiles[file] ?? file;
155
+ const toFilePath = path.resolve(targetDirPath, toFileName);
156
+ copyDirOrFile(fromFilePath, toFilePath, ignoreFileList);
157
+ }
158
+
159
+ const pkgJsonFromFile = path.resolve(templateDir, 'package.json');
160
+ const pkgJsonObj = readJsonFile(pkgJsonFromFile);
161
+
162
+ const newPkgJsonObj = Object.assign({ "name": path.basename(targetDirPath) }, pkgJsonObj); // 保证name在前面
163
+
164
+ const pkgJsonToFile = path.resolve(targetDirPath, 'package.json');
165
+ fs.writeFileSync(pkgJsonToFile, JSON.stringify(newPkgJsonObj, null, 2), 'utf8');
166
+
167
+ return newPkgJsonObj;
168
+ }
169
+
170
+ function makeDirEmpty(dirPath) {
171
+ if (dirNotEmpty(dirPath)) {
172
+ const fileList = fs.readdirSync(dirPath);
173
+ for (const file of fileList) {
174
+ const filePath = path.resolve(dirPath, file);
175
+ fs.rmSync(filePath, { force: true, recursive: true });
176
+ }
177
+ }
178
+ fs.mkdirSync(dirPath, { recursive: true });
179
+ }
180
+
181
+ function readJsonFile(filePath) {
182
+ try {
183
+ const pkgJsonContent = fs.readFileSync(filePath, 'utf8');
184
+ const pkgJsonObj = JSON.parse(pkgJsonContent);
185
+
186
+ return pkgJsonObj;
187
+ } catch (ex) {
188
+ }
189
+
190
+ return null;
191
+ }
192
+
193
+ function showLogo() {
194
+ console.info('');
195
+ console.info('');
196
+ console.info(cyan(' #'));
197
+ console.info(cyan(' ## ## '));
198
+ console.info(cyan(' ### ###'));
199
+ console.info(cyan(' ###'));
200
+ console.info(cyan(' ############ #####'));
201
+ console.info(cyan(' ###'));
202
+ console.info(cyan(' ### ###'));
203
+ console.info(cyan(' ## ## '));
204
+ console.info(cyan(' #'));
205
+ console.info('');
206
+ console.info(lightCyan(bold(' Welcome to JsView!')));
207
+ console.info('');
208
+ }
209
+
210
+ export {
211
+ copyDirOrFile,
212
+ deepMergeObjects,
213
+ deleteObjectKeys,
214
+ dirNotEmpty,
215
+ errorAndExit,
216
+ getFileFromScriptDir,
217
+ getFrameworkPrompt,
218
+ getProjectConfigAsync,
219
+ generateProjectAsync,
220
+ makeDirEmpty,
221
+ readJsonFile,
222
+ showLogo
223
+ };
package/index.mjs ADDED
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { showLogo } from './common.mjs'
4
+ import { createProjectAsync } from './project-creator.mjs'
5
+ import { upgradeProjectAsync } from './project-upgrader.mjs'
6
+
7
+ async function main() {
8
+ showLogo()
9
+
10
+ // arg0: node
11
+ // arg1: script.js
12
+ const arguList = process.argv.slice(2);
13
+
14
+ if (arguList.length == 0) {
15
+ await createProjectAsync();
16
+ } else if (arguList.length == 1 && arguList[0] === '--upgrade') {
17
+ await upgradeProjectAsync();
18
+ } else {
19
+ console.error('Unknown options: ', arguList);
20
+ }
21
+ }
22
+
23
+ main().catch((ex) => {
24
+ console.error(ex);
25
+ })
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "create-jsview",
3
+ "version": "2.1.5",
4
+ "license": "MIT",
5
+ "bin": {
6
+ "create-jsview": "index.mjs",
7
+ "cjsv": "index.mjs"
8
+ },
9
+ "type": "module",
10
+ "files": [
11
+ "*.mjs",
12
+ "template-vue-vite/*",
13
+ "template-react-webpack/*"
14
+ ],
15
+ "main": "index.mjs",
16
+ "engines": {
17
+ "node": ">=16.0.0"
18
+ },
19
+ "author": "mengxk",
20
+ "repository": {
21
+ "url": "https://github.com/shijiu-jsview/jsview-framework.git"
22
+ },
23
+ "bugs": {
24
+ "url": "https://github.com/shijiu-jsview/jsview-framework/issues"
25
+ },
26
+ "homepage": "http://jsview-dev.shijiutv.com/",
27
+ "dependencies": {
28
+ "kolorist": "^1.6.0",
29
+ "prompts": "^2.4.2"
30
+ },
31
+ "template": {
32
+ "vue": "2.1.5",
33
+ "react": "2.1.5"
34
+ }
35
+ }
@@ -0,0 +1,54 @@
1
+ import path from 'path'
2
+ import prompts from 'prompts'
3
+ import { red } from 'kolorist'
4
+ import {
5
+ dirNotEmpty,
6
+ getProjectConfigAsync,
7
+ generateProjectAsync,
8
+ makeDirEmpty,
9
+ } from './common.mjs'
10
+
11
+ async function createProjectAsync() {
12
+ const config = await configProjectAsync();
13
+
14
+ makeDirEmpty(config.targetDirPath);
15
+
16
+ console.log(`\nInitializing project in ${config.targetDirPath}...`);
17
+ await generateProjectAsync(config.framework, config.targetDirPath);
18
+
19
+ completedPrompt(config);
20
+ }
21
+
22
+ async function configProjectAsync() {
23
+ const config = await getProjectConfigAsync('create', 'jsview-project');
24
+
25
+ const questions = {
26
+ type: () => dirNotEmpty(config.targetDirPath) ? 'confirm': null,
27
+ name: 'overwrite',
28
+ message: () =>
29
+ (config.targetDirName === '.' ? 'Current directory' : `Target directory "${config.targetDirName}"`)
30
+ + ' is not empty. Overwrite existing files?'
31
+ };
32
+ const result = await prompts(questions, config.promptOptions);
33
+ if (result.overwrite === false) {
34
+ errorAndExit('User cancelled')
35
+ }
36
+
37
+ return config;
38
+ }
39
+
40
+ function completedPrompt(config) {
41
+ const cwd = process.cwd();
42
+
43
+ console.log(`\nDone. Now run:\n`);
44
+ if (config.targetDirPath !== cwd) {
45
+ console.log(` cd ${path.relative(cwd, config.targetDirPath)}`);
46
+ }
47
+ console.log(' npm ci');
48
+ console.log(' npm start');
49
+ console.log();
50
+ }
51
+
52
+ export {
53
+ createProjectAsync
54
+ }
@@ -0,0 +1,193 @@
1
+ import fs from 'fs'
2
+ import path from 'path'
3
+ import prompts from 'prompts'
4
+ import { bold } from 'kolorist'
5
+ import {
6
+ deepMergeObjects,
7
+ deleteObjectKeys,
8
+ dirNotEmpty,
9
+ errorAndExit,
10
+ getFileFromScriptDir,
11
+ getProjectConfigAsync,
12
+ generateProjectAsync,
13
+ readJsonFile,
14
+ } from './common.mjs'
15
+
16
+ async function upgradeProjectAsync() {
17
+ const config = await configProjectAsync();
18
+
19
+ console.log(`\nUpgrading project in ${config.targetDirPath}...`);
20
+
21
+ const currPkgJsonObj = checkProject(config);
22
+ const ignoreFileList = [
23
+ 'src/App.vue',
24
+ 'src/appConfig/app.config.mjs',
25
+ 'src/appConfig/jsview.config.mjs',
26
+ 'src/components/HelloWorld.vue',
27
+ ];
28
+ const newPkgJsonObj = await generateProjectAsync(config.framework, config.targetDirPath, ignoreFileList);
29
+
30
+ updatePackageJsonFile(config, currPkgJsonObj, newPkgJsonObj);
31
+
32
+ await removeUnusedFiles(config);
33
+
34
+ completedPrompt(config);
35
+ }
36
+
37
+ async function configProjectAsync() {
38
+ const config = await getProjectConfigAsync('upgrade', '.');
39
+
40
+ const templateDir = getFileFromScriptDir(`template-${config.framework}`);
41
+ const pkgJsonPath = path.resolve(templateDir, 'package.json');
42
+ const pkgJsonObj = readJsonFile(pkgJsonPath);
43
+ const frameworkVersion = pkgJsonObj.dependencies['@shijiu/jsview-vue'];
44
+
45
+ const questions = {
46
+ type: 'confirm',
47
+ name: 'upgrade',
48
+ initial: true,
49
+ message: `Are you sure upgrade JSView to ${frameworkVersion} ?`
50
+ };
51
+ const result = await prompts(questions, config.promptOptions);
52
+ if (result.upgrade !== true) {
53
+ errorAndExit('User cancelled');
54
+ }
55
+
56
+ return config;
57
+ }
58
+
59
+ function checkProject(config) {
60
+ if (dirNotEmpty(config.targetDirPath) == false) {
61
+ errorAndExit(`${config.targetDirPath} is not exists`);
62
+ }
63
+
64
+ const currPkgJsonPath = path.resolve(config.targetDirPath, 'package.json');
65
+ const currPkgJsonObj = readJsonFile(currPkgJsonPath);
66
+ if (!currPkgJsonObj) {
67
+ errorAndExit(`Failed to find valid package.json from ${config.targetDirPath}`);
68
+ }
69
+
70
+ const currFrameworkVersion = currPkgJsonObj.dependencies?.['@shijiu/jsview-vue'];
71
+ if (!currFrameworkVersion) {
72
+ errorAndExit(`Failed to find jsview-vue from ${config.targetDirPath}`);
73
+ }
74
+
75
+ return currPkgJsonObj;
76
+ }
77
+
78
+ function updatePackageJsonFile(config, currPkgJsonObj, newPkgJsonObj) {
79
+ deleteObjectKeys(currPkgJsonObj, [
80
+ 'main',
81
+ 'eslintConfig',
82
+ 'browserslist',
83
+ ]);
84
+ deleteObjectKeys(currPkgJsonObj.scripts, [
85
+ 'android',
86
+ 'build',
87
+ 'dev',
88
+ 'lint',
89
+ 'postinstall',
90
+ 'serve',
91
+ 'start',
92
+ ]);
93
+ deleteObjectKeys(currPkgJsonObj.dependencies, [
94
+ '@shijiu/jsview',
95
+ '@shijiu/jsview-vue',
96
+ 'core-js',
97
+ 'deepmerge',
98
+ 'fork-ts-checker-webpack-plugin',
99
+ 'gifuct-js',
100
+ 'qr.js',
101
+ 'vue',
102
+ 'vue-router',
103
+ ]);
104
+ deleteObjectKeys(currPkgJsonObj.devDependencies, [
105
+ '@typescript-eslint/eslint-plugin',
106
+ '@typescript-eslint/parser',
107
+ '@vue/cli-plugin-babel',
108
+ '@vue/cli-plugin-eslint',
109
+ '@vue/cli-plugin-typescript',
110
+ '@vue/cli-service',
111
+ '@vue/compiler-sfc',
112
+ '@vue/eslint-config-typescript',
113
+ 'babel-eslint',
114
+ 'cache-loader',
115
+ 'cosmiconfig',
116
+ 'eslint',
117
+ 'eslint-plugin-node',
118
+ 'eslint-plugin-promise',
119
+ 'eslint-plugin-standard',
120
+ 'eslint-plugin-vue',
121
+ 'fork-ts-checker-webpack-plugin-v5',
122
+ 'postcss-import-sync',
123
+ 'postcss-js',
124
+ 'typescript',
125
+ 'vue-class-component',
126
+ 'webpack',
127
+ ]);
128
+
129
+ const mergedPkgJsonObj = deepMergeObjects(newPkgJsonObj, currPkgJsonObj);
130
+
131
+ const pkgJsonFile = path.resolve(config.targetDirPath, 'package.json');
132
+ fs.writeFileSync(pkgJsonFile, JSON.stringify(mergedPkgJsonObj, null, 2), 'utf8');
133
+ }
134
+
135
+ async function removeUnusedFiles(config) {
136
+ const filesToRemove = [
137
+ 'babel.config.js',
138
+ 'dist',
139
+ 'node_modules',
140
+ 'public',
141
+ 'src/main.ts',
142
+ 'src/shims-vue.d.ts',
143
+ 'vue.config.js',
144
+ ];
145
+
146
+ console.log();
147
+ console.log('There are unused source files in ' + config.targetDirPath + ',');
148
+ console.log('If you have made any modifications to these files,');
149
+ console.log('then you will need to ' + bold('manually') + ' update the changes in these files to the new project.');
150
+ for (const file of filesToRemove) {
151
+ if (fs.existsSync(file) == false) {
152
+ continue;
153
+ }
154
+ console.log(' - ' + file);
155
+ }
156
+
157
+ const questions = {
158
+ type: 'confirm',
159
+ name: 'remove',
160
+ initial: true,
161
+ message: 'Would you like to delete them?'
162
+ };
163
+ const result = await prompts(questions, config.promptOptions);
164
+ if (result.remove !== true) {
165
+ return;
166
+ }
167
+
168
+ for (const file of filesToRemove) {
169
+ console.log('Removing ' + file);
170
+ const filePath = path.resolve(config.targetDirPath, file);
171
+ fs.rmSync(filePath, { force: true, recursive: true });
172
+ }
173
+ }
174
+
175
+ function completedPrompt(config) {
176
+ const cwd = process.cwd();
177
+
178
+ console.log(`\nDone. Now run:\n`);
179
+ if (config.targetDirPath !== cwd) {
180
+ console.log(` cd ${path.relative(cwd, config.targetDirPath)}`);
181
+ }
182
+ console.log(' npm install');
183
+ console.log(' npm start');
184
+ console.log();
185
+
186
+ console.log(bold('Notice:') + ' If you encounter any other compatibility issues during the runtime,');
187
+ console.log(' you can refer to the issue list on the official website');
188
+ console.log(bold(' http://jsview-dev.shijiutv.com/guide/2.environments/2.7.upgrade-project.html.'));
189
+ }
190
+
191
+ export {
192
+ upgradeProjectAsync
193
+ }
@@ -0,0 +1,16 @@
1
+ # JsView + React + Webpack
2
+
3
+ ## Project setup
4
+ ```
5
+ npm ci
6
+ ```
7
+
8
+ ### Compiles and hot-reloads for development
9
+ ```
10
+ npm start
11
+ ```
12
+
13
+ ### Compiles and minifies for production
14
+ ```
15
+ npm run build
16
+ ```
@@ -0,0 +1,23 @@
1
+ # See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2
+
3
+ # dependencies
4
+ /node_modules
5
+ /.pnp
6
+ .pnp.js
7
+
8
+ # testing
9
+ /coverage
10
+
11
+ # production
12
+ /build
13
+
14
+ # misc
15
+ .DS_Store
16
+ .env.local
17
+ .env.development.local
18
+ .env.test.local
19
+ .env.production.local
20
+
21
+ npm-debug.log*
22
+ yarn-debug.log*
23
+ yarn-error.log*