diff --git a/.npmignore b/.npmignore index 980258ff..187c844a 100644 --- a/.npmignore +++ b/.npmignore @@ -1,8 +1,12 @@ src/ !dist/src !android/src - +.github +.vscode +.yarn index.ts +tsconfig.json +jest/tsconfig.json jest/memoryStore.ts diff --git a/autolink/postlink/appGradleLinker.js b/autolink/postlink/appGradleLinker.js deleted file mode 100644 index 97a02de7..00000000 --- a/autolink/postlink/appGradleLinker.js +++ /dev/null @@ -1,62 +0,0 @@ -const path = require("./path"); -const fs = require("fs"); -const { warnn, errorn, logn, infon } = require("./log"); - -class AppGradleLinker { - constructor() { - this.gradlePath = path.appGradle; - this.setNdkVersion = false; - } - - link() { - if (!this.gradlePath) { - errorn( - "android/app/build.gradle not found! Does the file exist in the correct folder?\n Please check the manual installation docs." - ); - return; - } - - logn("Linking android/app/build.gradle..."); - - let content = fs.readFileSync(this.gradlePath, "utf8"); - - try { - content = this._setNdkVersion(content); - this.setNdkVersion = true; - } catch (e) { - errorn(" " + e); - } - - fs.writeFileSync(this.gradlePath, content); - - if (this.setNdkVersion) { - infon("android/app/build.gradle linked successfully!\n"); - } else if (!this.setNdkVersion) { - errorn( - "android/app/build.gradle link failed. Please review the information above and complete the necessary steps manually by following the instructions on https://rnmmkv.vercel.app/#/gettingstarted?id=android\n" - ); - } else { - warnn( - "android/app/build.gradle link partially succeeded. Please review the information above and complete the necessary steps manually by following the instructions on https://rnmmkv.vercel.app/#/gettingstarted?id=android\n" - ); - } - } - - _setNdkVersion(content) { - if (content.includes("rootProject.ext.ndkVersion")) { - warnn(" app/build.gradle already has ndkVersion"); - return content; - } - - if (content.includes("android {")) { - return content.replace( - "android {", - "android {\n ndkVersion rootProject.ext.ndkVersion" - ); - } - - throw new Error(" Could not add ndkVersion version"); - } -} - -module.exports = AppGradleLinker; diff --git a/autolink/postlink/gradlePropertiesLinker.js b/autolink/postlink/gradlePropertiesLinker.js deleted file mode 100644 index 5f0007ff..00000000 --- a/autolink/postlink/gradlePropertiesLinker.js +++ /dev/null @@ -1,84 +0,0 @@ -const path = require("./path"); -const fs = require("fs"); -const { warnn, errorn, logn, infon, debugn } = require("./log"); - -class GradlePropertiesLinker { - constructor(buildToolsVersion) { - this.gradleProperties = path.gradleProperties; - this.setGradleVersion = false; - this.buildToolsVersion = buildToolsVersion; - this.distUrlRegex = /distributionUrl=.*\n/; - this.versionRegex = /\d+(\.\d+)+/; - this.distUrl = `distributionUrl=https://services.gradle.org/distributions/gradle-`; - } - - needLinking(content) { - let distUrl = content.match(this.distUrlRegex)[0]; - let version = distUrl.match(this.versionRegex)[0]; - infon(`Detected gradle version: ${version}`); - return parseInt(version[0]) < 7; - } - - link() { - if (!this.gradleProperties) { - errorn( - "android/gradle/wrapper/gradle-wrapper.properties not found! Does the file exist in the correct folder?\n Please check the manual installation docs." - ); - return; - } - - logn("Linking android/gradle/wrapper/gradle-wrapper.properties..."); - - let content = fs.readFileSync(this.gradleProperties, "utf8"); - - try { - if (!this.needLinking(content)) { - infon("gradle.properties linked already.\n"); - return; - } - - content = this._setGradleVersion(content); - this.setGradleVersion = true; - } catch (e) { - errorn(" " + e); - } - - fs.writeFileSync(this.gradleProperties, content); - - if (this.setGradleVersion) { - infon("gradle-wrapper.properties linked successfully!\n"); - } else if (!this.setGradleVersion) { - errorn( - "gradle-wrapper.properties link failed. Please review the information above and complete the necessary steps manually by following the instructions on https://rnmmkv.vercel.app/#/gettingstarted?id=android\n" - ); - } else { - warnn( - "gradle-wrapper.properties link partially succeeded. Please review the information above and complete the necessary steps manually by following the instructions on https://rnmmkv.vercel.app/#/gettingstarted?id=android\n" - ); - } - } - - _setGradleVersion(content) { - if (this.buildToolsVersion >= 30) { - // https://developer.android.com/studio/releases/gradle-plugin#4-2-0 - return this._setGradleDistributionUrl("6.9", content); - } - - if (this.buildToolsVersion <= 29) { - // https://developer.android.com/studio/releases/gradle-plugin#4-1-0 - return this._setGradleDistributionUrl("6.7", content); - } - - return content; - } - - _setGradleDistributionUrl(version, content) { - return content.replace(this.distUrlRegex, () => { - debugn(` Updating Gradle distributionUrl ${version}`); - - return `distributionUrl=https://services.gradle.org/distributions/gradle-${version}-all.zip\n`; - }); - } -} - -module.exports = GradlePropertiesLinker; diff --git a/autolink/postlink/log.js b/autolink/postlink/log.js deleted file mode 100644 index b1d4bb23..00000000 --- a/autolink/postlink/log.js +++ /dev/null @@ -1,33 +0,0 @@ -const bColors = { - HEADER: '\033[95m', - OKBLUE: '\033[94m', - OKGREEN: '\033[92m', - WARNING: '\033[93m', - FAIL: '\033[91m', - ENDC: '\033[0m', - BOLD: '\033[1m', - UNDERLINE: '\033[4m', -}; - -const log = (text) => process.stdout.write(text); -const logn = (text) => process.stdout.write(text + '\n'); -const warn = (text) => process.stdout.write(`${bColors.WARNING}${text}${bColors.ENDC}`); -const warnn = (text) => warn(text + '\n'); -const error = (text) => process.stdout.write(`${bColors.FAIL}${text}${bColors.ENDC}`); -const errorn = (text) => error(text + '\n'); -const info = (text) => process.stdout.write(`${bColors.OKGREEN}${text}${bColors.ENDC}`); -const infon = (text) => info(text + '\n'); -const debug = (text) => process.stdout.write(`${bColors.OKBLUE}${text}${bColors.ENDC}`); -const debugn = (text) => debug(text + '\n'); - -module.exports = { - log, - logn, - warn, - warnn, - info, - infon, - debug, - debugn, - errorn, -}; diff --git a/autolink/postlink/path.js b/autolink/postlink/path.js deleted file mode 100644 index 1a5912cd..00000000 --- a/autolink/postlink/path.js +++ /dev/null @@ -1,30 +0,0 @@ -const glob = require("glob"); -const ignoreFolders = { - ignore: [ - "node_modules/**", - "**/build/**", - "**/Build/**", - "**/DerivedData/**", - "**/*-tvOS*/**", - ], -}; - -exports.mainApplicationJava = glob.sync( - "**/MainApplication.java", - ignoreFolders -)[0]; - -exports.rootGradle = exports.mainApplicationJava.replace( - /android\/app\/.*\.java/, - "android/build.gradle" -); - -exports.appGradle = exports.mainApplicationJava.replace( - /android\/app\/.*\.java/, - "android/app/build.gradle" -); - -exports.gradleProperties = exports.mainApplicationJava.replace( - /android\/app\/.*\.java/, - "android/gradle/wrapper/gradle-wrapper.properties" -); diff --git a/autolink/postlink/postLinkAndroid.js b/autolink/postlink/postLinkAndroid.js deleted file mode 100644 index a7065443..00000000 --- a/autolink/postlink/postLinkAndroid.js +++ /dev/null @@ -1,13 +0,0 @@ -const { infon } = require("./log"); -const RootGradleLinker = require("./rootGradleLinker"); -const AppGradleLinker = require("./appGradleLinker"); -const GradlePropertiesLinker = require("./gradlePropertiesLinker"); - -module.exports = () => { - infon("\nRunning Android postlink script.\n"); - - new RootGradleLinker().link((buildToolsVersion) => { - new AppGradleLinker(buildToolsVersion).link(); - new GradlePropertiesLinker(buildToolsVersion).link(); - }); -}; diff --git a/autolink/postlink/rootGradleLinker.js b/autolink/postlink/rootGradleLinker.js deleted file mode 100644 index fbd434fd..00000000 --- a/autolink/postlink/rootGradleLinker.js +++ /dev/null @@ -1,119 +0,0 @@ -const path = require("./path"); -const fs = require("fs"); -const { warnn, errorn, logn, infon, debugn } = require("./log"); - -class RootGradleLinker { - constructor() { - this.gradlePath = path.rootGradle; - this.setNdkVersion = false; - } - - link(cb) { - if (!this.gradlePath) { - errorn( - "Root build.gradle not found! Does the file exist in the correct folder?\n Please check the manual installation docs." - ); - return; - } - - logn("Linking android/build.gradle..."); - - let content = fs.readFileSync(this.gradlePath, "utf8"); - - try { - content = this._setNdkVersion(content); - content = this._setMinSdkVersion(content); - this.setNdkVersion = true; - } catch (e) { - errorn(" " + e); - } - - fs.writeFileSync(this.gradlePath, content); - - if (this.setNdkVersion) { - infon("Root build.gradle linked successfully!\n"); - cb && cb(this._getBuildToolsVersion(content)); - } else if (!this.setNdkVersion) { - errorn( - "Root build.gradle link failed. Please review the information above and complete the necessary steps manually by following the instructions on https://rnmmkv.vercel.app/#/gettingstarted?id=android\n" - ); - } else { - warnn( - "Root build.gradle link partially succeeded. Please review the information above and complete the necessary steps manually by following the instructions on https://rnmmkv.vercel.app/#/gettingstarted?id=android\n" - ); - } - } - - _getBuildToolsVersion(content) { - const minSdkVersion = content.match( - /buildToolsVersion\s{0,}=\s{0,}["']?(\d*)/ - ); - - if (minSdkVersion && minSdkVersion[1]) { - return parseInt(minSdkVersion[1], 10); - } - - return NaN; - } - - _setMinSdkVersion(content) { - let minSdkRegex = /minSdkVersion = ([0-9])\w+/; - let minSdk = content.match(minSdkRegex)[0]; - if (parseInt(minSdk.match(/([0-9])\w+/)) < 21) { - return content.replace(minSdk, "minSdkVersion = 21"); - } - return content; - } - - _setNdkVersion(content) { - if (content.includes("ndkVersion")) { - warnn(" ndkVersion version already declared"); - return content; - } - - // https://developer.android.com/studio/projects/install-ndk#default-ndk-per-agp - if (this._getBuildToolsVersion(content) <= 29) { - return this._setNdkVersionSdk29(content); - } else if (this._getBuildToolsVersion(content) >= 30) { - return this._setNdkVersionSdk30(content); - } - - throw new Error(" Could not add ndkVersion version"); - } - - _changeAndroidGradlePluginVersion(version, content) { - return content.replace( - /classpath\s*\(?["']com\.android\.tools\.build:gradle:(\d+\.\d+\.\d+)["']\)?/, - (all, ver) => { - debugn(` Updating AndroidGradlePlugin version ${ver} => ${version}`); - - return all.replace(ver, version); - } - ); - } - - _addNdkVersion(version, content) { - debugn(` Adding ndkVersion ${version}`); - - return content.replace( - "targetSdkVersion", - `ndkVersion = "${version}"\n targetSdkVersion` - ); - } - - _setNdkVersionSdk29(content) { - return this._addNdkVersion( - "21.1.6352462", - this._changeAndroidGradlePluginVersion("4.1.0", content) - ); - } - - _setNdkVersionSdk30(content) { - return this._addNdkVersion( - "21.4.7075529", - this._changeAndroidGradlePluginVersion("4.2.2", content) - ); - } -} - -module.exports = RootGradleLinker; diff --git a/autolink/postlink/run.js b/autolink/postlink/run.js deleted file mode 100755 index d9cc9736..00000000 --- a/autolink/postlink/run.js +++ /dev/null @@ -1,19 +0,0 @@ -#!/usr/bin/env node - -const { infon, warnn } = require("./log"); -const postLinkAndroid = require("./postLinkAndroid"); - -postLinkAndroid(); -//postLinkIOS(); - -infon( - "\nreact-native-mmkv-storage link is completed. Check the logs above for more information.\n" -); -warnn( - " If any of the steps failed, check the installation docs and go through the necessary steps manually:" -); -warnn(" https://rnmmkv.vercel.app/#/gettingstarted\n"); -infon( - "When you're done, don't forget to update the index.js file as mentioned in docs!\n" -); -infon("Thank you for using react-native-mmkv-storage!\n\n");