@@ -175,7 +175,7 @@ struct PackageToJSPlugin: CommandPlugin {
175175 }
176176
177177 var extractor = ArgumentExtractor ( arguments)
178- let buildOptions = PackageToJS . BuildOptions. parse ( from: & extractor)
178+ let buildOptions = try PackageToJS . BuildOptions. parse ( from: & extractor)
179179
180180 if extractor. remainingArguments. count > 0 {
181181 printStderr (
@@ -239,7 +239,7 @@ struct PackageToJSPlugin: CommandPlugin {
239239 }
240240
241241 var extractor = ArgumentExtractor ( arguments)
242- let testOptions = PackageToJS . TestOptions. parse ( from: & extractor)
242+ let testOptions = try PackageToJS . TestOptions. parse ( from: & extractor)
243243
244244 if extractor. remainingArguments. count > 0 {
245245 printStderr (
@@ -440,12 +440,28 @@ private func printStderr(_ message: String) {
440440
441441// MARK: - Options parsing
442442
443+ extension ArgumentExtractor {
444+ mutating func extractPlatformOption( named name: String ) throws -> PackageToJS . PackageOptions . Platform {
445+ guard let stringValue = self . extractOption ( named: name) . last else {
446+ return . browser
447+ }
448+
449+ guard let platform = PackageToJS . PackageOptions. Platform ( rawValue: stringValue) else {
450+ throw PackageToJSError (
451+ " Invalid platform: \( stringValue) , expected one of \( PackageToJS . PackageOptions. Platform. allCases. map ( \. rawValue) . joined ( separator: " , " ) ) "
452+ )
453+ }
454+ return platform
455+ }
456+ }
457+
443458extension PackageToJS . PackageOptions {
444- static func parse( from extractor: inout ArgumentExtractor ) -> PackageToJS . PackageOptions {
459+ static func parse( from extractor: inout ArgumentExtractor ) throws -> PackageToJS . PackageOptions {
445460 let outputPath = extractor. extractOption ( named: " output " ) . last
446461 let configuration : String ? =
447462 ( extractor. extractOption ( named: " configuration " ) + extractor. extractSingleDashOption ( named: " c " ) ) . last
448463 let packageName = extractor. extractOption ( named: " package-name " ) . last
464+ let defaultPlatform = try extractor. extractPlatformOption ( named: " default-platform " )
449465 let explain = extractor. extractFlag ( named: " explain " )
450466 let useCDN = extractor. extractFlag ( named: " use-cdn " )
451467 let verbose = extractor. extractFlag ( named: " verbose " )
@@ -454,6 +470,7 @@ extension PackageToJS.PackageOptions {
454470 outputPath: outputPath,
455471 configuration: configuration,
456472 packageName: packageName,
473+ defaultPlatform: defaultPlatform,
457474 explain: explain != 0 ,
458475 verbose: verbose != 0 ,
459476 useCDN: useCDN != 0 ,
@@ -466,6 +483,7 @@ extension PackageToJS.PackageOptions {
466483 --output <path> Path to the output directory (default: .build/plugins/PackageToJS/outputs/Package)
467484 -c, --configuration <name> The build configuration to use (values: debug, release; default: debug)
468485 --package-name <name> Name of the package (default: lowercased Package.swift name)
486+ --platform <name> Target platform for generated JavaScript (values: \( PackageToJS . PackageOptions. Platform. allCases. map ( \. rawValue) . joined ( separator: " , " ) ) ; default: \( PackageToJS . PackageOptions. Platform. browser) )
469487 --use-cdn Whether to use CDN for dependency packages
470488 --enable-code-coverage Whether to enable code coverage collection
471489 --explain Whether to explain the build plan
@@ -475,7 +493,7 @@ extension PackageToJS.PackageOptions {
475493}
476494
477495extension PackageToJS . BuildOptions {
478- static func parse( from extractor: inout ArgumentExtractor ) -> PackageToJS . BuildOptions {
496+ static func parse( from extractor: inout ArgumentExtractor ) throws -> PackageToJS . BuildOptions {
479497 let product = extractor. extractOption ( named: " product " ) . last
480498 let noOptimize = extractor. extractFlag ( named: " no-optimize " )
481499 let rawDebugInfoFormat = extractor. extractOption ( named: " debug-info-format " ) . last
@@ -488,7 +506,7 @@ extension PackageToJS.BuildOptions {
488506 }
489507 debugInfoFormat = format
490508 }
491- let packageOptions = PackageToJS . PackageOptions. parse ( from: & extractor)
509+ let packageOptions = try PackageToJS . PackageOptions. parse ( from: & extractor)
492510 return PackageToJS . BuildOptions (
493511 product: product,
494512 noOptimize: noOptimize != 0 ,
@@ -526,15 +544,15 @@ extension PackageToJS.BuildOptions {
526544}
527545
528546extension PackageToJS . TestOptions {
529- static func parse( from extractor: inout ArgumentExtractor ) -> PackageToJS . TestOptions {
547+ static func parse( from extractor: inout ArgumentExtractor ) throws -> PackageToJS . TestOptions {
530548 let buildOnly = extractor. extractFlag ( named: " build-only " )
531549 let listTests = extractor. extractFlag ( named: " list-tests " )
532550 let filter = extractor. extractOption ( named: " filter " )
533551 let prelude = extractor. extractOption ( named: " prelude " ) . last
534552 let environment = extractor. extractOption ( named: " environment " ) . last
535553 let inspect = extractor. extractFlag ( named: " inspect " )
536554 let extraNodeArguments = extractor. extractSingleDashOption ( named: " Xnode " )
537- let packageOptions = PackageToJS . PackageOptions. parse ( from: & extractor)
555+ let packageOptions = try PackageToJS . PackageOptions. parse ( from: & extractor)
538556 var options = PackageToJS . TestOptions (
539557 buildOnly: buildOnly != 0 ,
540558 listTests: listTests != 0 ,
0 commit comments