Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): rewrite to effect-ts #40

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dependencies": {
"arg": "5.0.1",
"dotenv": "16.0.3",
"effect": "2.0.3",
"esbuild": "0.14.18",
"postgres": "3.3.3",
"tasuku": "2.0.1",
Expand All @@ -22,9 +23,9 @@
"@trivago/prettier-plugin-sort-imports": "3.2.0",
"@turf/distance": "6.5.0",
"@turf/helpers": "6.5.0",
"@types/bun": "1.0.1",
"@types/dotenv": "8.2.0",
"@types/node": "18.11.7",
"bun-types": "1.0.1",
"mathjs": "11.6.0",
"object-hash": "3.0.0",
"prettier": "2.5.1",
Expand Down
244 changes: 167 additions & 77 deletions src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import dotenv from 'dotenv'
import { Effect } from 'effect'
import fs from 'fs'
import path from 'path'
import task from 'tasuku'
import { Database } from 'src/helpers/Database.js'
import { DatabaseLayer } from 'src/interfaces/Database.js'
import { tasukuTask } from 'src/lib/tasukuEffect.js'

import { Database } from '../helpers/Database.js'
import { ParseCLI } from '../helpers/ParseCLI.js'
import { Config } from '../helpers/ParseCLI.js'

dotenv.config()

Expand All @@ -14,93 +16,181 @@ function getFunctionNameFromFilePath(filePath: string) {
return fileName
}

