prepare-package 1.1.4 → 1.1.6

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.
Files changed (3) hide show
  1. package/dist/index.js +20 -171
  2. package/package.json +1 -2
  3. package/src/index.js +20 -171
package/dist/index.js CHANGED
@@ -12,8 +12,13 @@ module.exports = async function (options) {
12
12
  options.cwd = options.cwd || process.cwd();
13
13
  options.isPostInstall = typeof options.isPostInstall === 'undefined' ? false : options.isPostInstall;
14
14
 
15
+ // Set the paths
16
+ const theirPackageJSONPath = path.resolve(options.cwd, 'package.json');
17
+ const theirPackageJSONExists = jetpack.exists(theirPackageJSONPath);
18
+
19
+ // Get the package.json files
15
20
  const thisPackageJSON = require('../package.json');
16
- const theirPackageJSON = require(path.join(options.cwd, 'package.json'));
21
+ const theirPackageJSON = theirPackageJSONExists ? require(theirPackageJSONPath) : {};
17
22
  const isLivePreparation = theirPackageJSON.name !== 'prepare-package';
18
23
 
19
24
  // const options = {
@@ -58,29 +63,27 @@ module.exports = async function (options) {
58
63
  // Only do this part on the actual package that is using THIS package because we dont't want to replace THIS {version}
59
64
  if (isLivePreparation) {
60
65
  // Replace the main file
61
- jetpack.write(
62
- mainPath,
63
- jetpack.read(mainPath)
64
- .replace(/{version}/igm, theirPackageJSON.version),
65
- );
66
+ if (jetpack.exists(mainPath)) {
67
+ jetpack.write(
68
+ mainPath,
69
+ jetpack.read(mainPath)
70
+ .replace(/{version}/igm, theirPackageJSON.version),
71
+ );
72
+ }
66
73
 
67
74
  // Replace the package.json
68
- jetpack.write(
69
- path.resolve(options.cwd, 'package.json'),
70
- JSON.stringify(theirPackageJSON, null, 2)
71
- );
75
+ if (theirPackageJSONExists) {
76
+ jetpack.write(
77
+ theirPackageJSONPath,
78
+ JSON.stringify(theirPackageJSON, null, 2)
79
+ );
80
+ }
72
81
  }
73
82
 
74
83
  // Handle post install
75
84
  if (options.isPostInstall) {
76
85
  // Send analytics
77
- await sendAnalytics(thisPackageJSON, theirPackageJSON)
78
- .then((r) => {
79
- console.log(chalk.green(`[prepare-package]: Sent analytics code=${r.status}`));
80
- })
81
- .catch(e => {
82
- console.log(chalk.red(`[prepare-package]: Failed to send analytics`, e));
83
- });
86
+ // ... moveed to another package
84
87
  }
85
88
 
86
89
  // If purge is disabled, then return
@@ -101,157 +104,3 @@ module.exports = async function (options) {
101
104
  })
102
105
  }
103
106
 
