-
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.
- Loading branch information
1 parent
72f90d8
commit a5e397b
Showing
6 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
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,23 @@ | ||
export function toArray<T>(arr: void | T | T[]): T[] { | ||
if (!arr) { | ||
return []; | ||
} | ||
if (Array.isArray(arr)) { | ||
return arr; | ||
} | ||
return [arr]; | ||
} | ||
|
||
// Returns a serialized array for use in an API query string | ||
// e.g. arrayToQuery('num', [1, 2, 3]) => 'num[]=1&num[]=2&num[]=3' | ||
export function arrayToQuery(arrayName: string, array: Array<number | string>): string { | ||
let result = array.reduce( | ||
(prev: string, cur) => `${prev}&${arrayName}[]=${encodeURIComponent(cur.toString())}`, | ||
'', | ||
); | ||
// Remove & prefix | ||
if (result.startsWith('&')) { | ||
result = result.slice(1); | ||
} | ||
return result; | ||
} |
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,21 @@ | ||
import { BasicAuth, FetchRequestConfig } from '../types'; | ||
|
||
export function authConfig(config: FetchRequestConfig): FetchRequestConfig { | ||
return { | ||
...config, | ||
credentials: 'include', | ||
}; | ||
} | ||
|
||
export function basicAuth(auth: BasicAuth, config: RequestInit): RequestInit { | ||
const { headers, ...rest } = config; | ||
const authData = btoa(`${auth.username}:${auth.password}`); | ||
return { | ||
...rest, | ||
headers: { | ||
...headers, | ||
Authorization: `Basic ${authData}`, | ||
'Content-Type': 'application/x-www-form-urlencoded', | ||
}, | ||
}; | ||
} |
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,7 @@ | ||
export type AnyJson = boolean | number | string | null | IJsonArray | IJsonObject | ||
|
||
export interface IJsonObject { | ||
[key: string]: AnyJson | ||
} | ||
|
||
export interface IJsonArray extends Array<AnyJson> {} |
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,4 @@ | ||
export * from './auth'; | ||
export * from './i-json-object'; | ||
export * from './request'; | ||
export * from './array'; |
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,61 @@ | ||
import { RequestParams, ResolvedRequestParams } from '../types'; | ||
import { arrayToQuery } from './array'; | ||
|
||
type Filterable = { | ||
[key: string]: any; | ||
}; | ||
|
||
export function resolveSearchParams(obj: RequestParams): ResolvedRequestParams { | ||
if (typeof obj === 'object') { | ||
const filteredObj: Filterable = {}; | ||
Object.entries(obj).forEach(([key, val]) => { | ||
if (val !== undefined) { | ||
filteredObj[key] = val.toString(); | ||
} | ||
}); | ||
return filteredObj; | ||
} | ||
return obj; | ||
} | ||
|
||
export function prepareJson(data: any, config: RequestInit): RequestInit { | ||
const { headers, ...rest } = config; | ||
return { | ||
...rest, | ||
headers: { | ||
...headers, | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify(data), | ||
}; | ||
} | ||
|
||
export function encodeParams(url: string, params: ResolvedRequestParams) { | ||
const encodedUrl = new URL(url); | ||
if (params) { | ||
encodedUrl.search = new URLSearchParams(params).toString(); | ||
} | ||
return encodedUrl; | ||
} | ||
|
||
export const transformRequestParams = ( | ||
params?: RequestParams, | ||
): RequestParams | undefined => { | ||
if (!params || typeof params !== 'object') { | ||
return params; | ||
} | ||
const entries = Object.entries(params); | ||
if (entries.length === 0) { | ||
return params; | ||
} | ||
let queryString = ''; | ||
for (const [key, value] of entries) { | ||
if (Array.isArray(value)) { | ||
queryString += `&${arrayToQuery(key, value)}`; | ||
} else if (value !== undefined) { | ||
queryString += `&${key}=${encodeURIComponent(value.toString())}`; | ||
} | ||
} | ||
return `?${queryString.slice(1)}`; | ||
}; |
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,90 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { spawnSync } = require('child_process'); | ||
|
||
const pkgFile = path.resolve(__dirname, '..', 'package.json'); | ||
const pkg = require(pkgFile); | ||
|
||
const usageString = ` | ||
Release a new version of ${pkg?.name ?? 'unknown package'} | ||
> node release.js <version> | ||
version - x.x.x | ||
`; | ||
|
||
const usage = () => { | ||
console.info(usageString); | ||
}; | ||
|
||
const errorExit = (reason) => { | ||
usage(); | ||
console.error(`${reason}\n`); | ||
process.exit(1); | ||
}; | ||
|
||
const runOrExit = (cmd, args, message) => { | ||
const result = spawnSync(cmd, args, { stdio: 'pipe', encoding: 'utf-8' }); | ||
if (result.status !== 0) { | ||
console.log(result.stderr); | ||
errorExit(message); | ||
} | ||
}; | ||
|
||
if (!pkg) errorExit('Unable to resolve package.json'); | ||
|
||
let result = spawnSync('npm', ['show', pkg.name, 'version'], { | ||
stdio: 'pipe', | ||
encoding: 'utf-8', | ||
}); | ||
const prevVersion = result.stdout.trim(); | ||
const newVersion = process.argv[2]; | ||
|
||
console.log(`Previous version: ${prevVersion}`); | ||
|
||
console.log('...verifying branch'); | ||
result = spawnSync('git', ['branch', '--show-current'], { | ||
stdio: 'pipe', | ||
encoding: 'utf-8', | ||
}); | ||
const branch = result.stdout.trim(); | ||
if (branch !== 'main') errorExit('Error - must be on `main` branch'); | ||
|
||
if (newVersion === prevVersion) errorExit('New version cannot match previous version'); | ||
|
||
if (!/\d+\.\d+\.\d+/.test(newVersion)) errorExit('New version format must be x.x.x'); | ||
|
||
console.log('...building'); | ||
|
||
runOrExit('npm', ['run', 'build'], 'Build failed'); | ||
|
||
console.log('...updating version'); | ||
|
||
try { | ||
pkg.version = newVersion; | ||
fs.writeFileSync(pkgFile, `${JSON.stringify(pkg, null, 2)}\n`); | ||
} catch (e) { | ||
errorExit('Failed to write new version to package.json: ' + e.message); | ||
} | ||
|
||
console.log('...committing'); | ||
|
||
runOrExit('git', ['commit', '-am', `release: v${newVersion}`], 'Release commit failed'); | ||
|
||
console.log('...tagging'); | ||
|
||
runOrExit( | ||
'git', | ||
['tag', '-a', `v${newVersion}`, '-m', `Version ${newVersion}`], | ||
'Release tag failed', | ||
); | ||
|
||
console.log('...pushing commit'); | ||
|
||
runOrExit('git', ['push'], 'Release push failed'); | ||
|
||
console.log('...pushing tags'); | ||
|
||
runOrExit('git', ['push', '--tags'], 'Release tag push failed'); | ||
|
||
console.log(`Released version ${newVersion}\n`); |