Skip to content

Commit

Permalink
Merge pull request #123 from AngelMunoz/custom-paths
Browse files Browse the repository at this point in the history
custom paths
  • Loading branch information
AngelMunoz committed Apr 23, 2023
2 parents 0eb002c + 0a0537f commit 41a0a1a
Show file tree
Hide file tree
Showing 23 changed files with 1,423 additions and 339 deletions.
31 changes: 31 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,37 @@
"stopAtEntry": false,
"console": "integratedTerminal"
},
{
"name": "Perla resolution",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/Perla/bin/Debug/net7.0/Perla.dll",
"args": [
"resolution",
"@app/components",
"./components/exports.js"
],
"cwd": "${workspaceFolder}/sample",
"stopAtEntry": false,
"console": "integratedTerminal"
},
{
"name": "Perla add nested",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/src/Perla/bin/Debug/net7.0/Perla.dll",
"args": [
"add",
"lodash/fp/object",
"-s",
"unpkg"
],
"cwd": "${workspaceFolder}/sample",
"stopAtEntry": false,
"console": "integratedTerminal"
},
{
"name": "Perla test watch",
"type": "coreclr",
Expand Down
8 changes: 5 additions & 3 deletions src/Perla/Build.fs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ type Build =

if config.enableEnv && config.build.emitEnvFile then
UMX.untag config.envPath
Constants.EnvBareImport

yield! config.esbuild.externals
}
Expand Down Expand Up @@ -207,13 +208,14 @@ type Build =

lfsGlob |> Seq.toArray |> Array.Parallel.iter copyLocal)

static member EmitEnvFile(config: PerlaConfig) =
static member EmitEnvFile(config: PerlaConfig, ?tmpPath: string<SystemPath>) =
let tmpPath = defaultArg tmpPath config.build.outDir |> UMX.untag

match Env.GetEnvContent() with
| Some content ->
// remove the leading slash
let targetFile = (UMX.untag config.envPath)[1..]

let path = Path.Combine(UMX.untag config.build.outDir, targetFile)

let path = Path.Combine(tmpPath, targetFile) |> Path.GetFullPath
File.WriteAllText(path, content)
| None -> ()
3 changes: 2 additions & 1 deletion src/Perla/Build.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ type Build =
static member CopyGlobs:
config: BuildConfig * tempDir: string<SystemPath> -> unit

static member EmitEnvFile: config: PerlaConfig -> unit
static member EmitEnvFile:
config: PerlaConfig * ?tmpPath: string<SystemPath> -> unit
128 changes: 125 additions & 3 deletions src/Perla/Commands.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,47 @@ open System.CommandLine.Invocation
open System.CommandLine.Parsing

open FSharp.SystemCommandLine
open FSharp.SystemCommandLine.Aliases

open FsToolkit.ErrorHandling
open FSharp.UMX

open Perla
open Perla.PackageManager.Types
open Perla.Types
open Perla.Handlers


[<Class; Sealed>]
type PerlaOptions =

static member BoolOption
(
aliases: string seq,
description: string
) : Option<bool option> =
let parser (result: ArgumentResult) =
let defaultValue = Some true

let withToken =
result.Tokens
|> Seq.tryHead
|> Option.map (fun value ->
match value.Value.Trim().ToLowerInvariant() with
| "true" -> true
| "false" -> false
| _ -> false)

Option.orElse defaultValue withToken



Option<bool option>(
aliases |> Seq.toArray,
parseArgument = parser,
description = description,
Arity = ArgumentArity.ZeroOrOne
)

static member PackageSource: Option<Provider voption> =
let parser (result: ArgumentResult) =
match result.Tokens |> Seq.tryHead with
Expand Down Expand Up @@ -110,6 +140,22 @@ type PerlaOptions =

[<Class; Sealed>]
type PerlaArguments =

static member ArgStringMaybe
(
name: string,
?description: string
) : Argument<string option> =
let parser (result: ArgumentResult) =
result.Tokens |> Seq.tryHead |> Option.map (fun value -> value.Value)

Argument<string option>(
name,
parse = parser,
?description = description,
Arity = ArgumentArity.ZeroOrOne
)

