cpp.js 1.0.0-beta.1 → 1.0.0-beta.3

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.
package/package.json CHANGED
@@ -1,22 +1,24 @@
1
1
  {
2
2
  "name": "cpp.js",
3
- "version": "1.0.0-beta.1",
3
+ "version": "1.0.0-beta.3",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "homepage": "https://cpp.js.org",
7
7
  "repository": "https://github.com/bugra9/cpp.js.git",
8
8
  "bin": {
9
- "cpp.js": "./src/bin.js"
9
+ "cpp.js": "./src/bin.js",
10
+ "cppjs": "./src/bin.js"
10
11
  },
11
12
  "main": "src/index.js",
12
13
  "dependencies": {
13
14
  "@rollup/plugin-commonjs": "^26.0.1",
14
15
  "@rollup/plugin-node-resolve": "^15.2.3",
15
16
  "@rollup/plugin-virtual": "^3.0.2",
16
- "commander": "^9.4.1",
17
+ "commander": "^12.1.0",
17
18
  "glob": "^8.0.3",
18
19
  "rollup": "^4.18.0",
19
- "rollup-plugin-uglify": "^6.0.4"
20
+ "rollup-plugin-uglify": "^6.0.4",
21
+ "upath": "^2.0.1"
20
22
  },
21
23
  "devDependencies": {
22
24
  "mocha": "^10.2.0"
package/src/bin.js CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
2
 
3
- import { Command, Argument } from 'commander';
3
+ import { Command, Option } from 'commander';
4
4
  import fs from 'fs';
5
5
  import glob from 'glob';
6
6
  import { createDir } from './utils/createTempDir.js';
@@ -14,30 +14,26 @@ const program = new Command();
14
14
 
15
15
  program
16
16
  .name('cpp.js')
17
- .description('Compile c++ files to webassembly.')
17
+ .description('Compile C++ files to WebAssembly and native platforms.')
18
18
  .version(packageJSON.version)
19
19
  .showHelpAfterError();
20
20
 
21
- const commandGenerate = program.command('generate')
22
- .description('Generate app or lib.')
23
- .addArgument(new Argument('<type>', 'Generation type').choices(['app', 'lib']))
24
- .option('-b, --base <base>', 'base path')
25
- .option('-p, --platform <platform>', 'platform (all)', 'all', ['all', 'wasm', 'android', 'ios'])
26
- .option('-o, --output <string>', 'Output path');
21
+ const commandBuild = program.command('build')
22
+ .description('compile the project that was set up using Cpp.js')
23
+ .addOption(new Option('-p, --platform <platform>', 'target platform').default('all').choices(['all', 'wasm', 'android', 'ios']));
27
24
 
28
25
  const commandRun = program.command('run')
29
- .description('Run docker application');
26
+ .description('run docker application');
30
27
 
31
28
  const commandPostInstall = program.command('postinstall')
32
- .description('npm postinstall');
29
+ .description('prepare the required packages for Cpp.js after installation');
33
30
 
34
31
  program.parse(process.argv);
35
32
 
36
33
  switch (program.args[0]) {
37
- case 'generate': {
38
- const type = commandGenerate.args[0];
39
- const { output, platform, base } = commandGenerate.opts();
40
- generate(type, platform, output, base);
34
+ case 'build': {
35
+ const { platform } = commandBuild.opts();
36
+ build(platform);
41
37
  break;
42
38
  }
43
39
  case 'run': {
@@ -55,7 +51,15 @@ switch (program.args[0]) {
55
51
 
56
52
  function postInstall() {
57
53
  const projectPath = process.env.PWD;
58
- if (!fs.existsSync(`${projectPath}/cppjs.config.js`) && !fs.existsSync(`${projectPath}/cppjs.config.cjs`) && !fs.existsSync(`${projectPath}/cppjs.config.mjs`)) {
54
+ const isDarwin = process.platform === 'darwin';
55
+ if (
56
+ !isDarwin
57
+ || (
58
+ !fs.existsSync(`${projectPath}/cppjs.config.js`)
59
+ && !fs.existsSync(`${projectPath}/cppjs.config.cjs`)
60
+ && !fs.existsSync(`${projectPath}/cppjs.config.mjs`)
61
+ )
62
+ ) {
59
63
  return;
60
64
  }
61
65
 
@@ -78,49 +82,48 @@ function postInstall() {
78
82
 
79
83
  function run(programName, params) {
80
84
  const compiler = new CppjsCompiler();
81
- compiler.run(programName, params);
85
+ compiler.run(programName, params, { console: true });
82
86
  }
83
87
 
84
- function generate(type, platform) {
88
+ function build(platform) {
85
89
  const compiler2 = new CppjsCompiler();
86
- if (type === 'lib') {
87
- const modules = [];
88
- compiler2.config.paths.module.forEach((modulePath) => {
89
- modules.push(...glob.sync('**/*.i', { absolute: true, cwd: modulePath }));
90
- modules.push(...glob.sync('*.i', { absolute: true, cwd: modulePath }));
91
- });
92
-
93
- if (platform === 'all' || platform === 'wasm') {
94
- if (!fs.existsSync(`${compiler2.config.paths.output}/prebuilt/Emscripten-x86_64`)) {
95
- generateWasmLib();
96
- modules.forEach((modulePath) => {
97
- const fileName = modulePath.split('/').at(-1);
98
- createDir('prebuilt/Emscripten-x86_64/swig', compiler2.config.paths.output);
99
- fs.copyFileSync(modulePath, `${compiler2.config.paths.output}/prebuilt/Emscripten-x86_64/swig/${fileName}`);
100
- });
101
- }
90
+
91
+ const modules = [];
92
+ compiler2.config.paths.module.forEach((modulePath) => {
93
+ modules.push(...glob.sync('**/*.i', { absolute: true, cwd: modulePath }));
94
+ modules.push(...glob.sync('*.i', { absolute: true, cwd: modulePath }));
95
+ });
96
+
97
+ if (platform === 'all' || platform === 'wasm') {
98
+ if (!fs.existsSync(`${compiler2.config.paths.output}/prebuilt/Emscripten-x86_64`)) {
99
+ buildWasm();
100
+ modules.forEach((modulePath) => {
101
+ const fileName = modulePath.split('/').at(-1);
102
+ createDir('prebuilt/Emscripten-x86_64/swig', compiler2.config.paths.output);
103
+ fs.copyFileSync(modulePath, `${compiler2.config.paths.output}/prebuilt/Emscripten-x86_64/swig/${fileName}`);
104
+ });
102
105
  }
106
+ }
103
107
 
104
- if (platform === 'wasm') return;
105
- const platforms = {
106
- all: ['Android-arm64-v8a', 'iOS-iphoneos', 'iOS-iphonesimulator'],
107
- android: ['Android-arm64-v8a'],
108
- ios: ['iOS-iphoneos', 'iOS-iphonesimulator'],
109
- };
110
- platforms[platform].forEach((p) => {
111
- if (!fs.existsSync(`${compiler2.config.paths.output}/prebuilt/${p}`)) {
112
- const compiler = new CppjsCompiler(p);
113
- compiler.createLib();
114
- modules.forEach((modulePath) => {
115
- const fileName = modulePath.split('/').at(-1);
116
- createDir(`prebuilt/${p}/swig`, compiler2.config.paths.output);
117
- fs.copyFileSync(modulePath, `${compiler2.config.paths.output}/prebuilt/${p}/swig/${fileName}`);
118
- });
119
- }
120
- });
121
- if (platform === 'all' || platform === 'ios') {
122
- compiler2.finishBuild();
108
+ if (platform === 'wasm') return;
109
+ const platforms = {
110
+ all: ['Android-arm64-v8a', 'iOS-iphoneos', 'iOS-iphonesimulator'],
111
+ android: ['Android-arm64-v8a'],
112
+ ios: ['iOS-iphoneos', 'iOS-iphonesimulator'],
113
+ };
114
+ platforms[platform].forEach((p) => {
115
+ if (!fs.existsSync(`${compiler2.config.paths.output}/prebuilt/${p}`)) {
116
+ const compiler = new CppjsCompiler(p);
117
+ compiler.createLib();
118
+ modules.forEach((modulePath) => {
119
+ const fileName = modulePath.split('/').at(-1);
120
+ createDir(`prebuilt/${p}/swig`, compiler2.config.paths.output);
121
+ fs.copyFileSync(modulePath, `${compiler2.config.paths.output}/prebuilt/${p}/swig/${fileName}`);
122
+ });
123
123
  }
124
+ });
125
+ if (platform === 'all' || platform === 'ios') {
126
+ compiler2.finishBuild();
124
127
  }
125
128
 
126
129
  const distCmakeContent = fs.readFileSync(`${compiler2.config.paths.cli}/assets/dist.cmake`, { encoding: 'utf8', flag: 'r' })
@@ -128,7 +131,7 @@ function generate(type, platform) {
128
131
  fs.writeFileSync(`${compiler2.config.paths.output}/prebuilt/CMakeLists.txt`, distCmakeContent);
129
132
  }
130
133
 
131
- async function generateWasmLib() {
134
+ async function buildWasm() {
132
135
  let headers = [];
133
136
 
134
137
  const compiler = new CppjsCompiler();
@@ -6,10 +6,7 @@ import getLibs from './getLibs.js';
6
6
  import getData from './getData.js';
7
7
  import getPathInfo from '../utils/getPathInfo.js';
8
8
 
9
- export default async function createWasm(compiler, options = {}, buildAll = false) {
10
- if (fs.existsSync('/tmp/cppjs/live')) fs.unlinkSync('/tmp/cppjs/live');
11
- fs.symlinkSync(compiler.config.paths.base, '/tmp/cppjs/live');
12
-
9
+ export default async function createWasm(compiler, options = {}) {
13
10
  const output = `/tmp/cppjs/live/${getPathInfo(compiler.config.paths.temp, compiler.config.paths.base).relative}`;
14
11
 
15
12
  let params = getCmakeParams(compiler.config, '/tmp/cppjs/live/', true, false);
@@ -42,7 +39,7 @@ export default async function createWasm(compiler, options = {}, buildAll = fals
42
39
  '-o', `${output}/${compiler.config.general.name}.js`,
43
40
  ...data,
44
41
  ]);
45
- await buildJS(compiler, `${output}/${compiler.config.general.name}.js`, 'browser');
42
+ await buildJS(compiler, `${compiler.config.paths.temp}/${compiler.config.general.name}.js`, 'browser');
46
43
  run(compiler, 'emcc', [
47
44
  '-lembind', '-Wl,--whole-archive', '-lnodefs.js',
48
45
  ...libs, ...(options.cc || []),
@@ -54,15 +51,14 @@ export default async function createWasm(compiler, options = {}, buildAll = fals
54
51
  ]);
55
52
  Object.entries(getData(compiler.config, 'data', null, 'Emscripten-x86_64', 'node')).forEach(([key, value]) => {
56
53
  if (fs.existsSync(key)) {
57
- const dAssetPath = `${output}/data/${value}`;
54
+ const dAssetPath = `${compiler.config.paths.temp}/data/${value}`;
58
55
  if (!fs.existsSync(dAssetPath)) {
59
56
  fs.mkdirSync(dAssetPath, { recursive: true });
60
57
  fs.cpSync(key, dAssetPath, { recursive: true });
61
58
  }
62
59
  }
63
60
  });
64
- await buildJS(compiler, `${output}/${compiler.config.general.name}.js`, 'node');
65
- // await buildJS(compiler, `${output}/${compiler.config.general.name}.js`);
61
+ await buildJS(compiler, `${compiler.config.paths.temp}/${compiler.config.general.name}.js`, 'node');
66
62
  if (fs.existsSync(`${compiler.config.paths.temp}/${compiler.config.general.name}.data`)) {
67
63
  fs.renameSync(`${compiler.config.paths.temp}/${compiler.config.general.name}.data`, `${compiler.config.paths.temp}/${compiler.config.general.name}.data.txt`);
68
64
  }
@@ -1,5 +1,5 @@
1
1
  import fs from 'fs';
2
- import p from 'path';
2
+ import p from 'upath';
3
3
 
4
4
  export default function createTempDir(folder = `a${Math.random()}`, base = process.cwd()) {
5
5
  const path = p.join(base, '.cppjs');
@@ -11,5 +11,5 @@ export function createDir(folder, base = process.cwd()) {
11
11
 
12
12
  if (fs.existsSync(path)) fs.rmSync(path, { recursive: true, force: true });
13
13
  fs.mkdirSync(path, { recursive: true });
14
- return path;
14
+ return p.normalize(path);
15
15
  }
@@ -1,6 +1,7 @@
1
1
  import * as url from 'node:url';
2
+ import upath from 'upath';
2
3
 
3
- const filename = url.fileURLToPath(import.meta.url);
4
+ const filename = upath.normalize(url.fileURLToPath(import.meta.url));
4
5
  const temp = filename.split('/');
5
6
  temp.pop();
6
7
  temp.pop();
@@ -1,10 +1,10 @@
1
1
  import fs from 'fs';
2
- import nodePath from 'path';
2
+ import nodePath from 'upath';
3
3
  import * as url from 'node:url';
4
4
  import createTempDir, { createDir } from './createTempDir.js';
5
5
  import findCMakeListsFile from './findCMakeListsFile.js';
6
6
 
7
- const filename = url.fileURLToPath(import.meta.url);
7
+ const filename = nodePath.normalize(url.fileURLToPath(import.meta.url));
8
8
  const temp = filename.split('/'); temp.pop(); temp.pop();
9
9
  const dirname = temp.join('/');
10
10
 
@@ -58,7 +58,7 @@ async function initDefaultConfigFile() {
58
58
  });
59
59
 
60
60
  if (filePath) {
61
- let file = await import(filePath);
61
+ let file = await import('file:///' + filePath);
62
62
  if (file.default) file = file.default;
63
63
 
64
64
  if (typeof file === 'function') tempConfigDefault = file();
@@ -1,7 +1,8 @@
1
1
  import * as url from 'node:url';
2
+ import upath from 'upath';
2
3
 
3
4
  export default function getDirName(importUrl) {
4
- const filename = url.fileURLToPath(importUrl);
5
+ const filename = upath.normalize(url.fileURLToPath(importUrl));
5
6
  const temp = filename.split('/'); temp.pop();
6
7
  return temp.join('/');
7
8
  }
@@ -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
  }
@@ -1,9 +1,9 @@
1
- import p from 'path';
1
+ import p from 'upath';
2
2
 
3
3
  export default function getPathInfo(path, base) {
4
4
  const output = {
5
5
  relative: p.relative(base, path),
6
- absolute: path,
6
+ absolute: p.normalize(path),
7
7
  };
8
8
 
9
9
  return output;