Thingiverse MCP Server
by gpaul-mcp
Verified
- MCP_thingiverse
- scripts
import * as dotenv from 'dotenv';
import * as fs from 'fs';
import * as path from 'path';
// Paths
const rootDir = path.resolve(__dirname, '..');
const srcDir = path.resolve(rootDir, 'src');
const indexPath = path.resolve(srcDir, 'index.ts');
const prodEnvPath = path.resolve(rootDir, '.env.production');
// Function to extract APP_TOKEN from .env.production
function getAppToken(): string {
try {
// Check if .env.production exists
if (!fs.existsSync(prodEnvPath)) {
console.error('❌ .env.production file not found!');
process.exit(1);
}
// Parse the .env.production file
const envConfig = dotenv.config({ path: prodEnvPath });
if (envConfig.error) {
console.error('❌ Error parsing .env.production:', envConfig.error);
process.exit(1);
}
const appToken = envConfig.parsed?.APP_TOKEN;
if (!appToken) {
console.error('❌ APP_TOKEN not found in .env.production');
process.exit(1);
}
return appToken;
} catch (error) {
console.error('❌ Error reading APP_TOKEN:', error);
process.exit(1);
}
}
// Function to modify index.ts
function modifyIndexFile(appToken: string): void {
try {
// Read the index.ts file
let content = fs.readFileSync(indexPath, 'utf8');
// Replace the commented fallback with uncommented code including the token
const commentedPattern =
/\/\/ if \(!process\.env\.APP_TOKEN\) \{\s*\/\/\s*process\.env\.APP_TOKEN = ['"]XXXXXXX['"];\s*\/\/\s*\}/;
const uncommentedReplacement = `if (!process.env.APP_TOKEN) {\n process.env.APP_TOKEN = '${appToken}';\n}`;
// Check if the pattern exists
if (!commentedPattern.test(content)) {
const simplePattern = /\/\/ if \(!process\.env\.APP_TOKEN\) \{[\s\S]*?\/\/\s*\}/;
if (simplePattern.test(content)) {
content = content.replace(simplePattern, uncommentedReplacement);
} else {
console.error('❌ Could not find the token fallback pattern in index.ts');
process.exit(1);
}
} else {
content = content.replace(commentedPattern, uncommentedReplacement);
}
// Write the modified content back to index.ts
fs.writeFileSync(indexPath, content);
console.log('✅ Successfully updated index.ts with APP_TOKEN fallback');
} catch (error) {
console.error('❌ Error modifying index.ts:', error);
process.exit(1);
}
}
// Main function
function main(): void {
console.log('🔧 Preparing build with APP_TOKEN from .env.production...');
// Get the APP_TOKEN from .env.production
const appToken = getAppToken();
// Modify the index.ts file
modifyIndexFile(appToken);
console.log('✅ Build preparation completed successfully');
}
// Run the script
main();