feat: Update template config file

This commit is contained in:
rongbin666
2026-04-07 08:16:06 +08:00
parent 29df260747
commit fd65121c23
4 changed files with 79 additions and 13 deletions
+2 -1
View File
@@ -1,7 +1,8 @@
IPFS_API_URL=https://example.com
IPFS_PREVIEW_URL=https://example.com
PINME_TEMPLATE_BRANCH=main
SECRET_KEY=your-secret-key
FILE_SIZE_LIMIT=100 # MB
DIRECTORY_SIZE_LIMIT=500 # MB
DIRECTORY_SIZE_LIMIT=500 # MB
+74 -9
View File
@@ -16,22 +16,71 @@ import {
// Template directory - relative to bin folder (works both in dev and npm)
const PROJECT_DIR = process.cwd();
const API_BASE = process.env.PINME_API_BASE || '';
const TEMPLATE_BRANCH = process.env.PINME_TEMPLATE_BRANCH || 'main';
// 模板仓库地址 (使用 HTTPS 下载 zip)
const TEMPLATE_REPO = 'glitternetwork/pinme-worker-template';
const TEMPLATE_ZIP_URL = `https://github.com/${TEMPLATE_REPO}/archive/refs/heads/main.zip`;
const TEMPLATE_REPO_NAME = TEMPLATE_REPO.split('/').pop() || 'pinme-worker-template';
function getTemplateZipUrl(branch: string): string {
return `https://github.com/${TEMPLATE_REPO}/archive/refs/heads/${encodeURIComponent(branch)}.zip`;
}
interface CreateOptions {
name?: string;
force?: boolean;
}
interface AuthConfig {
tenant_id: string;
api_key: string;
auth_domain: string;
project_id: string;
}
interface CreateWorkerResponse {
api_domain: string;
metadata: string;
project_name: string;
uuid: string;
api_key?: string;
auth_config?: AuthConfig;
}
function buildAuthConfigExport(authConfig: AuthConfig): string {
return `export const auth_config = ${JSON.stringify(authConfig, null, 2)};\n`;
}
function injectAuthConfigIntoFile(fileContent: string, authConfig: AuthConfig): string {
const authConfigExport = buildAuthConfigExport(authConfig).trimEnd();
const authConfigPattern = /export\s+const\s+auth_config\s*=\s*\{[\s\S]*?\};?/m;
if (authConfigPattern.test(fileContent)) {
return fileContent.replace(authConfigPattern, authConfigExport);
}
const trimmed = fileContent.trimEnd();
if (!trimmed) {
return `${authConfigExport}\n`;
}
return `${trimmed}\n\n${authConfigExport}\n`;
}
function resolveExtractedTemplateDir(extractDir: string): string {
const entries = fs.readdirSync(extractDir, { withFileTypes: true });
const templateDir = entries.find((entry) => (
entry.isDirectory() && entry.name.startsWith(`${TEMPLATE_REPO_NAME}-`)
));
if (!templateDir) {
throw createConfigError('Downloaded template archive structure is invalid.', [
`Expected a directory starting with \`${TEMPLATE_REPO_NAME}-\` inside the extracted archive.`,
`Template branch: ${TEMPLATE_BRANCH}`,
]);
}
return path.join(extractDir, templateDir.name);
}
/**
@@ -136,7 +185,10 @@ export default async function createCmd(options: CreateOptions): Promise<void> {
// 2. Download template from repository (using HTTPS, no git required)
console.log(chalk.blue('\n2. Downloading template from repository...'));
const zipPath = path.join(PROJECT_DIR, 'template.zip');
const extractDir = path.join(PROJECT_DIR, `.pinme-template-${Date.now()}`);
const templateZipUrl = getTemplateZipUrl(TEMPLATE_BRANCH);
let downloadSuccess = false;
console.log(chalk.gray(` Template branch: ${TEMPLATE_BRANCH}`));
// Retry download up to 3 times
for (let attempt = 1; attempt <= 3 && !downloadSuccess; attempt++) {
@@ -144,7 +196,7 @@ export default async function createCmd(options: CreateOptions): Promise<void> {
console.log(chalk.gray(` Download attempt ${attempt}/3...`));
// Download zip file
execSync(`curl -L --retry 3 --retry-delay 2 -o "${zipPath}" "${TEMPLATE_ZIP_URL}"`, {
execSync(`curl -L --retry 3 --retry-delay 2 -o "${zipPath}" "${templateZipUrl}"`, {
stdio: 'inherit',
});
@@ -166,20 +218,20 @@ export default async function createCmd(options: CreateOptions): Promise<void> {
}
try {
fs.ensureDirSync(extractDir);
// Unzip to target directory
execSync(`unzip -o "${zipPath}" -d "${PROJECT_DIR}"`, {
execSync(`unzip -o "${zipPath}" -d "${extractDir}"`, {
stdio: 'inherit',
});
// Move files from subdirectory to target directory
const subDir = path.join(PROJECT_DIR, 'pinme-worker-template-main');
if (fs.existsSync(subDir)) {
fs.copySync(subDir, targetDir);
fs.removeSync(subDir);
}
// Move files from extracted subdirectory to target directory
const subDir = resolveExtractedTemplateDir(extractDir);
fs.copySync(subDir, targetDir);
// Clean up zip file
fs.removeSync(zipPath);
fs.removeSync(extractDir);
// Remove any existing node_modules and package-lock.json to ensure clean install
// This fixes issues with rollup platform-specific dependencies
@@ -250,6 +302,19 @@ export default async function createCmd(options: CreateOptions): Promise<void> {
console.log(chalk.green(` Updated backend/wrangler.toml API_KEY`));
}
const frontendConfigPath = path.join(targetDir, 'frontend', 'src', 'utils', 'config.ts');
if (workerData.auth_config) {
const frontendConfigContent = fs.existsSync(frontendConfigPath)
? fs.readFileSync(frontendConfigPath, 'utf-8')
: '';
fs.ensureDirSync(path.dirname(frontendConfigPath));
fs.writeFileSync(
frontendConfigPath,
injectAuthConfigIntoFile(frontendConfigContent, workerData.auth_config)
);
console.log(chalk.green(` Updated frontend/src/utils/config.ts auth_config`));
}
// 5. Create .env file from .env.example (in frontend directory)
const envExamplePath = path.join(targetDir, 'frontend', '.env.example');
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "pinme",
"version": "2.0.0-beta.9",
"version": "2.0.2-beta.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "pinme",
"version": "2.0.0-beta.9",
"version": "2.0.2-beta.0",
"license": "MIT",
"dependencies": {
"archiver": "^7.0.1",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "pinme",
"version": "2.0.1",
"version": "2.0.2-beta.1",
"publishConfig": {
"access": "public"
},