diff --git a/.cherry.cjs b/.cherry.cjs deleted file mode 100644 index cabde7d5..00000000 --- a/.cherry.cjs +++ /dev/null @@ -1,19 +0,0 @@ -const JS_FILES = '**/*.{js,jsx}' -const TS_FILES = '**/*.{ts,tsx}' - -module.exports = { - project_name: 'cherrypush/cherry-cli', - plugins: { - // npmOutdated: {}, // TODO: this requires an active internet connection thus should not be used in tests - loc: {}, - eslint: {}, - jsCircularDependencies: { include: 'src/**' }, - // jsUnimported: {}, // TODO: investigate why this takes so long with a slow internet connection - }, - metrics: [ - { - name: 'TODO', - pattern: /TODO/, - }, - ], -} diff --git a/.cherry.js b/.cherry.js new file mode 100644 index 00000000..b20fe243 --- /dev/null +++ b/.cherry.js @@ -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 + }, + ], +} diff --git a/bin/commands/init.ts b/bin/commands/init.ts index 6f97a672..332a632d 100755 --- a/bin/commands/init.ts +++ b/bin/commands/init.ts @@ -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 () => { @@ -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.') diff --git a/src/configuration.ts b/src/configuration.ts index e8b85ed4..078c16b3 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -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' @@ -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 = () => { diff --git a/src/repository.ts b/src/repository.ts index 5ce1eb4c..9e78ebf9 100644 --- a/src/repository.ts +++ b/src/repository.ts @@ -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}` @@ -34,7 +34,7 @@ export async function guessRepositoryInfo({ remoteUrl: string | null configFile: string | null projectRoot: string -}) { +}): Promise { if (remoteUrl === null) { throw new Error('Could not guess repository info: no remote URL found') } @@ -42,7 +42,7 @@ export async function guessRepositoryInfo({ // For github ssh remotes such as git@github.com:cherrypush/cherry-cli.git if (remoteUrl.includes('git@github.com')) { return { - host: 'github.com', + host: Host.Github, owner: remoteUrl.split(':')[1].split('/')[0], name: remoteUrl.split('/')[1].replace('.git', ''), subdir: guessRepositorySubdir({ configFile, projectRoot }), @@ -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 }), diff --git a/src/templates/.cherry.js.template b/src/templates/.cherry.js.template index 296fa4c6..89d918ff 100644 --- a/src/templates/.cherry.js.template +++ b/src/templates/.cherry.js.template @@ -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',