Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: electron cannot work #2060

Merged
merged 7 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"scripts": {
"preinstall": "node scripts/preinstall.js && husky install",
"clean": "rimraf './packages/*/lib'",
"clean": "rimraf \"./packages/*/lib\"",
"check:dep": "ts-node ./scripts/depcheck",
"init": "yarn run clean && yarn run build:all",
"start": "yarn run rebuild:node && cross-env HOST=127.0.0.1 WS_PATH=ws://127.0.0.1:8000 NODE_ENV=development ts-node ./scripts/start",
Expand Down
7 changes: 3 additions & 4 deletions packages/status-bar/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
"url": "git@github.com:opensumi/core.git"
},
"dependencies": {
"@opensumi/ide-components": "2.21.7",
"@opensumi/ide-core-common": "2.21.7",
"@opensumi/ide-theme": "2.21.7"
"@opensumi/ide-core-common": "2.21.7"
},
"devDependencies": {
"@opensumi/ide-components": "2.21.7",
"@opensumi/ide-core-browser": "2.21.7",
"@opensumi/ide-dev-tool": "^1.3.1",
"@opensumi/ide-theme": "2.14.4"
bytemain marked this conversation as resolved.
Show resolved Hide resolved
"@opensumi/ide-theme": "2.21.7"
}
}
14 changes: 6 additions & 8 deletions packages/utils/src/os.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,17 @@ export function isNodeIntegrated(): boolean {
return typeof module !== 'undefined' && !!module.exports;
}

/**
* @deprecated isElectronEnv is deprecated, please use appConfig#isElectronRenderer instead.
* 框架目前使用 isElectronEnv 的场景基本都与 isElectronRenderer 重复,所以直接废弃 isElectronEnv
*/
export function isElectronEnv(): boolean {
return isElectronRenderer() || isElectronNode();
}

/**
* @deprecated isElectronRenderer will deprecate, please use appConfig#isElectronRenderer instead.
*/
export function isElectronRenderer() {
return (global as any).isElectronRenderer;
return (
(global as any).isElectronRenderer ||
(typeof navigator === 'object' &&
typeof navigator.userAgent === 'string' &&
navigator.userAgent.indexOf('Electron') >= 0)
);
}

export function isElectronNode() {
Expand Down
43 changes: 28 additions & 15 deletions packages/utils/src/process.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isElectronRenderer } from './os';
import { isWindows, isMacintosh, setImmediate } from './platform';

