cpp.js 1.0.0-beta.2 → 1.0.0-beta.21

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 (56) hide show
  1. package/README.md +104 -0
  2. package/package.json +25 -6
  3. package/src/{functions/buildJS.js → actions/buildJs.js} +7 -5
  4. package/src/actions/buildWasm.js +68 -0
  5. package/src/actions/createInterface.js +127 -0
  6. package/src/actions/createLib.js +103 -0
  7. package/src/actions/createXCFramework.js +40 -0
  8. package/src/actions/getAllBridges.js +8 -0
  9. package/src/actions/getCmakeParameters.js +50 -0
  10. package/src/actions/getData.js +28 -0
  11. package/src/actions/getDependLibs.js +19 -0
  12. package/src/{functions → actions}/run.js +77 -40
  13. package/src/assets/CMakeLists.txt +13 -9
  14. package/src/assets/ios.toolchain.cmake +4 -4
  15. package/src/bin.js +147 -72
  16. package/src/export/react-native.js +51 -0
  17. package/src/export/web.js +42 -0
  18. package/src/index.js +13 -63
  19. package/src/state/calculateDependencyParameters.js +64 -0
  20. package/src/state/index.js +81 -0
  21. package/src/state/loadConfig.js +96 -0
  22. package/src/utils/downloadAndExtractFile.js +36 -0
  23. package/src/utils/fixPackageName.js +7 -0
  24. package/src/utils/getAbsolutePath.js +14 -0
  25. package/src/utils/{findCMakeListsFile.js → getCMakeListsFilePath.js} +4 -3
  26. package/src/utils/getOsUserAndGroupId.js +2 -1
  27. package/src/utils/getParentPath.js +12 -0
  28. package/src/utils/hash.js +20 -0
  29. package/src/utils/loadJs.js +32 -0
  30. package/src/utils/loadJson.js +16 -0
  31. package/src/utils/writeJson.js +9 -0
  32. package/.mocharc.yaml +0 -3
  33. package/src/functions/createBridge.js +0 -37
  34. package/src/functions/createLib.js +0 -44
  35. package/src/functions/createWasm.js +0 -71
  36. package/src/functions/findOrCreateInterfaceFile.js +0 -68
  37. package/src/functions/finishBuild.js +0 -43
  38. package/src/functions/getCmakeParams.js +0 -106
  39. package/src/functions/getData.js +0 -36
  40. package/src/functions/getLibs.js +0 -33
  41. package/src/functions/specs/createBridge.spec.js +0 -51
  42. package/src/functions/specs/findOrCreateInterfaceFile.spec.js +0 -49
  43. package/src/utils/createTempDir.js +0 -15
  44. package/src/utils/getBaseInfo.js +0 -15
  45. package/src/utils/getCliPath.js +0 -11
  46. package/src/utils/getConfig.js +0 -170
  47. package/src/utils/getDirName.js +0 -7
  48. package/src/utils/getPathInfo.js +0 -10
  49. package/src/utils/specs/findCMakeListsFile.spec.js +0 -34
  50. package/src/utils/specs/utils.spec.js +0 -81
  51. package/test/data/sample.cpp +0 -1
  52. package/test/data/sample.h +0 -10
  53. package/test/data/sample.i +0 -12
  54. package/test/data/sample2.cpp +0 -0
  55. package/test/data/sample2.ei +0 -12
  56. package/test/data/sample2.h +0 -10
