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

usage of javascript/node to allow cross-platform building #5825

Open
wants to merge 6 commits into
base: x
Choose a base branch
from
Open
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
90 changes: 90 additions & 0 deletions development/scripts/clean_workspace.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const fs = require('fs');
const path = require('path');

function removeDir(dirPath) {
if (fs.existsSync(dirPath)) {
console.log(`Removing directory: ${dirPath}`);
fs.rmSync(dirPath, { recursive: true, force: true });
}
}

function removeFile(filePath) {
if (fs.existsSync(filePath)) {
console.log(`Removing file: ${filePath}`);
fs.unlinkSync(filePath);
}
}

function cleanWorkspace() {
// Clean yarn cache
console.log('Cleaning yarn cache...');
require('child_process').execSync('yarn cache clean', { stdio: 'inherit' });

// Root folder cleanup
removeDir('node_modules');
removeDir('.expo');
removeDir('.husky/_');
removeDir('.app-mono-ts-cache');

// Desktop cleanup
removeDir('apps/desktop/node_modules');
removeDir('apps/desktop/.expo');
removeDir('apps/desktop/__generated__');
removeDir('apps/desktop/dist');
removeDir('apps/desktop/build');
removeDir('apps/desktop/build-electron');
removeDir('apps/desktop/public/static/js-sdk');
removeDir('apps/desktop/public/static/connect');
removeFile('apps/desktop/public/static/preload.js');

// Ext cleanup
removeDir('apps/ext/node_modules');
removeDir('apps/ext/.expo');
removeDir('apps/ext/build');
removeFile('apps/ext/src/entry/injected.js');
removeFile('apps/ext/src/entry/injected.text-js');

// Mobile cleanup
removeDir('apps/mobile/node_modules');
removeDir('apps/mobile/.expo');
removeDir('apps/mobile/__generated__');
removeDir('apps/mobile/ios/Pods');
removeDir('apps/mobile/ios/build');
removeDir('apps/mobile/ios/OneKeyWallet/web-embed');
removeDir('apps/mobile/ios/OneKeyWallet.xcworkspace/xcuserdata');
removeDir('apps/mobile/src/public/static/connect');
removeDir('apps/mobile/android/.gradle');
removeDir('apps/mobile/android/build');
removeDir('apps/mobile/android/app/build');
removeDir('apps/mobile/android/lib-keys-secret/build');
removeDir('apps/mobile/android/lib-keys-secret/.cxx');
removeDir('apps/mobile/android/app/src/main/assets/web-embed');

// Web cleanup
removeDir('apps/web/node_modules');
removeDir('apps/web/.expo');
removeDir('apps/web/__generated__');
removeDir('apps/web/dist');
removeDir('apps/web/web-build');
removeDir('apps/web/.expo-shared');

// Web-embed cleanup
removeDir('apps/web-embed/node_modules');
removeDir('apps/web-embed/.expo');
removeDir('apps/web-embed/__generated__');
removeDir('apps/web-embed/dist');
removeDir('apps/web-embed/web-build');
removeDir('apps/web-embed/.expo-shared');

// Package cleanup
removeDir('packages/components/node_modules');
removeDir('packages/core/node_modules');
removeDir('packages/kit/node_modules');
removeFile('packages/kit/src/components/WebView/injectedNative.text-js');
removeDir('packages/kit-bg/node_modules');
removeDir('packages/shared/node_modules');
removeFile('packages/shared/src/web/index.html');
}

cleanWorkspace();
console.log("Workspace cleaned.");
81 changes: 0 additions & 81 deletions development/scripts/clean_workspace.sh

This file was deleted.

25 changes: 0 additions & 25 deletions development/scripts/copy-injected.sh

This file was deleted.

55 changes: 55 additions & 0 deletions development/scripts/copy_injected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');

function copyFile(src, dest) {
if (fs.existsSync(src)) {
console.log(`Copying ${src} to ${dest}`);
fs.copyFileSync(src, dest);
} else {
console.log(`Source file ${src} does not exist.`);
}
}