interface IProcess {
Expand All @@ -8,21 +9,33 @@ interface IProcess {
}

declare const process: IProcess;
const safeProcess: IProcess =
typeof process === 'undefined'
? {
cwd(): string {
return '/';
},
env: Object.create(null),
get platform(): string {
return isWindows ? 'win32' : isMacintosh ? 'darwin' : 'linux';
},
nextTick(callback: (...args: any[]) => void): number {
return setImmediate(callback);
},
}
: process;

const _safeProcess = {
cwd(): string {
return '/';
},
env: Object.create(null),
get platform(): string {
return isWindows ? 'win32' : isMacintosh ? 'darwin' : 'linux';
},
nextTick(callback: (...args: any[]) => void): number {
return setImmediate(callback);
},
};

let safeProcess: IProcess;
if (typeof process === 'undefined') {
safeProcess = _safeProcess;
} else {
// 因为我们不推荐开启 browserNodeIntegrated,所以这里需要检查是否为 Electron Renderer 环境
if (isElectronRenderer()) {
safeProcess = _safeProcess;
} else if (typeof process.cwd === 'undefined') {
safeProcess = _safeProcess;
} else {
safeProcess = process;
}
}

export const cwd = safeProcess.cwd;
export const env = safeProcess.env;
Expand Down
37 changes: 20 additions & 17 deletions scripts/rebuild-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,29 @@ function rebuildModule(modulePath, type, version, arch) {
const cache = getBuildCacheDir(modulePath, type, version, arch);
console.log(`cache dir ${cache}`);
if (pathExistsSync(cache) && !force) {
console.log('cache found for ' + info.name);
if (process.platform === 'linux') {
execSync(`cp -r ${cache} ${join(modulePath, 'build')}`);
} else {
try {
rmSync(join(modulePath, 'build'), { recursive: true });
} catch (error) {
// do nothing
try {
console.log('cache found for ' + info.name);
removeSync(join(modulePath, 'build'));

if (process.platform === 'linux') {
execSync(`cp -r ${cache} ${join(modulePath, 'build')}`);
} else {
copySync(cache, join(modulePath, 'build'), { dereference: true });
}
copySync(cache, join(modulePath, 'build'));

return;
} catch (error) {
console.log('cache restore error', error);
console.log('build from source');
}
} else {
console.log(`running command ${commands.join(' ')}`);
execSync(commands.join(' '), {
cwd: modulePath,
stdio: 'inherit',
});
removeSync(cache);
copySync(join(modulePath, 'build'), cache);
}
console.log(`running command ${commands.join(' ')}`);
execSync(commands.join(' '), {
cwd: modulePath,
stdio: 'inherit',
});
removeSync(cache);
copySync(join(modulePath, 'build'), cache, { dereference: true });
}

function getBuildCacheDir(modulePath, type, version, arch) {
Expand Down
1 change: 1 addition & 0 deletions tools/electron/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.yarn/install-state.gz
16 changes: 8 additions & 8 deletions tools/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"link-local": "node ./scripts/link-local.js",
"build:browser": "webpack --config ./build/webpack.browser.config.js ",
"build:node": "webpack --config ./build/webpack.node.config.js ",
"build:extension": "webpack --config ./build/webpack.extension-host.config.js ",
"build:main": "webpack --config ./build/webpack.main.config.js ",
"build:webview": "webpack --config ./build/webpack.webview.config.js ",
"build:browser": "webpack --config ./build/webpack.browser.config.js",
"build:node": "webpack --config ./build/webpack.node.config.js",
"build:extension": "webpack --config ./build/webpack.extension-host.config.js",
"build:main": "webpack --config ./build/webpack.main.config.js",
"build:webview": "webpack --config ./build/webpack.webview.config.js",
"build-prod:browser": "webpack --config ./build/webpack.browser.config.js --mode=production",
"build-prod:node": "webpack --config ./build/webpack.node.config.js --mode=production",
"build-prod:extension": "webpack --config ./build/webpack.extension-host.config.js --mode=production",
Expand All @@ -21,9 +21,9 @@
"watch:extension": "webpack --config ./build/webpack.extension-host.config.js -w --mode=development",
"watch:main": "webpack --config ./build/webpack.main.config.js -w --mode=development",
"watch:webview": "webpack --config ./build/webpack.webview.config.js -w --mode=development",
"watch": "run-p watch:*",
"build": "rimraf -rf ./out && run-p build:*",
"build-prod": "rimraf -rf ./out && run-p build-prod:*",
"watch": "run-p \"watch:*\"",
"build": "rimraf -rf ./out && run-p \"build:*\"",
"build-prod": "rimraf -rf ./out && run-p \"build-prod:*\"",
"pack": "yarn run build && node build/pack.js",
"rebuild-native": "node ./scripts/rebuild-native.js --target=electron",
"start": "electron --inspect=9229 .",
Expand Down
36 changes: 22 additions & 14 deletions tools/electron/scripts/rebuild-native.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { execSync } = require('child_process');
const { rmSync, pathExistsSync, copySync, removeSync } = require('fs-extra');
const { pathExistsSync, copySync, removeSync } = require('fs-extra');
const { join } = require('path');
const os = require('os');
const mri = require('mri');
Expand All @@ -10,6 +10,7 @@ const nativeModules = [
join(__dirname, '../node_modules/node-pty'),
join(__dirname, '../node_modules/@parcel/watcher'),
join(__dirname, '../node_modules/spdlog'),
join(__dirname, '../node_modules/keytar'),
];

let commands;
Expand Down Expand Up @@ -43,29 +44,36 @@ function rebuildModule(modulePath, type, version, arch) {
const cache = getBuildCacheDir(modulePath, type, version, arch);
console.log(`cache dir ${cache}`);
if (pathExistsSync(cache) && !force) {
console.log('cache found for ' + info.name);
try {
rmSync(join(modulePath, 'build'), { recursive: true });
console.log('cache found for ' + info.name);
removeSync(join(modulePath, 'build'));

if (process.platform === 'linux') {
execSync(`cp -r ${cache} ${join(modulePath, 'build')}`);
} else {
copySync(cache, join(modulePath, 'build'), { dereference: true });
}

return;
} catch (error) {
// do nothing
console.log('cache restore error', error);
console.log('build from source');
}
copySync(cache, join(modulePath, 'build'));
} else {
console.log(`running command ${commands.join(' ')}`);
execSync(commands.join(' '), {
cwd: modulePath,
stdio: 'inherit',
});
removeSync(cache);
copySync(join(modulePath, 'build'), cache);
}
console.log(`running command ${commands.join(' ')}`);
execSync(commands.join(' '), {
cwd: modulePath,
stdio: 'inherit',
});
removeSync(cache);
copySync(join(modulePath, 'build'), cache, { dereference: true });
}

function getBuildCacheDir(modulePath, type, version, arch) {
const info = require(join(modulePath, './package.json'));
return join(
require('os').tmpdir(),
'kaitian_build_cache',
'opensumi_build_cache',
`${type}-${version}-${arch}`,
info.name + '-' + info.version,
);
Expand Down
Loading