Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
- #114
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelMunoz committed Apr 15, 2023
1 parent 2edceb3 commit c199305
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 88 deletions.
48 changes: 31 additions & 17 deletions src/Perla/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,11 @@ module SharedInputs =
"Use the dev mode configuration"
)

let source: HandlerInput<Provider voption> = PerlaOptions.PackageSource |> Input.OfOption
let mode: HandlerInput<RunConfiguration voption> = PerlaOptions.RunConfiguration |> Input.OfOption
let source: HandlerInput<Provider voption> =
PerlaOptions.PackageSource |> Input.OfOption

let mode: HandlerInput<RunConfiguration voption> =
PerlaOptions.RunConfiguration |> Input.OfOption

[<RequireQualifiedAccess>]
module DescribeInputs =
Expand Down Expand Up @@ -182,18 +185,19 @@ module BuildInputs =

[<RequireQualifiedAccess>]
module SetupInputs =
let skipPrompts: HandlerInput<bool option> =
let installTemplates: HandlerInput<bool option> =
Input.OptionMaybe(
[ "--skip-prompts"; "-y"; "--yes"; "-sp" ],
"Skip prompts"
[ "--templates"; "-t" ],
"Install Default templates (defaults to true)"
)

let skipPlaywright: HandlerInput<bool option> =
let skipPrompts: HandlerInput<bool option> =
Input.OptionMaybe(
[ "--skip-playwright" ],
"Skips installing playwright (defaults to true)"
[ "--skip"; "-s"; "-y" ],
"Skip Prompts and accept all defaults"
)


[<RequireQualifiedAccess>]
module PackageInputs =
let package: HandlerInput<string> =
Expand Down Expand Up @@ -409,12 +413,12 @@ module Commands =
let buildArgs
(
ctx: InvocationContext,
yes: bool option,
skipPlaywright: bool option
installTemplates: bool option,
skipPrompts: bool option
) : SetupOptions * CancellationToken =
{
skipPrompts = yes |> Option.defaultValue false
skipPlaywright = skipPlaywright |> Option.defaultValue true
installTemplates = defaultArg installTemplates true
skipPrompts = defaultArg skipPrompts false
},
ctx.GetCancellationToken()

Expand All @@ -424,8 +428,8 @@ module Commands =

inputs (
Input.Context(),
SetupInputs.skipPrompts,
SetupInputs.skipPlaywright
SetupInputs.installTemplates,
SetupInputs.skipPrompts
)

setHandler (buildArgs >> Handlers.runSetup)
Expand All @@ -445,14 +449,18 @@ module Commands =
},
ctx.GetCancellationToken()

command "search" {
let cmd = command "search" {
description
"Search a package name in the Skypack api, this will bring potential results"

inputs (Input.Context(), PackageInputs.package, PackageInputs.currentPage)

setHandler (buildArgs >> Handlers.runSearchPackage)
}

cmd.IsHidden <- true
cmd

let ShowPackage =

let buildArgs
Expand All @@ -462,14 +470,17 @@ module Commands =
) : ShowPackageOptions * CancellationToken =
{ package = package }, ctx.GetCancellationToken()

command "show" {
let cmd = command "show" {
description
"Shows information about a package if the name matches an existing one"

inputs (Input.Context(), PackageInputs.package)
setHandler (buildArgs >> Handlers.runShowPackage)
}

cmd.IsHidden <- true
cmd

let RemovePackage =

let buildArgs
Expand Down Expand Up @@ -705,7 +716,7 @@ module Commands =
},
ctx.GetCancellationToken()

