Skip to content

Commit

Permalink
inline templates
Browse files Browse the repository at this point in the history
  • Loading branch information
fwuensche committed Dec 7, 2024
1 parent a6bde3a commit ebfdb65
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 50 deletions.
2 changes: 1 addition & 1 deletion bin/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export default function (program: Command) {
throw new Error('Could not guess repository info. Please setup your config file manually.')

createConfigurationFile(repositoryInfo)

if (!workflowExists()) createWorkflowFile()

console.log('Your initial setup is complete! Run `cherry run` to view your metrics.')
})
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"dist"
],
"scripts": {
"install:global": "npm install -g .",
"build": "tsc",
"cherry": "tsx ./bin/cherry.ts",
"prepare": "husky install && npm run build",
Expand Down
52 changes: 43 additions & 9 deletions src/configuration.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { gitProjectRoot, gitRemoteUrl } from './git.js'

import fs from 'fs'
import { dirname } from 'path'
import { fileURLToPath } from 'url'
import buildAndImport from './build-and-import.cjs'
import { guessRepositoryInfo } from './repository.js'
import { Configuration, Repository } from './types.js'
Expand All @@ -13,19 +11,55 @@ export const WORKFLOW_FILE_LOCAL_PATH = '.github/workflows/cherry_push.yml'
export const CONFIG_FILE_FULL_PATHS = CONFIG_FILE_LOCAL_PATHS.map((filePath) => `${process.cwd()}/${filePath}`)
export const WORKFLOW_FILE_FULL_PATH = `${process.cwd()}/${WORKFLOW_FILE_LOCAL_PATH}`

const CONFIG_TEMPLATE_PATH = dirname(fileURLToPath(import.meta.url)) + '/templates/.cherry.js.template'
const WORKFLOW_TEMPLATE_PATH = dirname(fileURLToPath(import.meta.url)) + '/templates/.cherry_push.yml.template'
const CONFIG_TEMPLATE = `// For detailed configuration options, see the documentation:
// https://www.cherrypush.com/docs
//
// In this configuration file, you can set up your repository information,
// enable plugins, and build custom metrics for your codebase.
export default {
repository: {
host: '{{HOST}}',
owner: '{{OWNER}}',
name: '{{NAME}}',
subdir: '{{SUBDIR}}',
},
plugins: { loc: {} },
metrics: [
{
name: 'TODO/FIXME',
pattern: /(TODO|FIXME):/i, // the i flag makes the regex case insensitive
},
],
}`

const WORKFLOW_TEMPLATE = `name: cherry push
on:
push:
branches:
- \${{ github.event.repository.default_branch }}
jobs:
cherry_push:
runs-on: ubuntu-latest
steps:
- name: Checkout project
uses: actions/checkout@v4
- name: Install dependencies
run: npm i -g cherrypush
- name: Push metrics to Cherry
run: cherry push --quiet --api-key=\${{ secrets.CHERRY_API_KEY }}`

export const createConfigurationFile = (repositoryInfo: Repository) => {
const filePath = CONFIG_FILE_FULL_PATHS[0]
console.log('Creating configuration file at:', filePath)

fs.writeFileSync(
filePath,
fs
.readFileSync(CONFIG_TEMPLATE_PATH)
.toString()
.replace('{{NAME}}', repositoryInfo.name)
CONFIG_TEMPLATE.replace('{{NAME}}', repositoryInfo.name)
.replace('{{OWNER}}', repositoryInfo.owner)
.replace('{{HOST}}', repositoryInfo.host)
.replace('{{SUBDIR}}', repositoryInfo.subdir)
Expand All @@ -34,7 +68,7 @@ export const createConfigurationFile = (repositoryInfo: Repository) => {

export const createWorkflowFile = () => {
fs.mkdirSync(`${process.cwd()}/.github/workflows`, { recursive: true })
fs.writeFileSync(WORKFLOW_FILE_FULL_PATH, fs.readFileSync(WORKFLOW_TEMPLATE_PATH).toString())
fs.writeFileSync(WORKFLOW_FILE_FULL_PATH, WORKFLOW_TEMPLATE)
}

export const getConfigFile = () => CONFIG_FILE_FULL_PATHS.find((filePath) => fs.existsSync(filePath)) ?? null
Expand Down
21 changes: 0 additions & 21 deletions src/templates/.cherry.js.template

This file was deleted.

19 changes: 0 additions & 19 deletions src/templates/.cherry_push.yml.template

This file was deleted.

0 comments on commit ebfdb65

Please sign in to comment.