|
| 1 | +--- |
| 2 | +name: add-stats-module |
| 3 | +description: Guide for adding new GitHub statistics collection modules |
| 4 | +--- |
| 5 | + |
| 6 | +# Adding a New Statistics Module |
| 7 | + |
| 8 | +Follow these steps to add a new statistics collection module: |
| 9 | + |
| 10 | +## 1. Create the Module File |
| 11 | + |
| 12 | +Create `src/new-module.js`: |
| 13 | + |
| 14 | +```javascript |
| 15 | +const { graphql } = require('./api'); |
| 16 | + |
| 17 | +module.exports = async function(arg) { |
| 18 | + // Validate argument if needed |
| 19 | + if (!arg) { |
| 20 | + throw new Error('Argument required'); |
| 21 | + } |
| 22 | + |
| 23 | + // GraphQL query |
| 24 | + const query = ` |
| 25 | + query($arg: String!) { |
| 26 | + # Your GraphQL query here |
| 27 | + } |
| 28 | + `; |
| 29 | + |
| 30 | + try { |
| 31 | + const result = await graphql(query, { arg }); |
| 32 | + |
| 33 | + // Transform and return data |
| 34 | + return { |
| 35 | + // Your data structure |
| 36 | + }; |
| 37 | + } catch (error) { |
| 38 | + throw new Error(`Failed to fetch stats: ${error.message}`); |
| 39 | + } |
| 40 | +}; |
| 41 | +``` |
| 42 | + |
| 43 | +## 2. Export from Index |
| 44 | + |
| 45 | +Add to `src/index.js`: |
| 46 | + |
| 47 | +```javascript |
| 48 | +exports.newModule = require('./new-module'); |
| 49 | +``` |
| 50 | + |
| 51 | +## 3. Add CLI Command |
| 52 | + |
| 53 | +Update `src/cli.js` to add the command: |
| 54 | + |
| 55 | +```javascript |
| 56 | +case 'newmodule': |
| 57 | + return require('./new-module')(process.argv[3]); |
| 58 | +``` |
| 59 | + |
| 60 | +## 4. Add npm Script |
| 61 | + |
| 62 | +Update `package.json` scripts section: |
| 63 | + |
| 64 | +```json |
| 65 | +"newmodule": "run(){ node -e \"async function run() { console.log(JSON.stringify(await require('./src/new-module')('$1'), null, 2)) } run()\"; }; run" |
| 66 | +``` |
| 67 | + |
| 68 | +## 5. Add Test Command |
| 69 | + |
| 70 | +Create `.cursor/commands/test-newmodule.md`: |
| 71 | + |
| 72 | +```markdown |
| 73 | +--- |
| 74 | +name: test-newmodule |
| 75 | +description: Test the new module command |
| 76 | +--- |
| 77 | + |
| 78 | +Test the new module command: |
| 79 | + |
| 80 | +\`\`\`bash |
| 81 | +npm run newmodule <arg> |
| 82 | +\`\`\` |
| 83 | + |
| 84 | +**Prerequisites:** |
| 85 | +- `GITHUB_TOKEN` environment variable must be set |
| 86 | +- Token requires appropriate scopes |
| 87 | + |
| 88 | +The command will print statistics as JSON. |
| 89 | +``` |
| 90 | + |
| 91 | +## 6. Update Documentation |
| 92 | + |
| 93 | +Update README.md with usage example: |
| 94 | + |
| 95 | +```markdown |
| 96 | +\`\`\`shell |
| 97 | +npx github-viewer-stats newmodule <arg> |
| 98 | +\`\`\` |
| 99 | + |
| 100 | +\`\`\`json |
| 101 | +{ |
| 102 | + "example": "output" |
| 103 | +} |
| 104 | +\`\`\` |
| 105 | +``` |
| 106 | + |
| 107 | +## GraphQL Query Tips |
| 108 | + |
| 109 | +- Use the GitHub GraphQL Explorer: https://docs.github.com/en/graphql/overview/explorer |
| 110 | +- Test queries before implementing |
| 111 | +- Handle pagination if needed |
| 112 | +- Include error handling for rate limits |
0 commit comments