export async function deployCommand(
CLI: ReturnType<typeof ParseCLI.getCommand>
) {
const outputFolderPath = CLI.config.outputFolderPath

const checkOutputFolderTask = await task(
`Check if the --output-folder (${outputFolderPath}) exists`,
async ({ setError }) => {
if (!fs.statSync(outputFolderPath)) {
const errorMessage = `${outputFolderPath} doesn't exist`
setError(errorMessage)
function checkOutputFolderTaskEffectFn() {
return Config.pipe(
Effect.map((config) => {
const commandConfig = config.getCommand()
const { outputFolderPath } = commandConfig.config
return outputFolderPath
}),
Effect.flatMap((outputFolderPath) => {
const outputFolderExistsTask = tasukuTask(
`Check if the --output-folder (${outputFolderPath}) exists`,
async ({ setError }) => {
if (!fs.statSync(outputFolderPath)) {
const errorMessage = `${outputFolderPath} doesn't exist`
setError(errorMessage)
}
}
)
return outputFolderExistsTask
}),
Effect.mapError((e) => {
class OutputFolderDoesNotExistError extends Error {
readonly _tag = 'OutputFolderDoesNotExistError'
}
}
return new OutputFolderDoesNotExistError(`${e}`)
})
)
if (checkOutputFolderTask.state === 'error') {
ParseCLI.throwError()
}
}

// TODO: move process/env stuff to a separate file
const databaseUrl = process.env.DATABASE_URL
class DatabaseUrlNotSetError extends Error {
readonly _tag = 'DatabaseUrlNotSetError'
readonly message = `DATABASE_URL not set in environment`

const databaseUrlIsSetTask = await task(
'Check if the DATABASE_URL env var is set',
async ({ setError }) => {
if (!databaseUrl) {
const errorMessage = `DATABASE_URL not set in environment`
setError(errorMessage)
}
}
)
if (databaseUrlIsSetTask.state === 'error') {
ParseCLI.throwError()
public static getMessage() {
return new DatabaseUrlNotSetError().message
}

const database = new Database(databaseUrl)
const isDatabaseReachableTask = await task(
'Check if the provided DATABASE_URL is reachable',
async ({ setError }) => {
const isReachable = await database.isDatabaseReachable()
if (!isReachable) {
const errorMessage = `Provided DATABASE_URL: ${databaseUrl} is not reachable`
setError(errorMessage)
}
}
}
function checkDatabaseUrlIsSetTaskEffectFn() {
return DatabaseLayer.pipe(
Effect.flatMap((databaseLayer) => {
const databaseUrlIsSetTask = tasukuTask(
'Check if the DATABASE_URL env var is set',
async ({ setError }) => {
if (!databaseLayer.databaseUrl) {
setError(DatabaseUrlNotSetError.getMessage())
}
}
)
return databaseUrlIsSetTask
}),
Effect.mapError((e) => {
return new DatabaseUrlNotSetError()
})
)
if (isDatabaseReachableTask.state === 'error') {
ParseCLI.throwError()
}

class DatabaseNotReachableError extends Error {
readonly _tag = 'DatabaseNotReachableError'
readonly message = `Provided DATABASE_URL (${process.env.DATABASE_URL}) is not reachable`
public static getMessage() {
return new DatabaseNotReachableError().message
}
}
function checkDatabaseIsReachableTaskEffectFn() {
return DatabaseLayer.pipe(
Effect.flatMap((databaseLayer) => {
return databaseLayer.database
}),
Effect.flatMap((database) => {
const isDatabaseReachableTask = tasukuTask(
'Check if the provided DATABASE_URL is reachable',
async ({ setError }) => {
const isReachable = await database.isDatabaseReachable()
if (!isReachable) {
const errorMessage = DatabaseNotReachableError.getMessage()
setError(errorMessage)
}
}
)
return isDatabaseReachableTask
}),
Effect.mapError(() => {
return new DatabaseNotReachableError()
})
)
}

const db = database.getConnection()
let deployCommands = fs
.readdirSync(outputFolderPath)
// Only extract .plv8.sql files, this will need to change if we ever make the extension configurable
.filter((file) => file.endsWith('.plv8.sql'))
.map((file) => {
const filePath = path.join(outputFolderPath, file)
return {
filePath,
sqlQueryPromise: db.file(filePath),
}
class DeployCommandFailedError extends Error {
readonly _tag = 'DeployCommandFailedError'
readonly message = `Failed to deploy command`
public static getMessage() {
return new DeployCommandFailedError().message
}
}
function deployCommandsTaskEffectFn() {
const filePathsEffect = Config.pipe(
Effect.map((config) => {
const commandConfig = config.getCommand()
const { outputFolderPath } = commandConfig.config
return outputFolderPath
}),
Effect.map((outputFolderPath) => {
const filePaths = fs
.readdirSync(outputFolderPath)
// Only extract .plv8.sql files, this will need to change if we ever make the extension configurable
.filter((file) => file.endsWith('.plv8.sql'))
.map((file) => {
const filePath = path.join(outputFolderPath, file)
return filePath
})
return filePaths
})
)

await task(
`Deploying files from ${outputFolderPath} to the provided PostgreSQL database 🚧`.trim(),
async ({ setWarning }) => {
const taskGroup = await task.group((task) =>
deployCommands.map((deployCommand) => {
const name = getFunctionNameFromFilePath(deployCommand.filePath)
return task(
`Deploying ${name}`,
async ({ setTitle: _setTitle, setError: _setError }) => {
try {
await deployCommand.sqlQueryPromise
_setTitle(`Deployed ${name}`)
} catch (e) {
_setError(`Failed to deploy ${name} (because of ${e.message})`)
setWarning(`Failed to some functions (see below))`)
const deployCommandsEffect = DatabaseLayer.pipe(
Effect.flatMap((databaseLayer) => {
return databaseLayer.database
}),
Effect.flatMap((database) => {
return filePathsEffect.pipe(
Effect.flatMap((filePaths) => {
const db = database.getConnection()
const deployCommands = filePaths.map((filePath) => {
const name = getFunctionNameFromFilePath(filePath)
const deployCommandTasukuTaskEffect = tasukuTask(
`Deploying ${name}`,
async ({ setTitle, setError }) => {
try {
const r = await db.file(filePath)
setTitle(`Deployed ${name}`)
} catch (e) {
setError(`Failed to deploy ${name} (because of ${e.message})`)
}
}
}
)
)

return deployCommandTasukuTaskEffect
})
return Effect.all(deployCommands)
})
)

// TODO: add some batching here
await Promise.allSettled(taskGroup)
}
}),
Effect.mapError(() => {
return new DeployCommandFailedError()
})
)

database.endConnection()
return deployCommandsEffect
}

export function deployCommand() {
const checkOutputFolderTaskEffect = checkOutputFolderTaskEffectFn()
const checkDatabaseUrlIsSetTaskEffect = checkDatabaseUrlIsSetTaskEffectFn()

const checkDatabaseIsReachableTaskEffect =
checkDatabaseIsReachableTaskEffectFn()

const deployCommandsTaskEffect = deployCommandsTaskEffectFn()

return Effect.all([
checkOutputFolderTaskEffect,
checkDatabaseUrlIsSetTaskEffect,
checkDatabaseIsReachableTaskEffect,
deployCommandsTaskEffect,
]).pipe(
Effect.catchAll((e) => {
// This is a hack to make sure the tasuku tasks have time to print their output
setTimeout(() => {
// TODO: collect errors for actual deploy commands
// If 3 out of 4 functions can be deployed, they should be deployed
process.exit(1)
}, 100)
class ApplicationWillTerminateError {
readonly _tag = 'ApplicationWillTerminateError'
}
return Effect.fail(new ApplicationWillTerminateError())
})
)
}
Loading