static member Properties: Argument<string array> =
let parser (result: ArgumentResult) =
result.Tokens
Expand Down Expand Up @@ -142,7 +188,7 @@ module SharedInputs =
[<RequireQualifiedAccess>]
module DescribeInputs =
let perlaProperties: HandlerInput<string[] option> =
Arg<string[] option>(
Argument<string[] option>(
"properties",
(fun (result: ArgumentResult) ->
match result.Tokens |> Seq.toArray with
Expand Down Expand Up @@ -197,12 +243,36 @@ module SetupInputs =
"Skip Prompts and accept all defaults"
)


[<RequireQualifiedAccess>]
module PackageInputs =
let package: HandlerInput<string> =
Input.Argument("package", "Name of the JS Package")

let import: HandlerInput<string> =
Input.Argument(
"import",
"Name to assign to this import e.g 'app/buttons' 'lodashv3'"
)

let resolution: HandlerInput<string option> =
PerlaArguments.ArgStringMaybe(
"resolution",
"URL, or path (absolute or relative) to your import's actual source."
)
|> Input.OfArgument

let addOrUpdate: HandlerInput<bool option> =
Input.OptionMaybe(
[ "--add"; "--update"; "-u"; "-a" ],
"Attempts to add or update an import in paths configuration."
)

let removeResolution: HandlerInput<bool> =
Input.Option(
[ "--remove-resolution"; "-r" ],
"Remove the resolution from the config file"
)

let currentPage: HandlerInput<int option> =
Input.OptionMaybe(
[| "--page"; "-p" |],
Expand Down Expand Up @@ -334,6 +404,14 @@ module ServeInputs =

[<RequireQualifiedAccess>]
module Commands =
type HandlerInput<'T> with

member this.GetValue(ctx: CommandResult) : 'T =
match this.Source with
| ParsedOption o -> o :?> Option<'T> |> ctx.GetValueForOption
| ParsedArgument a -> a :?> Argument<'T> |> ctx.GetValueForArgument
| Context -> failwith "Unable to get a result from context"

let Build =

let buildArgs
Expand Down Expand Up @@ -545,6 +623,50 @@ module Commands =
setHandler (buildArgs >> Handlers.runAddPackage)
}

let AddResolution =

let buildArgs (import: string, resolution: string option, remove: bool) =
match resolution with
| Some resolution -> {
operation = AddOrUpdate(UMX.tag import, UMX.tag resolution)
}
| None ->
if remove then
{ operation = Remove import }
else
failwith "This should not have happened"

let cmd = command "resolution" {
addAlias "custom-path"

description
$"Saves a manual resolution in the {Constants.PerlaConfigName} file that will be included in the import map for this application."

inputs (
PackageInputs.import,
PackageInputs.resolution,
PackageInputs.removeResolution
)

setHandler (buildArgs >> Handlers.runAddResolution)
}

cmd.AddValidator(fun cmdResult ->
let resolution = PackageInputs.resolution.GetValue cmdResult

let remove = PackageInputs.removeResolution.GetValue cmdResult

match resolution, remove with
| Some _, true ->
cmdResult.ErrorMessage <-
"A resolution has been provided together with the '--remove' option, if you intend to add it, remove the flag otherwise remove the resolution and just use the flag."
| None, false ->
cmdResult.ErrorMessage <-
"You have to provide the '--remove' option to remove this import when the resolution is not present."
| _, _ -> ())

cmd

let ListPackages =

let buildArgs (asNpm: bool option) : ListPackagesOptions = {
Expand Down
9 changes: 9 additions & 0 deletions src/Perla/Commands.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type PerlaOptions =
type PerlaArguments =
static member Properties: Argument<string array>

static member ArgStringMaybe:
name: string * ?description: string -> Argument<string option>

[<RequireQualifiedAccess>]
module SharedInputs =
val asDev: HandlerInput<bool option>
Expand Down Expand Up @@ -49,6 +52,11 @@ module PackageInputs =
val currentPage: HandlerInput<int option>
val showAsNpm: HandlerInput<bool option>

val import: HandlerInput<string>
val resolution: HandlerInput<string option>
val addOrUpdate: HandlerInput<bool option>
val removeResolution: HandlerInput<bool>

[<RequireQualifiedAccess>]
module TemplateInputs =
val repositoryName: HandlerInput<string>
Expand Down Expand Up @@ -92,6 +100,7 @@ module Commands =
val SearchPackage: Command
val ShowPackage: Command
val AddPackage: Command
val AddResolution: Command
val RemovePackage: Command
val ListPackages: Command
val RestoreImportMap: Command
Expand Down
Loading

0 comments on commit 41a0a1a

Please sign in to comment.