onesignal-expo-plugin 1.0.0-beta10 → 1.0.0-beta11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.eslintignore ADDED
@@ -0,0 +1,6 @@
1
+ # don't ever lint node_modules
2
+ node_modules
3
+ # don't lint build output (make sure it's set to your correct build folder name)
4
+ build
5
+ # don't lint the examples projects
6
+ examples
package/.eslintrc.js ADDED
@@ -0,0 +1,18 @@
1
+ module.exports = {
2
+ root: true,
3
+ parser: '@typescript-eslint/parser',
4
+ plugins: [
5
+ '@typescript-eslint',
6
+ ],
7
+ parserOptions: {
8
+ project: "tsconfig.json",
9
+ },
10
+ extends: [
11
+ 'eslint:recommended',
12
+ 'plugin:@typescript-eslint/recommended',
13
+ ],
14
+ rules: {
15
+ "@typescript-eslint/no-floating-promises": "error",
16
+ "no-extra-boolean-cast": "off"
17
+ }
18
+ };
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <h1 align="center">Welcome to onesignal-expo-plugin 👋</h1>
2
2
  <p>
3
- <img alt="Version" src="https://img.shields.io/badge/version-1.0.0--beta10-blue.svg?cacheSeconds=2592000" />
3
+ <img alt="Version" src="https://img.shields.io/badge/version-1.0.0--beta11-blue.svg?cacheSeconds=2592000" />
4
4
  <a href="https://github.com/OneSignal/onesignal-expo-plugin#readme" target="_blank">
5
5
  <img alt="Documentation" src="https://img.shields.io/badge/documentation-yes-brightgreen.svg" />
6
6
  </a>
@@ -34,6 +34,7 @@ const iosConstants_1 = require("../support/iosConstants");
34
34
  const updatePodfile_1 = require("../support/updatePodfile");
35
35
  const NseUpdaterManager_1 = __importDefault(require("../support/NseUpdaterManager"));
36
36
  const OneSignalLog_1 = require("../support/OneSignalLog");
37
+ const FileManager_1 = require("../support/FileManager");
37
38
  /**
38
39
  * Add 'app-environment' record with current environment to '<project-name>.entitlements' file
39
40
  * @see https://documentation.onesignal.com/docs/react-native-sdk-setup#step-4-install-for-ios-using-cocoapods-for-ios-apps
@@ -79,8 +80,8 @@ const withAppGroupPermissions = (config) => {
79
80
  if (!Array.isArray(newConfig.modResults[APP_GROUP_KEY])) {
80
81
  newConfig.modResults[APP_GROUP_KEY] = [];
81
82
  }
82
- let modResultsArray = newConfig.modResults[APP_GROUP_KEY];
83
- let entitlement = `group.${((_a = newConfig === null || newConfig === void 0 ? void 0 : newConfig.ios) === null || _a === void 0 ? void 0 : _a.bundleIdentifier) || ""}.onesignal`;
83
+ const modResultsArray = newConfig.modResults[APP_GROUP_KEY];
84
+ const entitlement = `group.${((_a = newConfig === null || newConfig === void 0 ? void 0 : newConfig.ios) === null || _a === void 0 ? void 0 : _a.bundleIdentifier) || ""}.onesignal`;
84
85
  if (modResultsArray.indexOf(entitlement) !== -1) {
85
86
  return newConfig;
86
87
  }
@@ -113,41 +114,38 @@ const withOneSignalIos = (config, props) => {
113
114
  exports.withOneSignalIos = withOneSignalIos;
114
115
  function xcodeProjectAddNse(appName, options, sourceDir) {
115
116
  const { iosPath, devTeam, bundleIdentifier, bundleVersion, bundleShortVersion, iPhoneDeploymentTarget } = options;
116
- (0, updatePodfile_1.updatePodfile)(iosPath);
117
- NseUpdaterManager_1.default.updateNSEEntitlements(`group.${bundleIdentifier}.onesignal`);
118
- NseUpdaterManager_1.default.updateNSEBundleVersion(bundleVersion !== null && bundleVersion !== void 0 ? bundleVersion : iosConstants_1.DEFAULT_BUNDLE_VERSION);
119
- NseUpdaterManager_1.default.updateNSEBundleShortVersion(bundleShortVersion !== null && bundleShortVersion !== void 0 ? bundleShortVersion : iosConstants_1.DEFAULT_BUNDLE_SHORT_VERSION);
117
+ // not awaiting in order to not block main thread
118
+ (0, updatePodfile_1.updatePodfile)(iosPath).catch(err => { OneSignalLog_1.OneSignalLog.error(err); });
120
119
  const projPath = `${iosPath}/${appName}.xcodeproj/project.pbxproj`;
121
- const targetName = "OneSignalNotificationServiceExtension";
122
120
  const extFiles = [
123
121
  "NotificationService.h",
124
122
  "NotificationService.m",
125
- `${targetName}.entitlements`,
126
- `${targetName}-Info.plist`
123
+ `${iosConstants_1.NSE_TARGET_NAME}.entitlements`,
124
+ `${iosConstants_1.NSE_TARGET_NAME}-Info.plist`
127
125
  ];
128
126
  const xcodeProject = xcode_1.default.project(projPath);
129
- xcodeProject.parse(function (err) {
127
+ xcodeProject.parse(async function (err) {
130
128
  if (err) {
131
129
  OneSignalLog_1.OneSignalLog.log(`Error parsing iOS project: ${JSON.stringify(err)}`);
132
130
  return;
133
131
  }
134
- // Copy in the extension files
135
- fs.mkdirSync(`${iosPath}/${targetName}`, { recursive: true });
136
- extFiles.forEach(function (extFile) {
137
- let targetFile = `${iosPath}/${targetName}/${extFile}`;
138
- try {
139
- fs.createReadStream(`${sourceDir}${extFile}`).pipe(fs.createWriteStream(targetFile));
140
- }
141
- catch (err) {
142
- OneSignalLog_1.OneSignalLog.log(err);
143
- }
144
- });
145
- const projObjects = xcodeProject.hash.project.objects;
132
+ /* COPY OVER EXTENSION FILES */
133
+ fs.mkdirSync(`${iosPath}/${iosConstants_1.NSE_TARGET_NAME}`, { recursive: true });
134
+ for (let i = 0; i < extFiles.length; i++) {
135
+ const extFile = extFiles[i];
136
+ const targetFile = `${iosPath}/${iosConstants_1.NSE_TARGET_NAME}/${extFile}`;
137
+ await FileManager_1.FileManager.copyFile(`${sourceDir}${extFile}`, targetFile);
138
+ }
139
+ /* MODIFY COPIED EXTENSION FILES */
140
+ const nseUpdater = new NseUpdaterManager_1.default(iosPath);
141
+ await nseUpdater.updateNSEEntitlements(`group.${bundleIdentifier}.onesignal`);
142
+ await nseUpdater.updateNSEBundleVersion(bundleVersion !== null && bundleVersion !== void 0 ? bundleVersion : iosConstants_1.DEFAULT_BUNDLE_VERSION);
143
+ await nseUpdater.updateNSEBundleShortVersion(bundleShortVersion !== null && bundleShortVersion !== void 0 ? bundleShortVersion : iosConstants_1.DEFAULT_BUNDLE_SHORT_VERSION);
146
144
  // Create new PBXGroup for the extension
