|
1 |
| -import { join } from 'node:path' |
2 | 1 | import type { GenerateNotesContext } from 'semantic-release'
|
3 |
| -import { spawn } from 'node:child_process' |
4 |
| -import type { Signale } from 'signale' |
5 |
| -import PluginConfig from './@types/pluginConfig' |
6 |
| -import { getCommand } from './gradle' |
7 |
| -import { humanizeMavenStyleVersionRange } from './utils' |
8 |
| -import { readFileSync } from 'node:fs' |
9 |
| - |
10 |
| -export async function getAndroidVersion( |
11 |
| - cwd: string, |
12 |
| - androidPath: string, |
13 |
| - androidGradleTaskName: string, |
14 |
| - env: NodeJS.ProcessEnv, |
15 |
| - logger: Signale |
16 |
| -): Promise<string> { |
17 |
| - const androidFullPath = join(cwd, androidPath) |
18 |
| - const command = await getCommand(androidFullPath) |
19 |
| - |
20 |
| - return new Promise((resolve, reject) => { |
21 |
| - const child = spawn(command, [androidGradleTaskName, '-q', '--console=plain'], { |
22 |
| - cwd: androidPath, |
23 |
| - env, |
24 |
| - detached: true, |
25 |
| - stdio: ['inherit', 'pipe', 'pipe'], |
26 |
| - }) |
27 |
| - if (child.stdout === null) { |
28 |
| - reject(new Error('Unexpected error: stdout of subprocess is null')) |
29 |
| - return |
| 2 | +import PluginConfig, { PlatformConfig } from './@types/pluginConfig' |
| 3 | +import { resolve as androidResolve } from './platforms/android' |
| 4 | +import { resolve as iOSResolve } from './platforms/iOS' |
| 5 | + |
| 6 | +const generateNotes = async (config: PluginConfig, ctx: GenerateNotesContext) => { |
| 7 | + const platformVersions: { [key in keyof PlatformConfig]?: string } = {} |
| 8 | + |
| 9 | + // Normalize plugin config: prefer `platforms`, fall back to legacy top-level platform keys for backward compatibility |
| 10 | + // TODO: Remove support for top-level `android` and `iOS` keys in the next major release (BC) |
| 11 | + const platforms: PlatformConfig = config.platforms || {} |
| 12 | + if (!config.platforms) { |
| 13 | + if (config.android) { |
| 14 | + ctx.logger.warn('[DEPRECATED] Use `platforms.android` instead of top-level `android` in plugin config.') |
| 15 | + platforms.android = config.android |
30 | 16 | }
|
31 |
| - if (child.stderr === null) { |
32 |
| - reject(new Error('Unexpected error: stderr of subprocess is null')) |
33 |
| - return |
34 |
| - } |
35 |
| - |
36 |
| - let androidVersion: string | null = null |
37 |
| - child.stdout.on('data', (line: Buffer) => { |
38 |
| - if (!line || line.toString().trim() === '') { |
39 |
| - return |
40 |
| - } |
41 |
| - |
42 |
| - logger.debug(`Gradle stdout: ${line}`) |
43 |
| - androidVersion = line.toString().trim() |
44 |
| - }) |
45 |
| - child.stderr.on('data', (line: Buffer) => { |
46 |
| - logger.error(line.toString().trim()) |
47 |
| - }) |
48 |
| - child.on('close', (code: number) => { |
49 |
| - if (code !== 0) { |
50 |
| - reject(new Error(`Unexpected error: Gradle failed with status code ${code}`)) |
51 |
| - return |
52 |
| - } |
53 |
| - |
54 |
| - if (androidVersion === null) { |
55 |
| - reject(new Error(`Could not read output of \`${androidGradleTaskName}\` gradle task.`)) |
56 |
| - return |
57 |
| - } |
58 |
| - |
59 |
| - resolve(androidVersion) |
60 |
| - }) |
61 |
| - child.on('error', (err) => { |
62 |
| - logger.error(err) |
63 |
| - reject(err) |
64 |
| - }) |
65 |
| - }) |
66 |
| -} |
67 |
| - |
68 |
| -type PodspecJson = { |
69 |
| - dependencies: { |
70 |
| - [key: string]: [string] |
71 |
| - } |
72 |
| -} |
73 |
| - |
74 |
| -export const getIOSVersion = async (cwd: string, iOSPodSpecJsonPath: string, dependencyName: string) => { |
75 |
| - const jsonFile = join(cwd, iOSPodSpecJsonPath) |
76 | 17 |
|
77 |
| - let fileContent: string |
78 |
| - try { |
79 |
| - fileContent = readFileSync(jsonFile, 'utf8') |
80 |
| - } catch (error: any) { |
81 |
| - switch (error.code) { |
82 |
| - case 'ENOENT': |
83 |
| - throw new Error(`${iOSPodSpecJsonPath} file does not exist.`) |
84 |
| - case 'EACCES': |
85 |
| - throw new Error(`${iOSPodSpecJsonPath} file cannot be accessed.`) |
86 |
| - default: |
87 |
| - throw new Error(`${iOSPodSpecJsonPath} file cannot be read.`) |
| 18 | + if (config.iOS) { |
| 19 | + ctx.logger.warn('[DEPRECATED] Use `platforms.iOS` instead of top-level `iOS` in plugin config.') |
| 20 | + platforms.iOS = config.iOS |
88 | 21 | }
|
89 | 22 | }
|
90 | 23 |
|
91 |
| - let data: PodspecJson |
92 |
| - try { |
93 |
| - data = JSON.parse(fileContent) as PodspecJson |
94 |
| - } catch (error) { |
95 |
| - throw new Error(`${iOSPodSpecJsonPath} file cannot be parsed as JSON.`) |
| 24 | + if (!platforms.android && !platforms.iOS) { |
| 25 | + throw new Error('No platforms specified. You must configure at least one platform under `platforms`.') |
96 | 26 | }
|
97 | 27 |
|
98 |
| - if (!data.dependencies || !data.dependencies[dependencyName]) { |
99 |
| - throw new Error(`${iOSPodSpecJsonPath} file does not contain '${dependencyName}' in dependencies.`) |
| 28 | + if (platforms.android) { |
| 29 | + const androidVersion = await androidResolve(ctx, platforms.android) |
| 30 | + platformVersions.android = androidVersion |
| 31 | + ctx.logger.log(`Detected Android Version: \`${androidVersion}\``) |
100 | 32 | }
|
101 | 33 |
|
102 |
| - return data.dependencies[dependencyName].join(' and ') |
103 |
| -} |
104 |
| - |
105 |
| -const generateNotes = async ({ iOS, android }: PluginConfig, { logger, cwd, env }: GenerateNotesContext) => { |
106 |
| - if (!cwd) { |
107 |
| - throw new Error(`Current working directory is required to detect native dependency versions.`) |
108 |
| - } |
109 |
| - |
110 |
| - if (!android.gradleTaskName) { |
111 |
| - throw new Error('Android gradle task name should be defined.') |
112 |
| - } |
113 |
| - |
114 |
| - if (!iOS.podSpecJsonPath) { |
115 |
| - throw new Error('iOS Podspec Json path should be defined.') |
116 |
| - } |
117 |
| - |
118 |
| - if (!iOS.dependencyName) { |
119 |
| - throw new Error('iOS Dependency name should be defined.') |
| 34 | + if (platforms.iOS) { |
| 35 | + const iOSVersion = await iOSResolve(ctx, platforms.iOS) |
| 36 | + platformVersions.iOS = iOSVersion |
| 37 | + ctx.logger.log(`Detected iOS Version: \`${iOSVersion}\``) |
120 | 38 | }
|
121 | 39 |
|
122 |
| - if (!android.displayName) { |
123 |
| - android.displayName = 'Android Dependency' |
124 |
| - } |
125 |
| - |
126 |
| - if (!iOS.displayName) { |
127 |
| - iOS.displayName = 'iOS Dependency' |
128 |
| - } |
129 |
| - |
130 |
| - const androidVersion = await getAndroidVersion( |
131 |
| - cwd, |
132 |
| - android.path, |
133 |
| - android.gradleTaskName, |
134 |
| - env as NodeJS.ProcessEnv, |
135 |
| - logger |
136 |
| - ) |
137 |
| - const humanizedAndroidVersion = humanizeMavenStyleVersionRange(androidVersion) |
138 |
| - logger.log(`Detected ${android.displayName} Version: \`${androidVersion}\` \`${humanizedAndroidVersion}\``) |
139 |
| - |
140 |
| - const iosVersion = await getIOSVersion(cwd, iOS.podSpecJsonPath, iOS.dependencyName) |
141 |
| - logger.log(`Detected ${iOS.displayName} Version: \`${iosVersion}\``) |
142 |
| - |
143 |
| - return `${android.displayName} Version Range: **\`${humanizedAndroidVersion}\`** |
144 |
| -
|
145 |
| -${iOS.displayName} Version Range: **\`${iosVersion}\`**` |
| 40 | + return Object.keys(platformVersions) |
| 41 | + .map((platformKey) => { |
| 42 | + const platform = platformKey as keyof PlatformConfig |
| 43 | + const version = platformVersions[platform] |
| 44 | + return `${platforms[platform]?.displayName ?? platform} Version Range: **\`${version}\`**` |
| 45 | + }) |
| 46 | + .join('\n\n') |
146 | 47 | }
|
147 | 48 |
|
148 | 49 | export default generateNotes
|
0 commit comments