roc
Version:
Build modern web applications easily
65 lines (51 loc) • 1.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = unzip;
var _temp = require('temp');
var _temp2 = _interopRequireDefault(_temp);
var _fs = require('fs');
var _fs2 = _interopRequireDefault(_fs);
var _unzip = require('unzip');
var _unzip2 = _interopRequireDefault(_unzip);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Automatically track and cleanup files at exit
_temp2.default.track();
/**
* Unpacks the given zip file.
*
* @param {string} zipFile - The full path to a local zip file.
*
* @returns {Promise} The path to the temporary directory where the unzipped files are located.
*/
function unzip(zipFile) {
if (!zipFile) {
throw new Error('No zip file was given.');
}
return new Promise((resolve, reject) => {
_temp2.default.mkdir('roc', (err, dirPath) => {
if (err) {
return reject(err);
}
_fs2.default.createReadStream(zipFile).pipe(_unzip2.default.Extract({ path: dirPath })) // eslint-disable-line new-cap
.on('error', reject).on('close', () => {
// The template zip is assumed to have a root dir
try {
_fs2.default.readdirSync(dirPath).forEach(file => {
const rootDir = _path2.default.join(dirPath, file);
if (_fs2.default.statSync(rootDir).isDirectory()) {
return resolve(rootDir);
}
});
} catch (error) {
return reject(error);
}
return resolve(dirPath);
});
});
});
}
//# sourceMappingURL=unzip.js.map