Skip to content

Commit

Permalink
fix(aqua-api): Compilation to js/ts for file without exports [LNG-196] (
Browse files Browse the repository at this point in the history
  • Loading branch information
DieMyst committed Jun 13, 2023
1 parent e9c0044 commit 8c2240d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 59 deletions.
6 changes: 0 additions & 6 deletions .github/workflows/e2e.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ jobs:
with:
aqua-version: "${{ needs.aqua.outputs.aqua-version }}"

fluence-cli:
needs: aqua
uses: fluencelabs/fluence-cli/.github/workflows/tests.yml@main
with:
aqua-version: "${{ needs.aqua.outputs.aqua-version }}"

aqua-native:
name: "aqua"
strategy:
Expand Down
88 changes: 35 additions & 53 deletions api/aqua-api/.js/src/main/scala/api/AquaAPI.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,18 @@ object AquaAPI extends App with Logging {
.map(cjs => AquaConfig.fromJS(cjs))
.getOrElse(
validNec(AquaAPIConfig())
).map { config =>
val importsList = imports.toList
)
.map { config =>
val importsList = imports.toList

input match {
case i: (types.Input | types.Path) =>
compileAll(i, importsList, config)
case c: types.Call =>
compileCall(c, importsList, config)
input match {
case i: (types.Input | types.Path) =>
compileAll(i, importsList, config)
case c: types.Call =>
compileCall(c, importsList, config)

}
} match {
}
} match {
case Valid(v) => v.unsafeToFuture().toJSPromise
case Invalid(errs) => js.Promise.resolve(CompilationResult.errs(errs.toChain.toList))
}
Expand All @@ -100,15 +101,15 @@ object AquaAPI extends App with Logging {
}

extension (res: IO[ValidatedNec[String, Chain[AquaCompiled[FileModuleId]]]])
def toResult: IO[CompilationResult] = res.map(
_.andThen { compiled =>
def toResult: IO[CompilationResult] = res.map { compiledV =>
compiledV.map { compiled =>
config.targetType match {
case AirType => validNec(generatedToAirResult(compiled))
case AirType => generatedToAirResult(compiled)
case TypeScriptType => compiledToTsSourceResult(compiled)
case JavaScriptType => compiledToJsSourceResult(compiled)
}
}.leftMap(errorsToResult).merge
)
}

input match {
case i: types.Input =>
Expand Down Expand Up @@ -161,55 +162,36 @@ object AquaAPI extends App with Logging {
CompilationResult.errs(errors.toChain.toList)
}

private def findBySuffix(
compiled: Seq[Generated],
suffix: String,
errorMsg: String
): ValidatedNec[String, String] = {
Validated
.fromOption(
compiled.find(_.suffix == suffix).map(_.content), {
logger.error(errorMsg)
NonEmptyChain.one(errorMsg)
}
)
}
extension (res: List[GeneratedSource])

extension (res: Chain[Validated[NonEmptyChain[String], GeneratedSource]])
def toSourcesResult: ValidatedNec[String, CompilationResult] =
res.sequence.map(_.toList.toJSArray).map(sources => CompilationResult.result(sources = sources))
def toSourcesResult: CompilationResult =
CompilationResult.result(sources = res.toJSArray)

private def compiledToTsSourceResult(
compiled: Chain[AquaCompiled[FileModuleId]]
): ValidatedNec[String, CompilationResult] = {
compiled.map { c =>
findBySuffix(
c.compiled,
TypeScriptBackend.ext,
"Unreachable. TypeScript backend must generate content."
): CompilationResult =
compiled.toList
.flatMap(c =>
c.compiled
.find(_.suffix == TypeScriptBackend.ext)
.map(_.content)
.map(GeneratedSource.tsSource(c.sourceId.toString, _))
)
.map(GeneratedSource.tsSource(c.sourceId.toString, _))
}.toSourcesResult
}
.toSourcesResult

private def compiledToJsSourceResult(
compiled: Chain[AquaCompiled[FileModuleId]]
): ValidatedNec[String, CompilationResult] = {
compiled.map { c =>
findBySuffix(
c.compiled,
JavaScriptBackend.dtsExt,
"Unreachable. JavaScript backend must generate '.d.ts' content."
).andThen { tsTypes =>
findBySuffix(
c.compiled,
JavaScriptBackend.ext,
"Unreachable. JavaScript backend must generate '.js' content."
).map(jsSource => GeneratedSource.jsSource(c.sourceId.toString, jsSource, tsTypes))
}

): CompilationResult =
compiled.toList.flatMap { c =>
for {
dtsContent <- c.compiled
.find(_.suffix == JavaScriptBackend.dtsExt)
.map(_.content)
jsContent <- c.compiled
.find(_.suffix == JavaScriptBackend.dtsExt)
.map(_.content)
} yield GeneratedSource.jsSource(c.sourceId.toString, jsContent, dtsContent)
}.toSourcesResult
}

private def generatedToAirResult(
compiled: Chain[AquaCompiled[FileModuleId]]
Expand Down

0 comments on commit 8c2240d

Please sign in to comment.