Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/slow-meteors-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@fingerprintjs/changesets-native-dependency-format': minor
---

Initial release
1 change: 1 addition & 0 deletions .github/workflows/release-dx-packages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ jobs:
title: 'Release packages [changeset]'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
27 changes: 27 additions & 0 deletions packages/changesets-native-dependency-format/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "@fingerprintjs/changesets-native-dependency-format",
"description": "Changeset format that appends native dependencies note to the last changeset",
"version": "0.0.0",
"repository": {
"type": "git",
"url": "https://github.com/fingerprintjs/dx-team-toolkit"
},
"author": "FingerprintJS, Inc (https://fingerprint.com)",
"license": "MIT",
"publishConfig": {
"access": "public",
"provenance": true
},
"main": "dist/index.js",
"files": [
"dist/index.js"
],
"scripts": {
"build": "tsup src/index.ts --sourcemap"
},
"dependencies": {
"@changesets/get-release-plan": "^4.0.16",
"@changesets/types": "^6.0.0",
"@fingerprintjs/changesets-changelog-format": "workspace:^"
}
}
60 changes: 60 additions & 0 deletions packages/changesets-native-dependency-format/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { ChangelogFunctions, GetDependencyReleaseLine, GetReleaseLine } from '@changesets/types'
import fpFormat from '@fingerprintjs/changesets-changelog-format'
import { generateNativeDepsNote } from './native-dependency/note'
import { getLastChangeset } from './native-dependency/changeset'

export type Options = {
packageName?: string
androidPath?: string
iosPodspecPath?: string
repo?: string
}

const getReleaseLine: GetReleaseLine = async (changeset, type, opts: Options | null) => {
if (!opts?.packageName) {
throw new Error('Missing `opts.packageName`')
}

if (!opts?.androidPath) {
throw new Error('Missing `opts.androidPath`')
}

if (!opts?.iosPodspecPath) {
throw new Error('Missing `opts.iosPodspecPath`')
}

if (!opts?.repo) {
throw new Error('Missing `opts.repo`')
}

const lastChangeset = await getLastChangeset(opts?.packageName)
const isLastChangeset = lastChangeset === changeset.id

let line = await fpFormat.getReleaseLine(changeset, type, { repo: opts.repo })

if (isLastChangeset) {
try {
const nativeDepsNote = await generateNativeDepsNote(opts.androidPath, opts.iosPodspecPath)
line += `\n\n ${nativeDepsNote}`
} catch (e) {
console.error('Failed to generate native dependencies note', e)
}
}
Comment thread
TheUnderScorer marked this conversation as resolved.

return line
}

const getDependencyReleaseLine: GetDependencyReleaseLine = async (changesets, dependenciesUpdated, opts: Options) => {
if (!opts?.repo) {
throw new Error('Missing `opts.repo`')
}

return fpFormat.getDependencyReleaseLine(changesets, dependenciesUpdated, { repo: opts.repo })
}

const defaultChangelogFunctions: ChangelogFunctions = {
getReleaseLine,
getDependencyReleaseLine,
}

export default defaultChangelogFunctions
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { humanizeMavenStyleVersionRange } from './utils'
import { join } from 'node:path'
import { getCommand } from './gradle'
import { spawn } from 'node:child_process'

export interface AndroidPlatformConfiguration {
path: string
gradleTaskName: string
displayName: string | undefined
}

async function runGradleTask(androidPath: string, androidGradleTaskName: string): Promise<string> {
const androidFullPath = join(process.cwd(), androidPath)
const command = await getCommand(androidFullPath)

return new Promise((resolve, reject) => {
const child = spawn(command, [androidGradleTaskName, '-q', '--console=plain'], {
cwd: androidFullPath,
detached: true,
Comment thread
TheUnderScorer marked this conversation as resolved.
stdio: ['inherit', 'pipe', 'pipe'],
})
if (child.stdout === null) {
reject(new Error('Unexpected error: stdout of subprocess is null'))
return
}
if (child.stderr === null) {
reject(new Error('Unexpected error: stderr of subprocess is null'))
return
}

let androidVersion: string | null = null
child.stdout.on('data', (line: Buffer) => {
if (!line || line.toString().trim() === '') {
return
}

androidVersion = line.toString().trim()
})
child.stderr.on('data', (line: Buffer) => {
console.error(line.toString().trim())
})
child.on('close', (code: number) => {
if (code !== 0) {
reject(new Error(`Unexpected error: Gradle failed with status code ${code}`))
return
}
Comment thread
TheUnderScorer marked this conversation as resolved.

if (androidVersion === null) {
reject(new Error(`Could not read output of \`${androidGradleTaskName}\` gradle task.`))
return
}

resolve(androidVersion)
})
child.on('error', (err) => {
console.error(err)
reject(err)
})
})
}

