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

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 (54) hide show
  1. package/README.md +101 -0
  2. package/package.json +22 -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 +40 -0
  7. package/src/actions/createXCFramework.js +40 -0
  8. package/src/actions/getAllBridges.js +8 -0
  9. package/src/actions/getCmakeParameters.js +48 -0
  10. package/src/actions/getData.js +28 -0
  11. package/src/actions/getDependLibs.js +19 -0
  12. package/src/{functions → actions}/run.js +52 -30
  13. package/src/assets/CMakeLists.txt +13 -9
  14. package/src/assets/ios.toolchain.cmake +4 -4
  15. package/src/bin.js +42 -73
  16. package/src/index.js +10 -63
  17. package/src/state/calculateDependencyParameters.js +64 -0
  18. package/src/state/index.js +44 -0
  19. package/src/state/loadConfig.js +89 -0
  20. package/src/utils/fixPackageName.js +7 -0
  21. package/src/utils/getAbsolutePath.js +14 -0
  22. package/src/utils/{findCMakeListsFile.js → getCMakeListsFilePath.js} +4 -3
  23. package/src/utils/getOsUserAndGroupId.js +2 -1
  24. package/src/utils/getParentPath.js +12 -0
  25. package/src/utils/hash.js +20 -0
  26. package/src/utils/loadJs.js +32 -0
  27. package/src/utils/loadJson.js +16 -0
  28. package/src/utils/pullDockerImage.js +1 -1
  29. package/src/utils/writeJson.js +9 -0
  30. package/.mocharc.yaml +0 -3
  31. package/src/functions/createBridge.js +0 -37
  32. package/src/functions/createLib.js +0 -44
  33. package/src/functions/createWasm.js +0 -71
  34. package/src/functions/findOrCreateInterfaceFile.js +0 -68
  35. package/src/functions/finishBuild.js +0 -43
  36. package/src/functions/getCmakeParams.js +0 -106
  37. package/src/functions/getData.js +0 -36
  38. package/src/functions/getLibs.js +0 -33
  39. package/src/functions/specs/createBridge.spec.js +0 -51
  40. package/src/functions/specs/findOrCreateInterfaceFile.spec.js +0 -49
  41. package/src/utils/createTempDir.js +0 -15
  42. package/src/utils/getBaseInfo.js +0 -15
  43. package/src/utils/getCliPath.js +0 -11
  44. package/src/utils/getConfig.js +0 -170
  45. package/src/utils/getDirName.js +0 -7
  46. package/src/utils/getPathInfo.js +0 -10
  47. package/src/utils/specs/findCMakeListsFile.spec.js +0 -34
  48. package/src/utils/specs/utils.spec.js +0 -81
  49. package/test/data/sample.cpp +0 -1
  50. package/test/data/sample.h +0 -10
  51. package/test/data/sample.i +0 -12
  52. package/test/data/sample2.cpp +0 -0
  53. package/test/data/sample2.ei +0 -12
  54. package/test/data/sample2.h +0 -10
