-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add command line data gathering and basic usages
- also adjust package to allow it to run as an executable
- Loading branch information
Showing
8 changed files
with
480 additions
and
41 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright IBM Corp. 2023, 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import childProcess from 'node:child_process' | ||
|
||
function exec(cmd: string, options = {}) { | ||
guardShell(cmd) | ||
|
||
const execOptions = { | ||
env: process.env, | ||
...options | ||
} | ||
return childProcess.execSync(cmd, execOptions).toString().trim() | ||
} | ||
|
||
function guardShell(cmd: string) { | ||
if (/[\\$;`]/.exec(cmd) != null) { | ||
throw new Error( | ||
'Shell guard prevented a command from running because it contained special characters: ' + cmd | ||
) | ||
} | ||
} | ||
|
||
export { exec } |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright IBM Corp. 2023, 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import { exec } from './exec.js' | ||
|
||
function getGitOrigin() { | ||
let parsed: { | ||
host: string | undefined | ||
owner: string | undefined | ||
repository: string | undefined | ||
} = { | ||
host: undefined, | ||
owner: undefined, | ||
repository: undefined | ||
} | ||
|
||
// TODO: handle non-existant remote | ||
const raw = exec('git remote get-url origin') | ||
|
||
if (raw.startsWith('http')) { | ||
// Example: https://github.com/carbon-design-system/telemetrics-js.git | ||
parsed = parseHttp(raw) | ||
} else if (raw.startsWith('git@')) { | ||
// Example: git@github.com:carbon-design-system/telemetrics-js.git | ||
parsed = parseSsh(raw) | ||
} | ||
|
||
if (parsed.repository?.endsWith('.git') === true) { | ||
parsed.repository = parsed.repository.slice(0, -4) | ||
} | ||
|
||
return { | ||
raw, | ||
...parsed | ||
} | ||
} | ||
|
||
function parseHttp(raw: string) { | ||
const match = /^https?:\/\/((.*?)\/)?((.*?)\/)?(.*)/.exec(raw) ?? [] | ||
|
||
const [_raw, _hostGroup, host, _ownerGroup, owner, repository] = match | ||
|
||
return { | ||
host, | ||
owner, | ||
repository | ||
} | ||
} | ||
|
||
function parseSsh(raw: string) { | ||
const match = /^(.*?)@(.*?):((.*?)\/)?(.*)/.exec(raw) ?? [] | ||
|
||
const [_raw, _user, host, _ownerGroup, owner, repository] = match | ||
|
||
return { | ||
host, | ||
owner, | ||
repository | ||
} | ||
} | ||
|
||
export { getGitOrigin } |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright IBM Corp. 2023, 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import path from 'node:path' | ||
|
||
import { exec } from '../../core/exec.js' | ||
|
||
export function getPackageName() { | ||
const cwd = path.dirname(import.meta.url.substring(7)) | ||
|
||
return exec('npm pkg get name', { cwd }) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Copyright IBM Corp. 2023, 2023 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
import path from 'node:path' | ||
|
||
import { exec } from '../../core/exec.js' | ||
|
||
export function getPackageVersion() { | ||
const cwd = path.dirname(import.meta.url.substring(7)) | ||
|
||
return exec('npm pkg get version', { cwd }) | ||
} |