33// the apio binary is available at ~/.apio/bin/apio[.exe]. If not,
44// It's downloaded and installed on the fly.
55
6- "use strict" ;
7-
86// Standard imports
9- const fs = require ( "fs" ) ;
10- const path = require ( "path" ) ;
11- const stream = require ( "node:stream" ) ;
12- const childProcess = require ( "child_process" ) ;
13- const assert = require ( "node:assert" ) ;
7+ import * as fs from "fs" ;
8+ import * as path from "path" ;
9+ import * as stream from "node:stream" ;
10+ import * as childProcess from "child_process" ;
11+
12+ // Assert function.
13+ import assert from "node:assert" ;
1414
1515// Dependency imports
16- const zipExtract = require ( "extract-zip" ) ;
17- const tar = require ( "tar" ) ;
16+ import * as zipExtract from "extract-zip" ;
17+ import * as tar from "tar" ;
1818
1919// Local imports
20- const constants = require ( "./constants.js" ) ;
21- const platforms = require ( "./platforms.js" ) ;
22- const apioLog = require ( "./apio-log.js" ) ;
23- const jsonUtils = require ( "./json-utils.js" ) ;
24- const utils = require ( "./utils.js" ) ;
20+ import * as constants from "./constants.js" ;
21+ import * as platforms from "./platforms.js" ;
22+ import * as apioLog from "./apio-log.js" ;
23+ import * as jsonUtils from "./json-utils.js" ;
24+ import * as utils from "./utils.js" ;
2525
2626// Download url and local package name
2727const downloadMetadataFileName = "download-metadata.json" ;
@@ -30,7 +30,7 @@ let _downloadDstFilePath = null;
3030
3131// Initializes this module. Should be called once before any other
3232// function of this module.
33- function init ( ) {
33+ export function init ( ) {
3434 // Should be called only once.
3535 assert (
3636 _downloadSrcUrl == null ,
@@ -53,33 +53,41 @@ function init() {
5353}
5454
5555// Ensures the Apio binary is ready and if not download and installs it.
56- // @returns {Promise<string> } that govern the downloading.
57- async function ensureApioBinary ( ) {
58- // Check if the apio binary exists.
59- const binaryExists = await _testFsItem (
60- utils . apioBinaryPath ( ) ,
61- fs . constants . X_OK | fs . constants . R_OK
62- ) ;
63- apioLog . msg ( `Apio binary ${ utils . apioBinaryPath ( ) } exists = ${ binaryExists } ` ) ;
64-
65- // Check if the download metadata has a matching download url
66- const metadataFilePath = path . join (
67- utils . apioBinDir ( ) ,
68- downloadMetadataFileName
69- ) ;
70- // 'metadata' is {} if file doesn't exist or any error.
71- const metadataDict = await jsonUtils . readJson ( metadataFilePath ) ;
72- const lastUrl = metadataDict . url ?? null ;
73- const urlMatches = lastUrl == _downloadSrcUrl ;
74- apioLog . msg ( `Download url match = ${ urlMatches } ` ) ;
75-
76- if ( binaryExists && urlMatches ) {
77- // Binary is good, will use it.
78- apioLog . msg ( `Existing binary ok: ${ utils . apioBinaryPath ( ) } ` ) ;
79- } else {
80- // Binary is missing or not good, will download and install it.
81- apioLog . msg ( "Need to download and install a new binary." ) ;
82- await _downloadAndInstall ( ) ;
56+ // Throws an exception if failed.
57+ export async function ensureApioBinary ( ) {
58+ try {
59+ // Check if the apio binary exists.
60+ const binaryExists = await _testFsItem (
61+ utils . apioBinaryPath ( ) ,
62+ fs . constants . X_OK | fs . constants . R_OK
63+ ) ;
64+ apioLog . msg (
65+ `Apio binary ${ utils . apioBinaryPath ( ) } exists = ${ binaryExists } `
66+ ) ;
67+
68+ // Check if the download metadata has a matching download url
69+ const metadataFilePath = path . join (
70+ utils . apioBinDir ( ) ,
71+ downloadMetadataFileName
72+ ) ;
73+ // 'metadata' is {} if file doesn't exist or any error.
74+ const metadataDict = await jsonUtils . readJson ( metadataFilePath ) ;
75+ const lastUrl = metadataDict . url ?? null ;
76+ const urlMatches = lastUrl == _downloadSrcUrl ;
77+ apioLog . msg ( `Download url match = ${ urlMatches } ` ) ;
78+
79+ if ( binaryExists && urlMatches ) {
80+ // Binary is good, will use it.
81+ apioLog . msg ( `Existing binary ok: ${ utils . apioBinaryPath ( ) } ` ) ;
82+ } else {
83+ // Binary is missing or not good, will download and install it.
84+ apioLog . msg ( "Need to download and install a new binary." ) ;
85+ await _downloadAndInstall ( ) ;
86+ apioLog . msg ( `[Apio] Binary installed: ${ utils . apioBinaryPath ( ) } ` ) ;
87+ }
88+ } catch ( err ) {
89+ // Handle errors, we wrap with 'Apio' message and throw again.
90+ throw Error ( `[Apio] binary installation failed: ${ err . message } ` ) ;
8391 }
8492}
8593
@@ -224,6 +232,3 @@ async function _testFsItem(path, mode = fs.constants.F_OK) {
224232 return false ;
225233 }
226234}
227-
228- // Exported functions.
229- module . exports = { init, ensureApioBinary } ;
0 commit comments