dismjs
Version:
A NPM module to manipulate the "Deployment Image Servicing and Management" tool.
276 lines (240 loc) • 12.1 kB
JavaScript
class DISM{
constructor(execute_func){
this.execute_func = execute_func || null;
}
async isAdmin(){
let r = await this.execute_func(`dism /English`);
return !r.stdout.includes("Elevated permissions are required to run DISM.");
}
async checkHealth(){
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
let r = await this.execute_func('dism /Online /Cleanup-Image /CheckHealth /English');
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
return r.stdout.includes('No component store corruption detected.');
}
async scanHealth() {
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
var r = await this.execute_func('dism /Online /Cleanup-Image /ScanHealth /English');
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
var csc = !r.stdout.includes('No component store corruption detected.');
var success = r.stdout.includes('The operation completed successfully.');
return [csc, success];
}
async restoreHealth() {
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
var r = await this.execute_func('dism /Online /Cleanup-Image /RestoreHealth /English');
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
return r.stdout.includes('The restore operation completed successfully.');
}
async restoreHealthExt(external_source, limit_access = false) {
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
var cmd = ['dism', '/Online', '/Cleanup-Image', '/RestoreHealth', `/Source:${external_source}`, '/English'];
if (limit_access) {
cmd.push('/LimitAccess');
}
var r = await this.execute_func(cmd.join(' '));
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
return r.stdout.includes('No component store corruption detected.');
}
// Working with Windows Images
async getWimInfo(wim, index = 1) {
var r = await this.execute_func(`dism /English /Get-WimInfo /wimfile=${wim} /Index:${index}`);
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
class WIM {
constructor() {
this.index = this.size = this.version = this.ServicePack_build = this.ServicePack_level = this.directories = this.files = -1;
this.name = this.description = this.architecture = this.hal = this.edition = this.installation = this.productType = this.productSuite = this.systemRoot = this.defaultLanguage = this.path = null;
this.bootable = false;
this.created = this.modified = new Date();
this.languages = [];
}
}
function turnStringIntoDate(text) {
return new Date((text[3] + text[4]) + '/' + (text[0] + text[1]) + text.slice(5, 10) + ' ' + text.split('-')[1])
}
function getLanguages(output, defaultLang = false) {
var languages = [];
var lines = output.split('\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i].includes('Languages :')) {
for (let j = 0; j < lines.length - i; j++) {
if (!lines[j + i].includes('The operation completed successfully.') && !lines[j + i].includes('Languages :')) {
let lang = lines[j + i].replaceAll(/ /g, '').replaceAll(/\t/g, '');
if (!defaultLang) {
if (lang.includes('(Default)')) {
defaultLang = lang.replaceAll('(Default)', '');
}
lang = lang.replaceAll('(Default)', '');
}
if (lang !== '') {
languages.push(lang);
}
}
}
}
}
return defaultLang ? [languages, defaultLang] : languages;
}
function getDefaultLanguage(output) {
return getLanguages(output, true)[1];
}
var data_ = new WIM();
data_.path = wim;
for (var line of r.stdout.split('\n')) {
line = line.replaceAll('\r', '');
if (line.includes(' : ')) {
var one = line.split(' : ')[1];
switch (line.split(' : ')[0]) {
case 'Index':
data_.index = parseInt(one);
break;
case 'Name':
data_.name = one;
break;
case 'Description':
data_.description = one;
break;
case 'Size':
data_.size = parseInt(one.replaceAll('ÿ', '').replaceAll(' bytes', ''));
break;
case 'WIM Bootable':
data_.bootable = (one === 'Yes');
break;
case 'Architecture':
data_.architecture = one;
break;
case 'Hal':
data_.hal = one;
break;
case 'Version':
data_.version = one;
break;
case 'ServicePack Build':
data_.ServicePack_build = parseInt(one);
break;
case 'ServicePack Level':
data_.ServicePack_level = parseInt(one);
break;
case 'Edition':
data_.edition = one;
break;
case 'Installation':
data_.installation = one;
break;
case 'ProductType':
data_.productType = one;
break;
case 'ProductSuite':
data_.productSuite = one;
break;
case 'System Root':
data_.systemRoot = one;
break;
case 'Directories':
data_.directories = parseInt(one);
break;
case 'Files':
data_.files = parseInt(one);
break;
case 'Created':
data_.created = turnStringIntoDate(one);
break;
case 'Modified':
data_.modified = turnStringIntoDate(one);
break;
default:
break;
}
}
}
var _ = getLanguages(r.stdout);
data_.languages = _[0]
data_.defaultLanguage = _[1];
return data_;
}
async listWimImages(wim) {
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
function getImages(text_) {
function getLinesFromTo(text, from_, to_) {
var lines = [];
for (let i = from_ - 1; i < to_; i++) {
lines.push(text.split('\n')[i]);
}
return lines.join('\n');
}
var images = [];
class WIMImage {
constructor() {
this.index = this.size = -1;
this.name = this.description = null;
}
}
text_ = text_.replaceAll('\r', '');
var l = getLinesFromTo(text_, 7, text_.split('\n').indexOf('The operation completed successfully.') - 1);
for (var image of l.split('\n\n')) {
var img = new WIMImage();
img.index = Number(image.split('\n')[0].split(' : ')[1]);
img.name = image.split('\n')[1].split(' : ')[1];
img.description = image.split('\n')[2].split(' : ')[1];
img.size = Number(image.split('\n')[3].split(' : ')[1].replaceAll(/\s/g, '').replaceAll('bytes', '').replaceAll('ÿ', '').replaceAll('�', ''));
images.push(img);
}
return images;
}
var r = await this.execute_func(`dism /Get-ImageInfo /imagefile:${wim} /English`);
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
return getImages(r.stdout);
}
async mountWim(wim, mounting_path, index = 1, readonly = true) {
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
var cmd = [`dism`, `/Mount-Wim`, `/WimFile:${wim}`, `/Index:` + index, `/MountDir:${mounting_path}`];
if (readonly) {
cmd.push('/ReadOnly');
}
var r = await this.execute_func(cmd.join(' '));
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
return r.stdout;
}
async unmountWim(mounting_path, commit = false) {
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
var cmd = [`dism`, `/Unmount-Wim`, `/MountDir:${mounting_path}`];
cmd.push(commit ? '/Commit' : '/Discard');
var r = await this.execute_func(cmd.join(' '));
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
return r.stdout;
}
async addPackage(mounting_path, _package) {
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
let r = await this.execute_func(`dism /Image:${mounting_path} /Add-Package /PackagePath:${_package}`);
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
}
async removePackage(mounting_path, _package) {
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
let r = await this.execute_func(`dism /Image:${mounting_path} /Remove-Package /PackageName:${_package}`);
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
}
async addDriver(mounting_path, driver) {
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
let r = await this.execute_func(`dism /Image:${mounting_path} /Add-Driver /Driver:${driver}`);
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
}
async removeDriver(mounting_path, driver) {
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
let r = await this.execute_func(`dism /Image:${mounting_path} /Remove-Driver /Driver:${driver}`);
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
}
// Capture and Apply Windows Images
async captureImage(wim, capture_dir, name) {
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
var r = await this.execute_func(`dism /Capture-Image /ImageFile:${wim} /CaptureDir:${capture_dir} /Name:"${name}" /English`);
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
return r.stdout.includes('The operation completed successfully.');
}
async applyImage(wim, index = 1, apply_dir) {
if (typeof this.execute_func != 'function') throw Error('"execute_func" is not a function.')
var r = await this.execute_func(`dism /Apply-Image /ImageFile:${wim} /Index:${index} /ApplyDir:${apply_dir} /English`);
if (r.stdout.includes("Elevated permissions are required to run DISM.")) throw Error('dismjs doesn\'t have admin rights and therefore cannot execute DISM.');
return r.stdout.includes('The operation completed successfully.');
}
}
module.exports = DISM