export async function resolveAndroidDependency({ path, gradleTaskName }: AndroidPlatformConfiguration) {
const androidVersion = await runGradleTask(path, gradleTaskName)
return humanizeMavenStyleVersionRange(androidVersion)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import getReleasePlan from '@changesets/get-release-plan'

export function getChangesetStatus() {
return getReleasePlan(process.cwd())
}

// Order in which change types appear as last in the changelog
// We use it to find the changeset that appears last in the changelog and append native deps note to it, so that it always appears last
const changeTypesOrder = ['patch', 'minor', 'major']

export async function getPendingChangesets(packageName: string) {
const changesetStatus = await getChangesetStatus()

const changesets = changesetStatus.releases.find((release) => release.name === packageName)?.changesets ?? []

return changesets.map((changesetName) => {
const changeset = changesetStatus.changesets.find((c) => c.id === changesetName)

return {
id: changesetName,
type: changeset?.releases?.find((r) => r.name === packageName)?.type,
}
})
}

export function getLastChangeset(packageName: string) {
return getPendingChangesets(packageName).then((changesets) => {
for (const type of changeTypesOrder) {
const changeset = changesets.find((c) => c.type === type)

if (changeset) {
return changeset.id
}
}

return undefined
})
}
Comment thread
TheUnderScorer marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { join } from 'node:path'
import { access, constants } from 'node:fs'
import { exec } from 'node:child_process'
import { isNotFoundErrorCode } from './utils'

/**
* Check if gradle is installed to system-wide.
* @return A promise that resolves with `true` if gradle is found,
* `false` if gradle is not found,
* and rejects with an error if there is an issue checking for Gradle.
*/
export function isGradleAvailable(): Promise<boolean> {
return new Promise((resolve, reject) => {
exec('gradle --version', (err) => {
if (err) {
if (isNotFoundErrorCode(err.code)) {
resolve(false)
return
}
reject(err)
return
}

resolve(true)
})
})
}

/**
* @param {string} cwd the path of current working directory
* @return A promise that resolves name of command to trigger Gradle
*/
export function getCommand(cwd: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
const gradleWrapper = join(cwd, 'gradlew')
access(gradleWrapper, constants.F_OK, async (err) => {
if (err) {
Comment thread
TheUnderScorer marked this conversation as resolved.
if (err.code === 'ENOENT') {
if (await isGradleAvailable()) {
resolve('gradle')
return
}

reject(new Error('Gradle or Gradle wrapper not found.'))
} else {
reject(err)
}
} else {
resolve(gradleWrapper)
}
})
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { join } from 'node:path'
import { readFileSync } from 'node:fs'
import { isPodAvailable, podIpcSpec, type PodspecJson } from './pods'

export interface IOSPlatformConfiguration {
podSpecPath: string
dependencyName: string
displayName: string | undefined
}

export async function resolveIOSDependency({ podSpecPath, dependencyName }: IOSPlatformConfiguration) {
let podspecContents: PodspecJson | undefined

try {
podspecContents = readPodspecJson(podSpecPath)
} catch (e) {
const podspecDSLPath = join(podSpecPath)
podspecContents = await readPodspecDSL(podspecDSLPath)
}

if (!podspecContents.dependencies || !podspecContents.dependencies[dependencyName]) {
throw new Error(`${podSpecPath} file does not contain '${dependencyName}' in dependencies.`)
}

return podspecContents.dependencies[dependencyName].join(' and ')
}

async function readPodspecDSL(podspecFilePath: string): Promise<PodspecJson> {
if (!(await isPodAvailable())) {
throw new Error(`Pods not found in your system.`)
}

return podIpcSpec(podspecFilePath)
}

function readPodspecJson(podspecFilePath: string): PodspecJson {
const resolvedPodspecPath = join(process.cwd(), podspecFilePath)

let fileContent: string
try {
fileContent = readFileSync(resolvedPodspecPath, 'utf8')
} catch (error: any) {
switch (error.code) {
case 'ENOENT':
throw new Error(`${podspecFilePath} file does not exist.`)
case 'EACCES':
throw new Error(`${podspecFilePath} file cannot be accessed.`)
default:
throw new Error(`${podspecFilePath} file cannot be read. Error: ${error.message}`)
}
}

let data: PodspecJson
try {
data = JSON.parse(fileContent) as PodspecJson
} catch (error) {
throw new Error('Failed to parse podspec JSON. Please check the podspec file format.')
}

return data
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { resolveIOSDependency } from './ios'
import { resolveAndroidDependency } from './android'

export type NativePlatformDefinition = { displayName: string; versionRange: string }

function formatter(platforms: NativePlatformDefinition[]) {
let result = `### Supported Native SDK Version Range\n\n`

result += platforms
.map(({ displayName, versionRange }) => {
return `* ${displayName} Version Range: **\`${versionRange}\`**`
})
.join('\n')

return result
}

export async function generateNativeDepsNote(androidPath: string, iosPodspecPath: string) {
const platformVersions: NativePlatformDefinition[] = [
{
displayName: 'Fingerprint iOS SDK',
versionRange: await resolveIOSDependency({
displayName: 'Fingerprint iOS SDK',
dependencyName: 'FingerprintPro',
podSpecPath: iosPodspecPath,
}),
},

{
displayName: 'Fingerprint Android SDK',
versionRange: await resolveAndroidDependency({
path: androidPath,
displayName: 'Fingerprint Android SDK',
gradleTaskName: 'printFingerprintNativeSDKVersion',
}),
},
]

return formatter(platformVersions)
}
Loading
Loading