@platformos/pos-cli
Version:
Manage your platformOS application
141 lines (123 loc) • 3.38 kB
JavaScript
;
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var fs = _interopDefault(require('fs'));
var path = _interopDefault(require('path'));
var sander = require('sander');
var homeOrTmp = _interopDefault(require('home-or-tmp'));
var https = _interopDefault(require('https'));
var child_process = _interopDefault(require('child_process'));
var URL = _interopDefault(require('url'));
var Agent = _interopDefault(require('https-proxy-agent'));
const tmpDirName = 'tmp';
const degitConfigName = 'degit.json';
class DegitError extends Error {
constructor(message, opts) {
super(message);
Object.assign(this, opts);
}
}
function tryRequire(file, opts) {
try {
if (opts && opts.clearCache === true) {
delete require.cache[require.resolve(file)];
}
return require(file);
} catch (err) {
return null;
}
}
function exec(command) {
return new Promise((fulfil, reject) => {
child_process.exec(command, (err, stdout, stderr) => {
if (err) {
reject(err);
return;
}
fulfil({ stdout, stderr });
});
});
}
function mkdirp(dir) {
const parent = path.dirname(dir);
if (parent === dir) return;
mkdirp(parent);
try {
fs.mkdirSync(dir);
} catch (err) {
if (err.code !== 'EEXIST') throw err;
}
}
function fetch(url, dest, proxy) {
return new Promise((fulfil, reject) => {
let options = url;
if (proxy) {
const parsedUrl = URL.parse(url);
options = {
hostname: parsedUrl.host,
path: parsedUrl.path,
agent: new Agent(proxy)
};
}
https
.get(options, response => {
const code = response.statusCode;
if (code >= 400) {
reject({ code, message: response.statusMessage });
} else if (code >= 300) {
fetch(response.headers.location, dest, proxy).then(fulfil, reject);
} else {
response
.pipe(fs.createWriteStream(dest))
.on('finish', () => fulfil())
.on('error', reject);
}
})
.on('error', reject);
});
}
function stashFiles(dir, dest) {
const tmpDir = path.join(dir, tmpDirName);
sander.rimrafSync(tmpDir);
mkdirp(tmpDir);
fs.readdirSync(dest).forEach(file => {
const filePath = path.join(dest, file);
const targetPath = path.join(tmpDir, file);
const isDir = fs.lstatSync(filePath).isDirectory();
if (isDir) {
sander.copydirSync(filePath).to(targetPath);
sander.rimrafSync(filePath);
} else {
fs.copyFileSync(filePath, targetPath);
fs.unlinkSync(filePath);
}
});
}
function unstashFiles(dir, dest) {
const tmpDir = path.join(dir, tmpDirName);
fs.readdirSync(tmpDir).forEach(filename => {
const tmpFile = path.join(tmpDir, filename);
const targetPath = path.join(dest, filename);
const isDir = fs.lstatSync(tmpFile).isDirectory();
if (isDir) {
sander.copydirSync(tmpFile).to(targetPath);
sander.rimrafSync(tmpFile);
} else {
if (filename !== 'degit.json') {
fs.copyFileSync(tmpFile, targetPath);
}
fs.unlinkSync(tmpFile);
}
});
sander.rimrafSync(tmpDir);
}
const base = path.join(homeOrTmp, '.degit');
exports.DegitError = DegitError;
exports.base = base;
exports.degitConfigName = degitConfigName;
exports.exec = exec;
exports.fetch = fetch;
exports.mkdirp = mkdirp;
exports.stashFiles = stashFiles;
exports.tryRequire = tryRequire;
exports.unstashFiles = unstashFiles;
//# sourceMappingURL=utils-59234185.js.map