@remotion/cli
Version:
Control Remotion features using the `npx remotion` command
192 lines (191 loc) • 8.13 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateCatalogEntry = exports.updateCatalogEntryInPnpmWorkspace = exports.updateCatalogEntryInPackageJson = exports.parsePnpmWorkspaceCatalog = exports.getCatalogEntries = exports.findCatalogSource = exports.findWorkspaceRoot = exports.findVersionSpecifier = exports.isCatalogProtocol = void 0;
const node_fs_1 = __importDefault(require("node:fs"));
const node_path_1 = __importDefault(require("node:path"));
const WORKSPACE_ROOT_SEARCH_LIMIT = 5;
const isCatalogProtocol = (version) => {
return version.startsWith('catalog:');
};
exports.isCatalogProtocol = isCatalogProtocol;
const findVersionSpecifier = (depsWithVersions, pkg) => {
var _a, _b, _c, _d;
return ((_d = (_c = (_b = (_a = depsWithVersions.dependencies[pkg]) !== null && _a !== void 0 ? _a : depsWithVersions.devDependencies[pkg]) !== null && _b !== void 0 ? _b : depsWithVersions.optionalDependencies[pkg]) !== null && _c !== void 0 ? _c : depsWithVersions.peerDependencies[pkg]) !== null && _d !== void 0 ? _d : null);
};
exports.findVersionSpecifier = findVersionSpecifier;
const findWorkspaceRoot = (startDir) => {
let currentDir = node_path_1.default.resolve(startDir);
for (let i = 0; i < WORKSPACE_ROOT_SEARCH_LIMIT; i++) {
const packageJsonPath = node_path_1.default.join(currentDir, 'package.json');
if (node_fs_1.default.existsSync(packageJsonPath)) {
try {
const packageJson = JSON.parse(node_fs_1.default.readFileSync(packageJsonPath, 'utf-8'));
if (packageJson.workspaces) {
return currentDir;
}
}
catch (_a) { }
}
const pnpmWorkspacePath = node_path_1.default.join(currentDir, 'pnpm-workspace.yaml');
if (node_fs_1.default.existsSync(pnpmWorkspacePath)) {
return currentDir;
}
const parentDir = node_path_1.default.dirname(currentDir);
if (parentDir === currentDir) {
return null;
}
currentDir = parentDir;
}
return null;
};
exports.findWorkspaceRoot = findWorkspaceRoot;
const findCatalogSource = (workspaceRoot) => {
const packageJsonPath = node_path_1.default.join(workspaceRoot, 'package.json');
if (node_fs_1.default.existsSync(packageJsonPath)) {
try {
const packageJson = JSON.parse(node_fs_1.default.readFileSync(packageJsonPath, 'utf-8'));
if (packageJson.workspaces &&
typeof packageJson.workspaces === 'object' &&
!Array.isArray(packageJson.workspaces) &&
packageJson.workspaces.catalog) {
return {
type: 'package-json',
filePath: packageJsonPath,
catalogKey: 'workspaces',
};
}
if (packageJson.catalog) {
return {
type: 'package-json',
filePath: packageJsonPath,
catalogKey: 'root',
};
}
}
catch (_a) { }
}
const pnpmWorkspacePath = node_path_1.default.join(workspaceRoot, 'pnpm-workspace.yaml');
if (node_fs_1.default.existsSync(pnpmWorkspacePath)) {
const content = node_fs_1.default.readFileSync(pnpmWorkspacePath, 'utf-8');
if (/^catalog:/m.test(content)) {
return { type: 'pnpm-workspace', filePath: pnpmWorkspacePath };
}
}
return null;
};
exports.findCatalogSource = findCatalogSource;
const getCatalogEntries = (workspaceRoot) => {
var _a, _b;
const source = (0, exports.findCatalogSource)(workspaceRoot);
if (!source) {
return {};
}
if (source.type === 'package-json') {
const packageJson = JSON.parse(node_fs_1.default.readFileSync(source.filePath, 'utf-8'));
if (source.catalogKey === 'workspaces') {
return ((_a = packageJson.workspaces.catalog) !== null && _a !== void 0 ? _a : {});
}
return ((_b = packageJson.catalog) !== null && _b !== void 0 ? _b : {});
}
return (0, exports.parsePnpmWorkspaceCatalog)(node_fs_1.default.readFileSync(source.filePath, 'utf-8'));
};
exports.getCatalogEntries = getCatalogEntries;
const parsePnpmWorkspaceCatalog = (content) => {
const lines = content.split('\n');
const catalog = {};
let inCatalogSection = false;
for (const line of lines) {
if (/^catalog:\s*$/.test(line)) {
inCatalogSection = true;
continue;
}
if (inCatalogSection && /^\S/.test(line) && line.trim() !== '') {
inCatalogSection = false;
continue;
}
if (inCatalogSection && line.trim() !== '') {
const match = line.match(/^\s+(['"]?)([^'":\s]+)\1:\s*['"]?([^'"#\s]+)['"]?/);
if (match && match[2] && match[3]) {
catalog[match[2]] = match[3];
}
}
}
return catalog;
};
exports.parsePnpmWorkspaceCatalog = parsePnpmWorkspaceCatalog;
const updateCatalogEntryInPackageJson = ({ filePath, catalogKey, pkg, newVersion, }) => {
var _a;
const content = node_fs_1.default.readFileSync(filePath, 'utf-8');
const packageJson = JSON.parse(content);
const catalog = catalogKey === 'workspaces'
? (_a = packageJson.workspaces) === null || _a === void 0 ? void 0 : _a.catalog
: packageJson.catalog;
if (!catalog || !(pkg in catalog)) {
return false;
}
catalog[pkg] = newVersion;
const indentMatch = content.match(/^(\s+)"/m);
const indent = indentMatch ? indentMatch[1] : '\t';
node_fs_1.default.writeFileSync(filePath, JSON.stringify(packageJson, null, indent) + '\n');
return true;
};
exports.updateCatalogEntryInPackageJson = updateCatalogEntryInPackageJson;
const updateCatalogEntryInPnpmWorkspace = ({ filePath, pkg, newVersion, }) => {
var _a, _b, _c;
const content = node_fs_1.default.readFileSync(filePath, 'utf-8');
const lines = content.split('\n');
let inCatalogSection = false;
let updated = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (/^catalog:\s*$/.test(line)) {
inCatalogSection = true;
continue;
}
if (inCatalogSection && /^\S/.test(line) && line.trim() !== '') {
inCatalogSection = false;
continue;
}
if (inCatalogSection) {
const escapedPkg = pkg.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const lineRegex = new RegExp(`^(\\s+(?:['"]?)${escapedPkg}(?:['"]?):\\s*)(['"]?)([^'"#\\s]+)\\2(.*)$`);
const match = line.match(lineRegex);
if (match) {
const prefix = (_a = match[1]) !== null && _a !== void 0 ? _a : '';
const quote = (_b = match[2]) !== null && _b !== void 0 ? _b : '';
const suffix = (_c = match[4]) !== null && _c !== void 0 ? _c : '';
lines[i] = `${prefix}${quote}${newVersion}${quote}${suffix}`;
updated = true;
break;
}
}
}
if (updated) {
node_fs_1.default.writeFileSync(filePath, lines.join('\n'));
}
return updated;
};
exports.updateCatalogEntryInPnpmWorkspace = updateCatalogEntryInPnpmWorkspace;
const updateCatalogEntry = ({ workspaceRoot, pkg, newVersion, }) => {
const source = (0, exports.findCatalogSource)(workspaceRoot);
if (!source) {
return false;
}
if (source.type === 'package-json') {
return (0, exports.updateCatalogEntryInPackageJson)({
filePath: source.filePath,
catalogKey: source.catalogKey,
pkg,
newVersion,
});
}
return (0, exports.updateCatalogEntryInPnpmWorkspace)({
filePath: source.filePath,
pkg,
newVersion,
});
};
exports.updateCatalogEntry = updateCatalogEntry;