@qse/edu-scripts 1.12.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.
Files changed (85) hide show
  1. package/CHANGELOG.md +333 -0
  2. package/README.md +105 -0
  3. package/app.d.ts +68 -0
  4. package/docs/changelog.md +5 -0
  5. package/docs/deploy.md +53 -0
  6. package/docs/feat.md +74 -0
  7. package/docs/grayscale.md +31 -0
  8. package/docs/index.md +5 -0
  9. package/docs/mode.md +42 -0
  10. package/docs/override.md +165 -0
  11. package/docs/refactor-react-16.md +40 -0
  12. package/docs/refactor.md +139 -0
  13. package/jest.config.js +195 -0
  14. package/lib/asset/dll/libcommon3-manifest.json +181 -0
  15. package/lib/asset/template/edu-app-env.d.ts.tpl +15 -0
  16. package/lib/asset/template/edu-scripts.override.js.tpl +14 -0
  17. package/lib/asset/template/page/index.class.js.tpl +24 -0
  18. package/lib/asset/template/page/index.class.tsx.tpl +10 -0
  19. package/lib/asset/template/page/index.fc.js.tpl +16 -0
  20. package/lib/asset/template/page/index.fc.tsx.tpl +9 -0
  21. package/lib/asset/template/page/index.less.tpl +3 -0
  22. package/lib/asset/template/page/logic.js.tpl +4 -0
  23. package/lib/asset/template/page/route.js.tpl +12 -0
  24. package/lib/asset/template/tailwind.config.js.tpl +11 -0
  25. package/lib/asset/template/tsconfig.json.tpl +24 -0
  26. package/lib/auto-refactor.js +175 -0
  27. package/lib/build.js +87 -0
  28. package/lib/cli.js +63 -0
  29. package/lib/commit-dist.js +117 -0
  30. package/lib/config/babel.dependencies.js +56 -0
  31. package/lib/config/babel.js +82 -0
  32. package/lib/config/paths.js +44 -0
  33. package/lib/config/plugins/postcss-safe-area.js +22 -0
  34. package/lib/config/webpackConfig.js +384 -0
  35. package/lib/config/webpackDevServerConfig.js +47 -0
  36. package/lib/deploy.js +186 -0
  37. package/lib/generator.js +155 -0
  38. package/lib/index.d.ts +1 -0
  39. package/lib/index.js +13 -0
  40. package/lib/start.js +51 -0
  41. package/lib/utils/FileSizeReporter.js +131 -0
  42. package/lib/utils/appConfig.js +44 -0
  43. package/lib/utils/beforeStart.js +73 -0
  44. package/lib/utils/changeDeployVersion.js +125 -0
  45. package/lib/utils/defineConfig.d.ts +59 -0
  46. package/lib/utils/defineConfig.js +10 -0
  47. package/lib/utils/exec.js +13 -0
  48. package/lib/utils/getConfig.js +30 -0
  49. package/lib/utils/getOverride.js +33 -0
  50. package/package.json +102 -0
  51. package/src/asset/dll/libcommon3-manifest.json +181 -0
  52. package/src/asset/template/edu-app-env.d.ts.tpl +15 -0
  53. package/src/asset/template/edu-scripts.override.js.tpl +14 -0
  54. package/src/asset/template/page/index.class.js.tpl +24 -0
  55. package/src/asset/template/page/index.class.tsx.tpl +10 -0
  56. package/src/asset/template/page/index.fc.js.tpl +16 -0
  57. package/src/asset/template/page/index.fc.tsx.tpl +9 -0
  58. package/src/asset/template/page/index.less.tpl +3 -0
  59. package/src/asset/template/page/logic.js.tpl +4 -0
  60. package/src/asset/template/page/route.js.tpl +12 -0
  61. package/src/asset/template/tailwind.config.js.tpl +11 -0
  62. package/src/asset/template/tsconfig.json.tpl +24 -0
  63. package/src/auto-refactor.js +172 -0
  64. package/src/build.js +74 -0
  65. package/src/cli.js +91 -0
  66. package/src/commit-dist.js +103 -0
  67. package/src/config/babel.dependencies.js +65 -0
  68. package/src/config/babel.js +88 -0
  69. package/src/config/paths.js +37 -0
  70. package/src/config/plugins/postcss-safe-area.js +21 -0
  71. package/src/config/webpackConfig.js +410 -0
  72. package/src/config/webpackDevServerConfig.js +44 -0
  73. package/src/deploy.js +158 -0
  74. package/src/generator.js +138 -0
  75. package/src/index.ts +1 -0
  76. package/src/start.js +46 -0
  77. package/src/utils/FileSizeReporter.js +144 -0
  78. package/src/utils/appConfig.js +35 -0
  79. package/src/utils/beforeStart.js +66 -0
  80. package/src/utils/changeDeployVersion.js +115 -0
  81. package/src/utils/defineConfig.ts +63 -0
  82. package/src/utils/exec.js +7 -0
  83. package/src/utils/getConfig.js +26 -0
  84. package/src/utils/getOverride.js +32 -0
  85. package/tsconfig.json +31 -0
