diff --git a/src/main/generate.mjs b/src/main/generate.mjs index 00491d7a..3dfa5608 100644 --- a/src/main/generate.mjs +++ b/src/main/generate.mjs @@ -1,15 +1,18 @@ -import { stat } from 'node:fs/promises' +import { stat, mkdir } from 'node:fs/promises' +import { join, resolve } from 'node:path' import { getPkgManager } from './lib/get-package-manager.mjs' import { importOrLocal } from './lib/import-or-local.mjs' import errors from './errors.mjs' import split from 'split2' -export const prepareFolder = async (path, tempNames, logger) => { +export const prepareFolder = async (path, tempNames, logger, appName = 'appName') => { const s = await stat(path) if (!s.isDirectory()) { logger.error({ path }, `Path ${path} is not a directory`) throw new Error(`Path ${path} is not a directory`) } + const newFolder = join(resolve(path), appName) + await mkdir(newFolder) const pkgManager = await getPkgManager() const templateVariables = {} @@ -17,7 +20,7 @@ export const prepareFolder = async (path, tempNames, logger) => { for (const name of tempNames) { const template = await importOrLocal({ pkgManager, - projectDir: path, + projectDir: newFolder, pkg: name, logger }) @@ -49,8 +52,9 @@ export const prepareFolder = async (path, tempNames, logger) => { // } // ] // ] -export const createApp = async (projectDir, { projectName, services, entrypoint, port, logLevel, typescript, createGitHubRepository, installGitHubAction }, logger) => { +export const createApp = async (dir, { projectName, services, entrypoint, port, logLevel, typescript, createGitHubRepository, installGitHubAction }, logger) => { const { execa } = await import('execa') + const projectDir = join(dir, projectName) if (!services || services.length === 0) { logger.error('No services to create') diff --git a/test/main/prepare-folder.test.mjs b/test/main/prepare-folder.test.mjs index be93e198..6c761f94 100644 --- a/test/main/prepare-folder.test.mjs +++ b/test/main/prepare-folder.test.mjs @@ -25,13 +25,13 @@ beforeEach(() => { test('Installs no templates or plugins', async () => { const appDir = await mkdtemp(join(tmpdir(), 'plat-app-test-create')) - await prepareFolder(appDir, [], [], logger) + await prepareFolder(appDir, [], logger, 'test-app') expect(logger.errors.length).toBe(0) }) test('Install in non-existent folder', async () => { try { - await prepareFolder('testnonexitent', [], logger) + await prepareFolder('testnonexitent', [], logger, 'test-app') throw new Error('Should have thrown an error') } catch (err) { expect(err).toBeInstanceOf(Error) @@ -53,7 +53,7 @@ test('Install a non-existent template', async () => { test('Install one @platformatic/service template', async () => { const appDir = await mkdtemp(join(tmpdir(), 'plat-app-test-create')) - await prepareFolder(appDir, ['@platformatic/service'], logger) + await prepareFolder(appDir, ['@platformatic/service'], logger, 'test-app') expect(logger.infos[0][0].name).toEqual('@platformatic/service') expect(logger.errors.length).toBe(0) }, 30000)