@graphql-hive/cli
Version:
A CLI util to manage and control your GraphQL Hive
78 lines • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadSchemaFromGitHistory = loadSchemaFromGitHistory;
exports.parseBaselineGitFileReference = parseBaselineGitFileReference;
const tslib_1 = require("tslib");
const node_child_process_1 = require("node:child_process");
const win32_1 = tslib_1.__importDefault(require("node:path/win32"));
function isGlobPath(path) {
const globPattern = /[*?{}\[\]()]/;
return globPattern.test(path);
}
function parseSingleLocalFilePath(schemaPointer) {
if (isGlobPath(schemaPointer)) {
return {
status: 'error',
error: {
type: 'path',
message: 'Path is a glob pattern.',
},
};
}
return {
status: 'ok',
path: schemaPointer,
};
}
function loadGitFile(commit, path) {
try {
const sdl = (0, node_child_process_1.execFileSync)('git', ['show', `${commit}:${path}`], {
cwd: process.cwd(),
}).toString();
return {
status: 'ok',
sdl,
};
}
catch (error) {
if (error instanceof Error) {
return {
status: 'error',
error: {
type: 'git',
message: 'Failed to load schema SDL.\n' + error.message,
path,
},
};
}
throw error;
}
}
function loadSchemaFromGitHistory(filePointer, commit) {
const pathResult = parseSingleLocalFilePath(filePointer);
if (pathResult.error) {
return pathResult;
}
const fileResult = loadGitFile(commit, pathResult.path);
return fileResult;
}
function parseBaselineGitFileReference(schemaPointer) {
// mostly Windows :)
if (win32_1.default.isAbsolute(schemaPointer)) {
return {
status: 'error',
};
}
const [maybeCommit, maybeFilePath, ...rest] = schemaPointer.split(':');
if (!maybeCommit || !maybeFilePath || rest.length) {
return {
status: 'error',
};
}
return {
status: 'ok',
commit: maybeCommit,
filePath: maybeFilePath,
};
}
//# sourceMappingURL=git-schema-loader.js.map