44using System . Linq ;
55using System . Net . Http ;
66using System . Threading . Tasks ;
7- using Newtonsoft . Json . Linq ;
87
98namespace dotnet_unpkg
109{
11- static class UnpkgJson
12- {
13-
14- }
15-
16- static class Add
10+ public static class Add
1711 {
1812 private static readonly HttpClient Client = new HttpClient
1913 {
@@ -22,15 +16,22 @@ static class Add
2216
2317 private static readonly string BaseDirectory = Path . Combine ( "wwwroot" , "lib" ) ;
2418
25- public static async Task < List < UnpkgJsonEntry > > Run ( IEnumerable < string > args )
19+ public static async Task Run ( IEnumerable < string > args )
2620 {
21+ var argList = args . ToList ( ) ;
22+ if ( argList [ 0 ] == "--help" || argList [ 0 ] == "-h" )
23+ {
24+ Help . Add ( ) ;
25+ return ;
26+ }
27+
2728 if ( ! Directory . Exists ( BaseDirectory ) )
2829 {
2930 Directory . CreateDirectory ( BaseDirectory ) ;
3031 }
3132
32- var results = await Task . WhenAll ( args . Select ( AddPackage ) ) ;
33- return results . ToList ( ) ;
33+ var results = await Task . WhenAll ( argList . Select ( AddPackage ) ) ;
34+ await UnpkgJson . Save ( results ) ;
3435 }
3536
3637 private static async Task < UnpkgJsonEntry > AddPackage ( string package )
@@ -41,56 +42,26 @@ private static async Task<UnpkgJsonEntry> AddPackage(string package)
4142 return null ;
4243 }
4344
44- await Download ( package , distFile . BaseUrl , distFile . Files ) ;
45+ await DownloadPackage ( package , distFile . BaseUrl , distFile . Files ) ;
4546 return UnpkgJsonEntry . Create ( package , distFile ) ;
4647 }
4748
48- private static Task Download ( string package , string basePath , IEnumerable < DistFile > files )
49+ private static Task DownloadPackage ( string package , string basePath , IEnumerable < DistFile > files )
4950 {
5051 var tasks = new List < Task > ( ) ;
5152 foreach ( var file in files )
5253 {
5354 if ( file . Type == "file" )
5455 {
55- tasks . Add ( Download ( package , basePath , file . Path ) ) ;
56+ tasks . Add ( Download . DistFile ( package , $ " { basePath } { file . Path } " ) ) ;
5657 }
5758 else if ( file . Files ? . Count > 0 )
5859 {
59- tasks . Add ( Download ( package , basePath , file . Files ) ) ;
60+ tasks . Add ( DownloadPackage ( package , basePath , file . Files ) ) ;
6061 }
6162 }
6263
6364 return Task . WhenAll ( tasks ) ;
6465 }
65-
66- private static async Task Download ( string package , string basePath , string path )
67- {
68- using ( var response = await Client . GetAsync ( $ "{ basePath } { path } ") )
69- {
70- if ( response . IsSuccessStatusCode )
71- {
72- // Remove /dist/ from start of path
73- path = path . Substring ( 6 ) ;
74-
75- if ( Path . DirectorySeparatorChar != '/' )
76- {
77- path = path . Replace ( '/' , Path . DirectorySeparatorChar ) ;
78- }
79-
80- var file = Path . GetFileName ( path ) ;
81- var directory = Path . Combine ( BaseDirectory , package , Path . GetDirectoryName ( path ) ) ;
82-
83- if ( ! Directory . Exists ( directory ) )
84- {
85- Directory . CreateDirectory ( directory ) ;
86- }
87-
88- using ( var fileStream = File . Create ( Path . Combine ( directory , file ) ) )
89- {
90- await response . Content . CopyToAsync ( fileStream ) ;
91- }
92- }
93- }
94- }
9566 }
9667}
0 commit comments