@@ -0,0 +1,42 @@
1
+ import getData from '../actions/getData.js';
2
+ import loadJson from '../utils/loadJson.js';
3
+
4
+ const env = JSON.stringify(getData('env'));
5
+
6
+ const params = `{
7
+ ...config,
8
+ env: {...${env}, ...config.env},
9
+ paths: {
10
+ wasm: 'cpp.wasm',
11
+ data: 'cpp.data.txt'
12
+ }
13
+ }`;
14
+
15
+ export default function exportWeb(bridgePath = null) {
16
+ const bridgeExportFile = `${bridgePath}.exports.json`;
17
+ let symbols = null;
18
+ if (bridgePath) {
19
+ symbols = loadJson(bridgeExportFile);
20
+ }
21
+
22
+ let symbolExportDefineString = '';
23
+ let symbolExportAssignString = '';
24
+ if (symbols && Array.isArray(symbols)) {
25
+ symbolExportDefineString = symbols.map((s) => `export let ${s} = null;`).join('\n');
26
+ symbolExportAssignString = symbols.map((s) => `${s} = m.${s};`).join('\n');
27
+ }
28
+
29
+ return `
30
+ export let AllSymbols = {};
31
+ ${symbolExportDefineString}
32
+ export function initCppJs(config = {}) {
33
+ return new Promise(
34
+ (resolve, reject) => import('/cpp.js').then(n => { return window.CppJs.initCppJs(${params})}).then(m => {
35
+ AllSymbols = m;
36
+ ${symbolExportAssignString}
37
+ resolve(m);
38
+ })
39
+ );
40
+ }
41
+ `;
42
+ }
package/src/index.js CHANGED
@@ -1,63 +1,13 @@
1
- import createBridge from './functions/createBridge.js';
2
- import findOrCreateInterfaceFile from './functions/findOrCreateInterfaceFile.js';
3
- import run from './functions/run.js';
4
- import finishBuild from './functions/finishBuild.js';
5
- import getCmakeParams from './functions/getCmakeParams.js';
6
- import getData from './functions/getData.js';
7
- import createWasm from './functions/createWasm.js';
8
- import createLib from './functions/createLib.js';
9
- import getConfig from './utils/getConfig.js';
10
-
11
- const platforms = ['Emscripten-x86_64', 'Android-arm64-v8a', 'iOS-iphoneos', 'iOS-iphonesimulator'];
12
- export default class CppjsCompiler {
13
- constructor(platform) {
14
- this.config = getConfig();
15
- this.interfaces = [];
16
- this.platform = platform;
17
- }
18
-
19
- findOrCreateInterfaceFile(path) {
20
- return findOrCreateInterfaceFile(this, path);
21
- }
22
-
23
- createBridge() {
24
- return createBridge(this);
25
- }
26
-
27
- createWasm(options) {
28
- return createWasm(this, options);
29
- }
30
-
31
- createLib() {
32
- return createLib(this);
33
- }
34
-
35
- getCmakeParams() {
36
- return getCmakeParams(this.config, null, true, true);
37
- }
38
-
39
- getData(field, prefixPath, subPlatform) {
40
- return getData(this.config, field, prefixPath, this.platform, subPlatform);
41
- }
42
-
43
- run(program, params, dockerOptions) {
44
- run(this, program, params, dockerOptions);
45
- }
46
-
47
- finishBuild() {
48
- finishBuild(this);
49
- }
50
-
51
- // eslint-disable-next-line class-methods-use-this
52
- getAllPlatforms() {
53
- return platforms;
54
- }
55
- }
56
-
57
- export function initCppJs() {
58
- return new Promise((resolve) => {
59
- resolve();
60
- });
61
- }
62
-
63
- export const Native = {};
1
+ // eslint-disable-next-line import/prefer-default-export
2
+ export { default as state } from './state/index.js';
3
+ export { default as getParentPath } from './utils/getParentPath.js';
4
+ export { default as createLib } from './actions/createLib.js';
5
+ export { default as createBridgeFile } from './actions/createInterface.js';
6
+ export { default as buildWasm } from './actions/buildWasm.js';
7
+ export { default as getData } from './actions/getData.js';
8
+ export { default as getCmakeParameters } from './actions/getCmakeParameters.js';
9
+ export { default as createXCFramework } from './actions/createXCFramework.js';
10
+ export { default as getAllBridges } from './actions/getAllBridges.js';
11
+ export { default as run } from './actions/run.js';
12
+ export { default as exportWeb } from './export/web.js';
13
+ export { default as exportReactNative } from './export/react-native.js';
@@ -0,0 +1,64 @@
1
+ export default function calculateDependencyParameters(config) {
2
+ const sourceFilter = (d) => d === config || d.export.type === 'source';
3
+ let headerPathWithDepends = [];
4
+ setPath(headerPathWithDepends, config, 'header', sourceFilter);
5
+ headerPathWithDepends = [...new Set(headerPathWithDepends)].join(';');
6
+
7
+ const headerGlob = [];
8
+ headerPathWithDepends.split(';').forEach((h) => {
9
+ config.ext.header.forEach((ext) => {
10
+ headerGlob.push(`${h}/*.${ext}`);
11
+ });
12
+ });
13
+
14
+ let nativePathWithDepends = [];
15
+ setPath(nativePathWithDepends, config, 'native', sourceFilter);
16
+ nativePathWithDepends = [...new Set(nativePathWithDepends)].join(';');
17
+
18
+ const nativeGlob = [];
19
+ nativePathWithDepends.split(';').forEach((h) => {
20
+ config.ext.source.forEach((ext) => {
21
+ nativeGlob.push(`${h}/*.${ext}`);
22
+ });
23
+ });
24
+
25
+ const cmakeFilter = (d) => d !== config && d.export.type === 'cmake' && d.paths.cmake !== config.paths.cliCMakeListsTxt;
26
+ let cmakeDepends = [];
27
+ setPath(cmakeDepends, config, 'this', cmakeFilter);
28
+ cmakeDepends = [...new Set(cmakeDepends)];
29
+
30
+ const pathsOfCmakeDepends = [...new Set(cmakeDepends
31
+ .map((d) => getParentPath(d.paths.cmake)))].join(';');
32
+ const nameOfCmakeDepends = [...new Set(cmakeDepends.map((d) => d.general.name))].join(';');
33
+
34
+ return {
35
+ nativeGlob,
36
+ headerGlob,
37
+ headerPathWithDepends,
38
+ cmakeDepends,
39
+ pathsOfCmakeDepends,
40
+ nameOfCmakeDepends,
41
+ };
42
+ }
43
+
44
+ function setPath(arr, dependency, type, filter = () => {}) {
45
+ if (filter(dependency)) {
46
+ if (type === 'this') {
47
+ arr.push(dependency);
48
+ } else if (Array.isArray(dependency.paths[type])) {
49
+ arr.push(...dependency.paths[type]);
50
+ } else {
51
+ arr.push(dependency.paths[type]);
52
+ }
53
+ }
54
+
55
+ dependency.dependencies.forEach((dep) => {
56
+ setPath(arr, dep, type, filter);
57
+ });
58
+ }
59
+
60
+ function getParentPath(path) {
61
+ const pathArray = path.split('/');
62
+ pathArray.pop();
63
+ return pathArray.join('/');
64
+ }
@@ -0,0 +1,81 @@
1
+ import loadJson from '../utils/loadJson.js';
2
+ import writeJson from '../utils/writeJson.js';
3
+ import loadConfig from './loadConfig.js';
4
+
5
+ const cacheDir = `${process.cwd()}/.cppjs`;
6
+
7
+ const state = {
8
+ platforms: {
9
+ All: ['Emscripten-x86_64', 'Android-arm64-v8a', 'iOS-iphoneos', 'iOS-iphonesimulator'],
10
+ WebAssembly: ['Emscripten-x86_64'],
11
+ Android: ['Android-arm64-v8a'],
12
+ iOS: ['iOS-iphoneos', 'iOS-iphonesimulator'],
13
+ },
14
+ config: null,
15
+ cache: {
16
+ hashes: {},
17
+ interfaces: {},
18
+ bridges: {},
19
+ },
20
+ };
21
+
22
+ if (typeof module !== 'undefined' && module.exports) {
23
+ initProcessState();
24
+ } else {
25
+ await initProcessState();
26
+ }
27
+
28
+ await initProcessState();
29
+
30
+ async function initProcessState() {
31
+ state.cache = loadCacheState();
32
+ state.config = await loadConfig();
33
+ setAllDependecyPaths();
34
+ if (state.config.build?.setState) {
35
+ state.config.build.setState(state);
36
+ }
37
+ }
38
+
39
+ function loadCacheState() {
40
+ const stateFilePath = `${cacheDir}/cache.json`;
41
+ return loadJson(stateFilePath) || state.cache;
42
+ }
43
+
44
+ function setAllDependecyPaths() {
45
+ state.config.allDependencyPaths = {};
46
+ state.platforms.All.forEach((platform) => {
47
+ const basePlatform = platform.split('-', 1)[0];
48
+ state.config.allDependencyPaths[platform] = {};
49
+ state.config.allDependencies.forEach((d) => {
50
+ d.export.libName.forEach((name) => {
51
+ state.config.allDependencyPaths[platform][name] = {
52
+ root: `${d.paths.output}/prebuilt/${platform}`,
53
+ };
54
+ const dep = state.config.allDependencyPaths[platform][name];
55
+ if (basePlatform === 'iOS') {
56
+ let xcRoot;
57
+ if (platform === 'iOS-iphoneos') {
58
+ xcRoot = `${d.paths.output}/prebuilt/${name}.xcframework/ios-arm64_arm64e`;
59
+ } else if (platform === 'iOS-iphonesimulator') {
60
+ xcRoot = `${d.paths.output}/prebuilt/${name}.xcframework/ios-arm64_arm64e_x86_64-simulator`;
61
+ }
62
+ dep.header = `${xcRoot}/Headers`;
63
+ dep.libPath = xcRoot;
64
+ dep.lib = `${dep.libPath}/lib${name}.a`;
65
+ dep.bin = `${dep.root}/bin`;
66
+ } else {
67
+ dep.header = `${dep.root}/include`;
68
+ dep.libPath = `${dep.root}/lib`;
69
+ dep.lib = `${dep.libPath}/lib${name}.${basePlatform === 'Android' ? 'so' : 'a'}`;
70
+ dep.bin = `${dep.root}/bin`;
71
+ }
72
+ });
73
+ });
74
+ });
75
+ }
76
+
77
+ export function saveCache() {
78
+ writeJson(`${cacheDir}/cache.json`, state.cache);
79
+ }
80
+
81
+ export default state;
@@ -0,0 +1,96 @@
1
+ import os from 'node:os';
2
+ import loadJs from '../utils/loadJs.js';
3
+ import loadJson from '../utils/loadJson.js';
4
+ import getParentPath from '../utils/getParentPath.js';
5
+ import getAbsolutePath from '../utils/getAbsolutePath.js';
6
+ import fixPackageName from '../utils/fixPackageName.js';
7
+ import getCMakeListsFilePath from '../utils/getCMakeListsFilePath.js';
8
+ import calculateDependencyParameters from './calculateDependencyParameters.js';
9
+ // import getCmakeParameters from './getCmakeParameters.js';
10
+
11
+ export default async function loadConfig(configDir = process.cwd(), configName = 'cppjs.config') {
12
+ const config = await loadJs(configDir, configName) || {};
13
+ const output = getFilledConfig(config);
14
+ output.build = await loadJs(configDir, 'cppjs.build');
15
+
16
+ output.paths.systemConfig = `${os.homedir()}/.cppjs.json`;
17
+ output.system = loadJson(output.paths.systemConfig) || {};
18
+
19
+ return output;
20
+ }
21
+
22
+ function getFilledConfig(config, options = { isDepend: false }) {
23
+ const newConfig = {
24
+ general: config.general || {},
25
+ dependencies: (config.dependencies || []).map((d) => getFilledConfig(d, { isDepend: true })),
26
+ paths: config.paths || {},
27
+ ext: config.ext || {},
28
+ export: config.export || {},
29
+ platform: config.platform || {},
30
+ package: null,
31
+ };
32
+
33
+ if (newConfig.paths.config && !newConfig.paths.project) {
34
+ newConfig.paths.project = getParentPath(newConfig.paths.config);
35
+ }
36
+
37
+ if (!newConfig.paths.project) {
38
+ newConfig.paths.project = process.cwd();
39
+ } else {
40
+ newConfig.paths.project = getAbsolutePath(null, newConfig.paths.project);
41
+ }
42
+
43
+ newConfig.package = loadJson(`${newConfig.paths.project}/package.json`);
44
+
45
+ if (!newConfig?.general?.name) {
46
+ newConfig.general.name = fixPackageName(newConfig.package.name) || 'cppjssample';
47
+ }
48
+
49
+ const getPath = getAbsolutePath.bind(null, newConfig.paths.project);
50
+
51
+ newConfig.paths.base = getPath(newConfig.paths.base) || newConfig.paths.project;
52
+ newConfig.paths.cache = getPath(newConfig.paths.cache) || getPath('.cppjs');
53
+ newConfig.paths.build = getPath(newConfig.paths.build) || getPath(`${newConfig.paths.cache}/build`);
54
+ newConfig.paths.native = (newConfig.paths.native || ['src/native']).map((p) => getPath(p));
55
+ newConfig.paths.module = (newConfig.paths.module || newConfig.paths.native).map((p) => getPath(p));
56
+ newConfig.paths.header = (newConfig.paths.header || newConfig.paths.native).map((p) => getPath(p));
57
+ newConfig.paths.bridge = (newConfig.paths.bridge || [...newConfig.paths.native, newConfig.paths.build])
58
+ .map((p) => getPath(p));
59
+ newConfig.paths.output = getPath(newConfig.paths.output) || newConfig.paths.build;
60
+ newConfig.paths.cmake = options.isDepend ? getCMakeListsFilePath(newConfig.paths.output) : (
61
+ getPath(newConfig.paths.cmake || getCMakeListsFilePath(newConfig.paths.project))
62
+ );
63
+ newConfig.paths.cmakeDir = getParentPath(newConfig.paths.cmake);
64
+ newConfig.paths.cli = getParentPath(getParentPath(import.meta.url));
65
+ newConfig.paths.cliCMakeListsTxt = `${newConfig.paths.cli}/assets/CMakeLists.txt`;
66
+
67
+ newConfig.ext.header = newConfig.ext.header || ['h', 'hpp', 'hxx', 'hh'];
68
+ newConfig.ext.source = newConfig.ext.source || ['c', 'cpp', 'cxx', 'cc'];
69
+ newConfig.ext.module = newConfig.ext.module || ['i'];
70
+
71
+ newConfig.export.type = newConfig.export.type || 'cmake';
72
+ newConfig.export.header = newConfig.export.header || 'include';
73
+ newConfig.export.libPath = getPath(newConfig.export.libPath || 'lib');
74
+ newConfig.export.libName = newConfig.export.libName || [newConfig.general.name];
75
+ newConfig.export.binHeaders = newConfig.export.binHeaders || [];
76
+
77
+ newConfig.platform['Emscripten-x86_64'] = newConfig.platform['Emscripten-x86_64'] || {};
78
+ newConfig.platform['Emscripten-x86_64-browser'] = newConfig.platform['Emscripten-x86_64-browser'] || {};
79
+ newConfig.platform['Emscripten-x86_64-node'] = newConfig.platform['Emscripten-x86_64-node'] || {};
80
+ newConfig.platform['Android-arm64-v8a'] = newConfig.platform['Android-arm64-v8a'] || {};
81
+ newConfig.platform['iOS-iphoneos'] = newConfig.platform['iOS-iphoneos'] || {};
82
+ newConfig.platform['iOS-iphonesimulator'] = newConfig.platform['iOS-iphonesimulator'] || {};
83
+
84
+ newConfig.allDependencies = (() => {
85
+ const output = {};
86
+ [...newConfig.dependencies, ...newConfig.dependencies.map((d) => d.allDependencies).flat()].forEach((d) => {
87
+ output[d.paths.project] = d;
88
+ });
89
+ return Object.values(output);
90
+ })();
91
+
92
+ newConfig.dependencyParameters = calculateDependencyParameters(newConfig);
93
+ // newConfig.cmakeParameters = getCmakeParameters(newConfig);
94
+
95
+ return newConfig;
96
+ }
@@ -0,0 +1,36 @@
1
+ import path from 'node:path';
2
+ import fs, { mkdirSync } from 'node:fs';
3
+ import fr from 'follow-redirects';
4
+ import decompress from 'decompress';
5
+
6
+ export default async function downloadAndExtractFile(url, output) {
7
+ if (fs.existsSync(`${output}/source`)) {
8
+ return false;
9
+ }
10
+ const filePath = await downloadFile(url, output);
11
+ const a = await decompress(filePath, output);
12
+ const oldFolder = a[0].path.split('/')[0];
13
+ fs.renameSync(`${output}/${oldFolder}`, `${output}/source`);
14
+ return true;
15
+ }
16
+
17
+ function downloadFile(url, folder) {
18
+ mkdirSync(folder, { recursive: true });
19
+ return new Promise((resolve) => {
20
+ const filename = path.basename(url);
21
+ if (fs.existsSync(`${folder}/${filename}`)) {
22
+ resolve(`${folder}/${filename}`);
23
+ return;
24
+ }
25
+
26
+ fr.https.get(url, (res) => {
27
+ const fileStream = fs.createWriteStream(`${folder}/${filename}`);
28
+ res.pipe(fileStream);
29
+
30
+ fileStream.on('finish', () => {
31
+ fileStream.close();
32
+ resolve(`${folder}/${filename}`);
33
+ });
34
+ });
35
+ });
36
+ }
@@ -0,0 +1,7 @@
1
+ export default function fixPackageName(name) {
2
+ if (!name) {
3
+ return null;
4
+ }
5
+
6
+ return name.replace(/[^a-zA-Z0-9-_]+/g, '');
7
+ }
@@ -0,0 +1,14 @@
1
+ import upath from 'upath';
2
+
3
+ export default function getAbsolutePath(projectPath, path) {
4
+ if (!path) {
5
+ return null;
6
+ }
7
+ if (upath.isAbsolute(path)) {
8
+ return path;
9
+ }
10
+ if (projectPath) {
11
+ return upath.resolve(upath.join(upath.resolve(projectPath), path));
12
+ }
13
+ return upath.resolve(path);
14
+ }
@@ -1,7 +1,7 @@
1
1
  import glob from 'glob';