104
- // Send analytics
105
- function sendAnalytics(thisPackageJSON, theirPackageJSON) {
106
- return new Promise(async function(resolve, reject) {
107
- const uuidv5 = require('uuid').v5;
108
- const os = require('os');
109
- const userInfo = os.userInfo();
110
-
111
- // Build url and body
112
- const analyticsId = 'G-9YP4NNBLY3';
113
- const analyticsSecret = 'w3Z2tfucR9KFPB8it5WkyQ';
114
- const url = `https://www.google-analytics.com/mp/collect?measurement_id=${analyticsId}&api_secret=${analyticsSecret}`;
115
- const mac = getDeviceUniqueId();
116
- const uuid = uuidv5(mac, '4caf995a-3d43-451b-b34d-e535d2663bc1');
117
- const simpleOS = getSimpleOS(os.platform());
118
- const name = (theirPackageJSON.name || 'unknown')
119
- // Replace anything not a letter, number, or underscore with an underscore
120
- .replace(/[^a-zA-Z0-9_]/g, '_')
121
- // Remove leading and trailing underscores
122
- .replace(/^_+|_+$/g, '')
123
- // Remove multiple underscores
124
- .replace(/_+/g, '_');
125
-
126
- // Build body
127
- const body = {
128
- client_id: uuid,
129
- user_id: uuid,
130
- // timestamp_micros: new Date().getTime() * 1000,
131
- user_properties: {
132
- // operating_system: simpleOS, // CAUSES EVENT TO NOT BE SENT
133
- },
134
- user_data: {
135
- },
136
- // consent: {},
137
- // non_personalized_ads: false,
138
- events: [{
139
- name: name,
140
- params: {
141
- packageName: theirPackageJSON.name,
142
- packageVersion: theirPackageJSON.version,
143
- preparePackageVersion: thisPackageJSON.version,
144
- os: simpleOS,
145
- platform: os.platform(),
146
- arch: os.arch(),
147
- release: os.release(),
148
- hostname: os.hostname(),
149
- cpus: os.cpus().length,
150
- memory: os.totalmem(),
151
- uptime: os.uptime(),
152
- username: userInfo.username,
153
- homedir: userInfo.homedir,
154
- shell: userInfo.shell,
155
- uid: userInfo.uid,
156
- gid: userInfo.gid,
157
- },
158
- }],
159
- }
160
-
161
- // Get the user's location
162
- const savePath = path.resolve(os.tmpdir(), 'prepare-package-geolocation-cache.json');
163
- const geolocation = await fetch('https://ipapi.co/json/', {
164
- response: 'json',
165
- tries: 2,
166
- timeout: 30000,
167
- })
168
- .then((r) => {
169
- // Save to tmpdir
170
- jetpack.write(savePath, JSON.stringify(r, null, 2));
171
-
172
- return r;
173
- })
174
- .catch((e) => {
175
- console.log(chalk.red(`[prepare-package]: Failed to get geolocation`, e));
176
-
177
- // Try to get from cache
178
- if (jetpack.exists(savePath)) {
179
- console.log(chalk.blue(`[prepare-package]: Used cached geolocation`));
180
-
181
- return JSON.parse(jetpack.read(savePath));
182
- }
183
- });
184
-
185
- // Add the geolocation to the body
186
- if (geolocation) {
187
- body.user_data.city = geolocation.city || 'Unknown';
188
- body.user_data.region = geolocation.region || 'Unknown';
189
- body.user_data.country = geolocation.country || 'Unknown';
190
-
191
- body.user_properties.language = (geolocation.languages || 'Unknown').split(',')[0];
192
- }
193
-
194
- // Log the options
195
- // console.log(chalk.blue(`[prepare-package]: Sending analytics mac=${mac}, uuid=${uuid}...`), body, body.events[0].params);
196
- console.log(chalk.blue(`[prepare-package]: Sending analytics mac=${mac}, uuid=${uuid}...`));
197
-
198
- // Send event
199
- fetch(url, {
200
- method: 'post',
201
- response: 'raw',
202
- tries: 2,
203
- timeout: 30000,
204
- body: body,
205
- })
206
- .then((r) => {
207
- resolve(r);
208
- })
209
- .catch((e) => {
210
- reject(e);
211
- });
212
- });
213
- }
214
-
215
- const getDeviceUniqueId = () => {
216
- const os = require('os');
217
- const interfaces = os.networkInterfaces();
218
-
219
- // Find the first valid MAC address
220
- for (const name in interfaces) {
221
- for (const net of interfaces[name]) {
222
- if (!net.internal && net.mac && net.mac !== '00:00:00:00:00:00') {
223
- return net.mac;
224
- }
225
- }
226
- }
227
-
228
- // Log
229
- console.warn('No valid MAC address found. Generating a random MAC address.');
230
-
231
- // Generate a random MAC address
232
- const hexDigits = '0123456789ABCDEF';
233
- let mac = '';
234
- for (let i = 0; i < 6; i++) {
235
- let byte = '';
236
- for (let j = 0; j < 2; j++) {
237
- byte += hexDigits.charAt(Math.floor(Math.random() * 16));
238
- }
239
- mac += byte;
240
- if (i !== 5) mac += ':';
241
- }
242
- return mac;
243
- };
244
-
245
- const getSimpleOS = (platform) => {
246
- switch (platform) {
247
- case 'darwin':
248
- return 'mac';
249
- case 'win32':
250
- return 'windows';
251
- case 'linux':
252
- return 'linux';
253
- default:
254
- return platform;
255
- }
256
- };
257
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prepare-package",
3
- "version": "1.1.4",
3
+ "version": "1.1.6",
4
4
  "description": "Prepare a Node.js package before being published",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -37,7 +37,6 @@
