Skip to content

Commit

Permalink
improve init
Browse files Browse the repository at this point in the history
  • Loading branch information
fwuensche committed Dec 7, 2024
1 parent 6dd6b88 commit c83fd0d
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 42 deletions.
19 changes: 0 additions & 19 deletions .cherry.cjs

This file was deleted.

21 changes: 21 additions & 0 deletions .cherry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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.

module.exports = {
repository: {
host: 'github.com',
owner: 'cherrypush',
name: 'cherry-cli',
subdir: '',
},
plugins: { loc: {} },
metrics: [
{
name: 'TODO/FIXME',
pattern: /(TODO|FIXME):/i, // the i flag makes the regex case insensitive
},
],
}
22 changes: 9 additions & 13 deletions bin/commands/init.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#! /usr/bin/env node

import * as git from '../../src/git.js'

import { createConfigurationFile, createWorkflowFile, getConfigFile, workflowExists } from '../../src/configuration.js'

import { Command } from 'commander'
import prompt from 'prompt'
import { gitProjectRoot, gitRemoteUrl } from '../../src/git.js'
import { guessRepositoryInfo } from '../../src/repository.js'

export default function (program: Command) {
program.command('init').action(async () => {
Expand All @@ -19,19 +19,15 @@ export default function (program: Command) {
prompt.message = ''
prompt.start()

const remoteUrl = await git.gitRemoteUrl()
let projectName = git.guessProjectName(remoteUrl)

if (projectName === null) {
const { repo } = await prompt.get({
properties: { repo: { message: 'Enter your project name', required: true } },
})
if (typeof repo === 'string') projectName = repo
}
const remoteUrl = await gitRemoteUrl()
const projectRoot = await gitProjectRoot()
const repositoryInfo = await guessRepositoryInfo({ remoteUrl, configFile: null, projectRoot })

if (!projectName) throw new Error('Project name is required')
if (!repositoryInfo.host || !repositoryInfo.owner || !repositoryInfo.name)
throw new Error('Could not guess repository info. Please setup your config file manually.')

createConfigurationFile(projectName)
console.log(`Creating configuration file for ${repositoryInfo.owner}/${repositoryInfo.name}...`)
createConfigurationFile(repositoryInfo)

if (!workflowExists()) createWorkflowFile()
console.log('Your initial setup is done! Now try the command `cherry run` to see your first metrics.')
Expand Down
12 changes: 9 additions & 3 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { dirname } from 'path'
import { fileURLToPath } from 'url'
import buildAndImport from './build-and-import.cjs'
import { guessRepositoryInfo } from './repository.js'
import { Configuration } from './types.js'
import { Configuration, Repository } from './types.js'

export const CONFIG_FILE_LOCAL_PATHS = ['.cherry.js', '.cherry.cjs', '.cherry.ts']
export const WORKFLOW_FILE_LOCAL_PATH = '.github/workflows/cherry_push.yml'
Expand All @@ -16,10 +16,16 @@ export const WORKFLOW_FILE_FULL_PATH = `${process.cwd()}/${WORKFLOW_FILE_LOCAL_P
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'

export const createConfigurationFile = (projectName: string) =>
export const createConfigurationFile = (repositoryInfo: Repository) =>
fs.writeFileSync(
CONFIG_FILE_FULL_PATHS[0],
fs.readFileSync(CONFIG_TEMPLATE_PATH).toString().replace('PROJECT_NAME', projectName)
fs
.readFileSync(CONFIG_TEMPLATE_PATH)
.toString()
.replace('{{NAME}}', repositoryInfo.name)
.replace('{{OWNER}}', repositoryInfo.owner)
.replace('{{HOST}}', repositoryInfo.host)
.replace('{{SUBDIR}}', repositoryInfo.subdir)
)

export const createWorkflowFile = () => {
Expand Down
8 changes: 4 additions & 4 deletions src/repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import path from 'path'
import { PermalinkFn, Repository } from './types.js'
import { Host, PermalinkFn, Repository } from './types.js'

export const buildRepoURL = (repository: Repository) =>
`https://${repository.host}/${repository.owner}/${repository.name}`
Expand Down Expand Up @@ -34,15 +34,15 @@ export async function guessRepositoryInfo({
remoteUrl: string | null
configFile: string | null
projectRoot: string
}) {
}): Promise<Repository> {
if (remoteUrl === null) {
throw new Error('Could not guess repository info: no remote URL found')
}

// For github ssh remotes such as git@github.com:cherrypush/cherry-cli.git
if (remoteUrl.includes('[email protected]')) {
return {
host: 'github.com',
host: Host.Github,
owner: remoteUrl.split(':')[1].split('/')[0],
name: remoteUrl.split('/')[1].replace('.git', ''),
subdir: guessRepositorySubdir({ configFile, projectRoot }),
Expand All @@ -52,7 +52,7 @@ export async function guessRepositoryInfo({
// For github https remotes such as https://github.com/cherrypush/cherry-cli.git
if (remoteUrl.includes('https://github.com')) {
return {
host: 'github.com',
host: Host.Github,
owner: remoteUrl.split('/')[3],
name: remoteUrl.split('/')[4].replace('.git', ''),
subdir: guessRepositorySubdir({ configFile, projectRoot }),
Expand Down
15 changes: 12 additions & 3 deletions src/templates/.cherry.js.template
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
// Refer to the docs here: https://www.cherrypush.com/docs
// 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.

module.exports = {
project_name: 'PROJECT_NAME',
plugins: ['loc'],
repository: {
host: '{{HOST}}',
owner: '{{OWNER}}',
name: '{{NAME}}',
subdir: '{{SUBDIR}}',
},
plugins: { loc: {} },
metrics: [
{
name: 'TODO/FIXME',
Expand Down

0 comments on commit c83fd0d

Please sign in to comment.