Skip to content

Commit

Permalink
Simplify package installation for the create command
Browse files Browse the repository at this point in the history
  • Loading branch information
cezaraugusto committed Sep 4, 2024
1 parent fce84ab commit 9cc4728
Showing 1 changed file with 13 additions and 48 deletions.
61 changes: 13 additions & 48 deletions programs/create/steps/import-external-template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,72 +13,37 @@ import * as messages from '../lib/messages'
export async function importExternalTemplate(
projectPath: string,
projectName: string,
template: string
templateName: string
) {
const installationPath = path.dirname(projectPath)
const templateName = path.basename(template)
const examplesUrl =
'https://github.com/extension-js/extension.js/tree/main/examples'
const templateUrl = `${examplesUrl}/${template}`
const templateUrl = `${examplesUrl}/${templateName}`

try {
// Ensure the project path exists
await fs.mkdir(projectPath, {recursive: true})

let templatePath = ''

// Pull the template folder or copy locally depending on the environment
if (process.env.EXTENSION_ENV === 'development') {
console.log(messages.installingFromTemplate(projectName, template))

templatePath = path.join(projectPath, templateName)

await fs.cp(
path.join(__dirname, '..', '..', '..', 'examples', templateName),
templatePath,
{recursive: true}
console.log(messages.installingFromTemplate(projectName, templateName))
const localTemplatePath = path.join(
__dirname,
'..',
'..',
'..',
'examples',
templateName
)
await fs.cp(localTemplatePath, projectPath, {recursive: true})
} else {
await goGitIt(
templateUrl,
installationPath,
messages.installingFromTemplate(projectName, templateName)
)

templatePath = path.join(installationPath, templateName)
}

if (projectName !== templateName) {
// Instead of renaming, copy the contents and then remove the original folder
const destPath = path.join(installationPath, projectName)

// Copy the contents from templatePath to destPath
await fs.mkdir(destPath, {recursive: true})
const files = await fs.readdir(templatePath)
for (const file of files) {
await fs.rename(
path.join(templatePath, file),
path.join(destPath, file)
)
}

// Remove the original templatePath folder
await fs.rm(templatePath, {recursive: true, force: true})
} else {
// Handle the templatePath/templateName situation
const tempPath = path.join(installationPath, projectName + '-temp')
await fs.rename(templatePath, tempPath)

// Move the contents of the tempPath/templateName to projectName
const srcPath = path.join(tempPath, templateName)
const destPath = path.join(installationPath, projectName)
await fs.mkdir(destPath, {recursive: true})
const files = await fs.readdir(srcPath)
for (const file of files) {
await fs.rename(path.join(srcPath, file), path.join(destPath, file))
}

// Remove the temporary directory
await fs.rm(tempPath, {recursive: true, force: true})
await fs.rename(path.join(installationPath, templateName), projectPath)
}
} catch (error: any) {
console.error(
Expand Down

0 comments on commit 9cc4728

Please sign in to comment.