/
Main.scala
362 lines (323 loc) · 15.1 KB
/
Main.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package org.scalablytyped.converter.cli
import java.net.URI
import com.olvind.logging.{stdout, storing, LogLevel, Logger}
import fansi.{Attr, Color, Str}
import org.scalablytyped.converter.internal.importer._
import org.scalablytyped.converter.internal.importer.build.{
BinTrayPublisher,
BloopCompiler,
PublishedSbtProject,
SbtProject,
}
import org.scalablytyped.converter.internal.importer.documentation.Npmjs
import org.scalablytyped.converter.internal.importer.jsonCodecs._
import org.scalablytyped.converter.internal.phases.PhaseListener.NoListener
import org.scalablytyped.converter.internal.phases.{PhaseRes, PhaseRunner, RecPhase}
import org.scalablytyped.converter.internal.scalajs.{Name, Versions}
import org.scalablytyped.converter.internal.ts.CalculateLibraryVersion.PackageJsonOnly
import org.scalablytyped.converter.internal.ts.{PackageJsonDeps, TsIdentLibrary}
import org.scalablytyped.converter.internal.{constants, files, sets, BuildInfo, IArray, InFolder, Json}
import org.scalablytyped.converter.{Flavour, Selection}
import scopt.{OParser, OParserBuilder, Read}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration
import scala.concurrent.Await
import scala.util.{Failure, Success, Try}
object Main {
class Paths(base: os.Path) {
lazy val out: os.Path =
files.existing(base / 'out)
val node_modules: Option[os.Path] =
Option(base / 'node_modules) filter files.exists
val packageJson: Option[os.Path] =
Option(base / "package.json") filter files.exists
}
val DefaultOptions = ConversionOptions(
useScalaJsDomTypes = true,
outputPackage = Name.typings,
enableScalaJsDefined = Selection.None,
flavour = Flavour.Normal,
ignoredLibs = Set(TsIdentLibrary("typescript")),
ignoredModulePrefixes = Set(),
stdLibs = IArray("es6"),
expandTypeMappings = EnabledTypeMappingExpansion.DefaultSelection,
versions = Versions(Versions.Scala213, Versions.ScalaJs1),
organization = "org.scalablytyped",
)
case class Config(
conversion: ConversionOptions,
wantedLibs: Set[TsIdentLibrary],
publishBintrayRepo: Option[ProjectName],
publishGitUrl: Option[URI],
inDirectory: os.Path,
includeDev: Boolean,
includeProject: Boolean,
) {
lazy val paths = new Paths(inDirectory)
def mapConversion(f: ConversionOptions => ConversionOptions) = copy(conversion = f(conversion))
}
val DefaultConfig = Config(
DefaultOptions,
wantedLibs = Set(),
publishBintrayRepo = None,
publishGitUrl = None,
inDirectory = os.pwd,
includeDev = false,
includeProject = false,
)
val parseCachePath = Some(files.existing(constants.defaultCacheFolder / 'parse).toNIO)
val t0 = System.currentTimeMillis
val logger: Logger[(Array[Logger.Stored], Unit)] =
storing() zipWith stdout.filter(LogLevel.warn)
implicit val ReadsFlavour: Read[Flavour] =
Read.reads(s => Flavour.all.getOrElse(s, sys.error(s"'$s' is not among ${Flavour.all.keys}")))
implicit val ReadsVersionsScalaJs: Read[Versions.ScalaJs] =
Read.stringRead.map(Versions.ScalaJs)
implicit val ReadsVersionsScala: Read[Versions.Scala] =
Read.stringRead.map(Versions.Scala)
implicit val ReadsTsIdentLibrary: Read[TsIdentLibrary] =
Read.stringRead.map(TsIdentLibrary.apply)
implicit val ReadsProjectName: Read[ProjectName] =
Read.stringRead.map(ProjectName)
implicit val ReadsOsPath: Read[os.Path] =
Read.stringRead.map { str =>
Try(os.Path(str)) match {
case Failure(_) =>
os.pwd / os.RelPath(str)
case Success(absolute) => absolute
}
}
implicit def ReadsSelection[T](implicit ts: Read[Seq[T]]): Read[Selection[T]] = Read.reads {
case "all" => Selection.All
case "none" => Selection.All
case s if s.startsWith("+") => Selection.NoneExcept(ts.reads(s.drop(1)): _*)
case s if s.startsWith("-") => Selection.AllExcept(ts.reads(s.drop(1)): _*)
case _ =>
sys.error(
"Syntax: `all`/`none` for Selection.All/Selection.None, `+a,b` for Selection.NoneExcept(a, b), `-a,b` for Selection.AllExcept(a, b)",
)
}
val ParseConversionOptions: OParser[Unit, Config] = {
val builder: OParserBuilder[Config] = OParser.builder[Config]
import builder._
OParser.sequence(
programName("stc"),
head(s"ScalablyTyped Converter (version ${BuildInfo.version})"),
help('h', "help"),
version('v', "version"),
opt[os.Path]('d', "directory")
.action((x, c) => c.copy(inDirectory = x))
.text("Specify another directory instead of the current directory where your package.json and node_modules is"),
opt[Boolean]("includeDev")
.action((x, c) => c.copy(includeDev = x))
.text("Include dev dependencies"),
opt[Boolean]("includeProject")
.action((x, c) => c.copy(includeProject = x))
.text("Include project in current directory"),
opt[Boolean]("useScalaJsDomTypes")
.action((x, c) => c.mapConversion(_.copy(useScalaJsDomTypes = x)))
.text(
"When true (which is the default) uses scala-js-dom types when possible instead of types we translate from typescript in std",
),
opt[Flavour]('f', "flavour")
.action((x, c) => c.mapConversion(_.copy(flavour = x)))
.text(
s"One of ${Flavour.all.keys.mkString(", ")}. See https://scalablytyped.org/docs/flavour",
),
opt[Versions.ScalaJs]("scalajs")
.action((x, c) => c.mapConversion(cc => cc.copy(versions = cc.versions.copy(scalaJs = x))))
.text(s"Scala.js version"),
opt[Versions.Scala]("scala")
.action((x, c) => c.mapConversion(cc => cc.copy(versions = cc.versions.copy(scala = x))))
.text(s"Scala version"),
opt[String]("outputPackage")
.action((x, c) => c.mapConversion(_.copy(outputPackage = Name(x))))
.text(s"Output package"),
opt[Selection[TsIdentLibrary]]("enableScalaJSDefined")
.action((x, c) => c.mapConversion(_.copy(enableScalaJsDefined = x)))
.text(s"Libraries you want to enable @ScalaJSDefined traits for."),
opt[Seq[String]]('s', "stdlib")
.action((x, c) => c.mapConversion(_.copy(stdLibs = IArray.fromTraversable(x))))
.text(s"Which parts of typescript stdlib you want to enable"),
opt[String]("organization")
.action((x, c) => c.mapConversion(_.copy(organization = x)))
.text(s"Organization used (locally) publish artifacts"),
opt[Seq[TsIdentLibrary]]("ignoredLibs")
.action((x, c) => c.mapConversion(_.copy(ignoredLibs = x.toSet)))
.text(s"Libraries you want to ignore"),
opt[Boolean]("experimentalEnableImplicitOps")
.action { (x, c) =>
logger.warn("--experimentalEnableImplicitOps has no effect since it became the default encoding")
c
}
.text(s"Deprecated, doesn't do anything anymore"),
opt[ProjectName]("publish-to-bintray-repo")
.action((x, c) => c.copy(publishBintrayRepo = Some(x)))
.text(
s"Enable publishing to your bintray repo. You must also provide ${BinTrayPublisher.bintrayCredentialsFile} with user and password, and set publish-git-repo-link",
),
opt[URI]("publish-git-repo-link")
.action((x, c) => c.copy(publishGitUrl = Some(x)))
.text("Must set a public git repo uri since published artifacts must be open source"),
arg[Seq[TsIdentLibrary]]("libs")
.text("Libraries you want to convert from node_modules")
.unbounded()
.optional()
.action((x, c) => c.copy(wantedLibs = x.toSet)),
)
}
def table(Key: Attr)(kvs: (String, String)*): Str = {
val headerLength = kvs.map { case (header, _) => header }.maxBy(_.length).length + 1
val massaged = kvs.flatMap {
case (header, value) => Seq[Str](Key(header.padTo(headerLength, ' ')), value, "\n")
}
Str.join(massaged: _*)
}
def main(args: Array[String]): Unit =
OParser.parse(ParseConversionOptions, args, DefaultConfig) match {
case Some(
c @ Config(
conversion,
libsFromCmdLine,
publishBintrayRepoOpt,
publishGitUrlOpt,
inDir,
includeDev,
includeProject,
),
) =>
val packageJsonPath = c.paths.packageJson.getOrElse(sys.error(s"$inDir does not contain package.json"))
val nodeModulesPath = c.paths.node_modules.getOrElse(sys.error(s"$inDir does not contain node_modules"))
require(files.exists(nodeModulesPath / "typescript" / "lib"), "must install typescript")
val packageJson = Json.force[PackageJsonDeps](packageJsonPath)
val projectSource =
if (includeProject) Some(Source.FromFolder(InFolder(inDir), TsIdentLibrary(inDir.last))) else None
val wantedLibs: Set[TsIdentLibrary] =
libsFromCmdLine match {
case sets.EmptySet() =>
val fromPackageJson = packageJson.allLibs(includeDev, peer = false).map(TsIdentLibrary.apply)
require(fromPackageJson.nonEmpty, "No libraries found in package.json")
val ret = fromPackageJson -- conversion.ignoredLibs
require(ret.nonEmpty, s"All libraries in package.json ignored")
ret
case otherwise => otherwise
}
val publisherOpt: Option[Publisher] =
publishBintrayRepoOpt map { repo =>
BinTrayPublisher(None, repo, publishGitUrlOpt, global) match {
case Left(err) => sys.error(s"Couldn't setup publishing: $err")
case Right(value) => value
}
}
println(
table(fansi.Color.LightBlue)(
"directory" -> inDir.toString,
"includeDev" -> includeDev.toString,
"includeProject" -> includeProject.toString,
"wantedLibs" -> wantedLibs.map(_.value).mkString(", "),
"useScalaJsDomTypes" -> conversion.useScalaJsDomTypes.toString,
"flavour" -> conversion.flavour.toString,
"outputPackage" -> conversion.outputPackage.unescaped,
"enableScalaJsDefined" -> conversion.enableScalaJsDefined.map(_.value).toString,
"stdLibs" -> conversion.stdLibs.toString,
"expandTypeMappings" -> conversion.expandTypeMappings.map(_.value).toString,
"ignoredLibs" -> conversion.ignoredLibs.map(_.value).toString,
// "ignoredModulePrefixes" -> conversion.ignoredModulePrefixes.toString,
"versions" -> conversion.versions.toString,
"organization" -> conversion.organization,
),
)
val compiler = Await.result(
BloopCompiler(logger.filter(LogLevel.debug).void, conversion.versions, failureCacheFolderOpt = None),
Duration.Inf,
)
val Source.FromNodeModules(_sources, folders, libraryResolver, stdLibSource) =
Source.fromNodeModules(InFolder(nodeModulesPath), conversion, wantedLibs)
val sources = _sources ++ projectSource
val Pipeline: RecPhase[Source, PublishedSbtProject] =
RecPhase[Source]
.next(
new Phase1ReadTypescript(
calculateLibraryVersion = PackageJsonOnly,
resolve = libraryResolver,
ignored = conversion.ignoredLibs,
ignoredModulePrefixes = conversion.ignoredModulePrefixes,
stdlibSource = stdLibSource,
pedantic = false,
parser = PersistingParser(parseCachePath, folders, logger.void),
expandTypeMappings = conversion.expandTypeMappings,
),
"typescript",
)
.next(
new Phase2ToScalaJs(
pedantic = false,
enableScalaJsDefined = conversion.enableScalaJsDefined,
outputPkg = conversion.outputPackage,
flavour = conversion.flavourImpl,
),
"scala.js",
)
.next(new PhaseFlavour(conversion.flavourImpl), conversion.flavourImpl.toString)
.next(
new Phase3Compile(
resolve = libraryResolver,
versions = conversion.versions,
compiler = compiler,
targetFolder = c.paths.out,
organization = conversion.organization,
publisherOpt = None,
publishLocalFolder = constants.defaultLocalPublishFolder,
metadataFetcher = Npmjs.No,
softWrites = true,
flavour = conversion.flavourImpl,
generateScalaJsBundlerFile = false,
ensureSourceFilesWritten = true,
),
"build",
)
.nextOpt(publisherOpt.flatMap(_.enabled).map(Phase4Publish), "publish")
val results: Map[Source, PhaseRes[Source, PublishedSbtProject]] =
sources.toVector
.map(source => source -> PhaseRunner.go(Pipeline, source, Nil, (_: Source) => logger.void, NoListener))
.toMap
val td = System.currentTimeMillis - t0
logger warn td
val failures: Map[Source, Either[Throwable, String]] =
results.collect { case (_, PhaseRes.Failure(errors)) => errors }.reduceOption(_ ++ _).getOrElse(Map.empty)
if (failures.nonEmpty) {
println(
Color.Red(s"Failure: You might try --ignoredLibs ${failures.keys.map(_.libName.value).mkString(", ")}"),
)
failures.foreach {
case (source, Left(value)) =>
println(s"${source.libName.value}:")
value.printStackTrace()
case (source, Right(value)) =>
println(s"${source.libName.value}: $value")
}
System.exit(1)
} else {
val allSuccesses: Map[Source, PublishedSbtProject] = {
def go(source: Source, p: PublishedSbtProject): Map[Source, PublishedSbtProject] =
Map(source -> p) ++ p.project.deps.flatMap { case (k, v) => go(k, v) }
results.collect { case (s, PhaseRes.Ok(res)) => go(s, res) }.reduceOption(_ ++ _).getOrElse(Map.empty)
}
val short: Seq[SbtProject] =
results
.collect { case (_, PhaseRes.Ok(res)) => res.project }
.toSeq
.filter(_.name != Name.std.unescaped)
.sortBy(_.name)
println()
println(s"Successfully converted ${allSuccesses.keys.map(x => Color.Green(x.libName.value)).mkString(", ")}")
println("To use in sbt: ")
println(Color.DarkGray(s"""libraryDependencies ++= Seq(
| ${short.map(p => p.reference.asSbt).mkString("", ",\n ", "")}
|)""".stripMargin))
System.exit(0)
}
case _ =>
System.exit(1)
}
}