@@ -1,49 +0,0 @@
1
- import { assert } from 'chai';
2
- import fs from 'fs';
3
- import p, { dirname } from 'path';
4
- import * as url from 'node:url';
5
- import { tmpdir } from 'os';
6
- import findOrCreateInterfaceFile from './findOrCreateInterfaceFile.js';
7
-
8
- const __filename = url.fileURLToPath(import.meta.url);
9
- const temp = __filename.split('/');
10
- temp.pop();
11
- temp.pop();
12
- const __dirname = temp.join('/');
13
-
14
- export function createTempDir(folder) {
15
- let path = p.join(tmpdir(), 'cppjs-app-cli-test');
16
- if (folder) path = p.join(path, folder);
17
-
18
- if (fs.existsSync(path)) fs.rmSync(path, { recursive: true, force: true });
19
- fs.mkdirSync(path, { recursive: true });
20
-
21
- return path;
22
- }
23
-
24
- describe('findOrCreateInterfaceFile', () => {
25
- let tempdir;
26
- before(async () => {
27
- tempdir = createTempDir();
28
- });
29
-
30
- it('find interface file', async () => {
31
- const path = `${__dirname}/test/data/sample.h`;
32
- const interfaceFile = findOrCreateInterfaceFile(path, tempdir);
33
- assert.equal(interfaceFile, path.replace('.h', '.i'));
34
- });
35
-
36
- it('create interface file', async () => {
37
- const path = `${__dirname}/test/data/sample2.h`;
38
- const interfaceFile = findOrCreateInterfaceFile(path, tempdir);
39
- const interfaceFileData = fs.readFileSync(interfaceFile, 'utf8');
40
-
41
- const path2 = `${__dirname}/test/data/sample2.ei`;
42
- const interfaceFileData2 = fs.readFileSync(path2, 'utf8');
43
-
44
- assert.equal(
45
- interfaceFileData.trim().replace(`${__dirname}/test/data/`, ''),
46
- interfaceFileData2.trim().replace(`${__dirname}/test/data/`, ''),
47
- );
48
- });
49
- });
@@ -1,15 +0,0 @@
1
- import fs from 'fs';
2
- import p from 'path';
3
-
4
- export default function createTempDir(folder = `a${Math.random()}`, base = process.cwd()) {
5
- const path = p.join(base, '.cppjs');
6
- return createDir(folder, path);
7
- }
8
-
9
- export function createDir(folder, base = process.cwd()) {
10
- const path = p.join(base, folder);
11
-
12
- if (fs.existsSync(path)) fs.rmSync(path, { recursive: true, force: true });
13
- fs.mkdirSync(path, { recursive: true });
14
- return path;
15
- }
@@ -1,15 +0,0 @@
1
- export default function getBaseInfo(base) {
2
- let basePath = base;
3
-
4
- const output = {
5
- withSlash: '/',
6
- withoutSlash: '/',
7
- };
8
- if (basePath && basePath !== '/') {
9
- if (basePath.at(-1) !== '/') basePath += '/';
10
-
11
- output.withSlash = basePath;
12
- output.withoutSlash = basePath.substring(0, basePath.length - 1);
13
- }
14
- return output;
15
- }
@@ -1,11 +0,0 @@
1
- import * as url from 'node:url';
2
-
3
- const filename = url.fileURLToPath(import.meta.url);
4
- const temp = filename.split('/');
5
- temp.pop();
6
- temp.pop();
7
- const dirname = temp.join('/');
8
-
9
- export default function getCliPath() {
10
- return dirname;
11
- }
@@ -1,170 +0,0 @@
1
- import fs from 'fs';
2
- import nodePath from 'path';
3
- import * as url from 'node:url';
4
- import createTempDir, { createDir } from './createTempDir.js';
5
- import findCMakeListsFile from './findCMakeListsFile.js';
6
-
7
- const filename = url.fileURLToPath(import.meta.url);
8
- const temp = filename.split('/'); temp.pop(); temp.pop();
9
- const dirname = temp.join('/');
10
-
11
- /**
12
- * @typedef {Object} Config
13
- * @property {string} general General
14
- * @property {any[]} dependencies Dependencies
15
- * @property {ConfigPaths} paths Paths
16
- * @property {ConfigExtensions} ext Extensions
17
- */
18
-
19
- /**
20
- * @typedef {Object} ConfigPaths
21
- * @property {string} project Project path.
22
- * @property {string} base Base path (Use for monorepo structure)
23
- * @property {string} temp Temp path.
24
- * @property {string} native Native path (default: ['src/native']).
25
- * @property {string} module Module path (default: native path)
26
- * @property {string} header Header path (default: native path)
27
- * @property {string} bridge Bridge path (default: native and temp path)
28
- * @property {string} output Output path (default: 'dist')
29
- * @property {string} cmake CmakeLists.txt path
30
- */
31
-
32
- /**
33
- * @typedef {Object} ConfigExtensions
34
- * @property {string} header Header extensions (default: ['h', 'hpp', 'hxx', 'hh'])
35
- * @property {string} source Source extensions (default: ['c', 'cpp', 'cxx', 'cc'])
36
- * @property {string} module Module extensions (default: ['i'])
37
- */
38
-
39
- /**
40
- * @typedef {Object} ConfigGeneral
41
- * @property {string} name Project name
42
- */
43
-
44
- let tempConfigDefault = {
45
- general: {}, dependencies: [], paths: {}, ext: {}, export: {}, platform: {},
46
- };
47
- await initDefaultConfigFile();
48
-
49
- async function initDefaultConfigFile() {
50
- let filePath;
51
- ['json', 'js', 'mjs', 'cjs', 'ts'].some((e) => {
52
- filePath = `${process.cwd()}/cppjs.config.${e}`;
53
- if (!fs.existsSync(filePath)) {
54
- filePath = null;
55
- return false;
56
- }
57
- return true;
58
- });
59
-
60
- if (filePath) {
61
- let file = await import(filePath);
62
- if (file.default) file = file.default;
63
-
64
- if (typeof file === 'function') tempConfigDefault = file();
65
- else if (typeof file === 'object') tempConfigDefault = file;
66
- }
67
- }
68
-
69
- export default function getConfig() {
70
- return fillConfig(forceToConfigSchema(tempConfigDefault));
71
- }
72
-
73
- function forceToConfigSchema(tempConfig) {
74
- const config = {
75
- general: tempConfig && tempConfig.general ? tempConfig.general : {},
76
- dependencies: tempConfig && tempConfig.dependencies ? tempConfig.dependencies : [],
77
- paths: tempConfig && tempConfig.paths ? tempConfig.paths : {},
78
- ext: tempConfig && tempConfig.ext ? tempConfig.ext : {},
79
- export: tempConfig && tempConfig.export ? tempConfig.export : {},
80
- platform: tempConfig && tempConfig.platform ? tempConfig.platform : {},
81
- };
82
- return config;
83
- }
84
-
85
- function getAbsolutePath(projectPath, path) {
86
- if (!path) {
87
- return null;
88
- }
89
- if (nodePath.isAbsolute(path)) {
90
- return path;
91
- }
92
- if (projectPath) {
93
- return nodePath.resolve(nodePath.join(nodePath.resolve(projectPath), path));
94
- }
95
- return nodePath.resolve(path);
96
- }
97
-
98
- function fillConfig(tempConfig, options = {}) {
99
- const config = {
100
- general: {},
101
- dependencies: (tempConfig.dependencies || []).map((d) => fillConfig(forceToConfigSchema(d), { depend: true })),
102
- paths: {
103
- project: getAbsolutePath(null, tempConfig.paths.project) || process.cwd(),
104
- },
105
- ext: {},
106
- export: {},
107
- platform: {},
108
- package: {},
109
- };
110
-
111
- const packageJsonPath = `${config.paths.project}/package.json`;
112
- if (fs.existsSync(packageJsonPath)) {
113
- const file = JSON.parse(fs.readFileSync(packageJsonPath));
114
- if (file && typeof file === 'object' && file.name) {
115
- config.package = file;
116
- }
117
- }
118
-
119
- if (tempConfig?.general?.name) {
120
- config.general.name = tempConfig.general.name;
121
- } else {
122
- config.general.name = config.package.name || 'cppjssample';
123
- }
124
-
125
- const getPath = getAbsolutePath.bind(null, config.paths.project);
126
-
127
- config.paths.base = getPath(tempConfig.paths.base) || config.paths.project;
128
- config.paths.temp = getPath(tempConfig.paths.temp) || createTempDir(undefined, config.paths.project);
129
- config.paths.native = (tempConfig.paths.native || ['src/native']).map((p) => getPath(p));
130
- config.paths.module = (tempConfig.paths.module || config.paths.native).map((p) => getPath(p));
131
- config.paths.header = (tempConfig.paths.header || config.paths.native).map((p) => getPath(p));
132
- config.paths.bridge = (tempConfig.paths.bridge || [...config.paths.native, config.paths.temp]).map((p) => getPath(p));
133
- config.paths.output = getPath(tempConfig.paths.output) || config.paths.temp;
134
- config.paths.cmake = options.depend ? findCMakeListsFile(config.paths.output) : (
135
- getPath(tempConfig.paths.cmake || findCMakeListsFile(config.paths.project))
136
- );
137
- config.paths.cli = dirname;
138
-
139
- config.ext.header = tempConfig.ext.header || ['h', 'hpp', 'hxx', 'hh'];
140
- config.ext.source = tempConfig.ext.source || ['c', 'cpp', 'cxx', 'cc'];
141
- config.ext.module = tempConfig.ext.module || ['i'];
142
-
143
- config.export.type = tempConfig.export.type || 'cmake';
144
- config.export.header = tempConfig.export.header || 'include';
145
- config.export.libPath = getPath(tempConfig.export.libPath || 'lib');
146
- config.export.libName = tempConfig.export.libName || [config.general.name];
147
- config.export.binHeaders = tempConfig.export.binHeaders || [];
148
- config.export.entry = tempConfig.export.entry;
149
-
150
- config.platform['Emscripten-x86_64'] = tempConfig.platform['Emscripten-x86_64'] || {};
151
- config.platform['Emscripten-x86_64-browser'] = tempConfig.platform['Emscripten-x86_64-browser'] || {};
152
- config.platform['Emscripten-x86_64-node'] = tempConfig.platform['Emscripten-x86_64-node'] || {};
153
- config.platform['Android-arm64-v8a'] = tempConfig.platform['Android-arm64-v8a'] || {};
154
- config.platform['iOS-iphoneos'] = tempConfig.platform['iOS-iphoneos'] || {};
155
- config.platform['iOS-iphonesimulator'] = tempConfig.platform['iOS-iphonesimulator'] || {};
156
-
157
- createDir('interface', config.paths.temp);
158
- createDir('bridge', config.paths.temp);
159
-
160
- // eslint-disable-next-line max-len
161
- config.getAllDependencies = () => {
162
- const output = {};
163
- [...config.dependencies, ...config.dependencies.map((d) => d.getAllDependencies()).flat()].forEach((d) => {
164
- output[d.paths.project] = d;
165
- });
166
- return Object.values(output);
167
- };
168
-
169
- return config;
170
- }
@@ -1,7 +0,0 @@
1
- import * as url from 'node:url';
2
-
3
- export default function getDirName(importUrl) {
4
- const filename = url.fileURLToPath(importUrl);
5
- const temp = filename.split('/'); temp.pop();
6
- return temp.join('/');
7
- }
@@ -1,10 +0,0 @@
1
- import p from 'path';
2
-
3
- export default function getPathInfo(path, base) {
4
- const output = {
5
- relative: p.relative(base, path),
6
- absolute: path,
7
- };
8
-
9
- return output;
10
- }
@@ -1,34 +0,0 @@
1
- import { assert } from 'chai';
2
- import fs from 'fs';
3
- import p from 'path';
4
- import * as url from 'node:url';
5
- import { tmpdir } from "os";
6
- import findCMakeListsFile from './findCMakeListsFile.js';
7
-
8
- const __filename = url.fileURLToPath(import.meta.url);
9
- const temp = __filename.split('/');
10
- temp.pop();
11
- temp.pop();
12
- const __dirname = temp.join('/');
13
-
14
- export function createTempDir(folder) {
15
- let path = p.join(tmpdir(), "cppjs-app-cli-test");
16
- if (folder) path = p.join(path, folder);
17
-
18
- if (fs.existsSync(path)) fs.rmSync(path, { recursive: true, force: true });
19
- fs.mkdirSync(path, { recursive: true });
20
-
21
- return path;
22
- }
23
-
24
- describe('findCMakeListsFile', function () {
25
- let tempdir;
26
- before(async function () {
27
- tempdir = createTempDir();
28
- });
29
-
30
- it('find CMakeLists.txt file', async function () {
31
- const path = findCMakeListsFile(tempdir);
32
- assert.equal(path, __dirname + '/assets/CMakeLists.txt');
33
- });
34
- });
@@ -1,81 +0,0 @@
1
- import { assert } from 'chai';
2
- import { getBaseInfo, getPathInfo } from './utils.js';
3
-
4
- describe('getBaseInfo', function () {
5
- it('empty', async function () {
6
- const baseInfo = getBaseInfo('');
7
- assert.equal(baseInfo.withSlash, '/');
8
- assert.equal(baseInfo.withoutSlash, '/');
9
- });
10
-
11
- it('null', async function () {
12
- const baseInfo = getBaseInfo(null);
13
- assert.equal(baseInfo.withSlash, '/');
14
- assert.equal(baseInfo.withoutSlash, '/');
15
- });
16
-
17
- it('undefined', async function () {
18
- const baseInfo = getBaseInfo();
19
- assert.equal(baseInfo.withSlash, '/');
20
- assert.equal(baseInfo.withoutSlash, '/');
21
- });
22
-
23
- it('/', async function () {
24
- const baseInfo = getBaseInfo('/');
25
- assert.equal(baseInfo.withSlash, '/');
26
- assert.equal(baseInfo.withoutSlash, '/');
27
- });
28
-
29
- it('with /', async function () {
30
- const baseInfo = getBaseInfo('/home/cppjs/');
31
- assert.equal(baseInfo.withSlash, '/home/cppjs/');
32
- assert.equal(baseInfo.withoutSlash, '/home/cppjs');
33
- });
34
-
35
- it('without /', async function () {
36
- const baseInfo = getBaseInfo('/home/cppjs');
37
- assert.equal(baseInfo.withSlash, '/home/cppjs/');
38
- assert.equal(baseInfo.withoutSlash, '/home/cppjs');
39
- });
40
- });
41
-
42
- describe('getPathInfo', function () {
43
- it('path: absolute', async function () {
44
- const path = '/home/cppjs/cppjs.h';
45
- const pathInfo = getPathInfo(path);
46
- assert.equal(pathInfo.relative, path);
47
- assert.equal(pathInfo.absolute, path);
48
- });
49
-
50
- it('path: absolute, base: /', async function () {
51
- const basePath = '/home/cppjs/';
52
- const path = '/home/cppjs/cppjs.h';
53
- const pathInfo = getPathInfo(path, basePath);
54
- assert.equal(pathInfo.relative, 'cppjs.h');
55
- assert.equal(pathInfo.absolute, path);
56
- });
57
-
58
- it('path: absolute, base', async function () {
59
- const basePath = '/home/cppjs';
60
- const path = '/home/cppjs/cppjs.h';
61
- const pathInfo = getPathInfo(path, basePath);
62
- assert.equal(pathInfo.relative, 'cppjs.h');
63
- assert.equal(pathInfo.absolute, path);
64
- });
65
-
66
- it('path: relative, base: /', async function () {
67
- const basePath = '/home/cppjs/';
68
- const path = 'cppjs.h';
69
- const pathInfo = getPathInfo(path, basePath);
70
- assert.equal(pathInfo.relative, 'cppjs.h');
71
- assert.equal(pathInfo.absolute, basePath + path);
72
- });
73
-
74
- it('path: relative, base: ', async function () {
75
- const basePath = '/home/cppjs';
76
- const path = 'cppjs.h';
77
- const pathInfo = getPathInfo(path, basePath);
78
- assert.equal(pathInfo.relative, 'cppjs.h');
79
- assert.equal(pathInfo.absolute, basePath + '/' + path);
80
- });
81
- });
@@ -1 +0,0 @@
1
- #include "sample.h"
@@ -1,10 +0,0 @@
1
- #ifndef _SAMPLE_H
2
- #define _SAMPLE_H
3
-
4
- class Sample {
5
- public:
6
- Sample();
7
- void t();
8
- };
9
-
10
- #endif
@@ -1,12 +0,0 @@
1
- #ifndef _SAMPLE_I
2
- #define _SAMPLE_I
3
-
4
- %module Sample
5
-
6
- %{
7
- #include "sample.h"
8
- %}
9
-
10
- %include "sample.h"
11
-
12
- #endif
File without changes
@@ -1,12 +0,0 @@
1
- #ifndef _SAMPLE2_I
2
- #define _SAMPLE2_I
3
-
4
- %module SAMPLE2
5
-
6
- %{
7
- #include "sample2.h"
8
- %}
9
-
10
- %include "sample2.h"
11
-
12
- #endif
@@ -1,10 +0,0 @@
1
- #ifndef _SAMPLE2_H
2
- #define _SAMPLE2_H
3
-
4
- class Sample2 {
5
- public:
6
- Sample2();
7
- void y();
8
- };
9
-
10
- #endif