Sfoglia il codice sorgente

Fix some build issues.

oldj 4 anni fa
parent
commit
749576e215

+ 6 - 0
.babelrc

@@ -5,6 +5,12 @@
       {
         "loose": true
       }
+    ],
+    [
+      "@babel/plugin-proposal-private-property-in-object",
+      {
+        "loose": true
+      }
     ]
   ]
 }

+ 16 - 0
package-lock.json

@@ -75,6 +75,7 @@
         "webpack": "^5.51.1",
         "webpack-cli": "^4.8.0",
         "webpack-merge": "^5.8.0",
+        "webpack-node-externals": "^3.0.0",
         "yorkie": "^2.0.0"
       }
     },
@@ -23871,6 +23872,15 @@
         "node": ">=10.0.0"
       }
     },
+    "node_modules/webpack-node-externals": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/webpack-node-externals/-/webpack-node-externals-3.0.0.tgz",
+      "integrity": "sha512-LnL6Z3GGDPht/AigwRh2dvL9PQPFQ8skEpVrWZXLWBYmqcaojHNN0onvHzie6rq7EWKrrBfPYqNEzTJgiwEQDQ==",
+      "dev": true,
+      "engines": {
+        "node": ">=6"
+      }
+    },
     "node_modules/webpack-sources": {
       "version": "3.2.0",
       "resolved": "https://registry.npmjs.org/webpack-sources/-/webpack-sources-3.2.0.tgz",
@@ -43465,6 +43475,12 @@
         "wildcard": "^2.0.0"
       }
     },
+    "webpack-node-externals": {
+      "version": "3.0.0",
+      "resolved": "https://registry.npmjs.org/webpack-node-externals/-/webpack-node-externals-3.0.0.tgz",
+      "integrity": "sha512-LnL6Z3GGDPht/AigwRh2dvL9PQPFQ8skEpVrWZXLWBYmqcaojHNN0onvHzie6rq7EWKrrBfPYqNEzTJgiwEQDQ==",
+      "dev": true
+    },
     "webpack-sources": {
       "version": "3.2.0",
       "resolved": "https://registry.npmjs.org/webpack-sources/-/webpack-sources-3.2.0.tgz",

+ 1 - 0
package.json

@@ -85,6 +85,7 @@
     "webpack": "^5.51.1",
     "webpack-cli": "^4.8.0",
     "webpack-merge": "^5.8.0",
+    "webpack-node-externals": "^3.0.0",
     "yorkie": "^2.0.0"
   },
   "build": {

+ 58 - 50
scripts/make.js

@@ -19,38 +19,38 @@ const electronLanguages = ['en', 'zh_CN']
 
 const TARGET_PLATFORMS_configs = {
   mac: {
-    mac: ['default']
+    mac: ['default'],
   },
   mas: {
-    mac: ['mas']
+    mac: ['mas'],
   },
   macs: {
-    mac: ['default', 'mas']
+    mac: ['default', 'mas'],
   },
   win: {
-    win: ['nsis:ia32', 'portable:ia32']
+    win: ['nsis:ia32', 'portable:ia32'],
   },
   all: {
     mac: ['default'],
-    linux: [/*'zip:x64', */'AppImage:x64', 'deb:x64'],
-    win: ['nsis:ia32', 'nsis:x64', 'portable:ia32']
-  }
+    linux: [/*'zip:x64', */ 'AppImage:x64', 'deb:x64'],
+    win: ['nsis:ia32', 'nsis:x64', 'portable:ia32'],
+  },
 }
 
 const APP_NAME = 'SwitchHosts'
 const { IDENTITY } = process.env
 
 const cfg_common = {
-  copyright: `Copyright © ${(new Date()).getFullYear()}`,
+  copyright: `Copyright © ${new Date().getFullYear()}`,
   buildVersion: version[3].toString(),
   directories: {
     buildResources: 'build',
-    app: 'build'
+    app: 'build',
   },
   electronDownload: {
     cache: path.join(homedir, '.electron'),
-    mirror: 'https://npm.taobao.org/mirrors/electron/'
-  }
+    mirror: 'https://npm.taobao.org/mirrors/electron/',
+  },
 }
 
 const sign = async () => {
@@ -79,8 +79,8 @@ const beforeMake = async () => {
   const to_cp = [
     [
       path.join(root_dir, 'assets', 'app.png'),
-      path.join(root_dir, 'build', 'assets', 'app.png')
-    ]
+      path.join(root_dir, 'build', 'assets', 'app.png'),
+    ],
   ]
 
   to_cp.map(([src, target]) => {
@@ -97,7 +97,7 @@ const beforeMake = async () => {
   fse.writeFileSync(
     path.join(root_dir, 'build', 'package.json'),
     JSON.stringify(pkg_app, null, 2),
-    'utf-8'
+    'utf-8',
   )
 }
 
@@ -119,9 +119,9 @@ const makeMacArm = async () => {
             target: 'dmg',
             arch: [
               //'x64',
-              'arm64'
-            ]
-          }
+              'arm64',
+            ],
+          },
         ],
         category: 'public.app-category.productivity',
         icon: 'assets/app.icns',
