nezuhub/simple / node_modules /@remotion /cli /dist /get-github-repository.js
nezuhub's picture
download
raw
6.28 kB
"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 child_process_1 = require("child_process");
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const bundler_1 = require("@remotion/bundler");
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 getGitRoot = (remotionRoot) => {
return bundler_1.BundlerInternals.findClosestFolderWithItem(remotionRoot, '.git');
};
const getGitConfig = (remotionRoot) => {
const gitRoot = getGitRoot(remotionRoot);
if (!gitRoot) {
return null;
}
const dotGit = path_1.default.join(gitRoot, '.git');
let gitDirectory = dotGit;
if (!(0, fs_1.statSync)(dotGit).isDirectory()) {
const match = (0, fs_1.readFileSync)(dotGit, 'utf-8')
.trim()
.match(/^gitdir:\s*(.+)$/);
if (!(match === null || match === void 0 ? void 0 : match[1])) {
return null;
}
gitDirectory = path_1.default.resolve(gitRoot, match[1]);
const commonDirectoryFile = path_1.default.join(gitDirectory, 'commondir');
if ((0, fs_1.existsSync)(commonDirectoryFile)) {
gitDirectory = path_1.default.resolve(gitDirectory, (0, fs_1.readFileSync)(commonDirectoryFile, 'utf-8').trim());
}
}
const gitConfig = path_1.default.join(gitDirectory, '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, remotionRoot = process.cwd()) => {
try {
const branch = (0, child_process_1.execFileSync)('git', ['-C', remotionRoot, 'rev-parse', '--abbrev-ref', 'HEAD'], {
stdio: ['ignore', 'pipe', 'ignore'],
})
.toString('utf-8')
.trim();
if (branch !== 'HEAD') {
return branch;
}
return (0, child_process_1.execFileSync)('git', ['-C', remotionRoot, 'rev-parse', 'HEAD'], {
stdio: ['ignore', 'pipe', 'ignore'],
})
.toString('utf-8')
.trim();
}
catch (err) {
log_1.Log.verbose({ logLevel, indent: false }, 'Could not get git ref', err);
return null;
}
};
exports.getGifRef = getGifRef;
const getFromEnvVariables = (remotionRoot) => {
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') {
let relativeFromGitRoot = '';
const gitRoot = getGitRoot(remotionRoot);
if (gitRoot) {
relativeFromGitRoot = path_1.default.relative(gitRoot, remotionRoot);
}
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(remotionRoot);
if (fromEnv) {
return fromEnv;
}
const ref = (0, exports.getGifRef)(logLevel, remotionRoot);
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 = getGitRoot(remotionRoot);
if (!gitRoot) {
return null;
}
const relativeFromGitRoot = path_1.default.relative(gitRoot, remotionRoot);
return {
name: parsed.name,
org: parsed.org,
ref,
type: 'github',
relativeFromGitRoot,
};
};
exports.getGitSource = getGitSource;

Xet Storage Details

Size:
6.28 kB
·
Xet hash:
c4c3f70cdc04461f6d726051b15ada4779a19bc55a1d1345a5f90fcde9f49d99

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.