88// [...]
99// redirecting to latest generated builds by replacing latest with the latest available build date, using the format YYYYMMDD (i.e for 2019/Aug/06 latest is replaced with 20190806
1010
11- ( async ( ) => {
11+ ( ( ) => {
1212
1313 const DEFAULT_VERSION = 'latest' ; // require('moment')().format('YYYYMMDD');
1414
1515 const os = require ( 'os' ) ;
16- const fs = require ( 'fs' ) ;
1716 const path = require ( 'path' ) ;
1817 const shell = require ( 'shelljs' ) ;
19- const download = require ( 'download' ) ;
20- const decompress = require ( 'decompress' ) ;
21- const unzip = require ( 'decompress-unzip' ) ;
22- const untargz = require ( 'decompress-targz' ) ;
23-
24- process . on ( 'unhandledRejection' , ( reason , _ ) => {
25- shell . echo ( String ( reason ) ) ;
26- shell . exit ( 1 ) ;
27- throw reason ;
28- } ) ;
29- process . on ( 'uncaughtException' , error => {
30- shell . echo ( String ( error ) ) ;
31- shell . exit ( 1 ) ;
32- throw error ;
33- } ) ;
18+ const downloader = require ( './downloader' ) ;
3419
3520 const yargs = require ( 'yargs' )
3621 . option ( 'cli-version' , {
5035 const { platform, arch } = process ;
5136
5237 const build = path . join ( __dirname , '..' , 'build' ) ;
53- const downloads = path . join ( __dirname , '..' , 'downloads' ) ;
5438 const cli = path . join ( build , `arduino-cli${ os . platform ( ) === 'win32' ? '.exe' : '' } ` ) ;
5539
56- if ( fs . existsSync ( cli ) && ! force ) {
57- shell . echo ( `The 'arduino-cli' already exists at ${ cli } . Skipping download.` ) ;
58- shell . exit ( 0 ) ;
59- }
60- if ( ! fs . existsSync ( build ) ) {
61- if ( shell . mkdir ( '-p' , build ) . code !== 0 ) {
62- shell . echo ( 'Could not create new directory.' ) ;
63- shell . exit ( 1 ) ;
64- }
65- }
66- if ( shell . rm ( '-rf' , cli , downloads ) . code !== 0 ) {
67- shell . exit ( 1 ) ;
68- }
69-
7040 const suffix = ( ( ) => {
7141 switch ( platform ) {
7242 case 'darwin' : return 'macOS_64bit.tar.gz' ;
8757 }
8858
8959 const url = `https://downloads.arduino.cc/arduino-cli/nightly/arduino-cli_nightly-${ version } _${ suffix } ` ;
90- shell . echo ( `>>> Downloading 'arduino-cli' from '${ url } '...` ) ;
91- const data = await download ( url ) ;
92- shell . echo ( `<<< Download succeeded.` ) ;
93- shell . echo ( '>>> Decompressing CLI...' ) ;
94- const files = await decompress ( data , downloads , {
95- plugins : [
96- unzip ( ) ,
97- untargz ( )
98- ]
99- } ) ;
100- if ( files . length === 0 ) {
101- shell . echo ( 'Error ocurred when decompressing the CLI.' ) ;
102- shell . exit ( 1 ) ;
103- }
104- const cliIndex = files . findIndex ( f => f . path . startsWith ( 'arduino-cli' ) ) ;
105- if ( cliIndex === - 1 ) {
106- shell . echo ( 'The downloaded artifact does not contains the CLI.' ) ;
107- shell . exit ( 1 ) ;
108- }
109- shell . echo ( '<<< Decompressing succeeded.' ) ;
110-
111- if ( shell . mv ( '-f' , path . join ( downloads , files [ cliIndex ] . path ) , cli ) . code !== 0 ) {
112- shell . echo ( `Could not move file to ${ cli } .` ) ;
113- shell . exit ( 1 ) ;
114- }
115- if ( ! fs . existsSync ( cli ) ) {
116- shell . echo ( `Could not find CLI at ${ cli } .` ) ;
117- shell . exit ( 1 ) ;
118- } else {
119- shell . echo ( 'Done.' ) ;
120- }
60+ downloader . download ( url , cli , 'arduino-cli' , force ) ;
12161
12262} ) ( ) ;
0 commit comments