@@ -132,7 +132,7 @@ const makeMacArm = async () => {
         entitlements: 'scripts/entitlements.mac.plist',
         entitlementsInherit: 'scripts/entitlements.mac.plist',
         provisioningProfile: 'scripts/app.provisionprofile',
-        artifactName: '${productName}_arm64_${version}(${buildVersion}).${ext}'
+        artifactName: '${productName}_arm64_${version}(${buildVersion}).${ext}',
       },
       dmg: {
         //backgroundColor: '#f1f1f6',
@@ -141,21 +141,24 @@ const makeMacArm = async () => {
         iconSize: 160,
         window: {
           width: 600,
-          height: 420
+          height: 420,
         },
-        contents: [{
-          x: 150,
-          y: 200
-        }, {
-          x: 450,
-          y: 200,
-          type: 'link',
-          path: '/Applications'
-        }],
+        contents: [
+          {
+            x: 150,
+            y: 200,
+          },
+          {
+            x: 450,
+            y: 200,
+            type: 'link',
+            path: '/Applications',
+          },
+        ],
         sign: false,
-        artifactName: '${productName}_arm64_${version}(${buildVersion}).${ext}'
-      }
-    }
+        artifactName: '${productName}_arm64_${version}(${buildVersion}).${ext}',
+      },
+    },
   })
 
   console.log('done!')
@@ -184,7 +187,7 @@ const makeDefault = async () => {
         entitlements: 'scripts/entitlements.mac.plist',
         entitlementsInherit: 'scripts/entitlements.mac.plist',
         provisioningProfile: 'scripts/app.provisionprofile',
-        artifactName: '${productName}_${version}(${buildVersion}).${ext}'
+        artifactName: '${productName}_${version}(${buildVersion}).${ext}',
       },
       dmg: {
         //backgroundColor: '#f1f1f6',
@@ -193,45 +196,50 @@ const makeDefault = async () => {
         iconSize: 160,
         window: {
           width: 600,
-          height: 420
+          height: 420,
         },
-        contents: [{
-          x: 150,
-          y: 200
-        }, {
-          x: 450,
-          y: 200,
-          type: 'link',
-          path: '/Applications'
-        }],
+        contents: [
+          {
+            x: 150,
+            y: 200,
+          },
+          {
+            x: 450,
+            y: 200,
+            type: 'link',
+            path: '/Applications',
+          },
+        ],
         sign: false,
-        artifactName: '${productName}_${version}(${buildVersion}).${ext}'
+        artifactName: '${productName}_${version}(${buildVersion}).${ext}',
       },
       win: {
-        icon: 'assets/app.ico'
+        icon: 'assets/app.ico',
         //requestedExecutionLevel: 'requireAdministrator'
       },
       nsis: {
         installerIcon: 'assets/installer-icon.ico',
         oneClick: false,
         allowToChangeInstallationDirectory: true,
-        artifactName: '${productName}_installer_${version}(${buildVersion}).${ext}'
+        artifactName:
+          '${productName}_installer_${version}(${buildVersion}).${ext}',
       },
       portable: {
-        artifactName: '${productName}_portable_${version}(${buildVersion}).${ext}'
+        artifactName:
+          '${productName}_portable_${version}(${buildVersion}).${ext}',
       },
       linux: {
         icon: 'assets/app.png',
         artifactName: '${productName}_linux_${version}(${buildVersion}).${ext}',
-        category: 'Office'
-      }
-    }
+        category: 'Office',
+      },
+    },
   })
 
   console.log('done!')
 }
 
