install Docker on Windows | install Docker on Windows | Where do I download the Windows Docker installer | Search

This code downloads the "elevate" tool from GitHub, extracts it to a local directory, and signals completion of the process. It uses https to download the file, fs to write it to disk, and child_process to execute a PowerShell command for extraction.

Run example

npm run import -- "How do I installed elevated from the command line"

How do I installed elevated from the command line

$.async();
var exec = require('child_process').exec;
var http = require('https');
var fs = require('fs');
var elevateLoc = path.join(process.cwd(), 'elevate.zip');
var expandedLoc = path.join(process.cwd(), 'elevate');
http.get('https://github.com/jpassing/elevate/releases/download/1.0/elevate.zip', (r) => {
    http.get(r.headers['location'], (r) => {
        r.pipe(fs.createWriteStream(elevateLoc)).on('finish', () => {
            var expand = exec('powershell -c "Expand-Archive -Force ' + elevateLoc + ' ' + expandedLoc + '"', () => {
                $.done('downloaded and extracted elevate.exec');
            });
            expand.stdout.on('data', (d) => console.log(d));
            expand.stderr.on('data', (d) => console.log(d));
        });
    });
});

What the code could have been:

// Import required modules
const { promisify } = require('util');
const https = require('https');
const fs = require('fs');
const path = require('path');
const childProcess = require('child_process');

// Constants
const ELEVATE_ZIP_URL = 'https://github.com/jpassing/elevate/releases/download/1.0/elevate.zip';
const ELEVATE_ZIP_LOC = path.join(process.cwd(), 'elevate.zip');
const EXPANDED_LOC = path.join(process.cwd(), 'elevate');

// Define a function to download the elevate archive
async function downloadElevate() {
  const request = https.get(ELEVATE_ZIP_URL);
  const writeStream = fs.createWriteStream(ELEVATE_ZIP_LOC);
  return new Promise((resolve, reject) => {
    request.on('response', (r) => {
      writeStream.on('finish', () => {
        resolve(ELEVATE_ZIP_LOC);
      });
      r.pipe(writeStream).on('error', (e) => {
        reject(e);
      });
    });
  });
}

// Define a function to extract the archive
async function extractElevate(archiveLoc) {
  const expand = promisify(childProcess.exec);
  try {
    await expand(`powershell -c "Expand-Archive -Force ${archiveLoc} ${EXPANDED_LOC}"`);
    console.log('downloaded and extracted elevate.exec');
  } catch (e) {
    console.error('Error extracting archive:', e);
  }
}

// Download and extract the elevate archive
downloadElevate()
 .then((archiveLoc) => extractElevate(archiveLoc))
 .catch((e) => console.error('Error downloading or extracting archive:', e));

This code downloads and extracts the "elevate" tool, a utility for running commands with elevated privileges on Windows.

Breakdown:

  1. Initialization:

  2. Downloading elevate.zip:

  3. Extracting elevate: