@airmoney-degn/airmoney-cli 0.13.0 → 0.13.2

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/dist/cli/serve.js CHANGED
@@ -1,134 +1,12 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.serveCommand = serveCommand;
7
- const path_1 = __importDefault(require("path"));
8
- const open_1 = __importDefault(require("open"));
9
- // var cors = require('cors')
10
- // import * as cors from "cors"
11
- const cors_1 = __importDefault(require("cors"));
12
- const express_1 = __importDefault(require("express"));
13
- const express_ws_1 = __importDefault(require("express-ws"));
14
- const metadata_1 = require("../util/metadata");
15
- const server_1 = require("../util/server");
16
- const http_proxy_middleware_1 = require("http-proxy-middleware");
17
- const cryptoProcess_1 = require("../util/cryptoProcess");
18
- /**
19
- * Helper to get the path to the "public/simulator" folder
20
- * from the distributed code. We use __dirname, then move
21
- * up two levels to the project root, then into public/simulator.
22
- */
23
- function getSimulatorDir() {
24
- // after tsc compile, serve.js is in dist/cli/
25
- // we step up two dirs to get back to the project root
26
- // then go into public/simulator
27
- const base = path_1.default.resolve(__dirname, '..', '..');
28
- return path_1.default.join(base, 'public', 'simulator');
29
- }
4
+ const ServeOrchestrator_1 = require("../service/serve/ServeOrchestrator");
30
5
  async function serveCommand(noBrowser, locationFolder, appUrl) {
31
- // Check if simulator and API ports are already in use (crypto service handles its own port)
32
- const requiredPorts = [4040, 4041, 5050];
33
- const inUsePorts = await (0, cryptoProcess_1.checkPortsInUse)(requiredPorts);
34
- if (inUsePorts.length > 0) {
35
- console.error('\x1b[31m❌ Port conflict detected!\x1b[0m');
36
- console.error(`\x1b[31mThe following ports are already in use: ${inUsePorts.join(', ')}\x1b[0m`);
37
- console.error('\x1b[31m\nPlease kill the processes using these ports before running serve:\x1b[0m');
38
- const killCommand = `lsof -ti:${inUsePorts.join(',')} | xargs kill -9`;
39
- console.error(`\x1b[31m ${killCommand}\x1b[0m`);
40
- process.exit(1);
41
- }
42
- const simulatorPort = 4041;
43
- const simulatorExpress = (0, express_ws_1.default)((0, express_1.default)()).app;
44
- simulatorExpress.use((0, cors_1.default)());
45
- simulatorExpress.use(express_1.default.json());
46
- let simulatorClient;
47
- simulatorExpress.ws('/ws', function (ws, req) {
48
- simulatorClient = ws;
49
- ws.on('message', function (msg) {
50
- console.log(msg);
51
- });
52
- // console.log('socket', req.body);
53
- });
54
- const simulatorDir = getSimulatorDir();
55
- simulatorExpress.use('/simulator', express_1.default.static(simulatorDir));
56
- ////////////////////////////////////////////////////////////////
57
- let projectFolder = process.cwd();
58
- if (locationFolder != undefined) {
59
- projectFolder = path_1.default.join(process.cwd(), locationFolder);
60
- }
61
- const metadata = (0, metadata_1.loadMetadata)();
62
- if (!metadata) {
63
- console.log('\x1b[33mNo metadata found. Skipping some possible checks.\x1b[0m');
64
- }
65
- if (appUrl != undefined) {
66
- const proxyMiddleware = (0, http_proxy_middleware_1.createProxyMiddleware)({
67
- xfwd: true,
68
- target: appUrl,
69
- changeOrigin: true,
70
- pathFilter: '/',
71
- });
72
- simulatorExpress.use('/', proxyMiddleware);
73
- }
74
- else {
75
- simulatorExpress.use('/', express_1.default.static(projectFolder));
76
- }
77
- const simulatorServer = simulatorExpress.listen(simulatorPort, () => {
78
- const url = `http://localhost:${simulatorPort}/simulator`;
79
- console.log(`\x1b[32mStarting simulator server at ${url}\x1b[0m`);
80
- if (!noBrowser) {
81
- (0, open_1.default)(url)
82
- .then(() => {
83
- // success opening URL
84
- })
85
- .catch(err => {
86
- console.error('\x1b[31mFailed to open web browser\x1b[0m', err);
87
- });
88
- }
89
- });
90
- ////////////////////////////////////////////////////////////////
91
- const airmoneyServicePort = 4040;
92
- const airmoneyServiceExpress = (0, express_1.default)();
93
- airmoneyServiceExpress.use((0, cors_1.default)());
94
- airmoneyServiceExpress.use(express_1.default.json());
95
- airmoneyServiceExpress.post('/', async (req, res) => {
96
- let rpcReq = req.body;
97
- let event;
98
- let simulator = true;
99
- switch (rpcReq.method) {
100
- case 'setImage':
101
- case 'setAnimate':
102
- event = (0, server_1.displayImage)(rpcReq, metadata.name);
103
- break;
104
- }
105
- if (simulator) {
106
- if (event != null) {
107
- if (simulatorClient) {
108
- simulatorClient.send(JSON.stringify(event));
109
- res.status(200).json({ success: true });
110
- return;
111
- }
112
- else {
113
- console.log('\x1b[33mNo simulator client connected\x1b[0m');
114
- res.status(503).json({ error: 'Simulator not connected' });
115
- return;
116
- }
117
- }
118
- else {
119
- res.status(500).json({ error: 'Failed to process request' });
120
- return;
121
- }
122
- }
123
- else {
124
- res.json({ jsonrpc: '2.0', result: event, id: 1 });
125
- return;
126
- }
127
- });
128
- const airmoneyServiceServer = airmoneyServiceExpress.listen(airmoneyServicePort, () => {
129
- const url = `http://localhost:${airmoneyServicePort}`;
130
- console.log(`\x1b[32mStarting airmoney service server at ${url}\x1b[0m`);
6
+ const orchestrator = new ServeOrchestrator_1.ServeOrchestrator({
7
+ noBrowser,
8
+ locationFolder,
9
+ appUrl,
131
10
  });
132
- ////////////////////////////////////////////////////////////////
133
- (0, cryptoProcess_1.startCryptoServiceSimple)();
11
+ await orchestrator.start();
134
12
  }
package/dist/config.json CHANGED
@@ -1,3 +1,3 @@
1
1
  {
2
- "version": "0.13.0"
2
+ "version": "0.13.2"
3
3
  }
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.AirmoneyService = void 0;
7
+ const express_1 = __importDefault(require("express"));
8
+ const cors_1 = __importDefault(require("cors"));
9
+ const server_1 = require("../../util/server");
10
+ class AirmoneyService {
11
+ constructor(config) {
12
+ this.simulatorClient = null;
13
+ this.config = config;
14
+ this.app = (0, express_1.default)();
15
+ this.setupMiddleware();
16
+ this.setupRoutes();
17
+ }
18
+ setupMiddleware() {
19
+ this.app.use((0, cors_1.default)());
20
+ this.app.use(express_1.default.json());
21
+ }
22
+ setupRoutes() {
23
+ this.app.post('/', this.handleRpcRequest.bind(this));
24
+ }
25
+ async handleRpcRequest(req, res) {
26
+ const rpcReq = req.body;
27
+ let event;
28
+ const simulator = true;
29
+ switch (rpcReq.method) {
30
+ case 'setImage':
31
+ case 'setAnimate':
32
+ event = (0, server_1.displayImage)(rpcReq, this.config.metadata?.name || '');
33
+ break;
34
+ }
35
+ if (simulator) {
36
+ if (event != null) {
37
+ if (this.simulatorClient) {
38
+ this.simulatorClient.send(JSON.stringify(event));
39
+ res.status(200).json({ success: true });
40
+ return;
41
+ }
42
+ else {
43
+ console.log('\x1b[33mNo simulator client connected\x1b[0m');
44
+ res.status(503).json({ error: 'Simulator not connected' });
45
+ return;
46
+ }
47
+ }
48
+ else {
49
+ res.status(500).json({ error: 'Failed to process request' });
50
+ return;
51
+ }
52
+ }
53
+ else {
54
+ res.json({ jsonrpc: '2.0', result: event, id: 1 });
55
+ return;
56
+ }
57
+ }
58
+ setSimulatorClient(simulatorClient) {
59
+ this.simulatorClient = simulatorClient;
60
+ }
61
+ async start() {
62
+ return new Promise(resolve => {
63
+ this.server = this.app.listen(this.config.port, () => {
64
+ const url = `http://localhost:${this.config.port}`;
65
+ console.log(`\x1b[32mStarting airmoney service server at ${url}\x1b[0m`);
66
+ resolve();
67
+ });
68
+ });
69
+ }
70
+ async stop() {
71
+ return new Promise(resolve => {
72
+ if (this.server) {
73
+ this.server.close(() => {
74
+ resolve();
75
+ });
76
+ }
77
+ else {
78
+ resolve();
79
+ }
80
+ });
81
+ }
82
+ }
83
+ exports.AirmoneyService = AirmoneyService;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.PortManager = void 0;
4
+ const cryptoProcess_1 = require("../../util/cryptoProcess");
5
+ class PortManager {
6
+ /**
7
+ * Check if any of the required ports are in use
8
+ */
9
+ static async checkPortConflicts(ports = this.DEFAULT_PORTS) {
10
+ const inUsePorts = await (0, cryptoProcess_1.checkPortsInUse)(ports);
11
+ if (inUsePorts.length > 0) {
12
+ console.error('\x1b[31m❌ Port conflict detected!\x1b[0m');
13
+ console.error(`\x1b[31mThe following ports are already in use: ${inUsePorts.join(', ')}\x1b[0m`);
14
+ console.error('\x1b[31m\nPlease kill the processes using these ports before running serve:\x1b[0m');
15
+ const killCommand = `lsof -ti:${inUsePorts.join(',')} | xargs kill -9`;
16
+ console.error(`\x1b[31m ${killCommand}\x1b[0m`);
17
+ process.exit(1);
18
+ }
19
+ }
20
+ /**
21
+ * Get the default ports used by the application
22
+ */
23
+ static getDefaultPorts() {
24
+ return [...this.DEFAULT_PORTS];
25
+ }
26
+ }
27
+ exports.PortManager = PortManager;
28
+ PortManager.DEFAULT_PORTS = [4040, 4041, 5050];
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ServeOrchestrator = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const metadata_1 = require("../../util/metadata");
9
+ const cryptoProcess_1 = require("../../util/cryptoProcess");
10
+ const PortManager_1 = require("../port/PortManager");
11
+ const SimulatorService_1 = require("../simulator/SimulatorService");
12
+ const AirmoneyService_1 = require("../airmoney/AirmoneyService");
13
+ class ServeOrchestrator {
14
+ constructor(config) {
15
+ this.config = config;
16
+ this.metadata = (0, metadata_1.loadMetadata)();
17
+ this.simulatorService = this.createSimulatorService();
18
+ this.airmoneyService = this.createAirmoneyService();
19
+ }
20
+ createSimulatorService() {
21
+ const projectFolder = this.config.locationFolder
22
+ ? path_1.default.join(process.cwd(), this.config.locationFolder)
23
+ : process.cwd();
24
+ return this.config.appUrl
25
+ ? SimulatorService_1.SimulatorService.createWithProxy(this.config.appUrl, {
26
+ noBrowser: this.config.noBrowser,
27
+ })
28
+ : SimulatorService_1.SimulatorService.createWithProject(projectFolder, {
29
+ noBrowser: this.config.noBrowser,
30
+ });
31
+ }
32
+ createAirmoneyService() {
33
+ return new AirmoneyService_1.AirmoneyService({
34
+ port: 4040,
35
+ metadata: this.metadata ? { name: this.metadata.name } : undefined,
36
+ });
37
+ }
38
+ async start() {
39
+ // Check for port conflicts first
40
+ await PortManager_1.PortManager.checkPortConflicts();
41
+ // Validate metadata
42
+ if (!this.metadata) {
43
+ console.log('\x1b[33mNo metadata found. Skipping some possible checks.\x1b[0m');
44
+ }
45
+ // Start simulator service
46
+ await this.simulatorService.start();
47
+ // Set up callback to handle simulator client connections
48
+ this.simulatorService.setOnClientConnected((client) => {
49
+ this.airmoneyService.setSimulatorClient(client);
50
+ });
51
+ // Start airmoney service
52
+ await this.airmoneyService.start();
53
+ // Start crypto service
54
+ (0, cryptoProcess_1.startCryptoServiceSimple)();
55
+ }
56
+ async stop() {
57
+ await Promise.all([
58
+ this.simulatorService.stop(),
59
+ this.airmoneyService.stop(),
60
+ ]);
61
+ }
62
+ }
63
+ exports.ServeOrchestrator = ServeOrchestrator;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.BaseSimulatorService = void 0;
7
+ const path_1 = __importDefault(require("path"));
8
+ const express_1 = __importDefault(require("express"));
9
+ const express_ws_1 = __importDefault(require("express-ws"));
10
+ const cors_1 = __importDefault(require("cors"));
11
+ const open_1 = __importDefault(require("open"));
12
+ const http_proxy_middleware_1 = require("http-proxy-middleware");
13
+ class BaseSimulatorService {
14
+ constructor(config) {
15
+ this.simulatorClient = null;
16
+ this.onClientConnectedCallback = null;
17
+ this.config = config;
18
+ this.app = (0, express_ws_1.default)((0, express_1.default)()).app;
19
+ this.setupMiddleware();
20
+ }
21
+ setupMiddleware() {
22
+ this.app.use((0, cors_1.default)());
23
+ this.app.use(express_1.default.json());
24
+ }
25
+ getSimulatorDir() {
26
+ // after tsc compile, serve.js is in dist/cli/
27
+ // we step up two dirs to get back to the project root
28
+ // then go into public/simulator
29
+ const base = path_1.default.resolve(__dirname, '..', '..', '..');
30
+ return path_1.default.join(base, 'public', 'simulator');
31
+ }
32
+ setupWebSocket() {
33
+ this.app.ws('/ws', (ws, req) => {
34
+ this.simulatorClient = ws;
35
+ // Notify callback about new connection
36
+ if (this.onClientConnectedCallback) {
37
+ this.onClientConnectedCallback(this.getClient());
38
+ }
39
+ ws.on('message', (msg) => {
40
+ console.log(msg);
41
+ });
42
+ ws.on('close', () => {
43
+ this.simulatorClient = null;
44
+ // Notify callback about disconnection
45
+ if (this.onClientConnectedCallback) {
46
+ this.onClientConnectedCallback(null);
47
+ }
48
+ });
49
+ ws.on('error', (error) => {
50
+ console.error('WebSocket error:', error);
51
+ this.simulatorClient = null;
52
+ // Notify callback about disconnection due to error
53
+ if (this.onClientConnectedCallback) {
54
+ this.onClientConnectedCallback(null);
55
+ }
56
+ });
57
+ });
58
+ }
59
+ setupStaticFiles() {
60
+ const simulatorDir = this.getSimulatorDir();
61
+ this.app.use('/simulator', express_1.default.static(simulatorDir));
62
+ }
63
+ setupProjectFiles() {
64
+ if (this.config.appUrl) {
65
+ const proxyMiddleware = (0, http_proxy_middleware_1.createProxyMiddleware)({
66
+ xfwd: true,
67
+ target: this.config.appUrl,
68
+ changeOrigin: true,
69
+ pathFilter: '/',
70
+ });
71
+ this.app.use('/', proxyMiddleware);
72
+ }
73
+ else if (this.config.projectFolder) {
74
+ this.app.use('/', express_1.default.static(this.config.projectFolder));
75
+ }
76
+ }
77
+ async openBrowser() {
78
+ if (!this.config.noBrowser) {
79
+ const url = `http://localhost:${this.config.port}/simulator`;
80
+ try {
81
+ await (0, open_1.default)(url);
82
+ }
83
+ catch (err) {
84
+ console.error('\x1b[31mFailed to open web browser\x1b[0m', err);
85
+ }
86
+ }
87
+ }
88
+ getClient() {
89
+ if (!this.simulatorClient)
90
+ return null;
91
+ return {
92
+ ws: this.simulatorClient,
93
+ send: (data) => {
94
+ if (this.simulatorClient) {
95
+ this.simulatorClient.send(data);
96
+ }
97
+ },
98
+ };
99
+ }
100
+ sendToClient(data) {
101
+ if (this.simulatorClient) {
102
+ this.simulatorClient.send(data);
103
+ return true;
104
+ }
105
+ return false;
106
+ }
107
+ setOnClientConnected(callback) {
108
+ this.onClientConnectedCallback = callback;
109
+ }
110
+ async start() {
111
+ this.setupWebSocket();
112
+ this.setupStaticFiles();
113
+ this.setupProjectFiles();
114
+ return new Promise(resolve => {
115
+ this.server = this.app.listen(this.config.port, () => {
116
+ const url = `http://localhost:${this.config.port}/simulator`;
117
+ console.log(`\x1b[32mStarting simulator server at ${url}\x1b[0m`);
118
+ this.openBrowser();
119
+ resolve();
120
+ });
121
+ });
122
+ }
123
+ async stop() {
124
+ return new Promise(resolve => {
125
+ if (this.server) {
126
+ this.server.close(() => {
127
+ resolve();
128
+ });
129
+ }
130
+ else {
131
+ resolve();
132
+ }
133
+ });
134
+ }
135
+ }
136
+ exports.BaseSimulatorService = BaseSimulatorService;
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SimulatorService = void 0;
4
+ const BaseSimulatorService_1 = require("./BaseSimulatorService");
5
+ class SimulatorService extends BaseSimulatorService_1.BaseSimulatorService {
6
+ constructor(config) {
7
+ super(config);
8
+ }
9
+ /**
10
+ * Create a new SimulatorService instance with default configuration
11
+ */
12
+ static create(config = {}) {
13
+ const defaultConfig = {
14
+ port: 4041,
15
+ noBrowser: false,
16
+ ...config,
17
+ };
18
+ return new SimulatorService(defaultConfig);
19
+ }
20
+ /**
21
+ * Create a SimulatorService with project folder configuration
22
+ */
23
+ static createWithProject(projectFolder, config = {}) {
24
+ return SimulatorService.create({
25
+ projectFolder,
26
+ ...config,
27
+ });
28
+ }
29
+ /**
30
+ * Create a SimulatorService with app URL proxy configuration
31
+ */
32
+ static createWithProxy(appUrl, config = {}) {
33
+ return SimulatorService.create({
34
+ appUrl,
35
+ ...config,
36
+ });
37
+ }
38
+ }
39
+ exports.SimulatorService = SimulatorService;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -50,7 +50,7 @@ function loadMetadata(projectPath = '.') {
50
50
  return data;
51
51
  }
52
52
  catch (err) {
53
- console.log('\x1b[33mPlease run this command in Project directory\x1b[0m');
53
+ console.log('\x1b[33mError loading metadata\x1b[0m');
54
54
  return null;
55
55
  }
56
56
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@airmoney-degn/airmoney-cli",
3
- "version": "0.13.0",
3
+ "version": "0.13.2",
4
4
  "description": "airmoney-cli is a command-line interface tool designed to facilitate the development and management of decentralized applications (DApps) for Airmoney.",
5
5
  "publishConfig": {
6
6
  "access": "public"