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.
npm run import -- "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));
});
});
});
// 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:
Initialization:
$.async();
: Likely initializes an asynchronous operation.var exec = require('child_process').exec;
: Imports the exec
function for running shell commands.var http = require('https');
: Imports the https
module for making HTTP requests.var fs = require('fs');
: Imports the fs
module for file system operations.var elevateLoc = path.join(process.cwd(), 'elevate.zip');
: Defines the path to the downloaded zip file.var expandedLoc = path.join(process.cwd(), 'elevate');
: Defines the path to the extracted elevate directory.Downloading elevate.zip:
http.get('https://github.com/jpassing/elevate/releases/download/1.0/elevate.zip', (r) => { ... });
: Downloads the elevate.zip file from the specified URL.
http.get(r.headers['location'], (r) => { ... });
: Redirects to the actual download URL from the location
header.
r.pipe(fs.createWriteStream(elevateLoc)).on('finish', () => { ... });
: Pipes the downloaded data to a file stream and writes it to elevateLoc
.
on('finish')
event handler executes after the download completes:Extracting elevate:
var expand = exec('powershell -c "Expand-Archive -Force ' + elevateLoc + ' ' + expandedLoc + '"', () => { ... });
: Executes a PowerShell command to extract the contents of elevate.zip
to expandedLoc
.
$.done('downloaded and extracted elevate.exec');
: Signals completion of the asynchronous operation.expand.stdout.on('data', (d) => console.log(d));
: Logs the standard output of the extraction process.expand.stderr.on('data', (d) => console.log(d));
: Logs any standard error output from the extraction process.