Skip to content

Commit

Permalink
feat: support tags
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Jan 10, 2022
1 parent 08e46bb commit f22a13f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -30,7 +30,7 @@ npm install -g build-this-branch
| Flag | Description |
| - | - |
| `-c, --build-command <command>` | The command to build the branch. (default: `npm run build`) |
| `-b, --built-branch <branch name>` | The name of the built branch. Defaults to prefixing "built/" to the current branch. |
| `-b, --built-branch <branch name>` | The name of the built branch. Defaults to prefixing "built/" to the current branch or tag name. |
| `-r, --remote <remote>` | The remote to push to. (default: `origin`) |
| `-d, --dry` | Dry run mode. Will not build, commit, or push to the remote. |
| `-h, --help` | Show help |
Expand Down
38 changes: 27 additions & 11 deletions src/index.ts
Expand Up @@ -22,17 +22,33 @@ async function assertCleanTree() {
}
}

async function getCurrentBranchName() {
try {
const { stdout } = await execa('git', ['symbolic-ref', '--short', '-q', 'HEAD']);
return stdout.trim();
} catch (error) {
if (error instanceof Error) {
throw new TypeError(`Failed to get current branch name\n${error.message}`);
}
async function getCurrentBranchOrTagName() {
// eslint-disable-next-line @typescript-eslint/no-empty-function
const silenceError = () => {};

/**
* This commands supports older versions of Git, but since v2.22, you can do:
* git branch --show-current
*/
const branch = await execa('git', ['symbolic-ref', '--short', '-q', 'HEAD']).then(
({ stdout }) => stdout,
silenceError,
);

throw error;
if (branch) {
return branch;
}

const tag = await execa('git', ['describe', '--tags']).then(
({ stdout }) => stdout,
silenceError,
);

if (tag) {
return tag;
}

throw new Error('Failed to get current branch name');
}

async function readJson(path: string) {
Expand All @@ -57,7 +73,7 @@ const { stringify } = JSON;
type: String,
alias: 'b',
placeholder: '<branch name>',
description: 'The name of the built branch. Defaults to prefixing "built/" to the current branch.',
description: 'The name of the built branch. Defaults to prefixing "built/" to the current branch or tag name.',
},
buildCommand: {
type: String,
Expand Down Expand Up @@ -86,7 +102,7 @@ const { stringify } = JSON;
});

await assertCleanTree();
const branchFrom = await getCurrentBranchName();
const branchFrom = await getCurrentBranchOrTagName();
const packageJsonPath = await pkgUp();

if (!packageJsonPath) {
Expand Down

0 comments on commit f22a13f

Please sign in to comment.