2
- import getCliPath from './getCliPath.js';
2
+ import getParentPath from './getParentPath.js';
3
3
 
4
- export default function findCMakeListsFile(basePath = process.cwd()) {
4
+ export default function getCMakeListsFilePath(basePath = process.cwd()) {
5
5
  let temp = glob.sync('CMakeLists.txt', { absolute: true, cwd: basePath });
6
6
  if (temp.length === 0) {
7
7
  temp = glob.sync('*/CMakeLists.txt', { absolute: true, cwd: basePath, ignore: ['node_modules/*', 'dist/*', 'build/*'] });
@@ -12,5 +12,6 @@ export default function findCMakeListsFile(basePath = process.cwd()) {
12
12
  }
13
13
 
14
14
  export function getCliCMakeListsFile() {
15
- return `${getCliPath()}/assets/CMakeLists.txt`;
15
+ const cliPath = getParentPath(getParentPath(import.meta.url));
16
+ return `${cliPath}/assets/CMakeLists.txt`;
16
17
  }
@@ -4,7 +4,8 @@ let osUserAndGroupId;
4
4
  export default function getOsUserAndGroupId() {
5
5
  const userInfo = os.userInfo();
6
6
  if (!osUserAndGroupId) {
7
- osUserAndGroupId = `${userInfo.uid}:${userInfo.gid}`;
7
+ const isInvalid = userInfo.uid === -1 && userInfo.gid === -1;
8
+ osUserAndGroupId = isInvalid ? '0:0' : `${userInfo.uid}:${userInfo.gid}`;
8
9
  }
9
10
  return osUserAndGroupId;
10
11
  }
@@ -0,0 +1,12 @@
1
+ import * as url from 'node:url';
2
+ import upath from 'upath';
3
+
4
+ export default function getParentPath(importUrl) {
5
+ let input = importUrl;
6
+ try {
7
+ input = url.fileURLToPath(input);
8
+ } catch (e) { /* empty */ }
9
+ const filename = upath.normalize(input);
10
+ const temp = filename.split('/'); temp.pop();
11
+ return temp.join('/');
12
+ }
@@ -0,0 +1,20 @@
1
+ import fs from 'node:fs';
2
+ import { createHash } from 'node:crypto';
3
+
4
+ export function getFileHash(path) {
5
+ const data = fs.readFileSync(path);
6
+ return getContentHash(data);
7
+ /* return new Promise((resolve, reject) => {
8
+ const hash = createHash('sha256');
9
+ const rs = fs.createReadStream(path);
10
+ rs.on('error', reject);
11
+ rs.on('data', (chunk) => hash.update(chunk));
12
+ rs.on('end', () => resolve(hash.digest('hex')));
13
+ }); */
14
+ }
15
+
16
+ export function getContentHash(content) {
17
+ const hash = createHash('sha256');
18
+ hash.update(content);
19
+ return hash.digest('hex');
20
+ }
@@ -0,0 +1,32 @@
1
+ import fs from 'node:fs';
2
+
3
+ export default async function loadJs(path, fileName, fileExt = ['json', 'js', 'mjs', 'cjs', 'ts']) {
4
+ let filePath;
5
+ fileExt.some((e) => {
6
+ filePath = `${path}/${fileName}.${e}`;
7
+ if (!fs.existsSync(filePath)) {
8
+ filePath = null;
9
+ return false;
10
+ }
11
+ return true;
12
+ });
13
+
14
+ if (filePath) {
15
+ let file;
16
+ if (typeof module !== 'undefined' && module.exports) {
17
+ file = require(`file:///${filePath}`);
18
+ } else {
19
+ file = await import(`file:///${filePath}`);
20
+ }
21
+ if (file.default) file = file.default;
22
+
23
+ if (typeof file === 'function') {
24
+ return file();
25
+ }
26
+ if (typeof file === 'object') {
27
+ return file;
28
+ }
29
+ }
30
+
31
+ return null;
32
+ }
@@ -0,0 +1,16 @@
1
+ import fs from 'node:fs';
2
+
3
+ export default function loadJson(filePath) {
4
+ if (!fs.existsSync(filePath)) {
5
+ return null;
6
+ }
7
+
8
+ try {
9
+ const stateContent = fs.readFileSync(filePath, { encoding: 'utf8', flag: 'r' });
10
+ const state = JSON.parse(stateContent);
11
+ return state;
12
+ } catch (e) {
13
+ console.error(e);
14
+ return null;
15
+ }
16
+ }
@@ -0,0 +1,9 @@
1
+ import fs from 'node:fs';
2
+
3
+ export default function writeJson(filePath, data) {
4
+ try {
5
+ fs.writeFileSync(filePath, JSON.stringify(data, null, 4));
6
+ } catch (e) {
7
+ console.error(e);
8
+ }
9
+ }
package/.mocharc.yaml DELETED
@@ -1,3 +0,0 @@
1
- spec:
2
- - 'src/**/*.spec.js'
3
- - 'test/*.spec.js'
@@ -1,37 +0,0 @@
1
- import getBaseInfo from '../utils/getBaseInfo.js';
2
- import getPathInfo from '../utils/getPathInfo.js';
3
- import { getDependencyParams } from './getCmakeParams.js';
4
- import run from './run.js';
5
-
6
- const platform = 'Emscripten-x86_64';
7
-
8
- export default function createBridge(compiler) {
9
- const bridges = [];
10
-
11
- const allHeaders = getDependencyParams(compiler.config).headerPathWithDepends.split(';');
12
-
13
- let includePath = [
14
- ...compiler.config.getAllDependencies().map((d) => `${d.paths.output}/prebuilt/${platform}/include`),
15
- ...compiler.config.getAllDependencies().map((d) => `${d.paths.output}/prebuilt/${platform}/swig`),
16
- ...compiler.config.paths.header,
17
- ...allHeaders,
18
- ].filter((path) => !!path.toString()).map((path) => `-I/tmp/cppjs/live/${getPathInfo(path, compiler.config.paths.base).relative}`);
19
- includePath = [...new Set(includePath)];
20
-
21
- compiler.interfaces.forEach((filePath) => {
22
- const input = getPathInfo(filePath, compiler.config.paths.base);
23
- const output = getPathInfo(`${compiler.config.paths.temp}/bridge`, compiler.config.paths.base);
24
- const base = getBaseInfo(compiler.config.paths.base);
25
-
26
- run(compiler, 'swig', [
27
- '-c++',
28
- '-embind',
29
- '-o', `/tmp/cppjs/live/${output.relative}/${filePath.split('/').at(-1)}.cpp`,
30
- ...includePath,
31
- `/tmp/cppjs/live/${input.relative}`,
32
- ]);
33
-
34
- bridges.push(`${base.withSlash}${output.relative}/${filePath.split('/').at(-1)}.cpp`);
35
- });
36
- return bridges;
37
- }
@@ -1,44 +0,0 @@
1
- import fs from 'fs';
2
- import os from 'os';
3
- import getCmakeParams from './getCmakeParams.js';
4
- import getPathInfo from '../utils/getPathInfo.js';
5
-
6
- const cpuCount = os.cpus().length - 1;
7
-
8
- export default function createLib(compiler) {
9
- if (!compiler.platform) return;
10
-
11
- let platformParams = [];
12
- switch (compiler.platform) {
13
- case 'Emscripten-x86_64':
14
- platformParams = ['-DBUILD_TYPE=STATIC'];
15
- break;
16
- case 'Android-arm64-v8a':
17
- platformParams = ['-DBUILD_TYPE=SHARED'];
18
- break;
19
- case 'iOS-iphoneos':
20
- platformParams = ['-DBUILD_TYPE=STATIC'];
21
- break;
22
- case 'iOS-iphonesimulator':
23
- platformParams = ['-DBUILD_TYPE=STATIC'];
24
- break;
25
- default:
26
- }
27
-
28
- const libdir = `${getPathInfo(compiler.config.paths.output, compiler.config.paths.base).relative}/prebuilt/${compiler.platform}`;
29
- const basePlatform = compiler.platform.split('-', 1)[0];
30
- const params = getCmakeParams(compiler.config, '/tmp/cppjs/live/', true, false);
31
- compiler.run(null, [
32
- basePlatform === 'iOS' ? 'ios-cmake' : 'cmake', '/tmp/cppjs/cmake',
33
- '-DCMAKE_BUILD_TYPE=Release', ...platformParams,
34
- `-DCMAKE_INSTALL_PREFIX=/tmp/cppjs/live/${libdir}`, `-DPROJECT_NAME=${compiler.config.general.name}`,
35
- ...params,
36
- ]);
37
- if (basePlatform === 'iOS') {
38
- compiler.run(null, ['ios-cmake', '--build', '.', '--config', 'Release', '--target', 'install']);
39
- } else {
40
- compiler.run(null, ['make', `-j${cpuCount}`, 'install']);
41
- }
42
-
43
- fs.rmSync(compiler.config.paths.temp, { recursive: true, force: true });
44
- }