From b76c3714802c2c7a7c285e60d605d030eaee3e06 Mon Sep 17 00:00:00 2001 From: Nader Dabit Date: Wed, 17 Jan 2024 20:51:54 -0600 Subject: [PATCH] feat: add create app cli (#42) * feat: add cli * chore: removed log * chore: update repo url * chore: updated program name * chore: updated program description * chore: removed yarn lock files --- LICENSE | 2 +- libs/cli/.eslintrc.json | 18 +++++ libs/cli/README.md | 7 ++ libs/cli/jest.config.ts | 11 +++ libs/cli/package.json | 21 +++++ libs/cli/project.json | 41 ++++++++++ libs/cli/src/index.ts | 148 ++++++++++++++++++++++++++++++++++++ libs/cli/tsconfig.json | 13 ++++ libs/cli/tsconfig.lib.json | 22 ++++++ libs/cli/tsconfig.spec.json | 14 ++++ package.json | 4 + tsconfig.base.json | 1 + 12 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 libs/cli/.eslintrc.json create mode 100644 libs/cli/README.md create mode 100644 libs/cli/jest.config.ts create mode 100644 libs/cli/package.json create mode 100644 libs/cli/project.json create mode 100644 libs/cli/src/index.ts create mode 100644 libs/cli/tsconfig.json create mode 100644 libs/cli/tsconfig.lib.json create mode 100644 libs/cli/tsconfig.spec.json diff --git a/LICENSE b/LICENSE index 5d8adac..972348c 100644 --- a/LICENSE +++ b/LICENSE @@ -1,4 +1,4 @@ -Copyright 2023 https://fal.ai +Copyright 2024 https://fal.ai Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/libs/cli/.eslintrc.json b/libs/cli/.eslintrc.json new file mode 100644 index 0000000..9d9c0db --- /dev/null +++ b/libs/cli/.eslintrc.json @@ -0,0 +1,18 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], + "rules": {} + }, + { + "files": ["*.ts", "*.tsx"], + "rules": {} + }, + { + "files": ["*.js", "*.jsx"], + "rules": {} + } + ] +} diff --git a/libs/cli/README.md b/libs/cli/README.md new file mode 100644 index 0000000..72e6632 --- /dev/null +++ b/libs/cli/README.md @@ -0,0 +1,7 @@ +## Fal.ai App Generator + +Generate a new full stack app configured with Fal.ai proxy and models. + +```sh +npx @fal-ai/create +``` diff --git a/libs/cli/jest.config.ts b/libs/cli/jest.config.ts new file mode 100644 index 0000000..2d6a587 --- /dev/null +++ b/libs/cli/jest.config.ts @@ -0,0 +1,11 @@ +/* eslint-disable */ +export default { + displayName: 'cli', + preset: '../../jest.preset.js', + testEnvironment: 'node', + transform: { + '^.+\\.[tj]s$': ['ts-jest', { tsconfig: '/tsconfig.spec.json' }], + }, + moduleFileExtensions: ['ts', 'js', 'html'], + coverageDirectory: '../../coverage/libs/cli', +}; diff --git a/libs/cli/package.json b/libs/cli/package.json new file mode 100644 index 0000000..c779a97 --- /dev/null +++ b/libs/cli/package.json @@ -0,0 +1,21 @@ +{ + "name": "@fal-ai/create", + "version": "0.0.1", + "description": "The fal serverless app generator.", + "main": "index.js", + "keywords": [ + "fal", + "next", + "nextjs", + "express" + ], + "repository": { + "type": "git", + "url": "https://github.com/fal-ai/serverless-js.git", + "directory": "libs/cli" + }, + "type": "module", + "author": "", + "license": "MIT", + "dependencies": {} +} diff --git a/libs/cli/project.json b/libs/cli/project.json new file mode 100644 index 0000000..75e64ce --- /dev/null +++ b/libs/cli/project.json @@ -0,0 +1,41 @@ +{ + "name": "cli", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "sourceRoot": "libs/cli/src", + "projectType": "library", + "targets": { + "build": { + "executor": "@nx/js:tsc", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/libs/cli", + "tsConfig": "libs/cli/tsconfig.lib.json", + "packageJson": "libs/cli/package.json", + "main": "libs/cli/src/index.ts", + "assets": ["LICENSE", "CODE_OF_CONDUCT.md", "libs/cli/README.md"] + } + }, + "lint": { + "executor": "@nx/linter:eslint", + "outputs": ["{options.outputFile}"], + "options": { + "lintFilePatterns": ["libs/cli/**/*.ts"] + } + }, + "test": { + "executor": "@nx/jest:jest", + "outputs": ["{workspaceRoot}/coverage/{projectRoot}"], + "options": { + "jestConfig": "libs/cli/jest.config.ts", + "passWithNoTests": true + }, + "configurations": { + "ci": { + "ci": true, + "codeCoverage": true + } + } + } + }, + "tags": [] +} diff --git a/libs/cli/src/index.ts b/libs/cli/src/index.ts new file mode 100644 index 0000000..4c48c29 --- /dev/null +++ b/libs/cli/src/index.ts @@ -0,0 +1,148 @@ +#!/usr/bin/env node + +import chalk from 'chalk'; +import path from 'path'; +import fs from 'fs'; +import childProcess from 'child_process'; +import ora from 'ora'; +import select from '@inquirer/select'; +import { input } from '@inquirer/prompts'; +import { Command } from 'commander'; +import { execa, execaCommand } from 'execa'; +import open from 'open'; + +const program = new Command(); +const log = console.log; +const repoUrl = 'https://github.com/fal-ai/fal-nextjs-template.git'; +const green = chalk.green; +const purple = chalk.hex('#6e40c9'); + +async function main() { + const spinner = ora({ + text: 'Creating codebase', + }); + try { + const kebabRegez = /^([a-z]+)(-[a-z0-9]+)*$/; + + program + .name('Fal.ai App Generator') + .description('Generate full stack AI apps integrated with Fal.ai.'); + + program.parse(process.argv); + + const args = program.args; + let appName = args[0]; + + if (!appName || !kebabRegez.test(args[0])) { + appName = await input({ + message: 'Enter your app name', + default: 'model-playground', + validate: (d) => { + if (!kebabRegez.test(d)) { + return 'please enter your app name in the format of my-app-name'; + } + return true; + }, + }); + } + + const hasFalEnv = await select({ + message: 'Do you have a Fal.ai API key?', + choices: [ + { + name: 'Yes', + value: true, + }, + { + name: 'No', + value: false, + }, + ], + }); + + if (!hasFalEnv) { + await open('https://www.fal.ai/dashboard'); + } + + const fal_api_key = await input({ message: 'Fal AI API Key' }); + + let envs = ` + # environment, either PRODUCTION or DEVELOPMENT + ENVIRONMENT="PRODUCTION" + + # FAL AI API Key + FAL_KEY="${fal_api_key}" + `; + + log(`\nInitializing project. \n`); + + spinner.start(); + await execa('git', ['clone', repoUrl, appName]); + + let packageJson = fs.readFileSync(`${appName}/package.json`, 'utf8'); + const packageObj = JSON.parse(packageJson); + packageObj.name = appName; + packageJson = JSON.stringify(packageObj, null, 2); + fs.writeFileSync(`${appName}/package.json`, packageJson); + fs.writeFileSync(`${appName}/.env.local`, envs); + + process.chdir(path.join(process.cwd(), appName)); + spinner.text = ''; + let startCommand = ''; + + if (isBunInstalled()) { + spinner.text = 'Installing dependencies'; + await execaCommand('bun install').pipeStdout(process.stdout); + spinner.text = ''; + startCommand = 'bun dev'; + console.log('\n'); + } else if (isYarnInstalled()) { + await execaCommand('yarn').pipeStdout(process.stdout); + startCommand = 'yarn dev'; + } else { + spinner.text = 'Installing dependencies'; + await execa('npm', ['install', '--verbose']).pipeStdout(process.stdout); + spinner.text = ''; + startCommand = 'npm run dev'; + } + + spinner.stop(); + process.chdir('../'); + log( + `${green.bold('Success!')} Created ${purple.bold( + appName + )} at ${process.cwd()} \n` + ); + log( + `To get started, change into the new directory and run ${chalk.cyan( + startCommand + )}\n` + ); + } catch (err) { + log('\n'); + if (err.exitCode == 128) { + log('Error: directory already exists.'); + } + spinner.stop(); + } +} + +main(); + +function isYarnInstalled() { + try { + childProcess.execSync('yarn --version'); + return true; + } catch { + return false; + } +} + +function isBunInstalled() { + try { + childProcess.execSync('bun --version'); + return true; + } catch (err) { + return false; + } +} diff --git a/libs/cli/tsconfig.json b/libs/cli/tsconfig.json new file mode 100644 index 0000000..62ebbd9 --- /dev/null +++ b/libs/cli/tsconfig.json @@ -0,0 +1,13 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.lib.json" + }, + { + "path": "./tsconfig.spec.json" + } + ] +} diff --git a/libs/cli/tsconfig.lib.json b/libs/cli/tsconfig.lib.json new file mode 100644 index 0000000..c89a2cf --- /dev/null +++ b/libs/cli/tsconfig.lib.json @@ -0,0 +1,22 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "module": "ESNext", + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "target": "es6", + "noImplicitAny": true, + "moduleResolution": "node", + "sourceMap": true, + "outDir": "dist", + "baseUrl": ".", + "paths": { + "*": [ + "node_modules/*", + "src/types/*" + ] + } + }, + "exclude": ["jest.config.ts", "src/**/*.spec.ts", "src/**/*.test.ts"], + "include": ["src/**/*.ts"] +} diff --git a/libs/cli/tsconfig.spec.json b/libs/cli/tsconfig.spec.json new file mode 100644 index 0000000..9b2a121 --- /dev/null +++ b/libs/cli/tsconfig.spec.json @@ -0,0 +1,14 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "module": "commonjs", + "types": ["jest", "node"] + }, + "include": [ + "jest.config.ts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.d.ts" + ] +} diff --git a/package.json b/package.json index 62a84f2..4a455b0 100644 --- a/package.json +++ b/package.json @@ -25,10 +25,13 @@ ] }, "dependencies": { + "@inquirer/prompts": "^3.3.0", + "@inquirer/select": "^1.3.1", "@msgpack/msgpack": "^3.0.0-beta2", "@oclif/core": "^2.3.0", "@oclif/plugin-help": "^5.2.5", "axios": "^1.0.0", + "chalk": "^5.3.0", "change-case": "^4.1.2", "chokidar": "^3.5.3", "core-js": "^3.6.5", @@ -36,6 +39,7 @@ "cross-fetch": "^3.1.5", "dotenv": "^16.3.1", "encoding": "^0.1.13", + "execa": "^8.0.1", "express": "^4.18.2", "fast-glob": "^3.2.12", "http-proxy": "^1.18.1", diff --git a/tsconfig.base.json b/tsconfig.base.json index 261ff37..045bcdb 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -15,6 +15,7 @@ "skipDefaultLibCheck": true, "baseUrl": ".", "paths": { + "@fal-ai/cli": ["libs/cli/src/index.ts"], "@fal-ai/serverless-client": ["libs/client/src/index.ts"], "@fal-ai/serverless-proxy": ["libs/proxy/src/index.ts"], "@fal-ai/serverless-proxy/express": ["libs/proxy/src/express.ts"],