diff --git a/packages/cli/src/lib/dlFunctionTemplate.ts b/packages/cli/src/lib/dlFunctionTemplate.ts index ae64bb36..6f0ed1fa 100644 --- a/packages/cli/src/lib/dlFunctionTemplate.ts +++ b/packages/cli/src/lib/dlFunctionTemplate.ts @@ -2,16 +2,18 @@ import { TemplateType } from '@/config/config' import { TEMPLATE_VERSION } from '@/config/templateVersion' import { execAsync } from '@skeet-framework/utils' import chalk from 'chalk' +import {downloadTemplate} from '@/lib/files/downloadFiles' export const dlFunctionTemplate = async (functionName: string) => { const version = TEMPLATE_VERSION.BASE_FUNCTIONS const template: TemplateType = 'base-functions' - const BASE_TEMP = `https://registry.npmjs.org/@skeet-framework/${template}/-/${template}-${version}.tgz` - const fileName = `${template}-${version}.tgz` - const cmd = `wget ${BASE_TEMP}` - const functionPath = `functions/${functionName}-func` console.log(chalk.blue('🕰️ Downloading function template...')) - await execAsync(cmd) + if (!(await downloadTemplate(template, version))) { + console.log(chalk.yellow(`⚠️ Fail when download template`)) + return false + } + const functionPath = `functions/${functionName}-func` + const fileName = `${template}-${version}.tgz` await execAsync(`tar -xvzf ${fileName} -C ${functionPath}`) await execAsync( `find ${functionPath}/package -mindepth 1 -maxdepth 1 -exec mv {} ${functionPath}/ \\;`, diff --git a/packages/cli/src/lib/dlSQLTemplate.ts b/packages/cli/src/lib/dlSQLTemplate.ts index 8df62c01..104dc1db 100644 --- a/packages/cli/src/lib/dlSQLTemplate.ts +++ b/packages/cli/src/lib/dlSQLTemplate.ts @@ -1,16 +1,20 @@ import { TEMPLATE_VERSION } from '@/config/templateVersion' import { execAsync } from '@skeet-framework/utils' import chalk from 'chalk' +import {downloadTemplate} from '@/lib/files/downloadFiles' export const dlSQLTemplate = async (sqlName: string) => { const version = TEMPLATE_VERSION.BASE_SQL const template = 'base-sql' - const BASE_TEMP = `https://registry.npmjs.org/@skeet-framework/${template}/-/${template}-${version}.tgz` + + console.log(chalk.blue('🕰️ Downloading SQL Template...')) + if (await downloadTemplate(template, version)) { + console.log(chalk.yellow(`⚠️ Fail when download template`)) + return false + } + const fileName = `${template}-${version}.tgz` - const cmd = `wget ${BASE_TEMP}` const fileDir = `sql/${sqlName}` - console.log(chalk.blue('🕰️ Downloading SQL Template...')) - await execAsync(cmd) await execAsync(`tar -xvzf ${fileName} -C ${fileDir}`) await execAsync( `find ${fileDir}/package -mindepth 1 -maxdepth 1 -exec mv {} ${fileDir}/ \\;`, diff --git a/packages/cli/src/lib/dlSkeetFunctionTemplate.ts b/packages/cli/src/lib/dlSkeetFunctionTemplate.ts index 96d3364c..a505edb9 100644 --- a/packages/cli/src/lib/dlSkeetFunctionTemplate.ts +++ b/packages/cli/src/lib/dlSkeetFunctionTemplate.ts @@ -3,19 +3,23 @@ import { TEMPLATE_VERSION } from '@/config/templateVersion' import { execAsync, existsAsync } from '@skeet-framework/utils' import chalk from 'chalk' import { mkdir } from 'fs/promises' +import { downloadTemplate } from '@/lib/files/downloadFiles' export const dlSkeetFunctionTemplate = async (appName: string) => { const version = TEMPLATE_VERSION.SKEET_FUNC const template: TemplateType = 'skeet-func-template' - const BASE_TEMP = `https://registry.npmjs.org/@skeet-framework/${template}/-/${template}-${version}.tgz` - const fileName = `${template}-${version}.tgz` - const cmd = `wget ${BASE_TEMP}` - await execAsync(cmd) + + if (!(await downloadTemplate(template, version))) { + console.log(chalk.yellow(`⚠️ Fail when download template`)) + return false + } + if (await existsAsync(appName)) { console.log(chalk.yellow('⚠️ Folder already exists')) return false } await mkdir(appName) + const fileName = `${template}-${version}.tgz` await execAsync(`tar -xvzf ${fileName} -C ${appName}`) await execAsync( `find ${appName}/package -mindepth 1 -maxdepth 1 -exec mv {} ${appName}/ \\;`, diff --git a/packages/cli/src/lib/files/downloadFiles.ts b/packages/cli/src/lib/files/downloadFiles.ts new file mode 100644 index 00000000..15c11533 --- /dev/null +++ b/packages/cli/src/lib/files/downloadFiles.ts @@ -0,0 +1,16 @@ +import {execAsync} from '@skeet-framework/utils' + +export const downloadTemplate = async ( + template: string, + version: string, +): Promise => { + const fileName = `${template}-${version}.tgz` + const fileUrl = `https://registry.npmjs.org/@skeet-framework/${template}/-/${fileName}` + const cmd = `curl ${fileUrl} -o ${fileName} --fail --silent --show-error ` + const downloadTemplate = await execAsync(cmd) + if (downloadTemplate.stderr) { + console.error('Error downloading file:', downloadTemplate.stderr) + return false + } + return true +}