Skip to content

Commit

Permalink
feat: add command line data gathering and basic usages
Browse files Browse the repository at this point in the history
- also adjust package to allow it to run as an executable
  • Loading branch information
jharvey10 committed Aug 11, 2023
1 parent cf2a8b2 commit 51ee6ac
Show file tree
Hide file tree
Showing 8 changed files with 480 additions and 41 deletions.
330 changes: 315 additions & 15 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,18 @@
"url": "https://github.com/carbon-design-system/telemetrics-js/issues"
},
"type": "module",
"bin": {
"ibmtelemetrics": "./dist/index.js"
},
"engines": {
"node": "18",
"npm": "9"
},
"scripts": {
"build": "tsc",
"build:schema": "ts-json-schema-generator --path ./src/schemas/Schema.ts --type Schema > src/schemas/telemetrics-config.schema.json",
"build:watch": "tsc --watch",
"clean": "rimraf dist",
"lint": "eslint --max-warnings=0 .",
"lint:fix": "eslint --fix --max-warnings=0 .",
"prepare": "husky install",
Expand Down Expand Up @@ -54,6 +60,7 @@
"husky": "^8.0.3",
"lint-staged": "^13.2.3",
"prettier": "^3.0.1",
"rimraf": "^5.0.1",
"ts-json-schema-generator": "^1.2.0",
"typescript": "^5.1.6"
}
Expand Down
56 changes: 33 additions & 23 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#!/usr/bin/env node
/*
* 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 { initializeOpenTelemetry } from './main/core/instrumentation.js'
import { getGitOrigin } from './main/core/get-git-origin.js'
import { initializeOpenTelemetry } from './main/core/initialize-open-telemetry.js'
import * as ResourceAttributes from './main/core/resource-attributes.js'
import { getPackageName } from './main/scopes/npm/get-package-name.js'
import { getPackageVersion } from './main/scopes/npm/get-package-version.js'

/*
Expand All @@ -20,29 +23,36 @@ import * as ResourceAttributes from './main/core/resource-attributes.js'
*/

const date = new Date().toISOString()
export async function run() {
const date = new Date().toISOString()

// parseConfigFile()
const config = {
projectId: 'abecafa7681dfd65cc'
}
// parseConfigFile()
const config = {
projectId: 'abecafa7681dfd65cc'
}

const packageJsonInfo = {
name: '@ibm/telemetrics-js',
version: '0.1.0'
}
const packageJsonInfo = {
name: getPackageName(),
version: getPackageVersion()
}

const gitOrigin = getGitOrigin()

const { metricReader } = initializeOpenTelemetry({
[ResourceAttributes.EMITTER_NAME]: packageJsonInfo.name,
[ResourceAttributes.EMITTER_VERSION]: packageJsonInfo.version,
[ResourceAttributes.PROJECT_ID]: config.projectId,
[ResourceAttributes.ANALYZED_RAW]: gitOrigin.raw,
[ResourceAttributes.ANALYZED_HOST]: gitOrigin.host,
[ResourceAttributes.ANALYZED_OWNER]: gitOrigin.owner,
[ResourceAttributes.ANALYZED_REPOSITORY]: gitOrigin.repository,
[ResourceAttributes.DATE]: date
})

const results = await metricReader.collect()

const gitInfo = {
remote: 'https://example.com/example-org/example-repo'
// TODO: remove this test line
console.log(JSON.stringify(results, null, 2))
}

initializeOpenTelemetry({
[ResourceAttributes.EMITTER_NAME]: packageJsonInfo.name,
[ResourceAttributes.EMITTER_VERSION]: packageJsonInfo.version,
[ResourceAttributes.PROJECT_ID]: config.projectId,
[ResourceAttributes.ANALYZED_RAW]: gitInfo.remote,
[ResourceAttributes.ANALYZED_HOST]: 'example.com',
[ResourceAttributes.ANALYZED_OWNER]: 'example-org',
[ResourceAttributes.ANALYZED_REPOSITORY]: 'example-repo',
[ResourceAttributes.DATE]: date
})
await run()
27 changes: 27 additions & 0 deletions src/main/core/exec.ts
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 }
65 changes: 65 additions & 0 deletions src/main/core/get-git-origin.ts
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 }
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ interface InitializeOpenTelemetryConfig {
[ResourceAttributes.EMITTER_VERSION]: string
[ResourceAttributes.PROJECT_ID]: string
[ResourceAttributes.ANALYZED_RAW]: string
[ResourceAttributes.ANALYZED_HOST]: string
[ResourceAttributes.ANALYZED_OWNER]: string
[ResourceAttributes.ANALYZED_REPOSITORY]: string
[ResourceAttributes.ANALYZED_HOST]: string | undefined
[ResourceAttributes.ANALYZED_OWNER]: string | undefined
[ResourceAttributes.ANALYZED_REPOSITORY]: string | undefined
[ResourceAttributes.DATE]: string
}

Expand Down
15 changes: 15 additions & 0 deletions src/main/scopes/npm/get-package-name.ts
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 })
}
15 changes: 15 additions & 0 deletions src/main/scopes/npm/get-package-version.ts
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 })
}

0 comments on commit 51ee6ac

Please sign in to comment.