agp-cli
Version:
Agentic Programming Project CLI - Standardized knowledge layer for AI-assisted development
165 lines • 6.72 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.downloadTemplate = downloadTemplate;
const fs = __importStar(require("fs-extra"));
const path = __importStar(require("path"));
const node_fetch_1 = __importDefault(require("node-fetch"));
const yauzl = __importStar(require("yauzl"));
const stream_1 = require("stream");
const util_1 = require("util");
const pipelineAsync = (0, util_1.promisify)(stream_1.pipeline);
async function downloadTemplate(templateUrl, targetPath) {
// Ensure target directory doesn't exist
if (await fs.pathExists(targetPath)) {
await fs.remove(targetPath);
}
try {
// Convert git URL to ZIP download URL
const zipUrl = convertGitUrlToZip(templateUrl);
// Create target directory
await fs.ensureDir(targetPath);
// Download ZIP file
const tempZipPath = path.join(targetPath, 'template.zip');
await downloadZipFile(zipUrl, tempZipPath);
// Extract ZIP file
await extractZipFile(tempZipPath, targetPath);
// Clean up
await fs.remove(tempZipPath);
// Remove template-specific files that shouldn't be in user projects
const filesToRemove = ['.github', 'README.md', 'LICENSE'];
for (const file of filesToRemove) {
const filePath = path.join(targetPath, file);
if (await fs.pathExists(filePath)) {
await fs.remove(filePath);
}
}
}
catch (error) {
throw new Error(`Failed to download template: ${error instanceof Error ? error.message : 'Unknown error'}`);
}
}
function convertGitUrlToZip(gitUrl) {
// Convert various git URL formats to GitHub ZIP download URL
let repoUrl = gitUrl;
// Remove .git suffix if present
if (repoUrl.endsWith('.git')) {
repoUrl = repoUrl.slice(0, -4);
}
// Convert SSH URL to HTTPS
if (repoUrl.startsWith('git@github.com:')) {
repoUrl = repoUrl.replace('git@github.com:', 'https://github.com/');
}
// Ensure it's a GitHub URL
if (!repoUrl.includes('github.com')) {
throw new Error('Only GitHub repositories are supported');
}
// Convert to ZIP download URL
return `${repoUrl}/archive/refs/heads/main.zip`;
}
async function downloadZipFile(zipUrl, outputPath) {
const response = await (0, node_fetch_1.default)(zipUrl);
if (!response.ok) {
throw new Error(`Failed to download template: ${response.status} ${response.statusText}`);
}
if (!response.body) {
throw new Error('No response body received');
}
const fileStream = fs.createWriteStream(outputPath);
await pipelineAsync(response.body, fileStream);
}
async function extractZipFile(zipPath, extractPath) {
return new Promise((resolve, reject) => {
yauzl.open(zipPath, { lazyEntries: true }, (err, zipfile) => {
if (err || !zipfile) {
reject(err || new Error('Failed to open ZIP file'));
return;
}
let pendingEntries = 0;
let completed = false;
const complete = () => {
if (!completed && pendingEntries === 0) {
completed = true;
resolve();
}
};
zipfile.readEntry();
zipfile.on('entry', (entry) => {
// Skip the root directory (e.g., "agp-template-main/")
const relativePath = entry.fileName.split('/').slice(1).join('/');
if (!relativePath) {
zipfile.readEntry();
return;
}
const fullPath = path.join(extractPath, relativePath);
if (/\/$/.test(entry.fileName)) {
// Directory entry
fs.ensureDir(fullPath)
.then(() => zipfile.readEntry())
.catch(reject);
}
else {
// File entry
pendingEntries++;
zipfile.openReadStream(entry, (err, readStream) => {
if (err || !readStream) {
reject(err || new Error('Failed to open read stream'));
return;
}
// Ensure parent directory exists
fs.ensureDir(path.dirname(fullPath))
.then(() => {
const writeStream = fs.createWriteStream(fullPath);
writeStream.on('close', () => {
pendingEntries--;
complete();
zipfile.readEntry();
});
writeStream.on('error', reject);
readStream.pipe(writeStream);
})
.catch(reject);
});
}
});
zipfile.on('end', complete);
zipfile.on('error', reject);
});
});
}
//# sourceMappingURL=template-manager.js.map