veendor
Version:
a tool for stroing your npm dependencies in arbitraty storage
132 lines (131 loc) • 5 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const gitWrapper = __importStar(require("../commandWrappers/gitWrapper"));
const tarWrapper = __importStar(require("../commandWrappers/tarWrapper"));
const errors = __importStar(require("../errors"));
exports.keepCache = true;
let _remoteIsFresh = false;
function setRemoteFreshness(val) {
_remoteIsFresh = val;
}
exports.setRemoteFreshness = setRemoteFreshness;
function validateOptions(options) {
return new Promise((resolve, reject) => {
if (typeof options.repo !== 'string' || options.repo.length === 0) {
return reject(new errors.InvalidOptionsError('Invalid git repo'));
}
if (options.compression && !(options.compression in tarWrapper.compression)) {
return reject(new errors.InvalidOptionsError(`Invalid compression: ${options.compression}`));
}
if (options.compression === undefined) {
options.compression = 'gzip';
}
if (options.defaultBranch === undefined) {
options.defaultBranch = 'master';
}
if (options.checkLfsAvailability === undefined) {
options.checkLfsAvailability = false;
resolve();
}
else {
if (typeof options.checkLfsAvailability !== 'boolean') {
return reject(new errors.InvalidOptionsError(`Invalid 'checkLfsAvailability' option: ${options.checkLfsAvailability}`));
}
if (options.checkLfsAvailability) {
gitWrapper.isGitLfsAvailable().then(resolve, () => {
reject(new gitWrapper.GitLfsNotAvailableError('git-lfs is not available. Check git-lfs.github.com for docs.'));
});
}
else {
resolve();
}
}
});
}
exports.validateOptions = validateOptions;
function pull(hash, options, cacheDir) {
const repoDir = path_1.default.resolve(cacheDir, 'repo');
return gitWrapper.isGitRepo(repoDir)
.then(res => {
if (res) {
if (_remoteIsFresh) {
return Promise.resolve();
}
return gitWrapper.fetch(repoDir).then(() => { });
}
else {
if (_remoteIsFresh) {
return Promise.resolve();
}
return gitWrapper.clone(options.repo, repoDir).then(() => { });
}
})
.then(() => {
_remoteIsFresh = true;
return gitWrapper.checkout(repoDir, `veendor-${hash}`)
.then(() => {
return new Promise((resolve, reject) => {
gitWrapper.isGitLfsAvailable().then(() => {
gitWrapper.lfsPull(repoDir).then(resolve, reject);
}, resolve);
});
}, () => {
throw new errors.BundleNotFoundError;
})
.then(() => {
return tarWrapper.extractArchive(path_1.default.resolve(repoDir, `${hash}.tar${tarWrapper.compression[options.compression]}`));
});
});
}
exports.pull = pull;
function push(hash, options, cacheDir) {
const repoDir = path_1.default.resolve(cacheDir, 'repo');
const archivePath = path_1.default.resolve(repoDir, `${hash}.tar${tarWrapper.compression[options.compression]}`);
const tagName = `veendor-${hash}`;
return fs_extra_1.default.access(repoDir, fs_extra_1.default.constants.F_OK)
.then(() => {
return gitWrapper.fetch(repoDir);
}, () => {
return gitWrapper.clone(options.repo, repoDir);
})
.then(() => {
return gitWrapper.checkout(repoDir, options.defaultBranch);
})
.then(() => {
return gitWrapper.resetToRemote(repoDir, options.defaultBranch);
})
.then(() => {
return tarWrapper.createArchive(archivePath, [path_1.default.resolve(process.cwd(), 'node_modules')], options.compression);
})
.then(() => {
return gitWrapper.add(repoDir, [archivePath], true);
})
.then(() => {
return gitWrapper.commit(repoDir, hash);
})
.then(() => {
return gitWrapper.tag(repoDir, tagName);
})
.then(() => {
return gitWrapper.push(repoDir, tagName);
})
.catch(error => {
if (error instanceof gitWrapper.RefAlreadyExistsError) {
throw new errors.BundleAlreadyExistsError();
}
throw error;
});
}
exports.push = push;