Skip to content

Commit

Permalink
feat: automate client version number dumps
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyentoanit committed Mar 16, 2021
1 parent e95c5e9 commit e21d3d6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"start": "ts-node --transpile-only --pretty src",
"test": "jest --testPathIgnorePatterns test/integration/ ",
"test:integration": "jest test/integration/ --setupFiles dotenv/config",
"test:watch": "jest --watchAll"
"test:watch": "jest --watchAll",
"postversion": "ts-node --transpile-only --pretty utils/bump-version.ts"
},
"types": "lib/index.d.ts",
"dependencies": {
Expand Down
4 changes: 4 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/**
* The version value is automatically replaced during builds.
*/
export const USER_AGENT = '@scaleleap/selling-partner-api-sdk/0.0.0'
4 changes: 2 additions & 2 deletions src/helpers/api-client-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { aws4Interceptor } from 'aws4-axios'
import globalAxios, { AxiosError, AxiosInstance, AxiosResponse } from 'axios'

import { USER_AGENT } from '../constants'
import { APIConfigurationParameters } from '../types'
import { apiErrorFactory } from './api-error-factory'

Expand All @@ -16,8 +17,7 @@ export class ApiClientHelpers {

axiosInstance = globalAxios.create({
headers: {
// TODO: automate version number
'user-agent': '@scaleleap/selling-partner-api-sdk (Language=TypeScript/0.0.0)',
'user-agent': USER_AGENT,
'x-amz-access-token': accessToken,
},
})
Expand Down
20 changes: 20 additions & 0 deletions utils/bump-version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Syncs package.json version with constants file for release.
*
* Triggered inside `postversion` hook in package.json.
*
* See: https://semantic-release.gitbook.io/semantic-release/support/faq#how-can-i-use-a-npm-build-script-that-requires-the-package-jsons-version
*/

import { readFileSync, writeFileSync } from 'fs'
import path from 'path'

import { name, version } from '../package.json'
import { USER_AGENT } from '../src/constants'

const CONSTANTS_FILE = path.join(__dirname, '../lib/constants.js')

const constants = readFileSync(CONSTANTS_FILE, { encoding: 'utf8' })
const replaced = constants.replace(USER_AGENT, `${name}/${version}`)

writeFileSync(CONSTANTS_FILE, replaced, { encoding: 'utf8' })

0 comments on commit e21d3d6

Please sign in to comment.