Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Survicate plugin for Expo integration * React Native 4.2.0 * Fix newline issue in SurvicatePluginProps definition * Readme Expo installation instructions refactor
- Loading branch information
1 parent
611889d
commit ccf6412
Showing
11 changed files
with
2,512 additions
and
59 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const packageJson = require('./package.json'); | ||
|
||
const pkg = { | ||
name: packageJson.name, | ||
version: packageJson.version, | ||
}; | ||
|
||
const plugin = require('./lib/commonjs/expo/withSurvicate'); | ||
|
||
module.exports = plugin.default(pkg); |
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,3 @@ | ||
export type SurvicatePluginProps = { | ||
workspaceKey: string | ||
} |
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,20 @@ | ||
import { ConfigPlugin, createRunOncePlugin } from "@expo/config-plugins"; | ||
import { SurvicatePluginProps } from "./types"; | ||
import { withSurvicateAndroidManifest, withSurvicateAndroidMavenRepository } from "./withSurvicateAndroid"; | ||
import { withSurvicateIosInfoPlist } from "./withSurvicateiOS"; | ||
|
||
const withSurvicateReactNative: ConfigPlugin<SurvicatePluginProps> = ( | ||
config, | ||
props | ||
) => { | ||
let newConfig = config; | ||
newConfig = withSurvicateAndroidManifest(newConfig, props); | ||
newConfig = withSurvicateAndroidMavenRepository(newConfig); | ||
newConfig = withSurvicateIosInfoPlist(newConfig, props); | ||
return newConfig; | ||
}; | ||
|
||
const configPlugin = (pkg: { name: string; version: string }) => | ||
createRunOncePlugin(withSurvicateReactNative, pkg.name, pkg.version); | ||
|
||
export default configPlugin; |
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,100 @@ | ||
import { | ||
AndroidConfig, | ||
ConfigPlugin, | ||
withAndroidManifest, | ||
withProjectBuildGradle, | ||
} from "expo/config-plugins"; | ||
import { ExpoConfig } from "expo/config"; | ||
import { SurvicatePluginProps } from "./types"; | ||
import { | ||
MergeResults, | ||
createGeneratedHeaderComment, | ||
removeGeneratedContents, | ||
} from "@expo/config-plugins/build/utils/generateCode"; | ||
|
||
// Using helpers keeps error messages unified and helps cut down on XML format changes. | ||
const { addMetaDataItemToMainApplication, getMainApplicationOrThrow } = | ||
AndroidConfig.Manifest; | ||
|
||
export const withSurvicateAndroidManifest: ConfigPlugin< | ||
SurvicatePluginProps | ||
> = (config: ExpoConfig, props: SurvicatePluginProps) => { | ||
return withAndroidManifest(config, async (config) => { | ||
// Modifiers can be async, but try to keep them fast. | ||
config.modResults = await setCustomConfigAsync( | ||
config.modResults, | ||
props.workspaceKey | ||
); | ||
return config; | ||
}); | ||
}; | ||
|
||
async function setCustomConfigAsync( | ||
androidManifest: AndroidConfig.Manifest.AndroidManifest, | ||
workspaceKey: string | ||
): Promise<AndroidConfig.Manifest.AndroidManifest> { | ||
// Get the <application /> tag and assert if it doesn't exist. | ||
const mainApplication = getMainApplicationOrThrow(androidManifest); | ||
|
||
addMetaDataItemToMainApplication( | ||
mainApplication, | ||
// value for `android:name` | ||
"com.survicate.surveys.workspaceKey", | ||
// value for `android:value` | ||
workspaceKey | ||
); | ||
|
||
return androidManifest; | ||
} | ||
|
||
export const withSurvicateAndroidMavenRepository: ConfigPlugin = (config) => { | ||
return withProjectBuildGradle(config, async (config) => { | ||
if (config.modResults.language !== "groovy") { | ||
console.warn( | ||
"Unsupported language for build.gradle. Only 'groovy' is supported." | ||
); | ||
return config; | ||
} | ||
|
||
config.modResults.contents = await appendContents({ | ||
src: config.modResults.contents, | ||
newSrc: `allprojects { repositories { maven { url 'https://repo.survicate.com' } } }`, | ||
tag: "@survicate/react-native-survicate", | ||
comment: "//", | ||
}).contents; | ||
return config; | ||
}); | ||
}; | ||
|
||
// Fork of config-plugins mergeContents, but appends the contents to the end of the file. | ||
function appendContents({ | ||
src, | ||
newSrc, | ||
tag, | ||
comment, | ||
}: { | ||
src: string; | ||
newSrc: string; | ||
tag: string; | ||
comment: string; | ||
}): MergeResults { | ||
const header = createGeneratedHeaderComment(newSrc, tag, comment); | ||
if (!src.includes(header)) { | ||
// Ensure the old generated contents are removed. | ||
const sanitizedTarget = removeGeneratedContents(src, tag); | ||
const contentsToAdd = [ | ||
// @something | ||
header, | ||
// contents | ||
newSrc, | ||
// @end | ||
`${comment} @generated end ${tag}`, | ||
].join("\n"); | ||
return { | ||
contents: sanitizedTarget ?? src + contentsToAdd, | ||
didMerge: true, | ||
didClear: !!sanitizedTarget, | ||
}; | ||
} | ||
return { contents: src, didClear: false, didMerge: false }; | ||
} |
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,11 @@ | ||
import { ConfigPlugin, withInfoPlist } from 'expo/config-plugins'; | ||
import {SurvicatePluginProps} from './types'; | ||
|
||
export const withSurvicateIosInfoPlist: ConfigPlugin<SurvicatePluginProps> = (config, props: SurvicatePluginProps) => { | ||
return withInfoPlist(config, (config) => { | ||
config.modResults.Survicate = { | ||
WorkspaceKey: props.workspaceKey, | ||
}; | ||
return config; | ||
}); | ||
}; |
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
Oops, something went wrong.