forked from reddit/devvit-template-react
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsetup-devvit.cjs
More file actions
executable file
·105 lines (90 loc) · 3.25 KB
/
setup-devvit.cjs
File metadata and controls
executable file
·105 lines (90 loc) · 3.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// setup-devvit.cjs - Devvit project setup script
const fs = require('fs');
const path = require('path');
const { execSync, exec } = require('child_process');
const os = require('os');
const yaml = require('yaml');
function generateRandomSuffix(length) {
const letters = 'abcdefghijklmnopqrstuvwxyz';
let result = '';
for (let i = 0; i < length; i++) {
result += letters.charAt(Math.floor(Math.random() * letters.length));
}
return result;
}
function updateDevvitName() {
const devvitYamlPath = path.join(process.cwd(), 'devvit.yaml');
const content = fs.readFileSync(devvitYamlPath, 'utf8');
const parsedYaml = yaml.parse(content);
if (parsedYaml.name === 'YOUR_APP_NAME') {
const suffix = generateRandomSuffix(6);
parsedYaml.name = `bolt-${suffix}`;
fs.writeFileSync(devvitYamlPath, yaml.stringify(parsedYaml));
console.log(`Updated app name to bolt-${suffix}`);
}
}
async function runChecks() {
let allPassed = true;
const checks = [];
// Check 1: Devvit login
const devvitTokenPath = path.join(os.homedir(), '.devvit', 'token');
const isLoggedIn = fs.existsSync(devvitTokenPath);
checks.push({
name: 'Authentication',
passed: isLoggedIn,
message: isLoggedIn ? "You're logged in to Devvit!" : 'Please run npm run login to authenticate with Reddit'
});
// Check 2: App upload check
const uploadedPath = path.join(process.cwd(), '.initialized');
const isUploaded = fs.existsSync(uploadedPath);
checks.push({
name: 'App initialization',
passed: isUploaded,
message: isUploaded ? 'App has been initialized' : 'Please run npm run devvit:init to setup your app remotely'
});
// Check 3: Subreddit configuration
const packageJsonPath = path.join(process.cwd(), 'package.json');
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
const packageJson = JSON.parse(packageJsonContent);
const devScript = packageJson.scripts && packageJson.scripts['dev:devvit'];
const hasSubreddit = devScript && !devScript.includes('YOUR_SUBREDDIT_NAME');
checks.push({
name: 'Playtest subreddit',
passed: hasSubreddit,
message: hasSubreddit ? 'Subreddit is configured!' : 'Please update YOUR_SUBREDDIT_NAME in the dev:devvit script in package.json'
});
// Print check results
checks.forEach(check => {
const emoji = check.passed ? '✅' : '❌';
console.log(`${emoji} ${check.name}: ${check.message}`);
if (!check.passed) allPassed = false;
});
return allPassed;
}
async function main() {
try {
// Step 1: Update devvit.yaml name
updateDevvitName();
// Step 2: Run checks
const checksPass = await runChecks();
// Step 3: If all checks pass, run dev:all
if (checksPass) {
console.log('\nAll checks passed! Starting development server...');
const devProcess = exec('npm run dev:all', (error, stdout, stderr) => {
if (error) {
console.error(`Error running dev:all: ${error.message}`);
process.exit(1);
}
});
devProcess.stdout.pipe(process.stdout);
devProcess.stderr.pipe(process.stderr);
} else {
console.log('\nPlease fix the issues above and try again.');
process.exit(1);
}
} catch (error) {
console.error('Error:', error.message);
process.exit(1);
}
}
main();