Files
story-studio/remotion/node_modules/@remotion/cli/dist/get-github-repository.js
2026-02-21 10:33:18 +01:00

153 lines
4.8 KiB
JavaScript

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getGitSource = exports.getGifRef = exports.normalizeGitRemoteUrl = exports.getGitRemoteOrigin = exports.getGitConfig = void 0;
const bundler_1 = require("@remotion/bundler");
const child_process_1 = require("child_process");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const log_1 = require("./log");
const getGitRemotes = (lines) => {
const sections = [];
let open = false;
for (const line of lines) {
if (line.startsWith('[')) {
if (line.startsWith('[remote')) {
open = true;
sections.push([line]);
}
else {
open = false;
}
}
else if (open) {
sections[sections.length - 1].push(line);
}
}
return sections.map((s) => {
const url = s.find((l) => l.trimStart().startsWith('url = '));
return {
remote: s[0].replace('[remote "', '').replace('"]', ''),
url: url ? url.replace('url = ', '').trim() : null,
};
});
};
const getGitConfig = (remotionRoot) => {
const gitFolder = bundler_1.BundlerInternals.findClosestFolderWithItem(remotionRoot, '.git');
if (!gitFolder) {
return null;
}
const gitConfig = path_1.default.join(gitFolder, '.git', 'config');
if (!(0, fs_1.existsSync)(gitConfig)) {
return null;
}
return gitConfig;
};
exports.getGitConfig = getGitConfig;
const getGitRemoteOrigin = (gitConfig) => {
const contents = (0, fs_1.readFileSync)(gitConfig, 'utf-8');
const lines = contents.split('\n');
const remotes = getGitRemotes(lines);
const origin = remotes.find((r) => r.remote === 'origin');
return origin !== null && origin !== void 0 ? origin : null;
};
exports.getGitRemoteOrigin = getGitRemoteOrigin;
const normalizeGitRemoteUrl = (url) => {
if (url.startsWith('git@github.com')) {
const matched = url.match(/git@github.com:(.*)\/(.*)\.git/);
if (matched) {
return {
type: 'github',
name: matched[2],
org: matched[1],
};
}
}
const gitHubMatch = url.match(/https:\/\/github.com\/(.*)\/(.*)\.git/);
if (gitHubMatch) {
return {
type: 'github',
name: gitHubMatch[2],
org: gitHubMatch[1],
};
}
const gitHubMatchWithoutGit = url.match(/https:\/\/github.com\/(.*)\/(.*)/);
if (gitHubMatchWithoutGit) {
return {
type: 'github',
name: gitHubMatchWithoutGit[2],
org: gitHubMatchWithoutGit[1],
};
}
return null;
};
exports.normalizeGitRemoteUrl = normalizeGitRemoteUrl;
const getGifRef = (logLevel) => {
try {
const ret = (0, child_process_1.execSync)('git rev-parse --abbrev-ref HEAD', {
stdio: ['ignore', 'pipe', 'ignore'],
})
.toString('utf-8')
.trim();
return ret;
}
catch (err) {
log_1.Log.verbose({ logLevel, indent: false }, 'Could not get git ref', err);
return null;
}
};
exports.getGifRef = getGifRef;
const getFromEnvVariables = () => {
const { VERCEL_GIT_PROVIDER, VERCEL_GIT_COMMIT_SHA, VERCEL_GIT_REPO_OWNER, VERCEL_GIT_REPO_SLUG, } = process.env;
if (VERCEL_GIT_COMMIT_SHA &&
VERCEL_GIT_REPO_OWNER &&
VERCEL_GIT_REPO_SLUG &&
VERCEL_GIT_PROVIDER === 'github') {
return {
name: VERCEL_GIT_REPO_SLUG,
org: VERCEL_GIT_REPO_OWNER,
ref: VERCEL_GIT_COMMIT_SHA,
type: 'github',
relativeFromGitRoot: '',
};
}
return null;
};
const getGitSource = ({ remotionRoot, disableGitSource, logLevel, }) => {
if (disableGitSource) {
return null;
}
const fromEnv = getFromEnvVariables();
if (fromEnv) {
return getFromEnvVariables();
}
const ref = (0, exports.getGifRef)(logLevel);
if (!ref) {
return null;
}
const gitConfig = (0, exports.getGitConfig)(remotionRoot);
if (!gitConfig) {
return null;
}
const origin = (0, exports.getGitRemoteOrigin)(gitConfig);
if (!origin || !origin.url) {
return null;
}
const parsed = (0, exports.normalizeGitRemoteUrl)(origin.url);
if (!parsed) {
return null;
}
const gitRoot = path_1.default.dirname(path_1.default.dirname(gitConfig));
const relativeFromGitRoot = path_1.default.relative(gitRoot, remotionRoot);
return {
name: parsed.name,
org: parsed.org,
ref,
type: 'github',
relativeFromGitRoot,
};
};
exports.getGitSource = getGitSource;