Skip to content

Commit

Permalink
build: node-nim在编译后拷贝
Browse files Browse the repository at this point in the history
  • Loading branch information
Haochen Duan committed Jun 9, 2024
1 parent d2c80e4 commit 7b25846
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
29 changes: 11 additions & 18 deletions scripts/unpack.mjs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import path from 'node:path';
import fs from 'node:fs';
import fsP from 'node:fs/promises';
import { rimraf } from 'rimraf';
import fse from 'fs-extra/esm';
import builder from 'electron-builder';
import { cwd, appDir, wwwDir, staticsDir, build, nodeNimSDK, sdkDownloadDir, output, unpacked, isMacOS, isArm64 } from './utils.mjs';
import { cwd, appDir, wwwDir, staticsDir, build, sdkDownloadDir, output, unpacked, unpackedNodeModules, isMacOS, isArm64 } from './utils.mjs';
import taskfile from './taskfile.mjs';
import nimSdkDownload from './nimSdkDownload.mjs';
import packageJson from '../package.json' assert { type: 'json' };
Expand Down Expand Up @@ -69,7 +68,7 @@ function config(outputDir, target) {
icon: icon.linux,
executableName: '48tools'
},
includeSubNodeModules: true,
includeSubNodeModules: false,
npmRebuild: false,
nodeGypRebuild: false,
electronDownload: {
Expand Down Expand Up @@ -120,53 +119,48 @@ async function uglifyPackageJson() {

/**
* 替换sdk
* @param { string } [downloadDir] - 下载目录
* @param { string } downloadDir - 下载目录
* @param { string } nodeModulesDir - node_modules目录
*/
async function copySDK(downloadDir) {
if (fs.existsSync(nodeNimSDK)) {
await rimraf(nodeNimSDK);
}

if (downloadDir) {
await fse.copy(path.join(downloadDir, 'node-nim'), nodeNimSDK);
}
async function copySDK(downloadDir, nodeModulesDir) {
await fse.copy(path.join(downloadDir, 'node-nim'), path.join(nodeModulesDir, 'node-nim'));
}

async function unpackOthers() {
// 编译mac
if (isMacOS) {
// 编译mac
console.log('⏳正在编译:mac');
await copySDK(sdkDownloadDir.mac);
await builder.build({
targets: builder.Platform.MAC.createTarget(),
config: config(output.mac)
});
await copySDK(sdkDownloadDir.mac, unpackedNodeModules.mac);
}

// 编译win64
console.log('⏳正在编译:win64');
await copySDK(sdkDownloadDir.win64);
await builder.build({
targets: builder.Platform.WINDOWS.createTarget(),
config: config(output.win)
});
await copySDK(sdkDownloadDir.win64, unpackedNodeModules.win);

// 编译win32
console.log('⏳正在编译:win32');
await copySDK(sdkDownloadDir.win32);
await builder.build({
targets: builder.Platform.WINDOWS.createTarget(),
config: config(output.win32, ['win', { target: 'dir', arch: 'ia32' }])
});
await copySDK(sdkDownloadDir.win32, unpackedNodeModules.win32);

// 编译linux
console.log('⏳正在编译:linux');
await copySDK(sdkDownloadDir.linux);
await builder.build({
targets: builder.Platform.LINUX.createTarget(),
config: config(output.linux)
});
await copySDK(sdkDownloadDir.linux, unpackedNodeModules.linux);

// 拷贝许可文件
console.log('🚚正在拷贝许可文件');
Expand All @@ -184,11 +178,11 @@ async function unpackArm64() {
if (isMacOS) {
// 编译mac-arm64
console.log('⏳正在编译:mac-arm64');
await copySDK(sdkDownloadDir.macArm64);
await builder.build({
targets: builder.Platform.MAC.createTarget(),
config: config(output.macArm64, ['mac', { target: 'dir', arch: 'arm64' }])
});
await copySDK(sdkDownloadDir.macArm64, unpackedNodeModules.macArm64);
}

// 编译win-arm64
Expand All @@ -200,7 +194,6 @@ async function unpackArm64() {

// 拷贝许可文件
console.log('🚚正在拷贝许可文件');
await copySDK();
await Promise.all([
...isMacOS ? copy(unpacked.macArm64, true) : [],
...copy(unpacked.winArm64)
Expand Down
11 changes: 9 additions & 2 deletions scripts/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export const staticsDir = path.join(cwd, 'statics'); // 静态资源目录
export const build = path.join(cwd, 'build'); // 最终生成的可执行文件

// SDK
export const nodeNimSDK = path.join(wwwDir, 'node_modules/nim-sdk');

const sdkDownloadBasicDir = path.join(build, 'node-nim-sdk');

export const sdkDownloadDir = {
Expand Down Expand Up @@ -44,6 +42,15 @@ export const unpacked = {
linux: path.join(output.linux, 'linux-unpacked')
};

export const unpackedNodeModules = {
mac: path.join(unpacked.mac, '48tools.app/Contents/Resources/app/node_modules'),
macArm64: path.join(unpacked.macArm64, '48tools.app/Contents/Resources/app/node_modules'),
win: path.join(unpacked.win, 'resources/app/node_modules'),
win32: path.join(unpacked.win32, 'resources/app/node_modules'),
winArm64: path.join(unpacked.winArm64, 'resources/app/node_modules'),
linux: path.join(unpacked.linux, 'resources/app/node_modules')
};

// 系统环境
export const isMacOS = process.platform === 'darwin';
export const isWindows = process.platform === 'win32';
Expand Down

0 comments on commit 7b25846

Please sign in to comment.