|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import fs from 'node:fs'; |
| 4 | +import path from 'node:path'; |
| 5 | +import {execSync} from 'node:child_process'; |
| 6 | +import {fileURLToPath} from 'node:url'; |
| 7 | + |
| 8 | +const __filename = fileURLToPath(import.meta.url); |
| 9 | +const __dirname = path.dirname(__filename); |
| 10 | + |
| 11 | +function exec(command) { |
| 12 | + try { |
| 13 | + return execSync(command, {encoding: 'utf8', stdio: ['pipe', 'pipe', 'inherit']}); |
| 14 | + } catch (error) { |
| 15 | + console.error(`❌ Failed to run: ${command}`, error); |
| 16 | + throw error; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +function safeExec(command, description) { |
| 21 | + try { |
| 22 | + console.log(`⏳ ${description}...`); |
| 23 | + exec(command); |
| 24 | + console.log(`✅ ${description} complete`); |
| 25 | + return true; |
| 26 | + } catch (error) { |
| 27 | + console.warn(`⚠️ ${description} failed - you may need to do this manually`, error); |
| 28 | + return false; |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +function configureNpmToken(isPrivate) { |
| 33 | + if (!isPrivate) { |
| 34 | + console.log('⏳ Fetching NPM token from secrets repository...'); |
| 35 | + const npmToken = exec('gh api repos/domdomegg/secrets/contents/npm.txt --jq .content | base64 -d').trim(); |
| 36 | + exec(`gh secret set NPM_TOKEN --body "${npmToken}"`); |
| 37 | + console.log('✅ NPM_TOKEN secret configured'); |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +function updatePackageJson(packageName, repoUrl, isPrivate) { |
| 42 | + const packageJsonPath = path.join(__dirname, 'package.json'); |
| 43 | + const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')); |
| 44 | + |
| 45 | + packageJson.name = packageName; |
| 46 | + packageJson.description = 'TODO: A short description of what the library does and why people might want to use it.'; |
| 47 | + packageJson.repository.url = repoUrl; |
| 48 | + |
| 49 | + if (isPrivate) { |
| 50 | + packageJson.private = true; |
| 51 | + } |
| 52 | + |
| 53 | + fs.writeFileSync(packageJsonPath, `${JSON.stringify(packageJson, null, 2)}\n`); |
| 54 | + console.log('✅ Updated package.json'); |
| 55 | +} |
| 56 | + |
| 57 | +function enableGitHubActionsPermissions() { |
| 58 | + safeExec('gh repo edit --enable-issues --enable-wiki=false --enable-projects=false', 'Configuring repository settings'); |
| 59 | + safeExec('gh api repos/:owner/:repo/actions/permissions/fork-pr-contributor-approval -X PUT --input - << \'EOF\'\n{"approval_policy":"first_time_contributors_new_to_github"}\nEOF', 'Enabling first-time contributors to trigger GitHub Actions'); |
| 60 | + safeExec('gh api repos/:owner/:repo/actions/permissions/workflow -X PUT --input - << \'EOF\'\n{"can_approve_pull_request_reviews":true}\nEOF', 'Allowing GitHub Actions to create and approve pull requests'); |
| 61 | +} |
| 62 | + |
| 63 | +function setupBranchProtection() { |
| 64 | + safeExec(`gh api repos/:owner/:repo/branches/master/protection -X PUT --input - << 'EOF' |
| 65 | +{ |
| 66 | + "required_status_checks": { |
| 67 | + "strict": false, |
| 68 | + "contexts": ["ci lts/*", "ci (current)"] |
| 69 | + }, |
| 70 | + "enforce_admins": false, |
| 71 | + "required_pull_request_reviews": { |
| 72 | + "required_approving_review_count": 1 |
| 73 | + }, |
| 74 | + "restrictions": null, |
| 75 | + "allow_force_pushes": true |
| 76 | +} |
| 77 | +EOF`, 'Setting up branch protection'); |
| 78 | +} |
| 79 | + |
| 80 | +function addToFileSyncAutomation(repoUrl) { |
| 81 | + console.log('⏳ Adding repository to file sync automation...'); |
| 82 | + const syncConfigUrl = 'repos/domdomegg/domdomegg/contents/.github/workflows/repo-file-sync.yaml'; |
| 83 | + const syncConfig = exec(`gh api ${syncConfigUrl} --jq .content | base64 -d`); |
| 84 | + |
| 85 | + const repoMatch = repoUrl.match(/github\.com[/:]([\w-]+)\/([\w-]+)/); |
| 86 | + const [, owner, repo] = repoMatch; |
| 87 | + const newRepoEntry = ` ${owner}/${repo}`; |
| 88 | + |
| 89 | + if (!syncConfig.includes(newRepoEntry.trim())) { |
| 90 | + let updatedConfig = syncConfig; |
| 91 | + |
| 92 | + // Add to Dependabot automation section |
| 93 | + updatedConfig = updatedConfig.replace( |
| 94 | + /(- name: Dependabot automation[\s\S]*?REPOSITORIES: \|[\s\S]*?)( {12}domdomegg\/typescript-library-template)/, |
| 95 | + `$1$2\n${newRepoEntry}`, |
| 96 | + ); |
| 97 | + |
| 98 | + // Add to Node.js general template section |
| 99 | + updatedConfig = updatedConfig.replace( |
| 100 | + /(- name: Node\.js general template[\s\S]*?REPOSITORIES: \|[\s\S]*?)( {12}domdomegg\/typescript-library-template)/, |
| 101 | + `$1$2\n${newRepoEntry}`, |
| 102 | + ); |
| 103 | + |
| 104 | + const currentSha = exec(`gh api ${syncConfigUrl} --jq .sha`).trim(); |
| 105 | + |
| 106 | + const updatePayload = { |
| 107 | + message: `Add ${owner}/${repo} to file sync automation`, |
| 108 | + content: Buffer.from(updatedConfig).toString('base64'), |
| 109 | + sha: currentSha, |
| 110 | + }; |
| 111 | + |
| 112 | + exec(`gh api ${syncConfigUrl} -X PUT --input - << 'EOF' |
| 113 | +${JSON.stringify(updatePayload)} |
| 114 | +EOF`); |
| 115 | + console.log('✅ Added to file sync automation'); |
| 116 | + } else { |
| 117 | + console.log('ℹ️ Repository already in file sync automation'); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +function updateReadme(packageName, isPrivate) { |
| 122 | + const readmeContent = `# ${packageName} |
| 123 | +
|
| 124 | +TODO: A short description of what the library does and why people might want to use it. |
| 125 | +
|
| 126 | +## Usage |
| 127 | +
|
| 128 | +TODO: Add usage instructions |
| 129 | +
|
| 130 | +## Contributing |
| 131 | +
|
| 132 | +Pull requests are welcomed on GitHub! To get started: |
| 133 | +
|
| 134 | +1. Install Git and Node.js |
| 135 | +2. Clone the repository |
| 136 | +3. Install dependencies with \`npm install\` |
| 137 | +4. Run \`npm run test\` to run tests |
| 138 | +5. Build with \`npm run build\`${isPrivate |
| 139 | + ? '' |
| 140 | + : ` |
| 141 | +
|
| 142 | +## Releases |
| 143 | +
|
| 144 | +Versions follow the [semantic versioning spec](https://semver.org/). |
| 145 | +
|
| 146 | +To release: |
| 147 | +
|
| 148 | +1. Use \`npm version <major | minor | patch>\` to bump the version |
| 149 | +2. Run \`git push --follow-tags\` to push with tags |
| 150 | +3. Wait for GitHub Actions to publish to the NPM registry. |
| 151 | +`}`; |
| 152 | + |
| 153 | + fs.writeFileSync(path.join(__dirname, 'README.md'), readmeContent); |
| 154 | + console.log('✅ Updated README.md'); |
| 155 | +} |
| 156 | + |
| 157 | +function deleteSetupJs() { |
| 158 | + fs.unlinkSync(__filename); |
| 159 | + console.log('✅ Deleted setup.js'); |
| 160 | +} |
| 161 | + |
| 162 | +async function main() { |
| 163 | + console.log('🚀 Setting up your TypeScript library...\n'); |
| 164 | + |
| 165 | + // Check git and gh CLI is available |
| 166 | + exec('git --version'); |
| 167 | + exec('gh api user'); |
| 168 | + |
| 169 | + // Get current repo info |
| 170 | + const remoteUrl = exec('git remote get-url origin').trim(); |
| 171 | + const repoUrl = remoteUrl; |
| 172 | + const match = remoteUrl.match(/([^/]+)\.git$/); |
| 173 | + const packageName = match?.[1]; |
| 174 | + if (!match || !packageName) { |
| 175 | + throw new Error('Could not extract package name from git remote'); |
| 176 | + } |
| 177 | + |
| 178 | + const isPrivate = process.argv.includes('--private'); |
| 179 | + |
| 180 | + configureNpmToken(isPrivate); |
| 181 | + updatePackageJson(packageName, repoUrl, isPrivate); |
| 182 | + enableGitHubActionsPermissions(); |
| 183 | + setupBranchProtection(); |
| 184 | + addToFileSyncAutomation(repoUrl); |
| 185 | + updateReadme(packageName, isPrivate); |
| 186 | + deleteSetupJs(); |
| 187 | +} |
| 188 | + |
| 189 | +main().catch(console.error); |
0 commit comments