Skip to content

Commit

Permalink
replace replicate with DALL-E for icon generation
Browse files Browse the repository at this point in the history
  • Loading branch information
xavimondev committed Aug 9, 2024
1 parent fe32861 commit e6e48a0
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 245 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ npx seo-ai config clear
## Available Tags
| Command | Description |
|-------------------|------------------------------------------------------------------------------------ |
|-------------------|------------------------------------------------------------------------------------ |
| `core` | Generate meta tags: `title`, `description`, `keywords`, `openGraph`, `twitter`, `robots`, `category`, `generator`,`applicationName` |
| `icons` | Define icon meta tags. You'll be prompted for a Replicate Token. You can find it here: [Replicate](https://replicate.com/account/api-tokens) |
| `icons` | Define icon meta tags. It uses DALL-E 2, so you'll be prompted for a OpenAI Key |
| `metadataBase` | Set the base URL for metadata relative paths. Available only for Next.js projects |
| `authors` | List authors of the content |
| `creator` | Identify the creator of the content |
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"chalk": "5.3.0",
"commander": "12.1.0",
"conf": "13.0.1",
"replicate": "0.31.1",
"sharp": "0.33.4",
"sharp-ico": "0.1.5",
"zod": "3.23.8"
Expand Down
88 changes: 0 additions & 88 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 47 additions & 26 deletions src/commands/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export const generate = new Command()
.action(async (tags, opts) => {
const options = generateSchema.parse({ tags, ...opts })
let seoTags = options.tags ?? []
let isMetadata = isNextjsProject({ html: opts.html })
let { isNextAppDirectory: isMetadata, appDirectory } = getNextAppDirectory({ html: opts.html })

const lastProvider = Object.keys(getConf()).at(-1)
const lastProvider = Object.keys(getConf()).at(-1) as Providers | undefined
if (!lastProvider) {
logger.info(`You need to configure your provider first. Run:`)
logger.success(`npx seo-ai config set YOUR_AI_PROVIDER=YOUR_API_KEY`)
Expand Down Expand Up @@ -67,10 +67,10 @@ export const generate = new Command()
// AI
let PROJECT_OVERVIEW = ''
let model: LanguageModel | undefined = undefined
let replicateKey: string | symbol = ''
let openaiIconKey: string | symbol = ''

if (seoTags.includes('icons') || seoTags.includes('core')) {
const apiKey = getKey({ provider: lastProvider as Providers }) as string
const apiKey = getKey({ provider: lastProvider }) as string
model = await getAIProvider({ lastProvider, apiKey })
if (!model) {
logger.error('Invalid provider')
Expand Down Expand Up @@ -103,16 +103,16 @@ export const generate = new Command()

// Filtering files that are not in the ignore list
if (gitTree !== '') {
if (seoTags.includes('icons')) {
replicateKey = await text({
message: 'For generating icons, enter your Replicate API Key',
placeholder: 'r5_0398114l4312165232',
if (seoTags.includes('icons') && lastProvider !== 'openai') {
openaiIconKey = await text({
message: 'Using DALL-E for icons, please enter your OpenAI API Key',
placeholder: 'sk-proj-82mlo09s',
validate(value) {
const descriptionLength = value.trim().length
if (descriptionLength === 0) return `Replicate API Key is required!`
if (descriptionLength === 0) return `OpenAI API Key is required!`
}
})
if (isCancel(replicateKey)) {
if (isCancel(openaiIconKey)) {
cancel('Operation cancelled.')
process.exit(0)
}
Expand Down Expand Up @@ -156,21 +156,26 @@ export const generate = new Command()
HTML_METATAGS = await generateHTMLTags({ projectSummary: PROJECT_OVERVIEW, model })
}
} else if (seoTag === 'icons' && model) {
const keyForIcons = openaiIconKey || (getKey({ provider: 'openai' }) as string)
const icons = await generateIcons({
projectSummary: PROJECT_OVERVIEW,
metadata: isMetadata,
isMetadata,
model,
apiKey: replicateKey
apiKey: keyForIcons,
appDirectory
})

if (isMetadata && icons) {
SEO_METADATA = {
...SEO_METADATA,
icons: icons as Icon
}
} else {
HTML_METATAGS = HTML_METATAGS.concat(icons as string)
}
if (!icons) continue

HTML_METATAGS = HTML_METATAGS.concat(icons as string)
// if (isMetadata && icons) {
// SEO_METADATA = {
// ...SEO_METADATA,
// icons: icons as Icon
// }
// } else {
// HTML_METATAGS = HTML_METATAGS.concat(icons as string)
// }
} else {
if (isMetadata) {
const getTagContent = SEO_GENERATOR[seoTag]
Expand Down Expand Up @@ -217,15 +222,31 @@ export const generate = new Command()
})

// checking if it's a nextjs project, so let's build a metadata object
const isNextjsProject = ({ html }: { html: boolean }) => {
const getNextAppDirectory = ({ html }: { html: boolean }) => {
const pwd = path.resolve(process.cwd())

const nextMjsFile = path.join(pwd, 'next.config.mjs')
const nextJsFile = path.join(pwd, 'next.config.js')
// next-canary
const nextTsFile = path.join(pwd, 'next.config.ts')
const isNextjs =
(existsSync(nextMjsFile) || existsSync(nextJsFile) || existsSync(nextTsFile)) && !html
return isNextjs
const nextTsFile = path.join(pwd, 'next.config.ts') // next-canary
const isNextProject = existsSync(nextMjsFile) || existsSync(nextJsFile) || existsSync(nextTsFile)

const appPaths = [path.join('app'), path.join('src/app')]
let appDirectory = ''
for (const path of appPaths) {
if (existsSync(path)) {
appDirectory = path
break
}
}

const existsDirectory = appDirectory !== ''

const isNextAppDirectory = isNextProject && existsDirectory && !html

return {
isNextAppDirectory,
appDirectory
}
}

const getAIProvider = async ({
Expand Down
Loading

0 comments on commit e6e48a0

Please sign in to comment.