command "test" {
let cmd = command "test" {
description "Runs client side tests in a headless browser"

inputs (
Expand All @@ -721,6 +732,9 @@ module Commands =
setHandler (buildArgs >> Handlers.runTesting)
}

cmd.IsHidden <- true
cmd

let Describe =

let buildArgs (properties: string[] option, current: bool) = {
Expand Down
10 changes: 10 additions & 0 deletions src/Perla/Constants.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ val JsonSchemaUrl: string = "https://raw.githubusercontent.com/AngelMunoz/Perla/

[<Literal>]
val ArtifactsDirectoryname: string = "perla"

module CliDirectives =
[<Literal>]
val Preview: string = "preview"

[<Literal>]
val NoEsbuildPlugin: string = "no-esbuild-plugin"

[<Literal>]
val CiRun: string = "ci-run"
92 changes: 48 additions & 44 deletions src/Perla/Handlers.fs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ type BuildOptions = {
}

type SetupOptions = {
installTemplates: bool
skipPrompts: bool
skipPlaywright: bool
}

type SearchOptions = { package: string; page: int }
Expand Down Expand Up @@ -510,37 +510,25 @@ module Testing =

[<RequireQualifiedAccess>]
module Handlers =
open Perla.Database

let runSetup (options: SetupOptions, cancellationToken: CancellationToken) = task {
Logger.log "Perla will set up the following resources:"
Logger.log "- Esbuild"
Logger.log "- Default Templates"

if not options.skipPlaywright then
Logger.log "- Playwright browsers (required for the 'perla test' command)"
else
Logger.log "- Playwright browsers (skipped)"

Logger.log
"After that you should be able to run 'perla build' or 'perla new'"
"After that you should be able to run perla commands without extra effort."

do! FileSystem.SetupEsbuild(UMX.tag Constants.Esbuild_Version)
Logger.log ("[bold green]esbuild[/] has been setup!", escape = false)
do!
FileSystem.SetupEsbuild(
UMX.tag Constants.Esbuild_Version,
cancellationToken
)

match options.skipPrompts, options.skipPlaywright with
| true, true -> Logger.log "Skipping Playwright setup"
| true, false -> Testing.SetupPlaywright()
| false, true -> Logger.log "Skipping Playwright setup"
| false, false ->
if
AnsiConsole.Confirm(
"Install Playwright browsers? (required for 'perla test' command)",
false
)
then
Testing.SetupPlaywright()
else
Logger.log "Skipping Playwright setup"
Checks.SaveEsbuildBinPresent(UMX.tag Constants.Esbuild_Version) |> ignore

Logger.log ("[bold green]esbuild[/] has been setup!", escape = false)

let username, repository, branch =
PerlaTemplateRepository.DefaultTemplatesRepository
Expand All @@ -555,25 +543,37 @@ module Handlers =
Templates.TemplateOperation.Add(username, repository, branch)
))

if options.skipPrompts then
match options.skipPrompts, options.installTemplates with
| false, true
| true, true ->
let! operation = getTemplate ()

match operation with
| Ok() -> return 0
| Error err ->
Logger.log (err)
return 1
else if AnsiConsole.Confirm("Add default templates?", false) then
let! operation = getTemplate ()

match operation with
| Ok() -> return 0
| Ok() ->
Checks.SaveTemplatesPresent() |> ignore
return 0
| Error err ->
Logger.log (err)
Logger.log err
return 1
else
| true, false ->
if AnsiConsole.Confirm("Add default templates?", false) then
let! operation = getTemplate ()

match operation with
| Ok() ->
Checks.SaveTemplatesPresent() |> ignore
return 0
| Error err ->
Logger.log err
return 1
else
Logger.log "Skip installing templates"
return 0
| false, false ->
Logger.log "Skip installing templates"
return 0


}

let runNew
Expand Down Expand Up @@ -726,20 +726,24 @@ module Handlers =


if mentionQuickCommand then
Logger.log "This template has quick access commands:"
let ffCmd =
$"perla new [blue]<my-project-name>[/] [yellow]-t {item.shortName}[/]"

Logger.log (
$"[bold yellow]perla new <my-project-name> -t {item.shortName}[/]",
escape = false
)
let groupCmd =
$"perla new [blue]<my-project-name>[/] [yellow]-id {item.group}[/]"

Logger.log (
$"Next time you can run [bold yellow]perla new <my-project-name> -id {item.group}[/]",
$"You can run this template directly with:\n{ffCmd}\n{groupCmd}",
escape = false
)

Logger.log
$"Project [yellow]{options.projectName}[/] created successfully!"
let chdir = $"cd ./{options.projectName}"
let serve = "perla serve"

Logger.log (
$"Project [green]{options.projectName}[/] created!, to get started run:\n{chdir}\n{serve}",
escape = false
)

return 0

Expand All @@ -749,7 +753,7 @@ module Handlers =
Logger.log
"please check for typos or run 'perla new <my-project-name>' to run the templating wizard"

return 0
return 1
}

let runTemplate
Expand Down
2 changes: 1 addition & 1 deletion src/Perla/Handlers.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type BuildOptions = {
}

type SetupOptions = {
installTemplates: bool
skipPrompts: bool
skipPlaywright: bool
}

type SearchOptions = { package: string; page: int }
Expand Down
22 changes: 3 additions & 19 deletions src/Perla/Scaffolding.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ open Perla.Extensibility

open Perla.Json.TemplateDecoders
open Perla.Units
open Thoth.Json.Net

open Perla.Database


module Scaffolding =
Expand Down Expand Up @@ -87,7 +88,6 @@ module Scaffolding =
[<RequireQualifiedAccess>]
type TemplateSearchKind =
| Id of ObjectId
| Group of group: string<RepositoryGroup>
| Username of name: string
| Repository of repository: string
| FullName of username: string * repository: string
Expand All @@ -105,20 +105,14 @@ module Scaffolding =
| Template of template: TemplateItem
| Repository of repository: PerlaTemplateRepository

let Database =
lazy
(new LiteDatabase(
$"Filename='{UMX.untag FileSystem.Database}';Connection='shared'"
))

let RepositoriesCol =
lazy
(let database = Database.Value
let repo = database.GetCollection<PerlaTemplateRepository>()

repo.EnsureIndex(fun template -> template.username) |> ignore
repo.EnsureIndex(fun template -> template.repository) |> ignore
repo.EnsureIndex((fun template -> template.group), true) |> ignore
repo.EnsureIndex(fun template -> template.group) |> ignore
repo)

let TemplatesCol =
Expand Down Expand Up @@ -294,16 +288,6 @@ module Scaffolding =
StringComparison.InvariantCultureIgnoreCase
))
.SingleOrDefault()
| TemplateSearchKind.Group group ->
templates
.Query()
.Where(fun tplRepo ->
(UMX.untag tplRepo.group)
.Equals(
UMX.untag group,
StringComparison.InvariantCultureIgnoreCase
))
.SingleOrDefault()
| TemplateSearchKind.Repository repository ->
templates
.Query()
Expand Down
14 changes: 7 additions & 7 deletions src/Perla/Scaffolding.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ module Scaffolding =
fullPath: string<SystemPath>
}

[<RequireQualifiedAccess>]
type TemplateSearchKind =
| Id of ObjectId
| Username of name: string
| Repository of repository: string
| FullName of username: string * repository: string

[<RequireQualifiedAccess>]
type QuickAccessSearch =
| Id of ObjectId
Expand All @@ -64,13 +71,6 @@ module Scaffolding =
| Parent of ObjectId


[<RequireQualifiedAccess>]
type TemplateSearchKind =
| Id of ObjectId
| Group of group: string<RepositoryGroup>
| Username of name: string
| Repository of repository: string
| FullName of username: string * repository: string

[<RequireQualifiedAccess; Struct>]
type TemplateScriptKind =
Expand Down

0 comments on commit c199305

Please sign in to comment.