147
- let extGroup = xcodeProject.addPbxGroup(extFiles, targetName, targetName);
145
+ const extGroup = xcodeProject.addPbxGroup(extFiles, iosConstants_1.NSE_TARGET_NAME, iosConstants_1.NSE_TARGET_NAME);
148
146
  // Add the new PBXGroup to the top level group. This makes the
149
147
  // files / folder appear in the file explorer in Xcode.
150
- let groups = xcodeProject.hash.project.objects["PBXGroup"];
148
+ const groups = xcodeProject.hash.project.objects["PBXGroup"];
151
149
  Object.keys(groups).forEach(function (key) {
152
150
  if (groups[key].name === undefined) {
153
151
  xcodeProject.addToPbxGroup(extGroup.uuid, key);
@@ -157,30 +155,31 @@ function xcodeProjectAddNse(appName, options, sourceDir) {
157
155
  // Xcode projects don't contain these if there is only one target
158
156
  // An upstream fix should be made to the code referenced in this link:
159
157
  // - https://github.com/apache/cordova-node-xcode/blob/8b98cabc5978359db88dc9ff2d4c015cba40f150/lib/pbxProject.js#L860
158
+ const projObjects = xcodeProject.hash.project.objects;
160
159
  projObjects['PBXTargetDependency'] = projObjects['PBXTargetDependency'] || {};
161
160
  projObjects['PBXContainerItemProxy'] = projObjects['PBXTargetDependency'] || {};
162
- if (!!xcodeProject.pbxTargetByName(targetName)) {
163
- OneSignalLog_1.OneSignalLog.log(`${targetName} already exists in project. Skipping...`);
161
+ if (!!xcodeProject.pbxTargetByName(iosConstants_1.NSE_TARGET_NAME)) {
162
+ OneSignalLog_1.OneSignalLog.log(`${iosConstants_1.NSE_TARGET_NAME} already exists in project. Skipping...`);
164
163
  return;
165
164
  }
166
165
  // Add the NSE target
167
166
  // This adds PBXTargetDependency and PBXContainerItemProxy for you
168
- const nseTarget = xcodeProject.addTarget(targetName, "app_extension", targetName, `${bundleIdentifier}.${targetName}`);
167
+ const nseTarget = xcodeProject.addTarget(iosConstants_1.NSE_TARGET_NAME, "app_extension", iosConstants_1.NSE_TARGET_NAME, `${bundleIdentifier}.${iosConstants_1.NSE_TARGET_NAME}`);
169
168
  // Add build phases to the new target
170
169
  xcodeProject.addBuildPhase(["NotificationService.m"], "PBXSourcesBuildPhase", "Sources", nseTarget.uuid);
171
170
  xcodeProject.addBuildPhase([], "PBXResourcesBuildPhase", "Resources", nseTarget.uuid);
172
171
  xcodeProject.addBuildPhase([], "PBXFrameworksBuildPhase", "Frameworks", nseTarget.uuid);
173
172
  // Edit the Deployment info of the new Target, only IphoneOS and Targeted Device Family
174
173
  // However, can be more
175
- let configurations = xcodeProject.pbxXCBuildConfigurationSection();
176
- for (let key in configurations) {
174
+ const configurations = xcodeProject.pbxXCBuildConfigurationSection();
175
+ for (const key in configurations) {
177
176
  if (typeof configurations[key].buildSettings !== "undefined" &&
178
- configurations[key].buildSettings.PRODUCT_NAME == `"${targetName}"`) {
179
- let buildSettingsObj = configurations[key].buildSettings;
177
+ configurations[key].buildSettings.PRODUCT_NAME == `"${iosConstants_1.NSE_TARGET_NAME}"`) {
178
+ const buildSettingsObj = configurations[key].buildSettings;
180
179
  buildSettingsObj.DEVELOPMENT_TEAM = devTeam;
181
180
  buildSettingsObj.IPHONEOS_DEPLOYMENT_TARGET = iPhoneDeploymentTarget !== null && iPhoneDeploymentTarget !== void 0 ? iPhoneDeploymentTarget : iosConstants_1.IPHONEOS_DEPLOYMENT_TARGET;
182
181
  buildSettingsObj.TARGETED_DEVICE_FAMILY = iosConstants_1.TARGETED_DEVICE_FAMILY;
183
- buildSettingsObj.CODE_SIGN_ENTITLEMENTS = `${targetName}/${targetName}.entitlements`;
182
+ buildSettingsObj.CODE_SIGN_ENTITLEMENTS = `${iosConstants_1.NSE_TARGET_NAME}/${iosConstants_1.NSE_TARGET_NAME}.entitlements`;
184
183
  buildSettingsObj.CODE_SIGN_STYLE = "Automatic";
185
184
  }
186
185
  }
@@ -19,23 +19,40 @@ var __importStar = (this && this.__importStar) || function (mod) {
19
19
  return result;
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.ReaderManager = void 0;
22
+ exports.FileManager = void 0;
23
23
  const fs = __importStar(require("fs"));
24
24
  const OneSignalLog_1 = require("./OneSignalLog");
25
- class ReaderManager {
26
- static async readFile(path, replace) {
25
+ /**
26
+ * FileManager contains static *awaitable* file-system functions
27
+ */
28
+ class FileManager {
29
+ static async readFile(path) {
27
30
  return new Promise((resolve, reject) => {
28
31
  fs.readFile(path, 'utf8', (err, data) => {
29
- if (err) {
30
- reject(err);
31
- }
32
- if (!data) {
32
+ if (err || !data) {
33
33
  OneSignalLog_1.OneSignalLog.error("Couldn't read file:" + path);
34
+ reject(err);
34
35
  return;
35
36
  }
36
37
  resolve(data);
37
38
  });
38
39
  });
39
40
  }
41
+ static async writeFile(path, contents) {
42
+ return new Promise((resolve, reject) => {
43
+ fs.writeFile(path, contents, 'utf8', (err) => {
44
+ if (err) {
45
+ OneSignalLog_1.OneSignalLog.error("Couldn't write file:" + path);
46
+ reject(err);
47
+ return;
48
+ }
49
+ resolve();
50
+ });
51
+ });
52
+ }
53
+ static async copyFile(path1, path2) {
54
+ const fileContents = await FileManager.readFile(path1);
55
+ await FileManager.writeFile(path2, fileContents);
56
+ }
40
57
  }
41
- exports.ReaderManager = ReaderManager;
58
+ exports.FileManager = FileManager;
@@ -1,56 +1,32 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
2
  Object.defineProperty(exports, "__esModule", { value: true });
22
- const fs = __importStar(require("fs"));
3
+ const FileManager_1 = require("./FileManager");
23
4
  const iosConstants_1 = require("./iosConstants");
24
- const OneSignalLog_1 = require("./OneSignalLog");
25
- const ReaderManager_1 = require("./ReaderManager");
26
- const entitlementsFilePath = `${__dirname}/serviceExtensionFiles/OneSignalNotificationServiceExtension.entitlements`;
27
- const plistFilePath = `${__dirname}/serviceExtensionFiles/OneSignalNotificationServiceExtension-Info.plist`;
28
- const logIfError = (err) => {
29
- if (err) {
30
- OneSignalLog_1.OneSignalLog.error("Error updating OneSignal NSE Entitlement File.");
31
- }
32
- };
5
+ // project `ios/OneSignalNotificationServiceExtension` directory
6
+ const entitlementsFileName = `OneSignalNotificationServiceExtension.entitlements`;
7
+ const plistFileName = `OneSignalNotificationServiceExtension-Info.plist`;
33
8
  class NseUpdaterManager {
34
- static async updateNSEEntitlements(groupIdentifier) {
35
- let entitlementsFile = await ReaderManager_1.ReaderManager.readFile(entitlementsFilePath);
9
+ constructor(iosPath) {
10
+ this.nsePath = '';
11
+ this.nsePath = `${iosPath}/${iosConstants_1.NSE_TARGET_NAME}`;
12
+ }
13
+ async updateNSEEntitlements(groupIdentifier) {
14
+ const entitlementsFilePath = `${this.nsePath}/${entitlementsFileName}`;
15
+ let entitlementsFile = await FileManager_1.FileManager.readFile(entitlementsFilePath);
36
16
  entitlementsFile = entitlementsFile.replace(iosConstants_1.GROUP_IDENTIFIER_TEMPLATE_REGEX, groupIdentifier);
37
- fs.writeFile(entitlementsFilePath, entitlementsFile, 'utf-8', (err) => {
38
- logIfError(err);
39
- });
17
+ await FileManager_1.FileManager.writeFile(entitlementsFilePath, entitlementsFile);
40
18
  }
41
- static async updateNSEBundleVersion(version) {
42
- let plistFile = await ReaderManager_1.ReaderManager.readFile(plistFilePath);
19
+ async updateNSEBundleVersion(version) {
20
+ const plistFilePath = `${this.nsePath}/${plistFileName}`;
21
+ let plistFile = await FileManager_1.FileManager.readFile(plistFilePath);
43
22
  plistFile = plistFile.replace(iosConstants_1.BUNDLE_VERSION_TEMPLATE_REGEX, version);
44
- fs.writeFile(plistFilePath, plistFile, 'utf-8', err => {
45
- logIfError(err);
46
- });
23
+ await FileManager_1.FileManager.writeFile(plistFilePath, plistFile);
47
24
  }
48
- static async updateNSEBundleShortVersion(version) {
49
- let plistFile = await ReaderManager_1.ReaderManager.readFile(plistFilePath);
25
+ async updateNSEBundleShortVersion(version) {
26
+ const plistFilePath = `${this.nsePath}/${plistFileName}`;
27
+ let plistFile = await FileManager_1.FileManager.readFile(plistFilePath);
50
28
  plistFile = plistFile.replace(iosConstants_1.BUNDLE_SHORT_VERSION_TEMPLATE_REGEX, version);
51
- fs.writeFile(plistFilePath, plistFile, 'utf-8', err => {
52
- logIfError(err);
53
- });
29
+ await FileManager_1.FileManager.writeFile(plistFilePath, plistFile);
54
30
  }
55
31
  }
56
32
  exports.default = NseUpdaterManager;
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_BUNDLE_SHORT_VERSION = exports.DEFAULT_BUNDLE_VERSION = exports.BUNDLE_VERSION_TEMPLATE_REGEX = exports.BUNDLE_SHORT_VERSION_TEMPLATE_REGEX = exports.GROUP_IDENTIFIER_TEMPLATE_REGEX = exports.NSE_PODFILE_REGEX = exports.NSE_PODFILE_SNIPPET = exports.TARGETED_DEVICE_FAMILY = exports.IPHONEOS_DEPLOYMENT_TARGET = void 0;
3
+ exports.NSE_TARGET_NAME = exports.DEFAULT_BUNDLE_SHORT_VERSION = exports.DEFAULT_BUNDLE_VERSION = exports.BUNDLE_VERSION_TEMPLATE_REGEX = exports.BUNDLE_SHORT_VERSION_TEMPLATE_REGEX = exports.GROUP_IDENTIFIER_TEMPLATE_REGEX = exports.NSE_PODFILE_REGEX = exports.NSE_PODFILE_SNIPPET = exports.TARGETED_DEVICE_FAMILY = exports.IPHONEOS_DEPLOYMENT_TARGET = void 0;
4
4
  exports.IPHONEOS_DEPLOYMENT_TARGET = "11.0";
5
- exports.TARGETED_DEVICE_FAMILY = `\"1,2\"`;
5
+ exports.TARGETED_DEVICE_FAMILY = `"1,2"`;
6
6
  exports.NSE_PODFILE_SNIPPET = `
7
7
  target 'OneSignalNotificationServiceExtension' do
8
8
  pod 'OneSignalXCFramework', '>= 3.0', '< 4.0'
@@ -13,3 +13,4 @@ exports.BUNDLE_SHORT_VERSION_TEMPLATE_REGEX = /{{BUNDLE_SHORT_VERSION}}/gm;
13
13
  exports.BUNDLE_VERSION_TEMPLATE_REGEX = /{{BUNDLE_VERSION}}/gm;
14
14
  exports.DEFAULT_BUNDLE_VERSION = '1';
15
15
  exports.DEFAULT_BUNDLE_SHORT_VERSION = '1.0';
16
+ exports.NSE_TARGET_NAME = "OneSignalNotificationServiceExtension";
@@ -7,9 +7,9 @@ exports.updatePodfile = void 0;
7
7
  const fs_1 = __importDefault(require("fs"));
8
8
  const iosConstants_1 = require("./iosConstants");
9
9
  const OneSignalLog_1 = require("./OneSignalLog");
10
- const ReaderManager_1 = require("./ReaderManager");
10
+ const FileManager_1 = require("./FileManager");
11
11
  async function updatePodfile(iosPath) {
12
- const podfile = await ReaderManager_1.ReaderManager.readFile(`${iosPath}/Podfile`);
12
+ const podfile = await FileManager_1.FileManager.readFile(`${iosPath}/Podfile`);
13
13
  const matches = podfile.match(iosConstants_1.NSE_PODFILE_REGEX);
14
14
  if (matches) {
15
15
  OneSignalLog_1.OneSignalLog.log("OneSignalNotificationServiceExtension target already added to Podfile. Skipping...");
@@ -16,11 +16,13 @@ import {
16
16
  DEFAULT_BUNDLE_SHORT_VERSION,
17
17
  DEFAULT_BUNDLE_VERSION,
18
18
  IPHONEOS_DEPLOYMENT_TARGET,
19
+ NSE_TARGET_NAME,
19
20
  TARGETED_DEVICE_FAMILY
20
21
  } from "../support/iosConstants";
21
22
  import { updatePodfile } from "../support/updatePodfile";
22
23
  import NseUpdaterManager from "../support/NseUpdaterManager";
23
24
  import { OneSignalLog } from "../support/OneSignalLog";
25
+ import { FileManager } from "../support/FileManager";
24
26
 
25
27
  /* I N T E R F A C E S */
26
28
  interface PluginOptions {
@@ -87,8 +89,8 @@ const withAppGroupPermissions: ConfigPlugin<OneSignalPluginProps> = (
87
89
  if (!Array.isArray(newConfig.modResults[APP_GROUP_KEY])) {
88
90
  newConfig.modResults[APP_GROUP_KEY] = [];
89
91
  }
90
- let modResultsArray = (newConfig.modResults[APP_GROUP_KEY] as Array<any>);
91
- let entitlement = `group.${newConfig?.ios?.bundleIdentifier || ""}.onesignal`;
92
+ const modResultsArray = (newConfig.modResults[APP_GROUP_KEY] as Array<any>);
93
+ const entitlement = `group.${newConfig?.ios?.bundleIdentifier || ""}.onesignal`;
92
94
  if (modResultsArray.indexOf(entitlement) !== -1) {
93
95
  return newConfig;
94
96
  }
@@ -130,7 +132,6 @@ export const withOneSignalIos: ConfigPlugin<OneSignalPluginProps> = (
130
132
  return config;
131
133
  };
132
134
 
133
-
134
135
  export function xcodeProjectAddNse(
135
136
  appName: string,
136
137
  options: PluginOptions,
@@ -138,51 +139,47 @@ export function xcodeProjectAddNse(
138
139
  ): void {
139
140
  const { iosPath, devTeam, bundleIdentifier, bundleVersion, bundleShortVersion, iPhoneDeploymentTarget } = options;
140
141
 
141
- updatePodfile(iosPath);
142
- NseUpdaterManager.updateNSEEntitlements(`group.${bundleIdentifier}.onesignal`)
143
- NseUpdaterManager.updateNSEBundleVersion(bundleVersion ?? DEFAULT_BUNDLE_VERSION);
144
- NseUpdaterManager.updateNSEBundleShortVersion(bundleShortVersion ?? DEFAULT_BUNDLE_SHORT_VERSION);
142
+ // not awaiting in order to not block main thread
143
+ updatePodfile(iosPath).catch(err => { OneSignalLog.error(err) });
145
144
 
146
145
  const projPath = `${iosPath}/${appName}.xcodeproj/project.pbxproj`;
147
- const targetName = "OneSignalNotificationServiceExtension";
148
146
 
149
147
  const extFiles = [
150
148
  "NotificationService.h",
151
149
  "NotificationService.m",
152
- `${targetName}.entitlements`,
153
- `${targetName}-Info.plist`
150
+ `${NSE_TARGET_NAME}.entitlements`,
151
+ `${NSE_TARGET_NAME}-Info.plist`
154
152
  ];
155
153
 
156
154
  const xcodeProject = xcode.project(projPath);
157
155
 
158
- xcodeProject.parse(function(err: Error) {
156
+ xcodeProject.parse(async function(err: Error) {
159
157
  if (err) {
160
158
  OneSignalLog.log(`Error parsing iOS project: ${JSON.stringify(err)}`);
161
159
  return;
162
160
  }
163
161
 
164
- // Copy in the extension files
165
- fs.mkdirSync(`${iosPath}/${targetName}`, { recursive: true });
166
- extFiles.forEach(function (extFile) {
167
- let targetFile = `${iosPath}/${targetName}/${extFile}`;
168
-
169
- try {
170
- fs.createReadStream(`${sourceDir}${extFile}`).pipe(
171
- fs.createWriteStream(targetFile)
172
- );
173
- } catch (err) {
174
- OneSignalLog.log(err as string);
175
- }
176
- });
162
+ /* COPY OVER EXTENSION FILES */
163
+ fs.mkdirSync(`${iosPath}/${NSE_TARGET_NAME}`, { recursive: true });
177
164
 
178
- const projObjects = xcodeProject.hash.project.objects;
165
+ for(let i = 0; i < extFiles.length; i++) {
166
+ const extFile = extFiles[i];
167
+ const targetFile = `${iosPath}/${NSE_TARGET_NAME}/${extFile}`;
168
+ await FileManager.copyFile(`${sourceDir}${extFile}`, targetFile);
169
+ }
170
+
171
+ /* MODIFY COPIED EXTENSION FILES */
172
+ const nseUpdater = new NseUpdaterManager(iosPath);
173
+ await nseUpdater.updateNSEEntitlements(`group.${bundleIdentifier}.onesignal`)
174
+ await nseUpdater.updateNSEBundleVersion(bundleVersion ?? DEFAULT_BUNDLE_VERSION);
175
+ await nseUpdater.updateNSEBundleShortVersion(bundleShortVersion ?? DEFAULT_BUNDLE_SHORT_VERSION);
179
176
 
180
177
  // Create new PBXGroup for the extension
181
- let extGroup = xcodeProject.addPbxGroup(extFiles, targetName, targetName);
178
+ const extGroup = xcodeProject.addPbxGroup(extFiles, NSE_TARGET_NAME, NSE_TARGET_NAME);
182
179
 
183
180
  // Add the new PBXGroup to the top level group. This makes the
184
181
  // files / folder appear in the file explorer in Xcode.
185
- let groups = xcodeProject.hash.project.objects["PBXGroup"];
182
+ const groups = xcodeProject.hash.project.objects["PBXGroup"];
186
183
  Object.keys(groups).forEach(function (key) {
187
184
  if (groups[key].name === undefined) {
188
185
  xcodeProject.addToPbxGroup(extGroup.uuid, key);
@@ -193,17 +190,18 @@ export function xcodeProjectAddNse(
193
190
  // Xcode projects don't contain these if there is only one target
194
191
  // An upstream fix should be made to the code referenced in this link:
195
192
  // - https://github.com/apache/cordova-node-xcode/blob/8b98cabc5978359db88dc9ff2d4c015cba40f150/lib/pbxProject.js#L860
193
+ const projObjects = xcodeProject.hash.project.objects;
196
194
  projObjects['PBXTargetDependency'] = projObjects['PBXTargetDependency'] || {};
197
195
  projObjects['PBXContainerItemProxy'] = projObjects['PBXTargetDependency'] || {};
198
196
 
199
- if (!!xcodeProject.pbxTargetByName(targetName)) {
200
- OneSignalLog.log(`${targetName} already exists in project. Skipping...`);
197
+ if (!!xcodeProject.pbxTargetByName(NSE_TARGET_NAME)) {
198
+ OneSignalLog.log(`${NSE_TARGET_NAME} already exists in project. Skipping...`);
201
199
  return;
202
200
  }
203
201
 
204
202
  // Add the NSE target
205
203
  // This adds PBXTargetDependency and PBXContainerItemProxy for you
206
- const nseTarget = xcodeProject.addTarget(targetName, "app_extension", targetName, `${bundleIdentifier}.${targetName}`);
204
+ const nseTarget = xcodeProject.addTarget(NSE_TARGET_NAME, "app_extension", NSE_TARGET_NAME, `${bundleIdentifier}.${NSE_TARGET_NAME}`);
207
205
 
208
206
  // Add build phases to the new target
209
207
  xcodeProject.addBuildPhase(
@@ -223,17 +221,17 @@ export function xcodeProjectAddNse(
223
221
 
224
222
  // Edit the Deployment info of the new Target, only IphoneOS and Targeted Device Family
225
223
  // However, can be more
226
- let configurations = xcodeProject.pbxXCBuildConfigurationSection();
227
- for (let key in configurations) {
224
+ const configurations = xcodeProject.pbxXCBuildConfigurationSection();
225
+ for (const key in configurations) {
228
226
  if (
229
227
  typeof configurations[key].buildSettings !== "undefined" &&
230
- configurations[key].buildSettings.PRODUCT_NAME == `"${targetName}"`
228
+ configurations[key].buildSettings.PRODUCT_NAME == `"${NSE_TARGET_NAME}"`
231
229
  ) {
232
- let buildSettingsObj = configurations[key].buildSettings;
230
+ const buildSettingsObj = configurations[key].buildSettings;
233
231
  buildSettingsObj.DEVELOPMENT_TEAM = devTeam;
234
232
  buildSettingsObj.IPHONEOS_DEPLOYMENT_TARGET = iPhoneDeploymentTarget ?? IPHONEOS_DEPLOYMENT_TARGET;
235
233
  buildSettingsObj.TARGETED_DEVICE_FAMILY = TARGETED_DEVICE_FAMILY;
236
- buildSettingsObj.CODE_SIGN_ENTITLEMENTS = `${targetName}/${targetName}.entitlements`;
234
+ buildSettingsObj.CODE_SIGN_ENTITLEMENTS = `${NSE_TARGET_NAME}/${NSE_TARGET_NAME}.entitlements`;
237
235
  buildSettingsObj.CODE_SIGN_STYLE = "Automatic";
238
236
  }
239
237
  }
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "onesignal-expo-plugin",
3
- "version": "1.0.0-beta10",
3
+ "version": "1.0.0-beta11",
4
4
  "description": "The OneSignal Expo plugin allows you to use OneSignal without leaving the managed workflow. Developed in collaboration with SweetGreen.",
5
5
  "main": "./app.plugin.js",
6
6
  "scripts": {
7
7
  "test": "echo \"Error: no test specified\" && exit 1",
8
- "build": "rm -rf build && tsc && cp -a support/serviceExtensionFiles build/support/",
8
+ "build": "yarn run lint && rm -rf build && tsc && cp -a support/serviceExtensionFiles build/support/",
9
+ "lint": "eslint . --ext .ts",
9
10
  "prepare": "yarn run build"
10
11
  },
11
12
  "repository": {
@@ -31,11 +32,14 @@
31
32
  },
32
33
  "homepage": "https://github.com/OneSignal/onesignal-expo-plugin#readme",
33
34
  "devDependencies": {
35
+ "@typescript-eslint/eslint-plugin": "^5.10.1",
36
+ "@typescript-eslint/parser": "^5.10.1",
37
+ "eslint": "^8.8.0",
34
38
  "expo-module-scripts": "^2.0.0",
35
- "readme-md-generator": "^1.0.0"
39
+ "readme-md-generator": "^1.0.0",
40
+ "typescript": "^4.5.5"
36
41
  },
37
42
  "dependencies": {
38
- "react-native-onesignal": "^4.3.1",
39
43
  "xcode": "^3.0.1"
40
44
  }
41
45
  }
@@ -0,0 +1,38 @@
1
+ import * as fs from 'fs';
2
+ import { OneSignalLog } from './OneSignalLog';
3
+
4
+ /**
5
+ * FileManager contains static *awaitable* file-system functions
6
+ */
7
+ export class FileManager {
8
+ static async readFile(path: string): Promise<string> {
9
+ return new Promise<string>((resolve, reject) => {
10
+ fs.readFile(path, 'utf8', (err, data) => {
11
+ if (err || !data) {
12
+ OneSignalLog.error("Couldn't read file:" + path);
13
+ reject(err);
14
+ return;
15
+ }
16
+ resolve(data);
17
+ });
18
+ });
19
+ }
20
+
21
+ static async writeFile(path: string, contents: string): Promise<void> {
22
+ return new Promise<void>((resolve, reject) => {
23
+ fs.writeFile(path, contents, 'utf8', (err) => {
24
+ if (err) {
25
+ OneSignalLog.error("Couldn't write file:" + path);
26
+ reject(err);
27
+ return;
28
+ }
29
+ resolve();
30
+ });
31
+ });
32
+ }
33
+
34
+ static async copyFile(path1: string, path2: string): Promise<void> {
35
+ const fileContents = await FileManager.readFile(path1);
36
+ await FileManager.writeFile(path2, fileContents);
37
+ }
38
+ }
@@ -1,42 +1,39 @@
1
- import * as fs from 'fs';
2
- import { BUNDLE_SHORT_VERSION_TEMPLATE_REGEX, BUNDLE_VERSION_TEMPLATE_REGEX, GROUP_IDENTIFIER_TEMPLATE_REGEX } from './iosConstants';
3
- import { OneSignalLog } from './OneSignalLog';
4
- import { ReaderManager } from './ReaderManager';
1
+ import { FileManager } from './FileManager';
2
+ import {
3
+ BUNDLE_SHORT_VERSION_TEMPLATE_REGEX,
4
+ BUNDLE_VERSION_TEMPLATE_REGEX,
5
+ GROUP_IDENTIFIER_TEMPLATE_REGEX,
6
+ NSE_TARGET_NAME
7
+ } from './iosConstants';
8
+
9
+ // project `ios/OneSignalNotificationServiceExtension` directory
10
+ const entitlementsFileName =`OneSignalNotificationServiceExtension.entitlements`;
11
+ const plistFileName = `OneSignalNotificationServiceExtension-Info.plist`;
5
12
 
6
- const entitlementsFilePath =`${__dirname}/serviceExtensionFiles/OneSignalNotificationServiceExtension.entitlements`;
7
- const plistFilePath = `${__dirname}/serviceExtensionFiles/OneSignalNotificationServiceExtension-Info.plist`;
8
-
9
- const logIfError = (err: NodeJS.ErrnoException | null) => {
10
- if (err) {
11
- OneSignalLog.error("Error updating OneSignal NSE Entitlement File.");
13
+ export default class NseUpdaterManager {
14
+ private nsePath = '';
15
+ constructor(iosPath: string) {
16
+ this.nsePath = `${iosPath}/${NSE_TARGET_NAME}`;
12
17
  }
13
- }
14
18
 
15
- export default class NseUpdaterManager {
16
- static async updateNSEEntitlements(groupIdentifier: string) {
17
- let entitlementsFile = await ReaderManager.readFile(entitlementsFilePath);
19
+ async updateNSEEntitlements(groupIdentifier: string): Promise<void> {
20
+ const entitlementsFilePath = `${this.nsePath}/${entitlementsFileName}`;
21
+ let entitlementsFile = await FileManager.readFile(entitlementsFilePath);
18
22
  entitlementsFile = entitlementsFile.replace(GROUP_IDENTIFIER_TEMPLATE_REGEX, groupIdentifier);
19
-
20
- fs.writeFile(entitlementsFilePath, entitlementsFile, 'utf-8', (err) => {
21
- logIfError(err);
22
- })
23
+ await FileManager.writeFile(entitlementsFilePath, entitlementsFile);
23
24
  }
24
25
 
25
- static async updateNSEBundleVersion(version: string) {
26
- let plistFile = await ReaderManager.readFile(plistFilePath);
26
+ async updateNSEBundleVersion(version: string): Promise<void> {
27
+ const plistFilePath = `${this.nsePath}/${plistFileName}`;
28
+ let plistFile = await FileManager.readFile(plistFilePath);
27
29
  plistFile = plistFile.replace(BUNDLE_VERSION_TEMPLATE_REGEX, version);
28
-
29
- fs.writeFile(plistFilePath, plistFile, 'utf-8', err => {
30
- logIfError(err);
31
- });
30
+ await FileManager.writeFile(plistFilePath, plistFile);
32
31
  }
33
32
 
34
- static async updateNSEBundleShortVersion(version: string) {
35
- let plistFile = await ReaderManager.readFile(plistFilePath);
33
+ async updateNSEBundleShortVersion(version: string): Promise<void> {
34
+ const plistFilePath = `${this.nsePath}/${plistFileName}`;
35
+ let plistFile = await FileManager.readFile(plistFilePath);
36
36
  plistFile = plistFile.replace(BUNDLE_SHORT_VERSION_TEMPLATE_REGEX, version);
37
-
38
- fs.writeFile(plistFilePath, plistFile, 'utf-8', err => {
39
- logIfError(err);
40
- });
37
+ await FileManager.writeFile(plistFilePath, plistFile);
41
38
  }
42
39
  }
@@ -1,5 +1,5 @@
1
1
  export const IPHONEOS_DEPLOYMENT_TARGET = "11.0";
2
- export const TARGETED_DEVICE_FAMILY = `\"1,2\"`;
2
+ export const TARGETED_DEVICE_FAMILY = `"1,2"`;
3
3
 
4
4
  export const NSE_PODFILE_SNIPPET = `
5
5
  target 'OneSignalNotificationServiceExtension' do
@@ -14,3 +14,5 @@ export const BUNDLE_VERSION_TEMPLATE_REGEX = /{{BUNDLE_VERSION}}/gm;
14
14
 
15
15
  export const DEFAULT_BUNDLE_VERSION = '1';
16
16
  export const DEFAULT_BUNDLE_SHORT_VERSION = '1.0';
17
+
18
+ export const NSE_TARGET_NAME = "OneSignalNotificationServiceExtension";
@@ -1,10 +1,10 @@
1
1
  import fs from 'fs';
2
2
  import { NSE_PODFILE_REGEX, NSE_PODFILE_SNIPPET } from './iosConstants';
3
3
  import { OneSignalLog } from './OneSignalLog';
4
- import { ReaderManager } from './ReaderManager';
4
+ import { FileManager } from './FileManager';
5
5
 
6
6
  export async function updatePodfile(iosPath: string) {
7
- const podfile = await ReaderManager.readFile(`${iosPath}/Podfile`);
7
+ const podfile = await FileManager.readFile(`${iosPath}/Podfile`);
8
8
  const matches = podfile.match(NSE_PODFILE_REGEX);
9
9
 
10
10
  if (matches) {
@@ -1,19 +0,0 @@
1
- import * as fs from 'fs';
2
- import { OneSignalLog } from './OneSignalLog';
3
-
4
- export class ReaderManager {
5
- static async readFile(path: string, replace?: string): Promise<string> {
6
- return new Promise<string>((resolve, reject) => {
7
- fs.readFile(path, 'utf8', (err, data) => {
8
- if (err) {
9
- reject(err);
10
- }
11
- if (!data) {
12
- OneSignalLog.error("Couldn't read file:" + path);
13
- return;
14
- }
15
- resolve(data);
16
- });
17
- });
18
- }
19
- }