Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Video-Downloader/packages/version-info/index.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
78 lines (61 sloc)
1.77 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { existsSync } from 'node:fs'; | |
import { join, parse } from 'node:path'; | |
import { cwd } from 'node:process'; | |
import { readFile } from 'node:fs/promises'; | |
const findFile = (file) => { | |
let dir = cwd(); | |
while (dir !== parse(dir).root) { | |
if (existsSync(join(dir, file))) { | |
return dir; | |
} | |
dir = join(dir, '../'); | |
} | |
} | |
const root = findFile('.git'); | |
const pack = findFile('package.json'); | |
const readGit = (filename) => { | |
if (!root) { | |
throw 'no git repository root found'; | |
} | |
return readFile(join(root, filename), 'utf8'); | |
} | |
export const getCommit = async () => { | |
return (await readGit('.git/logs/HEAD')) | |
?.split('\n') | |
?.filter(String) | |
?.pop() | |
?.split(' ')[1]; | |
} | |
export const getBranch = async () => { | |
if (process.env.CF_PAGES_BRANCH) { | |
return process.env.CF_PAGES_BRANCH; | |
} | |
return (await readGit('.git/HEAD')) | |
?.replace(/^ref: refs\/heads\//, '') | |
?.trim(); | |
} | |
export const getRemote = async () => { | |
let remote = (await readGit('.git/config')) | |
?.split('\n') | |
?.find(line => line.includes('url = ')) | |
?.split('url = ')[1]; | |
if (remote?.startsWith('git@')) { | |
remote = remote.split(':')[1]; | |
} else if (remote?.startsWith('http')) { | |
remote = new URL(remote).pathname.substring(1); | |
} | |
remote = remote?.replace(/\.git$/, ''); | |
if (!remote) { | |
throw 'could not parse remote'; | |
} | |
return remote; | |
} | |
export const getVersion = async () => { | |
if (!pack) { | |
throw 'no package root found'; | |
} | |
const { version } = JSON.parse( | |
await readFile(join(pack, 'package.json'), 'utf8') | |
); | |
return version; | |
} |