-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add changesets native dependency format package #177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
29ab76e
feat: add changesets native dependency format package
TheUnderScorer 8df7210
ci: set NPM_TOKEN for first publish
TheUnderScorer 5d3bae7
refactor: simplify heading handling in native SDK version formatter f…
TheUnderScorer c2fe2e3
feat: add support for customizable Android and iOS paths in native de…
TheUnderScorer 63489b1
refactor: extract and reuse NativePlatformDefinition type in native d…
TheUnderScorer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| --- | ||
| '@fingerprintjs/changesets-native-dependency-format': minor | ||
| --- | ||
|
|
||
| Initial release |
This file contains hidden or 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 hidden or 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 @@ | ||
| { | ||
| "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:^" | ||
| } | ||
| } |
This file contains hidden or 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,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) | ||
| } | ||
| } | ||
|
|
||
| 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 | ||
65 changes: 65 additions & 0 deletions
65
packages/changesets-native-dependency-format/src/native-dependency/android.ts
This file contains hidden or 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 @@ | ||
| 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, | ||
|
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 | ||
| } | ||
|
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) | ||
| } | ||
38 changes: 38 additions & 0 deletions
38
packages/changesets-native-dependency-format/src/native-dependency/changeset.ts
This file contains hidden or 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,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 | ||
| }) | ||
| } | ||
|
TheUnderScorer marked this conversation as resolved.
|
||
53 changes: 53 additions & 0 deletions
53
packages/changesets-native-dependency-format/src/native-dependency/gradle.ts
This file contains hidden or 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,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) { | ||
|
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) | ||
| } | ||
| }) | ||
| }) | ||
| } | ||
61 changes: 61 additions & 0 deletions
61
packages/changesets-native-dependency-format/src/native-dependency/ios.ts
This file contains hidden or 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 { 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 | ||
| } |
40 changes: 40 additions & 0 deletions
40
packages/changesets-native-dependency-format/src/native-dependency/note.ts
This file contains hidden or 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,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) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.