37
37
  "dependencies": {
38
38
  "chalk": "^4.1.2",
39
39
  "fs-jetpack": "^4.3.1",
40
- "uuid": "^9.0.1",
41
40
  "wonderful-fetch": "^1.1.9"
42
41
  },
43
42
  "devDependencies": {
package/src/index.js CHANGED
@@ -12,8 +12,13 @@ module.exports = async function (options) {
12
12
  options.cwd = options.cwd || process.cwd();
13
13
  options.isPostInstall = typeof options.isPostInstall === 'undefined' ? false : options.isPostInstall;
14
14
 
15
+ // Set the paths
16
+ const theirPackageJSONPath = path.resolve(options.cwd, 'package.json');
17
+ const theirPackageJSONExists = jetpack.exists(theirPackageJSONPath);
18
+
19
+ // Get the package.json files
15
20
  const thisPackageJSON = require('../package.json');
16
- const theirPackageJSON = require(path.join(options.cwd, 'package.json'));
21
+ const theirPackageJSON = theirPackageJSONExists ? require(theirPackageJSONPath) : {};
17
22
  const isLivePreparation = theirPackageJSON.name !== 'prepare-package';
18
23
 
19
24
  // const options = {
@@ -58,29 +63,27 @@ module.exports = async function (options) {
58
63
  // Only do this part on the actual package that is using THIS package because we dont't want to replace THIS {version}
59
64
  if (isLivePreparation) {
60
65
  // Replace the main file
61
- jetpack.write(
62
- mainPath,
63
- jetpack.read(mainPath)
64
- .replace(/{version}/igm, theirPackageJSON.version),
65
- );
66
+ if (jetpack.exists(mainPath)) {
67
+ jetpack.write(
68
+ mainPath,
69
+ jetpack.read(mainPath)
70
+ .replace(/{version}/igm, theirPackageJSON.version),
71
+ );
72
+ }
66
73
 
67
74
  // Replace the package.json
68
- jetpack.write(
69
- path.resolve(options.cwd, 'package.json'),
70
- JSON.stringify(theirPackageJSON, null, 2)
71
- );
75
+ if (theirPackageJSONExists) {
76
+ jetpack.write(
77
+ theirPackageJSONPath,
78
+ JSON.stringify(theirPackageJSON, null, 2)
79
+ );
80
+ }
72
81
  }
73
82
 
74
83
  // Handle post install
75
84
  if (options.isPostInstall) {
76
85
  // Send analytics
77
- await sendAnalytics(thisPackageJSON, theirPackageJSON)
78
- .then((r) => {
79
- console.log(chalk.green(`[prepare-package]: Sent analytics code=${r.status}`));
80
- })
81
- .catch(e => {
82
- console.log(chalk.red(`[prepare-package]: Failed to send analytics`, e));
83
- });
86
+ // ... moveed to another package
84
87
  }
85
88
 
86
89
  // If purge is disabled, then return
@@ -101,157 +104,3 @@ module.exports = async function (options) {
101
104
  })
102
105
  }
103
106
 
104
- // Send analytics
105
- function sendAnalytics(thisPackageJSON, theirPackageJSON) {
106
- return new Promise(async function(resolve, reject) {
107
- const uuidv5 = require('uuid').v5;
108
- const os = require('os');
109
- const userInfo = os.userInfo();
110
-
111
- // Build url and body
112
- const analyticsId = 'G-9YP4NNBLY3';
113
- const analyticsSecret = 'w3Z2tfucR9KFPB8it5WkyQ';
114
- const url = `https://www.google-analytics.com/mp/collect?measurement_id=${analyticsId}&api_secret=${analyticsSecret}`;
115
- const mac = getDeviceUniqueId();
116
- const uuid = uuidv5(mac, '4caf995a-3d43-451b-b34d-e535d2663bc1');
117
- const simpleOS = getSimpleOS(os.platform());
118
- const name = (theirPackageJSON.name || 'unknown')
119
- // Replace anything not a letter, number, or underscore with an underscore
120
- .replace(/[^a-zA-Z0-9_]/g, '_')
121
- // Remove leading and trailing underscores
122
- .replace(/^_+|_+$/g, '')
123
- // Remove multiple underscores
124
- .replace(/_+/g, '_');
125
-
126
- // Build body
127
- const body = {
128
- client_id: uuid,
129
- user_id: uuid,
130
- // timestamp_micros: new Date().getTime() * 1000,
131
- user_properties: {
132
- // operating_system: simpleOS, // CAUSES EVENT TO NOT BE SENT
133
- },
134
- user_data: {
135
- },
136
- // consent: {},
137
- // non_personalized_ads: false,
138
- events: [{
139
- name: name,
140
- params: {
141
- packageName: theirPackageJSON.name,
142
- packageVersion: theirPackageJSON.version,
143
- preparePackageVersion: thisPackageJSON.version,
144
- os: simpleOS,
145
- platform: os.platform(),
146
- arch: os.arch(),
147
- release: os.release(),
148
- hostname: os.hostname(),
149
- cpus: os.cpus().length,
150
- memory: os.totalmem(),
151
- uptime: os.uptime(),
152
- username: userInfo.username,
153
- homedir: userInfo.homedir,
154
- shell: userInfo.shell,
155
- uid: userInfo.uid,
156
- gid: userInfo.gid,
157
- },
158
- }],
159
- }
160
-
161
- // Get the user's location
162
- const savePath = path.resolve(os.tmpdir(), 'prepare-package-geolocation-cache.json');
163
- const geolocation = await fetch('https://ipapi.co/json/', {
164
- response: 'json',
165
- tries: 2,
166
- timeout: 30000,
167
- })
168
- .then((r) => {
169
- // Save to tmpdir
170
- jetpack.write(savePath, JSON.stringify(r, null, 2));
171
-
172
- return r;
173
- })
174
- .catch((e) => {
175
- console.log(chalk.red(`[prepare-package]: Failed to get geolocation`, e));
176
-
177
- // Try to get from cache
178
- if (jetpack.exists(savePath)) {
179
- console.log(chalk.blue(`[prepare-package]: Used cached geolocation`));
180
-
181
- return JSON.parse(jetpack.read(savePath));
182
- }
183
- });
184
-
185
- // Add the geolocation to the body
186
- if (geolocation) {
187
- body.user_data.city = geolocation.city || 'Unknown';
188
- body.user_data.region = geolocation.region || 'Unknown';
189
- body.user_data.country = geolocation.country || 'Unknown';
190
-
191
- body.user_properties.language = (geolocation.languages || 'Unknown').split(',')[0];
192
- }
193
-
194
- // Log the options
195
- // console.log(chalk.blue(`[prepare-package]: Sending analytics mac=${mac}, uuid=${uuid}...`), body, body.events[0].params);
196
- console.log(chalk.blue(`[prepare-package]: Sending analytics mac=${mac}, uuid=${uuid}...`));
197
-
198
- // Send event
199
- fetch(url, {
200
- method: 'post',
201
- response: 'raw',
202
- tries: 2,
203
- timeout: 30000,
204
- body: body,
205
- })
206
- .then((r) => {
207
- resolve(r);
208
- })
209
- .catch((e) => {
210
- reject(e);
211
- });
212
- });
213
- }
214
-
215
- const getDeviceUniqueId = () => {
216
- const os = require('os');
217
- const interfaces = os.networkInterfaces();
218
-
219
- // Find the first valid MAC address
220
- for (const name in interfaces) {
221
- for (const net of interfaces[name]) {
222
- if (!net.internal && net.mac && net.mac !== '00:00:00:00:00:00') {
223
- return net.mac;
224
- }
225
- }
226
- }
227
-
228
- // Log
229
- console.warn('No valid MAC address found. Generating a random MAC address.');
230
-
231
- // Generate a random MAC address
232
- const hexDigits = '0123456789ABCDEF';
233
- let mac = '';
234
- for (let i = 0; i < 6; i++) {
235
- let byte = '';
236
- for (let j = 0; j < 2; j++) {
237
- byte += hexDigits.charAt(Math.floor(Math.random() * 16));
238
- }
239
- mac += byte;
240
- if (i !== 5) mac += ':';
241
- }
242
- return mac;
243
- };
244
-
245
- const getSimpleOS = (platform) => {
246
- switch (platform) {
247
- case 'darwin':
248
- return 'mac';
249
- case 'win32':
250
- return 'windows';
251
- case 'linux':
252
- return 'linux';
253
- default:
254
- return platform;
255
- }
256
- };
257
-