diff --git a/src/main/lib/applications.mjs b/src/main/lib/applications.mjs index 13749d72..5244a63d 100644 --- a/src/main/lib/applications.mjs +++ b/src/main/lib/applications.mjs @@ -5,7 +5,7 @@ import { createRequire } from 'node:module' import { npmInstall } from './run-npm.mjs' import { upgradePlt } from './upgrade-plt.mjs' import { getLatestPlatformaticVersion, findExecutable } from './utils.mjs' -import { resolve, join } from 'node:path' +import { resolve, join, basename } from 'node:path' import getSqlMapper from './db.mjs' import { inspectApp } from './inspect-app.mjs' import split from 'split2' @@ -36,7 +36,8 @@ class Applications { for (const runtime of runningRuntimes) { let app = apps.find((app) => app.path === runtime.projectDir) if (!app) { - app = await this.createApplication(runtime.packageName, runtime.projectDir, true) + const name = runtime.packageName || basename(runtime.projectDir) + app = await this.createApplication(name, runtime.projectDir, true) } } } @@ -210,11 +211,11 @@ class Applications { } const packageJson = require(packageJsonPath) let { name } = packageJson - // This is a workaround for a plt bug that sets the name to "undefined" - // TODO: remove - if (name && name === 'undefined') { - name = folderName + // This is a workaround for a plt bug that sets the name to "undefined" as string + if (!name || name === 'undefined') { + name = folderName || basename(path) } + const app = this.createApplication(name || folderName, path) await this.#refreshApplications() return app diff --git a/test/fixtures/runtime-noname/.env b/test/fixtures/runtime-noname/.env new file mode 100644 index 00000000..5f385071 --- /dev/null +++ b/test/fixtures/runtime-noname/.env @@ -0,0 +1,5 @@ +PLT_SERVER_HOSTNAME=127.0.0.1 +PORT=3042 +PLT_SERVER_LOGGER_LEVEL=info +PLT_SERVICE_1_CONFIG_PARAM=true +PLT_SERVICE_2_CONFIG_PARAM=false diff --git a/test/fixtures/runtime-noname/package.json b/test/fixtures/runtime-noname/package.json new file mode 100644 index 00000000..e05e0c84 --- /dev/null +++ b/test/fixtures/runtime-noname/package.json @@ -0,0 +1,6 @@ +{ + "version": "1.0.42", + "dependencies": { + "platformatic": "1.30.0" + } +} diff --git a/test/fixtures/runtime-noname/platformatic.json b/test/fixtures/runtime-noname/platformatic.json new file mode 100644 index 00000000..3255964f --- /dev/null +++ b/test/fixtures/runtime-noname/platformatic.json @@ -0,0 +1,17 @@ +{ + "$schema": "https://platformatic.dev/schemas/v1.22.0/runtime", + "entrypoint": "service-1", + "allowCycles": true, + "hotReload": false, + "autoload": { + "path": "./services" + }, + "server": { + "hostname": "{PLT_SERVER_HOSTNAME}", + "port": "{PORT}", + "logger": { + "level": "{PLT_SERVER_LOGGER_LEVEL}" + } + }, + "managementApi": true +} diff --git a/test/fixtures/runtime-noname/services/service-1/platformatic.json b/test/fixtures/runtime-noname/services/service-1/platformatic.json new file mode 100644 index 00000000..bb16c9d5 --- /dev/null +++ b/test/fixtures/runtime-noname/services/service-1/platformatic.json @@ -0,0 +1,15 @@ +{ + "$schema": "https://platformatic.dev/schemas/v1.22.0/service", + "service": { + "openapi": true + }, + "plugins": { + "paths": [ + "plugin.js" + ] + }, + "watch": true, + "metrics": { + "server": "parent" + } +} diff --git a/test/fixtures/runtime-noname/services/service-1/plugin.js b/test/fixtures/runtime-noname/services/service-1/plugin.js new file mode 100644 index 00000000..a613e5bc --- /dev/null +++ b/test/fixtures/runtime-noname/services/service-1/plugin.js @@ -0,0 +1,21 @@ +'use strict' + +/** @param {import('fastify').FastifyInstance} app */ +module.exports = async function (app) { + app.log.trace('trace log') + app.log.debug('debug log') + app.log.info('info log') + app.log.warn('warn log') + app.log.error('error log') + app.log.fatal('fatal log') + app.log.fatal('logs finished') + + app.get('/hello', async () => { + return { runtime: 'runtime-1', service: 'service-1' } + }) + + app.post('/mirror', async (req, reply) => { + reply.headers(req.headers) + return req.body + }) +} diff --git a/test/fixtures/runtime-noname/services/service-2/platformatic.json b/test/fixtures/runtime-noname/services/service-2/platformatic.json new file mode 100644 index 00000000..2beb4e04 --- /dev/null +++ b/test/fixtures/runtime-noname/services/service-2/platformatic.json @@ -0,0 +1,9 @@ +{ + "$schema": "https://platformatic.dev/schemas/v1.22.0/service", + "service": { + "openapi": true + }, + "plugins": { + "paths": ["plugin.js"] + } +} diff --git a/test/fixtures/runtime-noname/services/service-2/plugin.js b/test/fixtures/runtime-noname/services/service-2/plugin.js new file mode 100644 index 00000000..c7e26a44 --- /dev/null +++ b/test/fixtures/runtime-noname/services/service-2/plugin.js @@ -0,0 +1,16 @@ +'use strict' + +/** @param {import('fastify').FastifyInstance} app */ +module.exports = async function (app, options) { + app.log.trace('trace log') + app.log.debug('debug log') + app.log.info('info log') + app.log.warn('warn log') + app.log.error('error log') + app.log.fatal('fatal log') + app.log.fatal('logs finished') + + app.get('/hello', async () => { + return { runtime: 'runtime-1', service: 'service-2' } + }) +} diff --git a/test/main/applications.test.mjs b/test/main/applications.test.mjs index 4ee73c0b..2dcbef0e 100644 --- a/test/main/applications.test.mjs +++ b/test/main/applications.test.mjs @@ -188,6 +188,33 @@ test('throws an error if application doesn\'t start', async (t) => { expect(applicationsApi.startRuntime(id)).rejects.toThrowError(/The runtime exited before the operation completed/) }, 60000) +test('import and start a runtime with no name', async (t) => { + const appDir = await mkdtemp(join(tmpdir(), 'plat-app-test')) + onTestFinished(() => rm(appDir, { recursive: true, force: true })) + const appFixture = join('test', 'fixtures', 'runtime-noname') + await cp(appFixture, appDir, { recursive: true }) + mockAgent + .get('https://registry.npmjs.org') + .intercept({ + method: 'GET', + path: '/platformatic' + }) + .reply(200, { + 'dist-tags': { + latest: '2.0.0' + } + }) + + const applicationsApi = await Applications.create() + const { id } = await applicationsApi.importApplication(appDir) + const { runtime } = await applicationsApi.startRuntime(id) + + const pid = applicationsApi.getPid(id) + expect(pid).toBe(runtime.pid) + + onTestFinished(() => runtime.kill('SIGINT')) +}, 60000) + test('import automatically a running runtime, started externally', async (t) => { const appDir = await mkdtemp(join(tmpdir(), 'plat-app-test')) onTestFinished(() => rm(appDir, { recursive: true, force: true })) @@ -221,6 +248,39 @@ test('import automatically a running runtime, started externally', async (t) => expect(pid).toBe(runtime.pid) }, 60000) +test('import automatically a running runtime with no name, defaultin the name to folder name', async (t) => { + const appDir = await mkdtemp(join(tmpdir(), 'plat-app-test')) + onTestFinished(() => rm(appDir, { recursive: true, force: true })) + const appFixture = join('test', 'fixtures', 'runtime-noname') + await cp(appFixture, appDir, { recursive: true }) + mockAgent + .get('https://registry.npmjs.org') + .intercept({ + method: 'GET', + path: '/platformatic' + }) + .reply(200, { + 'dist-tags': { + latest: '2.0.0' + } + }) + + const applicationsApi = await Applications.create() + + // We start the runtime but not through the API + const { runtime } = await startRuntimeInFolder(appDir) + onTestFinished(() => runtime.kill('SIGINT')) + + const applications = await applicationsApi.getApplications() + expect(applications.length).toBe(1) + expect(applications[0].running).toBe(true) + expect(applications[0].insideMeraki).toBe(false) + expect(applications[0].automaticallyImported).toBe(true) + + const pid = applicationsApi.getPid(applications[0].id) + expect(pid).toBe(runtime.pid) +}, 60000) + test('open application', async (t) => { const appDir = await mkdtemp(join(tmpdir(), 'plat-app-test')) onTestFinished(() => rm(appDir, { recursive: true, force: true }))