-(async () => {
+;(async () => {
   try {
     await beforeMake()
 

+ 2 - 2
scripts/notarize.js

@@ -7,7 +7,7 @@
 require('dotenv').config()
 const { notarize } = require('electron-notarize')
 
-exports.default = async function notarizing (context) {
+exports.default = async function notarizing(context) {
   const appName = context.packager.appInfo.productFilename
   const { electronPlatformName, appOutDir } = context
   if (electronPlatformName !== 'darwin') {
@@ -30,6 +30,6 @@ exports.default = async function notarizing (context) {
     appPath,
     ascProvider,
     appleId,
-    appleIdPassword
+    appleIdPassword,
   })
 }

+ 4 - 4
scripts/webpack.base.config.js

@@ -6,15 +6,15 @@ module.exports = {
   mode: 'development',
   output: {
     path: path.resolve(__dirname, '..', 'build'),
-    filename: '[name].js'
+    filename: '[name].js',
   },
   node: {
     __dirname: false,
-    __filename: false
+    __filename: false,
   },
   resolve: {
-    extensions: ['.tsx', '.ts', '.js', '.json']
+    extensions: ['.tsx', '.ts', '.js', '.json'],
   },
   devtool: 'source-map',
-  plugins: []
+  plugins: [],
 }

+ 23 - 23
scripts/webpack.main.config.js

@@ -1,8 +1,8 @@
 const webpack = require('webpack')
 const { merge } = require('webpack-merge')
-const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin')
 const TsconfigPathsPlugin = require('tsconfig-paths-webpack-plugin')
 const CopyPlugin = require('copy-webpack-plugin')
+const nodeExternals = require('webpack-node-externals')
 
 const baseConfig = require('./webpack.base.config')
 
@@ -10,46 +10,46 @@ module.exports = merge(baseConfig, {
   target: 'electron-main',
   entry: {
     main: './src/main/main.ts',
-    preload: './src/main/preload.ts'
+    preload: './src/main/preload.ts',
   },
   resolve: {
-    plugins: [ new TsconfigPathsPlugin({}) ]
+    plugins: [new TsconfigPathsPlugin({})],
   },
+  externals: [nodeExternals()],
   module: {
     rules: [
       {
-        test: /\.tsx?$/,
+        test: /\.ts$/,
         exclude: /node_modules/,
         loader: 'babel-loader',
         options: {
           cacheDirectory: true,
           babelrc: false,
           presets: [
+            ['@babel/preset-env', { targets: 'maintained node versions' }],
+            '@babel/preset-typescript',
+          ],
+          plugins: [
+            ['@babel/plugin-proposal-decorators', { legacy: true }],
+            ['@babel/plugin-proposal-class-properties', { loose: true }],
+            ['@babel/plugin-proposal-private-methods', { loose: true }],
             [
-              '@babel/preset-env',
-              { targets: 'maintained node versions' }
+              '@babel/plugin-proposal-private-property-in-object',
+              { loose: true },
             ],
-            '@babel/preset-typescript'
           ],
-          plugins: [
-            [ '@babel/plugin-proposal-decorators', { legacy: true } ],
-            [ '@babel/plugin-proposal-class-properties', { loose: true } ]
-          ]
-        }
-      }
-    ]
+        },
+      },
+    ],
   },
   plugins: [
-    new ForkTsCheckerWebpackPlugin({
-      //reportFiles: ['src/main/**/*']
-    }),
     new webpack.DefinePlugin({
-      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development')
+      'process.env.NODE_ENV': JSON.stringify(
+        process.env.NODE_ENV || 'development',
+      ),
     }),
     new CopyPlugin({
-      patterns: [
-        { from: 'src/assets', to: 'assets' }
-      ]
-    })
-  ]
+      patterns: [{ from: 'src/assets', to: 'assets' }],
+    }),
+  ],
 })

+ 1 - 1
scripts/webpack.main.prod.config.js

@@ -3,5 +3,5 @@ const { merge } = require('webpack-merge')
 const baseConfig = require('./webpack.main.config')
 
 module.exports = merge(baseConfig, {
-  mode: 'production'
+  mode: 'production',
 })

+ 1 - 1
src/common/data.d.ts

@@ -1,4 +1,4 @@
-import { ITreeNodeData } from '@renderer/components/Tree/Node'
+import { ITreeNodeData } from './tree'
 
 export type HostsType = 'local' | 'remote' | 'group' | 'folder'
 export type FolderModeType = 0 | 1 | 2 // 0: 默认; 1: 单选; 2: 多选

+ 48 - 0
src/common/tree.ts

