UNPKG

@graphql-tools/git-loader

Version:

A set of utils for faster development of GraphQL tools

93 lines (92 loc) 2.84 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseGitTreeOutput = parseGitTreeOutput; exports.readTreeAtRef = readTreeAtRef; exports.readTreeAtRefSync = readTreeAtRefSync; exports.loadFromGit = loadFromGit; exports.loadFromGitSync = loadFromGitSync; const tslib_1 = require("tslib"); const child_process_1 = require("child_process"); const unixify_1 = tslib_1.__importDefault(require("unixify")); const createLoadError = (error) => new Error('Unable to load file from git: ' + error); const createShowCommand = ({ ref, path }) => { return ['show', `${ref}:${path}`]; }; const createTreeError = (error) => new Error('Unable to load the file tree from git: ' + error); const createTreeCommand = ({ ref }) => { return ['ls-tree', '-r', '--name-only', ref]; }; /** * @internal */ function parseGitTreeOutput(stdout) { // Git always emits LF; do not use os.EOL (CRLF on Windows) or the tree // collapses to a single unusable entry and micromatch matches nothing. // Do not trim entries — git pathnames can legally have leading/trailing spaces. return stdout .split(/\r?\n/) .filter(line => line.length > 0) .map(line => (0, unixify_1.default)(line)); } /** * @internal */ async function readTreeAtRef(ref) { try { return await new Promise((resolve, reject) => { (0, child_process_1.execFile)('git', createTreeCommand({ ref }), { encoding: 'utf-8', maxBuffer: 1024 * 1024 * 1024 }, (error, stdout) => { if (error) { reject(error); } else { resolve(parseGitTreeOutput(stdout)); } }); }); } catch (error) { throw createTreeError(error); } } /** * @internal */ function readTreeAtRefSync(ref) { try { return parseGitTreeOutput((0, child_process_1.execFileSync)('git', createTreeCommand({ ref }), { encoding: 'utf-8' })); } catch (error) { throw createTreeError(error); } } /** * @internal */ async function loadFromGit(input) { try { return await new Promise((resolve, reject) => { (0, child_process_1.execFile)('git', createShowCommand(input), { encoding: 'utf-8', maxBuffer: 1024 * 1024 * 1024 }, (error, stdout) => { if (error) { reject(error); } else { resolve(stdout); } }); }); } catch (error) { throw createLoadError(error); } } /** * @internal */ function loadFromGitSync(input) { try { return (0, child_process_1.execFileSync)('git', createShowCommand(input), { encoding: 'utf-8' }); } catch (error) { throw createLoadError(error); } }