@pnpm/git-fetcher
Version:
A fetcher for git-hosted packages
87 lines • 3.7 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createGitFetcher = createGitFetcher;
const assert_1 = __importDefault(require("assert"));
const path_1 = __importDefault(require("path"));
const util_1 = __importDefault(require("util"));
const fs_packlist_1 = require("@pnpm/fs.packlist");
const logger_1 = require("@pnpm/logger");
const prepare_package_1 = require("@pnpm/prepare-package");
const worker_1 = require("@pnpm/worker");
const rimraf_1 = __importDefault(require("@zkochan/rimraf"));
const execa_1 = __importDefault(require("execa"));
const url_1 = require("url");
function createGitFetcher(createOpts) {
const allowedHosts = new Set(createOpts?.gitShallowHosts ?? []);
const ignoreScripts = createOpts.ignoreScripts ?? false;
const preparePkg = prepare_package_1.preparePackage.bind(null, {
ignoreScripts: createOpts.ignoreScripts,
rawConfig: createOpts.rawConfig,
unsafePerm: createOpts.unsafePerm,
});
const gitFetcher = async (cafs, resolution, opts) => {
const tempLocation = await cafs.tempDir();
if (allowedHosts.size > 0 && shouldUseShallow(resolution.repo, allowedHosts)) {
await execGit(['init'], { cwd: tempLocation });
await execGit(['remote', 'add', 'origin', resolution.repo], { cwd: tempLocation });
await execGit(['fetch', '--depth', '1', 'origin', resolution.commit], { cwd: tempLocation });
}
else {
await execGit(['clone', resolution.repo, tempLocation]);
}
await execGit(['checkout', resolution.commit], { cwd: tempLocation });
let pkgDir;
try {
const prepareResult = await preparePkg(tempLocation, resolution.path ?? '');
pkgDir = prepareResult.pkgDir;
if (ignoreScripts && prepareResult.shouldBeBuilt) {
(0, logger_1.globalWarn)(`The git-hosted package fetched from "${resolution.repo}" has to be built but the build scripts were ignored.`);
}
}
catch (err) {
(0, assert_1.default)(util_1.default.types.isNativeError(err));
err.message = `Failed to prepare git-hosted package fetched from "${resolution.repo}": ${err.message}`;
throw err;
}
// removing /.git to make directory integrity calculation faster
await (0, rimraf_1.default)(path_1.default.join(tempLocation, '.git'));
const files = await (0, fs_packlist_1.packlist)(pkgDir);
// Important! We cannot remove the temp location at this stage.
// Even though we have the index of the package,
// the linking of files to the store is in progress.
return (0, worker_1.addFilesFromDir)({
storeDir: cafs.storeDir,
dir: pkgDir,
files,
filesIndexFile: opts.filesIndexFile,
readManifest: opts.readManifest,
pkg: opts.pkg,
});
};
return {
git: gitFetcher,
};
}
function shouldUseShallow(repoUrl, allowedHosts) {
try {
const { host } = new url_1.URL(repoUrl);
if (allowedHosts.has(host)) {
return true;
}
}
catch {
// URL might be malformed
}
return false;
}
function prefixGitArgs() {
return process.platform === 'win32' ? ['-c', 'core.longpaths=true'] : [];
}
async function execGit(args, opts) {
const fullArgs = prefixGitArgs().concat(args || []);
await (0, execa_1.default)('git', fullArgs, opts);
}
//# sourceMappingURL=index.js.map
;