Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uses the app name as folder name for generation #47

Merged
merged 3 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/main/generate.mjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
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 = {}
try {
for (const name of tempNames) {
const template = await importOrLocal({
pkgManager,
projectDir: path,
projectDir: newFolder,
pkg: name,
logger
})
Expand Down Expand Up @@ -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')
Expand Down
4 changes: 2 additions & 2 deletions src/main/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ app.whenReady().then(() => {
return getPlugins()
})

ipcMain.handle('prepare-folder', async (_, path, templates) => {
return prepareFolder(path, templates, logger)
ipcMain.handle('prepare-folder', async (_, path, templates, appName) => {
return prepareFolder(path, templates, logger, appName)
})

ipcMain.handle('create-app', async (_, path, project) => {
Expand Down
2 changes: 1 addition & 1 deletion src/preload/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ if (process.contextIsolated) {
contextBridge.exposeInMainWorld('api', {
getTemplates: () => (ipcRenderer.invoke('get-templates')),
getPlugins: () => (ipcRenderer.invoke('get-plugins')),
prepareFolder: (folder, templates) => (ipcRenderer.invoke('prepare-folder', folder, templates)),
prepareFolder: (folder, templates, appName) => (ipcRenderer.invoke('prepare-folder', folder, templates, appName)),
onLog: callback => ipcRenderer.on('log', callback),
createApp: (folder, project) => (ipcRenderer.invoke('create-app', folder, project)),
quitApp: () => (ipcRenderer.invoke('quit-app'))
Expand Down
4 changes: 2 additions & 2 deletions src/renderer/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export const getApiPlugins = async () => {
return await window.api.getPlugins()
}

export const callPrepareFolder = async (path, templates) => {
return await window.api.prepareFolder(path, templates)
export const callPrepareFolder = async (path, templates, application) => {
return await window.api.prepareFolder(path, templates, application)
}
export const callCreateApp = async (path, project) => {
return await window.api.createApp(path, project)
Expand Down
2 changes: 1 addition & 1 deletion src/renderer/src/components/steps/PrepareFolder.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const PrepareFolder = React.forwardRef(({ onNext }, ref) => {
async function prepareFolder () {
try {
setCountDownStatus(RUNNING)
const response = await callPrepareFolder(formData.createApplication.path, templateNames)
const response = await callPrepareFolder(formData.createApplication.path, templateNames, formData.createApplication.application)
let tmpTemplate
let envVars
services.forEach(service => {
Expand Down
6 changes: 3 additions & 3 deletions test/main/prepare-folder.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)