Skip to content

Commit

Permalink
Removes ts-node and compiled versions of apps/api and apps/core in fa…
Browse files Browse the repository at this point in the history
…vor of modern, fast and lightweight tsx
  • Loading branch information
jcarlosn committed Mar 9, 2025
1 parent 000f0f1 commit 02b5700
Show file tree
Hide file tree
Showing 10 changed files with 504 additions and 82 deletions.
13 changes: 6 additions & 7 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,37 @@
"src"
],
"scripts": {
"dev-standalone": "NODE_ENV=development ts-node --files --project tsconfig.json src/index.ts",
"prod-standalone": "NODE_ENV=production node -r module-alias/register dist/apps/api/src/index.js",
"package": "tsc && shx mkdir -p dist/packages/protolib/lib && shx cp ../../packages/protolib/lib/RemoteTransport.ts dist/packages/protolib/lib/RemoteTransport.ts",
"dev-standalone": "NODE_ENV=development tsx src/index.ts",
"prod-standalone": "NODE_ENV=production tsx src/index.ts",
"prepare-dev": "node prepare.js",
"prod": "NODE_ENV=production pm2 start pm2.config.js --no-daemon",
"dev": "NODE_ENV=development pm2 start pm2.config.js --no-daemon",
"prod-service": "NODE_ENV=production pm2 start pm2.config.js",
"dev-service": "NODE_ENV=development pm2 start pm2.config.js",
"clean": "shx rm -rf dist"
"dev-service": "NODE_ENV=development pm2 start pm2.config.js"
},
"dependencies": {
"@my/config": "*",
"aedes": "^0.50.0",
"app": "*",
"bcrypt": "^5.1.1",
"chokidar": "^3.5.3",
"cookie-parser": "^1.4.6",
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"hot-hook": "^0.4.0",
"jsonwebtoken": "^9.0.2",
"level": "8.0.0",
"level-party": "^5.1.1",
"mime-types": "^2.1.35",
"module-alias": "^2.2.3",
"moment": "2.29.4",
"pino": "9.2.0",
"pino-http": "10.2.0",
"pino-pretty": "11.2.1",
"pm2": "^5.3.0",
"subleveldown": "^6.0.1",
"ts-node": "10.9.1",
"tslib": "2.6.2",
"tsx": "4.19.3",
"zod": "^3.22.2"
},
"devDependencies": {
Expand Down
12 changes: 8 additions & 4 deletions apps/api/pm2.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ const currentDir = path.dirname(__filename);

const api = {
name: 'api',
script: path.join(currentDir, 'dist', 'apps', 'api', 'src', 'index.js'),
node_args: "-r module-alias/register",
script: 'src/index.ts',
interpreter: 'node',
interpreter_args: '--import tsx',
watch: false,
autorestart: true,
windowsHide: true,
env: {
NODE_ENV: 'production'
},
Expand All @@ -20,10 +22,12 @@ const api = {

const apiDev = {
name: 'api-dev',
script: path.join(currentDir, '..', '..', 'node_modules', 'ts-node', 'dist', 'bin.js'),
args: '--files --project tsconfig.json src/index.ts',
script: 'src/index.ts',
interpreter: 'node',
interpreter_args: '--import tsx',
watch: false,
autorestart: true,
windowsHide: true,
env: {
NODE_ENV: 'development'
},
Expand Down
5 changes: 3 additions & 2 deletions apps/api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getLogger, getConfig } from 'protobase';
import { getConfigWithoutSecrets } from '@my/config'
import BundleContext from 'app/bundles/apiContext'
import { generateEvent } from 'protolib/bundles/events/eventsLibrary';
import { pathToFileURL } from 'url';

const logger = getLogger()
const subscriptions = {}
Expand Down Expand Up @@ -35,8 +36,8 @@ const mqtt = getMQTTClient(serviceName, getServiceToken(), async () => {
}

try {
const BundleAPI = await import('app/bundles/apis');
const BundleChatbotsAPI = await import('app/bundles/chatbots');
const BundleAPI = await import(pathToFileURL(require.resolve('app/bundles/apis')).href);
const BundleChatbotsAPI = await import(pathToFileURL(require.resolve('app/bundles/chatbots')).href);
//wait for mqtt before starting API
BundleAPI.default(app, { mqtt, topicPub, topicSub, ...BundleContext })
BundleChatbotsAPI.default(app, { mqtt, topicPub, topicSub, ...BundleContext })
Expand Down
20 changes: 0 additions & 20 deletions apps/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
const moduleAlias = require('module-alias')
import path from 'path';

const resolveNodeModule = (moduleName) => {
try {
return require.resolve(moduleName);
} catch (e) {
// Handle the error if the module is not found
console.error(`Module ${moduleName} not found`);
return null;
}
};

const moduleConfig = {
"app": path.resolve(__dirname, "../../../packages/app"),
"protolib": path.join(resolveNodeModule("protolib"), "..")
}

moduleAlias.addAliases(moduleConfig);

import dotenv from 'dotenv'
dotenv.config({ path: '../../.env' });
import { getServiceToken } from 'protonode'
Expand Down
5 changes: 3 additions & 2 deletions apps/api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"target": "ES2019", // Define ECMAScript target version
"module": "commonjs", // Specify module code generation: 'commonjs' for Node.js apps
"module": "nodenext", // Specify module code generation: 'commonjs' for Node.js apps
"moduleResolution": "nodenext", // Resolve modules using Node.js-style module resolution
"strict": false, // Enable all strict type-checking options
"esModuleInterop": true, // Allows default imports from modules with no default export
"skipLibCheck": true, // Skip type checking of declaration files
Expand All @@ -23,7 +24,7 @@

"paths": {
"protolib/*": ["protolib/dist/*"],
"app/*": ["./app/*"]
"app/*": ["app/*"]
}
},
"include": [
Expand Down
11 changes: 4 additions & 7 deletions apps/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@
"dev": "NODE_ENV=development pm2 start pm2.config.js --no-daemon",
"prod-service": "NODE_ENV=production pm2 start pm2.config.js",
"dev-service": "NODE_ENV=development pm2 start pm2.config.js",
"dev-full": "FULL_DEV=1 NODE_ENV=development ts-node --files --project tsconfig.json src/index.ts",
"dev-standalone": "NODE_ENV=development ts-node --files --project tsconfig.json src/index.ts",
"prod-standalone": "NODE_ENV=production && node -r module-alias/register dist/apps/core/src/index.js",
"package": "tsc && shx mkdir -p dist/packages/protolib/lib && shx cp ../../packages/protolib/lib/RemoteTransport.ts dist/packages/protolib/lib/RemoteTransport.ts && shx cp ../../system.js dist/",
"dev-full": "FULL_DEV=1 NODE_ENV=development tsx src/index.ts",
"dev-standalone": "NODE_ENV=development tsx src/index.ts",
"prod-standalone": "NODE_ENV=production tsx src/index.ts",
"prepare-dev": "node prepare.js",
"clean": "shx rm -rf dist",
"prepare-test": "shx rm -rf ../../data/databases/events",
"test": "yarn prepare-test && MAX_EVENTS=500 node test.js"
},
Expand All @@ -34,7 +32,6 @@
"level": "8.0.0",
"level-party": "^5.1.1",
"mime-types": "^2.1.35",
"module-alias": "^2.2.3",
"moment": "2.29.4",
"multer": "1.4.5-lts.1",
"pino": "9.2.0",
Expand All @@ -45,8 +42,8 @@
"protolib": "*",
"protonode": "*",
"subleveldown": "^6.0.1",
"ts-node": "10.9.1",
"tslib": "2.6.2",
"tsx": "4.19.3",
"zod": "^3.22.2"
},
"devDependencies": {
Expand Down
10 changes: 6 additions & 4 deletions apps/core/pm2.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ module.exports = {
apps: [
isFullDev ? {
name: 'core-dev',
script: path.join(currentDir, '..', '..', 'node_modules', 'ts-node', 'dist', 'bin.js'),
args: '--files --project tsconfig.json src/index.ts',
script: 'src/index.ts',
interpreter: 'node',
interpreter_args: '--import tsx',
watch: false,
autorestart: true,
env: {
Expand All @@ -20,8 +21,9 @@ module.exports = {
error_file: '../../logs/raw/core-dev.stderr.log'
} : {
name: 'core',
script: path.join(currentDir, 'dist', 'apps', 'core', 'src', 'index.js'),
node_args: "-r module-alias/register",
script: 'src/index.ts',
interpreter: 'node',
interpreter_args: '--import tsx',
watch: false,
autorestart: true,
env: {
Expand Down
26 changes: 4 additions & 22 deletions apps/core/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@

import path from 'path'
const moduleAlias = require('module-alias')

const resolveNodeModule = (moduleName) => {
try {
return require.resolve(moduleName);
} catch (e) {
// Handle the error if the module is not found
console.error(`Module ${moduleName} not found`);
return null;
}
};

const moduleConfig = {
"app": path.resolve(__dirname, "../../../packages/app"),
"protolib": path.join(resolveNodeModule("protolib"), "..")
}

moduleAlias.addAliases(moduleConfig);

import dotenv from 'dotenv'
import { setConfig, getConfig, getLogger } from 'protobase';
import { getBaseConfig, getConfigWithoutSecrets } from '@my/config'
import { pathToFileURL } from 'url';

// get config vars
dotenv.config({ path: '../../.env' });
global.defaultRoute = '/api/core/v1'
Expand Down Expand Up @@ -73,7 +54,8 @@ export const startCore = (ready?) => {
}

try {
import('app/bundles/coreApis').then((BundleAPI) => {

import(pathToFileURL(require.resolve('app/bundles/coreApis')).href).then((BundleAPI) => {
BundleAPI.default(app, { mqtt, topicSub, topicPub, ...BundleContext })
})

Expand Down
5 changes: 3 additions & 2 deletions apps/core/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"compilerOptions": {
"target": "ES2019", // Define ECMAScript target version
"module": "commonjs", // Specify module code generation: 'commonjs' for Node.js apps
"module": "nodenext", // Specify module code generation: 'commonjs' for Node.js apps
"moduleResolution": "nodenext", // Resolve modules using Node.js-style module resolution
"strict": false, // Enable all strict type-checking options
"esModuleInterop": true, // Allows default imports from modules with no default export
"skipLibCheck": true, // Skip type checking of declaration files
Expand All @@ -23,7 +24,7 @@

"paths": {
"protolib/*": ["protolib/dist/*"],
"app/*": ["./app/*"]
"app/*": ["app/*"]
}
},
"include": [
Expand Down
Loading

0 comments on commit 02b5700

Please sign in to comment.