nativescript-doctor
Version:
Library that helps identifying if the environment can be used for development of {N} apps.
67 lines (66 loc) • 2.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const fs = require("fs");
const path = require("path");
const yauzl = require("yauzl");
const util = require("util");
const shelljs = require("shelljs");
const access = util.promisify(fs.access);
const mkdir = util.promisify(fs.mkdir);
class FileSystem {
exists(filePath) {
return fs.existsSync(path.resolve(filePath));
}
extractZip(pathToZip, outputDir) {
return new Promise((resolve, reject) => {
yauzl.open(pathToZip, { autoClose: true, lazyEntries: true }, (openError, zipFile) => {
if (openError) {
return reject(openError);
}
zipFile.on('entry', entry => {
const fn = entry.fileName;
if (/\/$/.test(fn)) {
return zipFile.readEntry();
}
zipFile.openReadStream(entry, (openStreamError, stream) => {
if (openStreamError) {
return reject(openStreamError);
}
const filePath = `${outputDir}/${fn}`;
return createParentDirsIfNeeded(filePath)
.catch(createParentDirError => reject(createParentDirError))
.then(() => {
const outfile = fs.createWriteStream(filePath);
stream.once('end', () => {
zipFile.readEntry();
});
stream.pipe(outfile);
});
});
});
zipFile.once('end', () => resolve());
zipFile.readEntry();
});
});
}
readDirectory(directoryPath) {
return fs.readdirSync(directoryPath);
}
readJson(filePath, options) {
const content = fs.readFileSync(filePath, options);
return JSON.parse(content.toString());
}
deleteEntry(filePath) {
shelljs.rm("-rf", filePath);
}
}
exports.FileSystem = FileSystem;
function createParentDirsIfNeeded(filePath) {
const dirs = path.dirname(filePath).split(path.sep);
return dirs.reduce((p, dir) => p.then(parent => {
const current = `${parent}${path.sep}${dir}`;
return access(current)
.catch(e => mkdir(current))
.then(() => current);
}), Promise.resolve(''));
}