Skip to content

Commit

Permalink
add options parsing while importing .env from cli
Browse files Browse the repository at this point in the history
  • Loading branch information
muntaxir4 committed Dec 31, 2024
1 parent 854f104 commit f62b41c
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions apps/cli/src/commands/project/import.project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,17 @@ export default class ImportFromEnv extends BaseCommand {

async action({ args, options }: CommandActionData): Promise<void> {
const [projectSlug] = args
const { envFile } = options

try {
const resolvedPath = path.resolve(envFile)
const exists = await fs
.access(resolvedPath)
.then(() => true)
.catch(() => false)
if (!exists) {
throw new Error(`The .env file does not exist at path: ${resolvedPath}`)
}
const envFileContent = await fs.readFile(resolvedPath, 'utf-8')
const { envFilePath } = await this.parseOptions(options)
if (!envFilePath) return
const envFileContent = await fs.readFile(envFilePath, 'utf-8')
// Logger.info('File contents:\n' + envFileContent)

const envVariables = dotenv.parse(envFileContent)
if (Object.keys(envVariables).length === 0) {
throw new Error('No environment variables found in the provided file')
Logger.warn('No environment variables found in the provided file')
return
}

const secretsAndVariables = secretDetector.detectJsObject(envVariables)
Expand Down Expand Up @@ -163,4 +158,25 @@ export default class ImportFromEnv extends BaseCommand {
)
}
}

private async parseOptions(options: CommandActionData['options']): Promise<{
envFilePath: string | undefined
}> {
const { envFile } = options
if (!envFile) {
Logger.error('No .env file path provided.')
return { envFilePath: undefined }
}
const resolvedPath = path.resolve(envFile)
const exists = await fs
.access(resolvedPath)
.then(() => true)
.catch(() => false)
if (!exists) {
Logger.error(`The .env file does not exist at path: ${resolvedPath}`)
return { envFilePath: undefined }
}

return { envFilePath: resolvedPath }
}
}

0 comments on commit f62b41c

Please sign in to comment.