doclyft
Version:
CLI for DocLyft - Interactive documentation generator with hosted documentation support
134 lines (133 loc) • 5.81 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.isProtectedRepository = isProtectedRepository;
exports.isProtectedRepoName = isProtectedRepoName;
exports.ensureSafeWorkingDirectory = ensureSafeWorkingDirectory;
exports.getSafeAnalysisPath = getSafeAnalysisPath;
exports.confirmDangerousOperation = confirmDangerousOperation;
const fs_1 = require("fs");
const path_1 = __importDefault(require("path"));
const chalk_1 = __importDefault(require("chalk"));
// List of protected repositories that should not be modified
const PROTECTED_REPOSITORIES = [
'docuforge-github-connect',
'doclyft',
'doclyft-cli',
// Add more protected repo names as needed
];
// Check if the current directory is a protected repository
async function isProtectedRepository() {
try {
const packageJsonPath = path_1.default.join(process.cwd(), 'package.json');
// Check if package.json exists
try {
await fs_1.promises.access(packageJsonPath);
const packageContent = await fs_1.promises.readFile(packageJsonPath, 'utf-8');
const packageData = JSON.parse(packageContent);
if (packageData.name && PROTECTED_REPOSITORIES.includes(packageData.name)) {
return { isProtected: true, repoName: packageData.name };
}
}
catch {
// package.json doesn't exist or is invalid, continue with other checks
}
// Check for git remote URL
try {
const { exec } = await Promise.resolve().then(() => __importStar(require('child_process')));
const { promisify } = await Promise.resolve().then(() => __importStar(require('util')));
const execAsync = promisify(exec);
const { stdout } = await execAsync('git remote get-url origin', { cwd: process.cwd() });
const remoteUrl = stdout.trim();
// Extract repository name from various URL formats
const repoMatch = remoteUrl.match(/\/([^/]+?)(?:\.git)?$/);
if (repoMatch) {
const repoName = repoMatch[1];
if (PROTECTED_REPOSITORIES.includes(repoName)) {
return { isProtected: true, repoName };
}
}
}
catch {
// Git command failed or not in a git repository
}
return { isProtected: false };
}
catch (error) {
console.warn(chalk_1.default.yellow('Warning: Could not check repository protection status'));
return { isProtected: false };
}
}
// Check if a repository name is protected
function isProtectedRepoName(repoFullName) {
const repoName = repoFullName.split('/')[1];
return PROTECTED_REPOSITORIES.includes(repoName);
}
// Create a safe working directory for CLI operations
async function ensureSafeWorkingDirectory() {
const homeDir = process.env.HOME || process.env.USERPROFILE || process.cwd();
const doclyftDir = path_1.default.join(homeDir, '.doclyft');
try {
await fs_1.promises.mkdir(doclyftDir, { recursive: true });
return doclyftDir;
}
catch (error) {
console.warn(chalk_1.default.yellow('Warning: Could not create safe working directory, using current directory'));
return process.cwd();
}
}
// Get a safe file path for analysis data
function getSafeAnalysisPath(repoName) {
const safeRepoName = repoName.replace(/[^a-zA-Z0-9-_]/g, '_');
const homeDir = process.env.HOME || process.env.USERPROFILE || process.cwd();
const doclyftDir = path_1.default.join(homeDir, '.doclyft');
return path_1.default.join(doclyftDir, `${safeRepoName}-analysis.json`);
}
// Confirm dangerous operations
async function confirmDangerousOperation(operation, targetRepo) {
const prompts = await Promise.resolve().then(() => __importStar(require('prompts')));
console.log(chalk_1.default.yellow(`⚠️ WARNING: You are about to ${operation} on repository: ${targetRepo}`));
console.log(chalk_1.default.yellow('This operation may modify files in the repository.'));
const response = await prompts.default({
type: 'confirm',
name: 'confirmed',
message: 'Are you sure you want to continue?',
initial: false
});
return response.confirmed;
}