function copyInjected() {
// Copy to Desktop preload.js
copyFile('node_modules/@onekeyfe/cross-inpage-provider-injected/dist/injected/injectedDesktop.js',
'apps/desktop/public/static/preload.js');

// Copy to Extension injected.js
copyFile('node_modules/@onekeyfe/cross-inpage-provider-injected/dist/injected/injectedExtension.js',
'apps/ext/src/entry/injected.js');
copyFile('apps/ext/src/entry/injected.js', 'apps/ext/src/entry/injected.text-js');

// Copy to Native injectedCode
copyFile('node_modules/@onekeyfe/cross-inpage-provider-injected/dist/injected/injectedNative.js',
'packages/kit/src/components/WebView/injectedNative.text-js');

// Copy index html
copyFile('packages/shared/src/web/index.html.ejs', 'packages/shared/src/web/index.html');

// Create directory for js-sdk if it doesn't exist
const jsSdkDir = 'apps/desktop/public/static/js-sdk/';
if (!fs.existsSync(jsSdkDir)) {
console.log(`Creating directory: ${jsSdkDir}`);
fs.mkdirSync(jsSdkDir, { recursive: true });
}

// Copy hardware js-sdk iframe files to desktop
const srcJsSdk = 'node_modules/@onekeyfe/hd-web-sdk/build/';
const destJsSdk = 'apps/desktop/public/static/js-sdk/';
if (fs.existsSync(srcJsSdk)) {
console.log(`Copying contents of ${srcJsSdk} to ${destJsSdk}`);
execSync(`cp -r ${srcJsSdk}* ${destJsSdk}`, { stdio: 'inherit' });
} else {
console.log(`Source directory ${srcJsSdk} does not exist.`);
}

// Build and copy web-embed
const baseDir = path.dirname(__filename);
console.log(`Running web-embed.js in ${baseDir}`);
execSync(`node ${path.join(baseDir, 'web-embed.js')}`, { stdio: 'inherit' });
}

copyInjected();
console.log("Injected files copied.");
26 changes: 26 additions & 0 deletions development/scripts/postinstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fs = require('fs');
const { execSync } = require('child_process');

function postinstall() {
// Run yarn setup:env
console.log('Running yarn setup:env...');
execSync('yarn setup:env', { stdio: 'inherit' });

// Run patch-package
console.log('Running patch-package...');
execSync('patch-package', { stdio: 'inherit' });

// Run yarn copy:inject
console.log('Running yarn copy:inject...');
execSync('yarn copy:inject', { stdio: 'inherit' });

// Remove realm-flipper-plugin-device src directory
const realmDir = 'node_modules/realm-flipper-plugin-device/src';
if (fs.existsSync(realmDir)) {
console.log(`Removing directory: ${realmDir}`);
fs.rmSync(realmDir, { recursive: true, force: true });
}
}

postinstall();
console.log('Post-installation steps completed.');
6 changes: 0 additions & 6 deletions development/scripts/postinstall.sh

This file was deleted.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
"node": ">=20"
},
"scripts": {
"setup:env": "bash -c 'if [ ! -f .env ]; then cp .env.example .env; fi'",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to write it like this?

"setup:env": "node -e \"const fs=require('fs');if(!fs.existsSync('.env'))fs.copyFileSync('.env.example','.env');\"",

"postinstall": "bash development/scripts/postinstall.sh",
"copy:inject": "bash development/scripts/copy-injected.sh",
"setup:env": "cross-env-shell \"[ ! -f .env ] && cp .env.example .env || exit 0\"",
"clean": "node development/scripts/clean_workspace.js",
"postinstall": "node development/scripts/postinstall.js",
"copy:inject": "node development/scripts/copy_injected.js",
"app:desktop": "yarn workspace @onekeyhq/desktop start",
"app:ext": "yarn workspace @onekeyhq/ext start:v3",
"app:ext:proxy": "yarn workspace @onekeyhq/ext start:v3:proxy",
Expand All @@ -34,7 +35,6 @@
"app:android:device": "yarn workspace @onekeyhq/mobile android:device",
"icon:build": "yarn workspace @onekeyhq/components icon:build",
"fetch:locale": "yarn workspace @onekeyhq/shared fetch:locale",
"clean": "yarn workspaces foreach -A -p run clean && ./development/scripts/clean_workspace.sh",
"clean:cache": "yarn workspaces foreach -A run clean:build",
"_tsc": "node ./development/lint/ts",
"reinstall": "yarn clean && yarn install",
Expand Down Expand Up @@ -88,6 +88,7 @@
"bignumber.js": "9.1.2",
"browserify-zlib": "^0.2.0",
"check-disk-space": "^3.4.0",
"cross-env-shell": "^7.0.3",
"crypto-browserify": "^3.12.0",
"ejs-loader": "^0.5.0",
"electron-root-path": "^1.1.0",
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6500,6 +6500,7 @@ __metadata:
cheerio: "npm:^1.0.0-rc.12"
copy-webpack-plugin: "npm:^6.0.2"
cross-env: "npm:^7.0.3"
cross-env-shell: "npm:^7.0.3"
crypto-browserify: "npm:^3.12.0"
css-loader: "npm:^6.8.1"
date-fns: "npm:^2.30.0"
Expand Down Expand Up @@ -18146,6 +18147,15 @@ __metadata:
languageName: node
linkType: hard

"cross-env-shell@npm:^7.0.3":
version: 7.0.3
resolution: "cross-env-shell@npm:7.0.3"
bin:
cross-env-shell: wrapper.sh
checksum: 10/3989ea13b255eb6474004c51ccde9c9f2ad19e71f21e80be2ce00ab54917329bf28e11f82be5ef50e2dd5268d2184b6bb21367f0a82adbdee220ab6246bef321
languageName: node
linkType: hard

"cross-env@npm:^7.0.3":
version: 7.0.3
resolution: "cross-env@npm:7.0.3"
Expand Down