@@ -0,0 +1,410 @@
1
+ const fs = require('fs')
2
+ const webpack = require('webpack')
3
+ const HtmlWebpackPlugin = require('html-webpack-plugin')
4
+ const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer')
5
+ const ReactRefreshPlugin = require('@pmmmwh/react-refresh-webpack-plugin')
6
+ const paths = require('./paths')
7
+ const TerserPlugin = require('terser-webpack-plugin')
8
+ const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin')
9
+ const appPkg = require(paths.package)
10
+ const appConfig = require('../utils/appConfig')
11
+ const { ESBuildMinifyPlugin } = require('esbuild-loader')
12
+
13
+ const jsMainPath = appConfig.grayscale
14
+ ? `${appPkg.name}/beta/${appPkg.name}`
15
+ : `${appPkg.name}/${appPkg.name}`
16
+ const assetPath = appConfig.grayscale
17
+ ? `${appPkg.name}/beta/${appPkg.version}`
18
+ : `${appPkg.name}/${appPkg.version}`
19
+
20
+ const cssRegex = /\.css$/
21
+ const cssModuleRegex = /\.module\.css$/
22
+ const lessRegex = /\.less$/
23
+ const lessModuleRegex = /\.module\.less$/
24
+
25
+ const imageInlineSizeLimit = 10 * 1024
26
+
27
+ const qsbCDN = (() => {
28
+ const contents = paths.indexHTML.map((url) => fs.readFileSync(url, 'utf-8'))
29
+ const isUseCommon = contents.some((content) => /react16.14.*_common31?.js/.test(content))
30
+ const isUseAxios = contents.some((content) => /react16.14.*_axios0.21.1/.test(content))
31
+ const isUseMoment = contents.some((content) => content.includes('moment2.29.1.js'))
32
+ const isUseAntd = contents.some((content) => content.includes('antd3.26.20.js'))
33
+
34
+ const isUse = isUseAntd || isUseCommon || isUseMoment || isUseAxios
35
+ return { isUse, isUseAntd, isUseCommon, isUseMoment, isUseAxios }
36
+ })()
37
+
38
+ /**
39
+ * @param {*} args
40
+ * @param {import('../utils/defineConfig').Config} override
41
+ */
42
+ module.exports = function getWebpackConfig(args, override) {
43
+ const isDev = process.env.NODE_ENV === 'development'
44
+ const isProd = process.env.NODE_ENV === 'production'
45
+
46
+ // common function to get style loaders
47
+ const getStyleLoaders = (cssOptions, preProcessor) => {
48
+ const loaders = [
49
+ {
50
+ loader: 'style-loader',
51
+ options: { attributes: { 'data-module': appPkg.name, 'data-version': appPkg.version } },
52
+ },
53
+ {
54
+ loader: 'css-loader',
55
+ options: cssOptions,
56
+ },
57
+ {
58
+ // Options for PostCSS as we reference these options twice
59
+ // Adds vendor prefixing based on your specified browser support in
60
+ // package.json
61
+ loader: 'postcss-loader',
62
+ options: {
63
+ postcssOptions: {
64
+ // Necessary for external CSS imports to work
65
+ // https://github.com/facebook/create-react-app/issues/2677
66
+ ident: 'postcss',
67
+ config: false,
68
+ plugins: [
69
+ isProd && require('cssnano')({ preset: 'default' }),
70
+ fs.existsSync(paths.tailwind) && 'tailwindcss',
71
+ 'postcss-flexbugs-fixes',
72
+ [
73
+ 'postcss-preset-env',
74
+ {
75
+ autoprefixer: {
76
+ flexbox: 'no-2009',
77
+ },
78
+ // https://preset-env.cssdb.org/features/#stage-2
79
+ },
80
+ ],
81
+ isProd && require('./plugins/postcss-safe-area')(),
82
+ isProd && ['postcss-momentum-scrolling', ['scroll', 'auto']],
83
+ 'postcss-normalize',
84
+ ...override.extraPostCSSPlugins,
85
+ ].filter(Boolean),
86
+ },
87
+ sourceMap: isDev,
88
+ },
89
+ },
90
+ ]
91
+ if (preProcessor === 'less-loader') {
92
+ loaders.push({
93
+ loader: 'less-loader',
94
+ options: {
95
+ lessOptions: {
96
+ javascriptEnabled: true,
97
+ modifyVars: fs.existsSync(paths.theme) ? require(paths.theme) : undefined,
98
+ },
99
+ sourceMap: true,
100
+ },
101
+ })
102
+ }
103
+ return loaders
104
+ }
105
+
106
+ /** @type {webpack.Configuration} */
107
+ const config = {
108
+ context: process.cwd(),
109
+ mode: process.env.NODE_ENV,
110
+ entry: './src/index',
111
+ target: 'browserslist',
112
+ output: {
113
+ filename: appConfig.single
114
+ ? `js/${jsMainPath}_${appPkg.version}.[contenthash:6].js`
115
+ : `js/${jsMainPath}_${appPkg.version}.js`,
116
+ chunkFilename: `js/${assetPath}/[name].[chunkhash:8].js`,
117
+ assetModuleFilename: `images/${assetPath}/[name].[hash:6][ext]`,
118
+ uniqueName: appPkg.name,
119
+ publicPath: '',
120
+ },
121
+ externals: Object.assign(
122
+ {},
123
+ qsbCDN.isUseCommon && {
124
+ react: 'React',
125
+ 'react-dom': 'ReactDOM',
126
+ 'natty-fetch': 'nattyFetch',
127
+ 'natty-storage': 'nattyStorage',
128
+ 'common-utils': 'CommonUtils',
129
+ },
130
+ qsbCDN.isUseAxios && { axios: 'axios' },
131
+ qsbCDN.isUseMoment && { moment: 'moment' },
132
+ qsbCDN.isUseAntd && {
133
+ react: 'React',
134
+ 'react-dom': 'ReactDOM',
135
+ moment: 'moment',
136
+ antd: 'antd',
137
+ ...(isProd && !appConfig.single
138
+ ? {
139
+ '@qse/antd': 'qsbAntd',
140
+ '@qse/scheme-render': 'qsbSchemeRender',
141
+ '@qsb/antd': 'qsbAntd',
142
+ '@qsb/scheme-render': 'qsbSchemeRender',
143
+ }
144
+ : {}),
145
+ },
146
+ override.externals
147
+ ),
148
+ resolve: {
149
+ alias: {
150
+ '@': paths.src,
151
+ ...override.alias,
152
+ },
153
+ extensions: ['.web.js', '.web.mjs', '.js', '.mjs', '.jsx', '.ts', '.tsx', '.json', '.wasm'],
154
+ },
155
+ stats: isDev ? { preset: 'errors-warnings', timings: true } : undefined,
156
+ devtool: isDev ? 'cheap-module-source-map' : false,
157
+ cache: {
158
+ type: 'filesystem',
159
+ version: require('../../package.json').version,
160
+ buildDependencies: {
161
+ config: [__filename],
162
+ override: [paths.override].filter((f) => fs.existsSync(f)),
163
+ tsconfig: [paths.tsconfig, paths.jsconfig].filter((f) => fs.existsSync(f)),
164
+ },
165
+ },
166
+ module: {
167
+ parser: { javascript: { exportsPresence: 'error' } },
168
+ rules: [
169
+ {
170
+ oneOf: [
171
+ // 两个 babel-loader 是为了处理 pdfjs-dist 与 ofd.js
172
+ {
173
+ test: /\.(j|t)sx?$/,
174
+ loader: 'babel-loader',
175
+ include: paths.src,
176
+ options: require('./babel')(),
177
+ },
178
+ (override.transformNodeModules || isProd) && {
179
+ test: /\.m?js$/,
180
+ loader: 'babel-loader',
181
+ exclude: /@babel(?:\/|\\{1,2})runtime/,
182
+ options: require('./babel.dependencies')(),
183
+ },
184
+ {
185
+ test: cssRegex,
186
+ exclude: cssModuleRegex,
187
+ use: getStyleLoaders({
188
+ importLoaders: 1,
189
+ sourceMap: isDev,
190
+ modules: {
191
+ mode: 'global',
192
+ localIdentName: '[local]--[hash:base64:6]',
193
+ },
194
+ }),
195
+ sideEffects: true,
196
+ },
197
+ {
198
+ test: cssModuleRegex,
199
+ use: getStyleLoaders({
200
+ importLoaders: 1,
201
+ sourceMap: isDev,
202
+ modules: {
203
+ mode: 'local',
204
+ localIdentName: '[local]--[hash:base64:6]',
205
+ },
206
+ }),
207
+ },
208
+ {
209
+ test: lessRegex,
210
+ exclude: lessModuleRegex,
211
+ use: getStyleLoaders(
212
+ {
213
+ importLoaders: 2,
214
+ sourceMap: isDev,
215
+ modules: {
216
+ mode: 'global',
217
+ localIdentName: '[local]--[hash:base64:6]',
218
+ },
219
+ },
220
+ 'less-loader'
221
+ ),
222
+ sideEffects: true,
223
+ },
224
+ {
225
+ test: lessModuleRegex,
226
+ use: getStyleLoaders(
227
+ {
228
+ importLoaders: 2,
229
+ sourceMap: isDev,
230
+ modules: {
231
+ mode: 'local',
232
+ localIdentName: '[local]--[hash:base64:6]',
233
+ },
234
+ },
235
+ 'less-loader'
236
+ ),
237
+ },
238
+ {
239
+ test: /\.(bmp|png|jpe?g|gif|webp)$/,
240
+ type: 'asset',
241
+ parser: {
242
+ dataUrlCondition: {
243
+ maxSize: imageInlineSizeLimit, // 10kb
244
+ },
245
+ },
246
+ },
247
+ {
248
+ test: /\.svg$/,
249
+ type: 'asset',
250
+ parser: {
251
+ dataUrlCondition: {
252
+ maxSize: imageInlineSizeLimit, // 10kb
253
+ },
254
+ },
255
+ issuer: {
256
+ and: [/\.(css|less)$/],
257
+ },
258
+ },
259
+ {
260
+ test: /\.svg$/,
261
+ use: [
262
+ {
263
+ loader: '@svgr/webpack',
264
+ options: {
265
+ prettier: false,
266
+ svgo: false,
267
+ svgoConfig: {
268
+ plugins: [{ removeViewBox: false }],
269
+ },
270
+ titleProp: true,
271
+ ref: false,
272
+ },
273
+ },
274
+ {
275
+ loader: 'url-loader',
276
+ options: {
277
+ name: `images/${assetPath}/[name].[hash:6].[ext]`,
278
+ limit: imageInlineSizeLimit,
279
+ },
280
+ },
281
+ ],
282
+ issuer: {
283
+ and: [/\.(ts|tsx|js|jsx|md|mdx)$/],
284
+ },
285
+ },
286
+ {
287
+ // Exclude `js` files to keep "css" loader working as it injects
288
+ // its runtime that would otherwise be processed through "file" loader.
289
+ // Also exclude `html` and `json` extensions so they get processed
290
+ // by webpacks internal loaders.
291
+ exclude: [/^$/, /\.(js|mjs|jsx|ts|tsx)$/, /\.html$/, /\.json$/],
292
+ type: 'asset/resource',
293
+ },
294
+ ].filter(Boolean),
295
+ },
296
+ ],
297
+ },
298
+ plugins: [
299
+ qsbCDN.isUseCommon &&
300
+ new webpack.DllReferencePlugin({
301
+ manifest: require('../asset/dll/libcommon3-manifest.json'),
302
+ }),
303
+ new webpack.IgnorePlugin({
304
+ resourceRegExp: /^\.\/locale$/,
305
+ contextRegExp: /moment$/,
306
+ }),
307
+ new webpack.DefinePlugin({
308
+ 'process.env.APP_NAME': JSON.stringify(appPkg.name),
309
+ 'process.env.APP_VERSION': JSON.stringify(appPkg.version),
310
+ 'process.env.BABEL_ENV': JSON.stringify(process.env.BABEL_ENV),
311
+ 'process.env.BROWSERSLIST': JSON.stringify(process.env.BROWSERSLIST),
312
+ 'process.env.WDS_SOCKET_HOST': JSON.stringify(process.env.WDS_SOCKET_HOST),
313
+ 'process.env.WDS_SOCKET_PORT': JSON.stringify(process.env.WDS_SOCKET_PORT),
314
+ ...override.define,
315
+ }),
316
+ new webpack.ProgressPlugin(),
317
+ isDev && new CaseSensitivePathsPlugin(),
318
+ isDev && new ReactRefreshPlugin({ overlay: false }),
319
+ ...(isDev || process.env.OUTPUT_HTML || appConfig.single || appConfig.mainProject
320
+ ? paths.indexHTML.map(
321
+ (template) =>
322
+ new HtmlWebpackPlugin({
323
+ template: template,
324
+ filename: template.split('/').pop(),
325
+ inject: false,
326
+ minify: {
327
+ removeComments: true,
328
+ collapseWhitespace: true,
329
+ removeRedundantAttributes: true,
330
+ useShortDoctype: true,
331
+ removeEmptyAttributes: true,
332
+ removeStyleLinkTypeAttributes: true,
333
+ keepClosingSlash: true,
334
+ minifyJS: true,
335
+ minifyCSS: true,
336
+ minifyURLs: true,
337
+ },
338
+ })
339
+ )
340
+ : []),
341
+ process.env.ANALYZE && isProd && new BundleAnalyzerPlugin(),
342
+ ].filter(Boolean),
343
+ optimization: {
344
+ minimize: isProd && override.minify !== false,
345
+ minimizer: [
346
+ override.minify === 'esbuild' &&
347
+ new ESBuildMinifyPlugin({
348
+ pure: override.pure_funcs ?? ['console.log'],
349
+ drop: ['debugger'],
350
+ keepNames: false,
351
+ legalComments: 'none',
352
+ target: 'es5',
353
+ }),
354
+ // This is only used in production mode
355
+ override.minify === 'terser' &&
356
+ new TerserPlugin({
357
+ extractComments: false,
358
+ terserOptions: {
359
+ parse: {
360
+ // We want terser to parse ecma 8 code. However, we don't want it
361
+ // to apply any minification steps that turns valid ecma 5 code
362
+ // into invalid ecma 5 code. This is why the 'compress' and 'output'
363
+ // sections only apply transformations that are ecma 5 safe
364
+ // https://github.com/facebook/create-react-app/pull/4234
365
+ ecma: 8,
366
+ },
367
+ compress: {
368
+ pure_funcs: override.pure_funcs ?? ['console.log'],
369
+ drop_debugger: true,
370
+ ecma: 5,
371
+ warnings: false,
372
+ // Disabled because of an issue with Uglify breaking seemingly valid code:
373
+ // https://github.com/facebook/create-react-app/issues/2376
374
+ // Pending further investigation:
375
+ // https://github.com/mishoo/UglifyJS2/issues/2011
376
+ comparisons: false,
377
+ // Disabled because of an issue with Terser breaking valid code:
378
+ // https://github.com/facebook/create-react-app/issues/5250
379
+ // Pending further investigation:
380
+ // https://github.com/terser-js/terser/issues/120
381
+ inline: 2,
382
+ },
383
+ mangle: {
384
+ safari10: true,
385
+ },
386
+ // Added for profiling in devtools
387
+ keep_classnames: false,
388
+ keep_fnames: false,
389
+ output: {
390
+ ecma: 5,
391
+ comments: false,
392
+ // Turned on because emoji and regex is not minified properly using default
393
+ // https://github.com/facebook/create-react-app/issues/2488
394
+ ascii_only: true,
395
+ },
396
+ },
397
+ }),
398
+ ].filter(Boolean),
399
+ splitChunks: {
400
+ minChunks: 2,
401
+ },
402
+ },
403
+ performance: {
404
+ maxEntrypointSize: appConfig.single ? 1024 * 1024 : 30 * 1024,
405
+ maxAssetSize: 1024 * 1024,
406
+ },
407
+ }
408
+
409
+ return config
410
+ }
@@ -0,0 +1,44 @@
1
+ const WebpackDevServer = require('webpack-dev-server')
2
+
3
+ /**
4
+ * @param {*} args
5
+ * @param {import('../utils/defineConfig').Config} override
6
+ */
7
+ module.exports = function getWebpackDevServerConfig(args, override) {
8
+ const host = process.env.HOST || '0.0.0.0'
9
+ const sockHost = process.env.WDS_SOCKET_HOST
10
+ const sockPort = process.env.WDS_SOCKET_PORT
11
+
12
+ /** @type {WebpackDevServer.Configuration} */
13
+ const devServer = {
14
+ hot: true,
15
+ allowedHosts: 'all',
16
+ historyApiFallback: true,
17
+ port: args.port,
18
+ open: args.open,
19
+ host,
20
+ client: {
21
+ webSocketURL: { protocol: 'auto:', hostname: sockHost, port: sockPort },
22
+ },
23
+ headers: {
24
+ 'Access-Control-Allow-Origin': '*',
25
+ 'Access-Control-Allow-Methods': '*',
26
+ 'Access-Control-Allow-Headers': '*',
27
+ },
28
+ proxy: [
29
+ ...override.proxy,
30
+ {
31
+ context: ['/api'],
32
+ target: 'http://192.168.10.19:3339/qsxxwapdev',
33
+ changeOrigin: true,
34
+ headers: {
35
+ host: 'www.zhidianbao.cn',
36
+ origin: 'http://www.zhidianbao.cn',
37
+ },
38
+ },
39
+ ],
40
+ compress: true,
41
+ }
42
+
43
+ return devServer
44
+ }
package/src/deploy.js ADDED
@@ -0,0 +1,158 @@
1
+ const { sshSftp } = require('@qse/ssh-sftp')
2
+ const path = require('path')
3
+ const paths = require('./config/paths')
4
+ const pkg = require(paths.package)
5
+ const chalk = require('chalk')
6
+ const fs = require('fs-extra')
7
+ const changeDeployVersion = require('./utils/changeDeployVersion')
8
+ const ora = require('ora')
9
+ const appConfig = require('./utils/appConfig')
10
+
11
+ const baseConfig = {
12
+ localPath: 'dist',
13
+ ignore: [],
14
+ cleanRemoteFiles: false,
15
+ securityLock: false,
16
+ keepAlive: true,
17
+ noWarn: true,
18
+ }
19
+
20
+ async function normalDeploy(args) {
21
+ const presetConfig = {
22
+ s: {
23
+ context: 'eduwebngv1',
24
+ folder: 'userportal',
25
+ },
26
+ b: {
27
+ context: 'eduwebngv1',
28
+ folder: 'bureaupc',
29
+ },
30
+ d: {
31
+ context: 'eduwebngv1',
32
+ folder: 'documentshelves',
33
+ },
34
+ }
35
+
36
+ const resolve = (...pathSegments) => path.resolve(process.cwd(), ...pathSegments)
37
+
38
+ /**
39
+ * 生成路径
40
+ * @name remoteFile 远程文件
41
+ * @name tmpFile 本地文件
42
+ * @name tmpDir 本地临时文件夹 `tmpFile`存在的文件夹
43
+ * @name tmpBase 本地临时文件夹 到`__tmp__`层
44
+ * @param {string} remoteFilePath 远程文件相对`opts.remotePath`地址
45
+ */
46
+ function getLocalAndRemoteFilePath(remoteFilePath, opts) {
47
+ const splited = remoteFilePath.split('/')
48
+ const fileName = splited[splited.length - 1]
49
+ const tmpBase = resolve(opts.localPath, '__tmp__')
50
+ const tmpDir = resolve(opts.localPath, '__tmp__', ...splited.slice(0, -1))
51
+ const tmpFile = resolve(tmpDir, fileName)
52
+
53
+ const remoteFile = [opts.remotePath, remoteFilePath].join('/')
54
+
55
+ return { tmpDir, tmpFile, remoteFile, tmpBase }
56
+ }
57
+
58
+ async function upload(opts) {
59
+ // 上传dist文件
60
+ const { sftp, opts: fullOpts } = await sshSftp(opts)
61
+
62
+ const spinner = ora('自动更新 ver.js 版本配置').start()
63
+ // 指定远程需要修改的文件
64
+ const fileName = 'js/ver.js'
65
+ // 生成各种路径
66
+ const { remoteFile, tmpDir, tmpFile, tmpBase } = getLocalAndRemoteFilePath(fileName, fullOpts)
67
+
68
+ try {
69
+ // 创建本地临时文件夹,位置在 `opts.localPath` 下的 `__tmp__` 文件夹
70
+ fs.mkdirSync(tmpDir, { recursive: true })
71
+
72
+ // 拉取远程文件到本地
73
+ await sftp.fastGet(remoteFile, tmpFile)
74
+
75
+ // 读取本地文件内容
76
+ let code = await fs.readFile(tmpFile, { encoding: 'utf-8' })
77
+ code = changeDeployVersion(code, {
78
+ name: pkg.name,
79
+ version: pkg.version,
80
+ grayscale: appConfig.grayscale,
81
+ })
82
+
83
+ await sftp.fastPut(tmpFile, remoteFile + '.bak')
84
+ await fs.writeFile(tmpFile, code)
85
+ // 将修改完的内容传回服务器
86
+ await sftp.fastPut(tmpFile, remoteFile)
87
+
88
+ spinner.succeed('已更新 ver.js 版本配置')
89
+ } catch (e) {
90
+ spinner.fail(`自动修改 ver.js 失败,请手动修改`)
91
+ console.log(chalk.bgRed(e.message))
92
+ } finally {
93
+ await sftp.end()
94
+ // 清除临时文件夹
95
+ fs.removeSync(tmpBase)
96
+ }
97
+ }
98
+
99
+ const presets = []
100
+ if (args.b) {
101
+ presets.push(presetConfig.b)
102
+ }
103
+ if (args.s) {
104
+ presets.push(presetConfig.s)
105
+ }
106
+ if (args.d) {
107
+ presets.push(presetConfig.d)
108
+ }
109
+ if (presets.length === 0) {
110
+ console.log(
111
+ `
112
+ ${chalk.red('指定 deploy 部署范围')}
113
+
114
+ # 部署代码 校端
115
+ ${chalk.green('edu-scripts deploy -s')}
116
+ # 部署代码 局端
117
+ ${chalk.green('edu-scripts deploy -b')}
118
+ # 部署代码 公文
119
+ ${chalk.green('edu-scripts deploy -d')}
120
+ # 部署代码 校端 + 局端
121
+ ${chalk.green('edu-scripts deploy -s -b')}
122
+ `
123
+ )
124
+ process.exit()
125
+ }
126
+
127
+ const uploadConfig = { ...baseConfig, ignore: [...baseConfig.ignore, 'js/ver.js'] }
128
+ if (!appConfig.mainProject) {
129
+ uploadConfig.ignore = [...uploadConfig.ignore, '!(js|images)']
130
+ }
131
+
132
+ for (const preset of presets) {
133
+ await upload({ ...uploadConfig, preset })
134
+ }
135
+ }
136
+
137
+ async function singleDeploy() {
138
+ const config = fs.existsSync(paths.sshSftp)
139
+ ? fs.readJsonSync(paths.sshSftp)
140
+ : {
141
+ ...baseConfig,
142
+ preset: { context: 'qsxxwapdev' },
143
+ cleanRemoteFiles: true,
144
+ securityLock: true,
145
+ keepAlive: false,
146
+ noWarn: false,
147
+ }
148
+
149
+ sshSftp(config)
150
+ }
151
+
152
+ module.exports = function deploy(args) {
153
+ if (appConfig.single) {
154
+ singleDeploy()
155
+ } else {
156
+ normalDeploy(args)
157
+ }
158
+ }