Skip to content

Commit

Permalink
automate the process of adding an API token to .env (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
zeke authored Feb 1, 2024
1 parent 48508c2 commit 9497c97
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
template/package-lock.json
my-replicate-app
my-replicate-app
foo
24 changes: 23 additions & 1 deletion index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,18 @@ import path, { dirname } from 'path'
import { execSync } from 'child_process'
import minimist from 'minimist'
import JSON5 from 'json5'
import readline from 'readline'
import { promisify } from 'util'
import open from 'open'

// ESM shenanigans for dealing with local files
import { fileURLToPath } from 'url'
import { getModel, getModelInputs, getModelNameWithVersion } from './lib/models.js'
const readlineSync = await import('readline-sync').then(module => module.default)
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)

const argv = minimist(process.argv.slice(2))

const targetAppName = argv._[0] || 'my-replicate-app'
const modelName = argv.model || 'stability-ai/sdxl'

Expand All @@ -35,6 +38,25 @@ const templateDir = path.join(__dirname, 'template')
const targetDir = path.join(process.cwd(), targetAppName)
fs.cpSync(templateDir, targetDir, { recursive: true })

// Get env from existing REPLICATE_API_TOKEN, or prompt user to get it from the browser
const envFile = path.join(targetDir, '.env')

if (process.env.REPLICATE_API_TOKEN) {
fs.writeFileSync(envFile, `REPLICATE_API_TOKEN=${process.env.REPLICATE_API_TOKEN}`)
console.log('Adding API token to .env file')
} else {
console.log('API token not found in environment.')
const rl = readline.createInterface({ input: process.stdin, output: process.stdout })
const question = promisify(rl.question).bind(rl)
const answer = await question('Open your browser to copy a Replicate API token? (Y/n) ')
if (answer.toLowerCase() === 'y' || answer === '') {
await open('https://replicate.com/account')
const token = readlineSync.question('Paste your API token here: ', { hideEchoBack: true })
fs.writeFileSync(envFile, `REPLICATE_API_TOKEN=${token}`)
console.log('API token written to .env file')
}
}

console.log('Setting package name...')
execSync(`npm pkg set name=${targetAppName}`, { cwd: targetDir, stdio: 'ignore' })

Expand Down
136 changes: 135 additions & 1 deletion package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
"dependencies": {
"all-the-public-replicate-models": "^1.97.0",
"json5": "^2.2.3",
"minimist": "^1.2.8"
"minimist": "^1.2.8",
"open": "^10.0.3",
"readline-sync": "^1.4.10"
}
}
2 changes: 2 additions & 0 deletions template/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
.npmrc
10 changes: 10 additions & 0 deletions template/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import Replicate from 'replicate'
import dotenv from 'dotenv'
dotenv.config()

if (!process.env.REPLICATE_API_TOKEN) {
console.error('REPLICATE_API_TOKEN not found in environment')
console.error('To copy a token, go to https://replicate.com/account')
console.error('Then add your token to a local .env file like this:')
console.error('echo "REPLICATE_API_TOKEN=your-token" > .env')
process.exit()
}

const replicate = new Replicate({ auth: process.env.REPLICATE_API_TOKEN })
const model = '{{MODEL}}'
Expand Down
1 change: 1 addition & 0 deletions template/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"start": "node index.js"
},
"dependencies": {
"dotenv": "^16.4.1",
"replicate": "*"
}
}

0 comments on commit 9497c97

Please sign in to comment.