|
| 1 | +import fs from 'fs-extra'; |
| 2 | +import junk from 'junk'; |
| 3 | +import path from 'path'; |
| 4 | +import axios from 'axios'; |
| 5 | + |
| 6 | +import { fetchConfig } from '@codeshift/fetcher'; |
| 7 | + |
| 8 | +const COMMUNITY_PATH = path.join(__dirname, '..', 'community'); |
| 9 | +const CODESHIFT_WORKER_URL = 'http://codeshift.delcore.workers.dev/packages'; |
| 10 | + |
| 11 | +interface DocsData { |
| 12 | + pkgName: string; |
| 13 | + targets: string; |
| 14 | +} |
| 15 | + |
| 16 | +async function main() { |
| 17 | + console.log('🚚 Syncing community packages with the codeshift worker'); |
| 18 | + |
| 19 | + const communityCodemods = fs.readdirSync(COMMUNITY_PATH); |
| 20 | + const data: DocsData[] = []; |
| 21 | + const directories = communityCodemods.filter(dir => junk.not(dir)); |
| 22 | + |
| 23 | + for (const dir of directories) { |
| 24 | + const config = await fetchConfig(path.join(COMMUNITY_PATH, dir)); |
| 25 | + |
| 26 | + if (!config) { |
| 27 | + throw new Error(`Unable to locate config for path: ${dir}`); |
| 28 | + } |
| 29 | + |
| 30 | + const pkgName = `@codeshift/mod-${dir.replace('@', '').replace('/', '__')}`; |
| 31 | + const rawPkgName = dir.replace('__', '/'); |
| 32 | + data.push({ |
| 33 | + pkgName, |
| 34 | + targets: rawPkgName + (config.targets ? `, ${config.targets}` : ''), |
| 35 | + }); |
| 36 | + } |
| 37 | + |
| 38 | + const response = await axios.post( |
| 39 | + CODESHIFT_WORKER_URL, |
| 40 | + JSON.stringify(data), |
| 41 | + { |
| 42 | + headers: { |
| 43 | + 'Content-Type': 'application/json', |
| 44 | + 'X-Custom-PSK': process.env.WORKER_PRESHARED_KEY || '', |
| 45 | + }, |
| 46 | + }, |
| 47 | + ); |
| 48 | + |
| 49 | + console.log(response.data); |
| 50 | +} |
| 51 | + |
| 52 | +main().catch(error => { |
| 53 | + console.error('Syncing error:', error.message); |
| 54 | + process.exit(1); |
| 55 | +}); |
0 commit comments