forked from brandingbrand/flagship
-
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.
Merge pull request brandingbrand#2657 from crherman7/feat/ios_deep_li…
…nking feat(code): add ios deep linking delegate
- Loading branch information
Showing
7 changed files
with
181 additions
and
2 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
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
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
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,48 @@ | ||
/** | ||
* @jest-environment-options {"requireTemplate": true} | ||
*/ | ||
|
||
/// <reference types="@brandingbrand/code-jest-config" /> | ||
|
||
import { type BuildConfig, fs, path } from "@brandingbrand/code-cli-kit"; | ||
|
||
import transformer from "../src/transformers/ios/app-delegate-mm"; | ||
|
||
describe("AppDelegate.mm transformers", () => { | ||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
}); | ||
|
||
it("should not update AppDelegate.mm with RCTLinkingManager", async () => { | ||
const config = { | ||
...__flagship_code_build_config, | ||
} as BuildConfig; | ||
|
||
await transformer.transform(config, {} as any); | ||
const content = await fs.readFile(path.ios.appDelegate, "utf-8"); | ||
|
||
expect(content).not.toContain("RCTLinkingManager"); | ||
}); | ||
|
||
it("should update AppDelegate.mm with RCTLinkingManager", async () => { | ||
const config = { | ||
...__flagship_code_build_config, | ||
ios: { | ||
...__flagship_code_build_config, | ||
plist: { | ||
urlScheme: { | ||
scheme: "app", | ||
}, | ||
}, | ||
}, | ||
} as BuildConfig; | ||
|
||
await transformer.transform(config, {} as any); | ||
const content = await fs.readFile(path.ios.appDelegate, "utf-8"); | ||
|
||
expect(content).toContain("#import <React/RCTLinkingManager.h>"); | ||
expect(content).toContain( | ||
"if ([RCTLinkingManager application:application openURL:url options:options]) {" | ||
); | ||
}); | ||
}); |
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
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,107 @@ | ||
import { | ||
type BuildConfig, | ||
type PrebuildOptions, | ||
withUTF8, | ||
path, | ||
string, | ||
} from "@brandingbrand/code-cli-kit"; | ||
|
||
import { Transforms, defineTransformer } from "@/lib"; | ||
|
||
/** | ||
* Defines a transformer for the iOS project's "AppDelegate.mm" file. | ||
* | ||
* @type {typeof defineTransformer<(content: string, config: BuildConfig) => string>} - The type of the transformer. | ||
* @property {string} file - The name of the file to be transformed ("AppDelegate.mm"). | ||
* @property {Array<(content: string, config: BuildConfig) => string>} transforms - An array of transformer functions. | ||
* @property {Function} transform - The main transform function that applies all specified transformations. | ||
* @returns {Promise<string>} The updated content of the "AppDelegate.mm" file. | ||
*/ | ||
export default defineTransformer<Transforms<string>>({ | ||
/** | ||
* The name of the file to be transformed ("AppDelegate.mm"). | ||
* @type {string} | ||
*/ | ||
file: "AppDelegate.mm", | ||
|
||
/** | ||
* An array of transformer functions to be applied to the "AppDelegate.mm" file. | ||
* Each function receives the content of the file and the build configuration, | ||
* and returns the updated content after applying specific transformations. | ||
* @type {Array<(content: string, config: BuildConfig) => string>} | ||
*/ | ||
transforms: [ | ||
/** | ||
* Transformer for add RCTLinkingManager import in "AppDelegate.mm". | ||
* @param {string} content - The content of the file. | ||
* @param {BuildConfig} config - The build configuration. | ||
* @returns {string} - The updated content. | ||
*/ | ||
( | ||
content: string, | ||
config: BuildConfig, | ||
options: PrebuildOptions | ||
): string => { | ||
if (!config.ios.plist?.urlScheme) { | ||
return content; | ||
} | ||
|
||
return string.replace( | ||
content, | ||
/(#import "AppDelegate\.h")/, | ||
`$1 | ||
#import <React/RCTLinkingManager.h>` | ||
); | ||
}, | ||
|
||
/** | ||
* Transformer for add delegate method in "AppDelegate.mm". | ||
* @param {string} content - The content of the file. | ||
* @param {BuildConfig} config - The build configuration. | ||
* @returns {string} - The updated content. | ||
*/ | ||
( | ||
content: string, | ||
config: BuildConfig, | ||
options: PrebuildOptions | ||
): string => { | ||
if (!config.ios.plist?.urlScheme) { | ||
return content; | ||
} | ||
|
||
return string.replace( | ||
content, | ||
/(@end)/, | ||
`- (BOOL)application:(UIApplication *)application | ||
openURL:(NSURL *)url | ||
options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options | ||
{ | ||
if ([RCTLinkingManager application:application openURL:url options:options]) { | ||
return YES; | ||
} | ||
return NO; | ||
} | ||
$1` | ||
); | ||
}, | ||
], | ||
|
||
/** | ||
* The main transform function that applies all specified transformations to the "AppDelegate.mm" file. | ||
* @param {BuildConfig} config - The build configuration. | ||
* @returns {Promise<void>} - The updated content of the "AppDelegate.mm" file. | ||
*/ | ||
transform: async function ( | ||
config: BuildConfig, | ||
options: PrebuildOptions | ||
): Promise<void> { | ||
return withUTF8(path.ios.appDelegate, (content: string) => { | ||
return this.transforms.reduce((acc, curr) => { | ||
return curr(acc, config, options); | ||
}, content); | ||
}); | ||
}, | ||
}); |
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