@@ -0,0 +1,48 @@
+export type NodeIdType = string
+
+export interface ITreeNodeData {
+  id: NodeIdType
+  title?: string
+  can_select?: boolean // 是否可以被选中,默认为 true
+  can_drag?: boolean // 是否可以拖动,默认为 true
+  can_drop_before?: boolean // 是否可以接受 drop before,默认为 true
+  can_drop_in?: boolean // 是否可以接受 drop in,默认为 true
+  can_drop_after?: boolean // 是否可以接受 drop after,默认为 true
+  is_collapsed?: boolean
+  children?: ITreeNodeData[]
+
+  [key: string]: any
+}
+
+interface IWithChildren {
+  children?: IWithChildren[]
+}
+
+export function flatten<T extends IWithChildren>(tree_list: T[]): T[] {
+  let arr: any[] = []
+
+  Array.isArray(tree_list) &&
+    tree_list.map((item) => {
+      if (!item) return
+
+      arr.push(item)
+
+      if (Array.isArray(item.children)) {
+        let a2 = flatten(item.children)
+        arr = arr.concat(a2)
+      }
+    })
+
+  return arr
+}
+
+interface IWidthId extends IWithChildren {
+  id: string
+}
+
+export function getNodeById<T extends IWidthId>(
+  tree_list: T[],
+  id: string,
+): T | undefined {
+  return flatten(tree_list).find((i) => i.id === id)
+}

+ 6 - 3
src/main/actions/trashcan/restoreItem.ts

@@ -5,8 +5,11 @@
 
 import { getList, setList } from '@main/actions'
 import { swhdb } from '@main/data'
-import { getNodeById } from '@renderer/components/Tree/fn'
-import { ITrashcanListObject } from '@root/common/data'
+import { getNodeById } from '@root/common/tree'
+import {
+  IHostsListObject,
+  ITrashcanListObject,
+} from '@root/common/data'
 
 export default async (id: string): Promise<boolean> => {
   let trashcan_item: ITrashcanListObject = await swhdb.list.trashcan.find(
@@ -30,7 +33,7 @@ export default async (id: string): Promise<boolean> => {
   if (!parent_id) {
     await setList([...list, hosts])
   } else {
-    let parent_hosts = getNodeById(list, parent_id)
+    let parent_hosts = getNodeById<IHostsListObject>(list, parent_id)
     if (!parent_hosts) {
       console.log(`can't find parent_hosts with id #${parent_id}.`)
       return false

+ 2 - 15
src/renderer/components/Tree/Node.tsx

@@ -4,12 +4,13 @@
  * @homepage: https://oldj.net
  */
 
+import { ITreeNodeData, NodeIdType } from '@root/common/tree'
 import clsx from 'clsx'
 import lodash from 'lodash'
 import React, { useRef } from 'react'
 import { isChildOf, isSelfOrChild } from './fn'
 import styles from './style.less'
-import { DropWhereType, MultipleSelectType, NodeIdType } from './Tree'
+import { DropWhereType, MultipleSelectType } from './Tree'
 
 declare global {
   interface Window {
@@ -20,20 +21,6 @@ declare global {
 
 export type NodeUpdate = (data: Partial<ITreeNodeData>) => void
 
-export interface ITreeNodeData {
-  id: NodeIdType
-  title?: string
-  can_select?: boolean // 是否可以被选中,默认为 true
-  can_drag?: boolean // 是否可以拖动,默认为 true
-  can_drop_before?: boolean // 是否可以接受 drop before,默认为 true
-  can_drop_in?: boolean // 是否可以接受 drop in,默认为 true
-  can_drop_after?: boolean // 是否可以接受 drop after,默认为 true
-  is_collapsed?: boolean
-  children?: ITreeNodeData[]
-
-  [key: string]: any
-}
-
 interface INodeProps {
   tree: ITreeNodeData[]
   data: ITreeNodeData

+ 2 - 2
src/renderer/components/Tree/Tree.tsx

@@ -4,6 +4,7 @@
  * @homepage: https://oldj.net
  */
 
+import { ITreeNodeData, NodeIdType } from '@root/common/tree'
 import clsx from 'clsx'
 import lodash from 'lodash'
 import React, { useEffect, useState } from 'react'
@@ -14,10 +15,9 @@ import {
   selectTo,
   treeMoveNode,
 } from './fn'
-import Node, { ITreeNodeData, NodeUpdate } from './Node'
+import Node, { NodeUpdate } from './Node'
 import styles from './style.less'
 
-export type NodeIdType = string
 export type DropWhereType = 'before' | 'in' | 'after'
 export type MultipleSelectType = 0 | 1 | 2
 

+ 2 - 2
src/renderer/components/Tree/fn.ts

@@ -1,6 +1,6 @@
+import { ITreeNodeData, NodeIdType } from '@root/common/tree'
 import lodash from 'lodash'
-import { ITreeNodeData } from './Node'
-import { DropWhereType, NodeIdType } from './Tree'
+import { DropWhereType } from './Tree'
 
 interface IObj {
   [key: string]: any

+ 1 - 1
src/version.json

@@ -1 +1 @@
-[4, 0, 3, 6070]
+[4, 0, 3, 6073]