1+ import * as fs from 'fs' ;
2+ import * as os from 'os' ;
3+ import { Package } from '../packages' ;
4+
5+ interface PackageJSONFile
6+ {
7+ runtimeDependencies : Package [ ] ;
8+ }
9+
10+ export function updatePackageDependencies ( ) {
11+
12+ const urlsIndex = process . argv . indexOf ( "--urls" ) ;
13+ const newPrimaryUrls = urlsIndex >= 0 ? process . argv [ urlsIndex + 1 ] : undefined ;
14+
15+ const fallbackUrlsIndex = process . argv . indexOf ( "--fallbackUrls" ) ;
16+ const newFallbackUrls = fallbackUrlsIndex >= 0 ? process . argv [ fallbackUrlsIndex + 1 ] : undefined ;
17+
18+ if ( newPrimaryUrls === undefined || newPrimaryUrls === "-?" || newPrimaryUrls === "-h" ) {
19+ console . log ( "This command will update the URLs for package dependencies in package.json" ) ;
20+ console . log ( ) ;
21+ console . log ( "Syntax: updatePackageDependencies --urls \"<url1>,<url2>,...\" [--fallbackUrls \"<fallback-url-1>,<fallback-url-2>...\"]" ) ;
22+ console . log ( ) ;
23+ return ;
24+ }
25+
26+ if ( newPrimaryUrls . length === 0 ) {
27+ throw new Error ( "Invalid first argument to updatePackageDependencies. URL string argument expected." ) ;
28+ }
29+
30+ let packageJSON : PackageJSONFile = JSON . parse ( fs . readFileSync ( 'package.json' ) . toString ( ) ) ;
31+
32+ // map from lowercase filename to Package
33+ const mapFileNameToDependency = { } ;
34+
35+ // First build the map
36+ packageJSON . runtimeDependencies . forEach ( dependency => {
37+ let fileName = getLowercaseFileNameFromUrl ( dependency . url ) ;
38+ let existingDependency = mapFileNameToDependency [ fileName ] ;
39+ if ( existingDependency !== undefined ) {
40+ throw new Error ( `Multiple dependencies found with filename '${ fileName } ': '${ existingDependency . url } ' and '${ dependency . url } '.` ) ;
41+ }
42+ mapFileNameToDependency [ fileName ] = dependency ;
43+ } ) ;
44+
45+ let findDependencyToUpdate = ( url : string ) : Package => {
46+ let fileName = getLowercaseFileNameFromUrl ( url ) ;
47+ let dependency = mapFileNameToDependency [ fileName ] ;
48+ if ( dependency === undefined ) {
49+ throw new Error ( `Unable to update item for url '${ url } '. No 'runtimeDependency' found with filename '${ fileName } '.` ) ;
50+ }
51+
52+ console . log ( `Updating ${ url } ` ) ;
53+ return dependency ;
54+ } ;
55+
56+ newPrimaryUrls . split ( ',' ) . forEach ( urlToUpdate => {
57+ let depedency = findDependencyToUpdate ( urlToUpdate ) ;
58+ depedency . url = urlToUpdate ;
59+ } ) ;
60+
61+ if ( newFallbackUrls !== undefined ) {
62+ newFallbackUrls . split ( ',' ) . forEach ( urlToUpdate => {
63+ let depedency = findDependencyToUpdate ( urlToUpdate ) ;
64+ depedency . fallbackUrl = urlToUpdate ;
65+ } ) ;
66+ }
67+
68+ let content = JSON . stringify ( packageJSON , null , 2 ) ;
69+ if ( os . platform ( ) === 'win32' ) {
70+ content = content . replace ( / \n / gm, "\r\n" ) ;
71+ }
72+ fs . writeFileSync ( 'package.json' , content ) ;
73+ }
74+
75+ function getLowercaseFileNameFromUrl ( url : string ) : string {
76+
77+ if ( ! url . startsWith ( "https://" ) ) {
78+ throw new Error ( `Unexpected URL '${ url } '. URL expected to start with 'https://'.` ) ;
79+ }
80+
81+ if ( ! url . endsWith ( ".zip" ) ) {
82+ throw new Error ( `Unexpected URL '${ url } '. URL expected to end with '.zip'.` ) ;
83+ }
84+
85+ let index = url . lastIndexOf ( "/" ) ;
86+ return url . substr ( index + 1 ) . toLowerCase ( ) ;
87+ }
0 commit comments