Skip to content

Commit

Permalink
馃悰 (forge) Fix CLI auth gen
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Apr 26, 2024
1 parent 40a10c7 commit 72a5f4a
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions packages/forge/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import * as p from '@clack/prompts'
import { spinner } from '@clack/prompts'
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs'
import {
existsSync,
mkdirSync,
readFileSync,
readdirSync,
writeFileSync,
} from 'fs'
import { join } from 'path'
import prettier from 'prettier'
import { spawn } from 'child_process'
Expand Down Expand Up @@ -70,14 +76,14 @@ const main = async () => {
const s = spinner()
s.start('Creating files...')
const newBlockPath = join(process.cwd(), `../blocks/${prompt.camelCaseId}`)
if (existsSync(newBlockPath)) {
if (existsSync(newBlockPath) && readdirSync(newBlockPath).length > 1) {
s.stop('Creating files...')
p.log.error(
`An integration with the ID "${prompt.id}" already exists. Please choose a different ID.`
)
process.exit(1)
}
mkdirSync(newBlockPath)
if (!existsSync(newBlockPath)) mkdirSync(newBlockPath)
await createPackageJson(newBlockPath, prompt)
await createTsConfig(newBlockPath)
await createIndexFile(newBlockPath, prompt)
Expand Down Expand Up @@ -247,16 +253,20 @@ const createAuthFile = async (
)

const addBlockToRepository = async ({
auth,
camelCaseId,
id,
}: {
auth: string
camelCaseId: string
id: string
}) => {
const schemasPath = await addBlockToRepoPackageJson(id)
await addBlockToRepoDefinitions(schemasPath, camelCaseId, id)
await addBlockToRepoConstants(schemasPath, id)
await addBlockToRepoSchemas(schemasPath, camelCaseId, id)
if (auth !== 'none')
await addBlockToCredentialsRepoSchemas(schemasPath, camelCaseId, id)
}

const createSchemasFile = async (
Expand Down Expand Up @@ -351,6 +361,42 @@ ${existingDefinitionsData.slice(newObjectEntryIndex)}`
)
}

async function addBlockToCredentialsRepoSchemas(
schemasPath: string,
camelCaseId: string,
id: string
) {
const existingSchemasData = readFileSync(
join(schemasPath, 'credentials.ts')
).toString()
const importStatement = `import { ${camelCaseId}Block } from '@typebot.io/${id}-block'
import { ${camelCaseId}CredentialsSchema } from '@typebot.io/${id}-block/schemas'`
const objectEntry = ` [${camelCaseId}Block.id]: ${camelCaseId}CredentialsSchema,`
const nextLineImportIndex = existingSchemasData.indexOf(
'\n',
existingSchemasData.lastIndexOf('import')
)

const newObjectEntryIndex = existingSchemasData.lastIndexOf(',') + 1

const newDefinitionsData = `${existingSchemasData.slice(
0,
nextLineImportIndex
)}
${importStatement}
${existingSchemasData.slice(nextLineImportIndex, newObjectEntryIndex)}
${objectEntry}
${existingSchemasData.slice(newObjectEntryIndex)}`

writeFileSync(
join(schemasPath, 'credentials.ts'),
await prettier.format(newDefinitionsData, {
parser: 'typescript',
...prettierRc,
})
)
}

async function addBlockToRepoConstants(schemasPath: string, id: string) {
const existingDefinitionsData = readFileSync(
join(schemasPath, 'constants.ts')
Expand Down

0 comments on commit 72a5f4a

Please sign in to comment.