diff --git a/.gitignore b/.gitignore index c18ed016..88edb628 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ node_modules/ -lib/ \ No newline at end of file +lib/ diff --git a/src/changefile.ts b/src/changefile.ts index e69de29b..48facb83 100644 --- a/src/changefile.ts +++ b/src/changefile.ts @@ -0,0 +1,19 @@ +import prompts from 'prompts'; +import { getRecentCommitMessages } from './git'; + +export async function promptForChange() { + const recentMessages = getRecentCommitMessages() || []; + + const response = await prompts({ + type: 'autocomplete', + name: 'description', + message: 'Describe your changes', + suggest: input => { + return Promise.resolve(recentMessages.find(msg => msg.startsWith(input)) || input); + } + }); + + console.log(response); // => { value: 24 } +} + +promptForChange(); diff --git a/src/git.ts b/src/git.ts index 646361cf..7c93c2a9 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,5 +1,85 @@ import { spawnSync } from 'child_process'; export function git(args: string[]) { - return spawnSync('git', args); + const results = spawnSync('git', args); + + if (results.status === 0) { + return { + stderr: results.stderr.toString().trim(), + stdout: results.stdout.toString().trim() + }; + } else { + return null; + } +} + +export function getUncommittedChanges() { + try { + const results = git(['status', '--porcelain']); + + if (!results) { + return []; + } + + const changes = results.stdout; + + if (changes.length == 0) { + return []; + } + + const lines = changes.split(/\n/) || []; + + return lines.map(line => line.trim().split(/ /)[1]); + } catch (e) { + console.error('Cannot gather information about changes: ', e.message); + } +} + +export function getForkPoint(targetBranch: string = 'origin/master') { + try { + const results = git(['git', 'merge-base', '--fork-point', targetBranch, 'HEAD']); + if (!results) { + return targetBranch; + } + + return results.stdout; + } catch (e) { + return targetBranch; + } +} + +export function getChanges() { + try { + const forkPoint = getForkPoint(); + const results = git(['--no-pager', 'diff', '--name-only', forkPoint + '...']); + + if (!results) { + return []; + } + + let changes = results.stdout; + let lines = changes.split(/\n/) || []; + + return lines.map(line => line.trim()); + } catch (e) { + console.error('Cannot gather information about changes: ', e.message); + } +} + +export function getRecentCommitMessages() { + try { + const forkPoint = getForkPoint(); + const results = git(['log', '--decorate', '--pretty=format:%s', '--reverse', forkPoint, 'HEAD']); + + if (!results) { + return []; + } + + let changes = results.stdout; + let lines = changes.split(/\n/) || []; + + return lines.map(line => line.trim()); + } catch (e) { + console.error('Cannot gather information about changes: ', e.message); + } } diff --git a/src/paths.ts b/src/paths.ts index 322cee3e..e04dd519 100644 --- a/src/paths.ts +++ b/src/paths.ts @@ -43,3 +43,8 @@ export function getChangeFilePath(cwd?: string) { return null; } + +export function isChildOf(child: string, parent: string) { + const relativePath = path.relative(child, parent); + return /^[.\/\\]+$/.test(relativePath); +}