prepare-package 1.1.1 → 1.1.3

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 +36 -13
  2. package/package.json +1 -1
  3. package/src/index.js +36 -13
package/dist/index.js CHANGED
@@ -35,7 +35,7 @@ module.exports = async function (options) {
35
35
  || `nodemon -w ./src -e '*' --exec 'npm run prepare'`
36
36
 
37
37
  // Log the options
38
- console.log(chalk.blue(`[prepare-package]: Options... purge=${options.purge}`));
38
+ console.log(chalk.blue(`[prepare-package]: Options purge=${options.purge}`));
39
39
  console.log(chalk.blue(`[prepare-package]: input=${theirPackageJSON.preparePackage.input}`));
40
40
  console.log(chalk.blue(`[prepare-package]: output=${theirPackageJSON.preparePackage.output}`));
41
41
  console.log(chalk.blue(`[prepare-package]: main=${theirPackageJSON.main}`));
@@ -73,11 +73,11 @@ module.exports = async function (options) {
73
73
  if (options.isPostInstall) {
74
74
  // Send analytics
75
75
  await sendAnalytics(thisPackageJSON, theirPackageJSON)
76
- .then(() => {
77
- console.log(chalk.green(`[prepare-package]: Sent analytics...`));
76
+ .then((r) => {
77
+ console.log(chalk.green(`[prepare-package]: Sent analytics code=${r.status}`));
78
78
  })
79
79
  .catch(e => {
80
- console.log(chalk.red(`[prepare-package]: Failed to send analytics...`, e));
80
+ console.log(chalk.red(`[prepare-package]: Failed to send analytics`, e));
81
81
  });
82
82
  }
83
83
 
@@ -92,10 +92,10 @@ module.exports = async function (options) {
92
92
  tries: 3,
93
93
  })
94
94
  .then(result => {
95
- console.log(chalk.green(`[prepare-package]: Purged... ${theirPackageJSON.name}`));
95
+ console.log(chalk.green(`[prepare-package]: Purged ${theirPackageJSON.name}`));
96
96
  })
97
97
  .catch(e => {
98
- console.log(chalk.red(`[prepare-package]: Failed to purge... ${theirPackageJSON.name}`, e));
98
+ console.log(chalk.red(`[prepare-package]: Failed to purge ${theirPackageJSON.name}`, e));
99
99
  })
100
100
  }
101
101
 
@@ -113,19 +113,28 @@ function sendAnalytics(thisPackageJSON, theirPackageJSON) {
113
113
  const mac = getDeviceUniqueId();
114
114
  const uuid = uuidv5(mac, '4caf995a-3d43-451b-b34d-e535d2663bc1');
115
115
  const simpleOS = getSimpleOS(os.platform());
116
+ const name = (theirPackageJSON.name || 'unknown')
117
+ // Replace anything not a letter, number, or underscore with an underscore
118
+ .replace(/[^a-zA-Z0-9_]/g, '_')
119
+ // Remove leading and trailing underscores
120
+ .replace(/^_+|_+$/g, '')
121
+ // Remove multiple underscores
122
+ .replace(/_+/g, '_');
123
+
124
+ // Build body
116
125
  const body = {
117
126
  client_id: uuid,
118
127
  user_id: uuid,
119
128
  // timestamp_micros: new Date().getTime() * 1000,
120
129
  user_properties: {
121
- operating_system: simpleOS,
130
+ // operating_system: simpleOS, // CAUSES EVENT TO NOT BE SENT
122
131
  },
123
132
  user_data: {
124
133
  },
125
134
  // consent: {},
126
135
  // non_personalized_ads: false,
127
136
  events: [{
128
- name: theirPackageJSON.name,
137
+ name: name,
129
138
  params: {
130
139
  packageName: theirPackageJSON.name,
131
140
  packageVersion: theirPackageJSON.version,
@@ -148,13 +157,27 @@ function sendAnalytics(thisPackageJSON, theirPackageJSON) {
148
157
  }
149
158
 
150
159
  // Get the user's location
160
+ const savePath = path.resolve(os.tmpdir(), 'prepare-package-geolocation-cache.json');
151
161
  const geolocation = await fetch('https://ipapi.co/json/', {
152
162
  response: 'json',
153
163
  tries: 2,
154
164
  timeout: 30000,
155
165
  })
156
- .catch(e => {
157
- console.log(chalk.red(`[prepare-package]: Failed to get geolocation...`, e));
166
+ .then((r) => {
167
+ // Save to tmpdir
168
+ jetpack.write(savePath, JSON.stringify(r, null, 2));
169
+
170
+ return r;
171
+ })
172
+ .catch((e) => {
173
+ console.log(chalk.red(`[prepare-package]: Failed to get geolocation`, e));
174
+
175
+ // Try to get from cache
176
+ if (jetpack.exists(savePath)) {
177
+ console.log(chalk.blue(`[prepare-package]: Used cached geolocation`));
178
+
179
+ return JSON.parse(jetpack.read(savePath));
180
+ }
158
181
  });
159
182
 
160
183
  // Add the geolocation to the body
@@ -167,13 +190,13 @@ function sendAnalytics(thisPackageJSON, theirPackageJSON) {
167
190
  }
168
191
 
169
192
  // Log the options
170
- // console.log(chalk.blue(`[prepare-package]: Sending analytics...`, mac, uuid), body, body.events[0].params);
171
- console.log(chalk.blue(`[prepare-package]: Sending analytics...`, mac, uuid));
193
+ // console.log(chalk.blue(`[prepare-package]: Sending analytics mac=${mac}, uuid=${uuid}...`), body, body.events[0].params);
194
+ console.log(chalk.blue(`[prepare-package]: Sending analytics mac=${mac}, uuid=${uuid}...`));
172
195
 
173
196
  // Send event
174
197
  fetch(url, {
175
198
  method: 'post',
176
- response: 'text',
199
+ response: 'raw',
177
200
  tries: 2,
178
201
  timeout: 30000,
179
202
  body: body,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prepare-package",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "Prepare a Node.js package before being published",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.js CHANGED
@@ -35,7 +35,7 @@ module.exports = async function (options) {
35
35
  || `nodemon -w ./src -e '*' --exec 'npm run prepare'`
36
36
 
37
37
  // Log the options
38
- console.log(chalk.blue(`[prepare-package]: Options... purge=${options.purge}`));
38
+ console.log(chalk.blue(`[prepare-package]: Options purge=${options.purge}`));
39
39
  console.log(chalk.blue(`[prepare-package]: input=${theirPackageJSON.preparePackage.input}`));
40
40
  console.log(chalk.blue(`[prepare-package]: output=${theirPackageJSON.preparePackage.output}`));
41
41
  console.log(chalk.blue(`[prepare-package]: main=${theirPackageJSON.main}`));
@@ -73,11 +73,11 @@ module.exports = async function (options) {
73
73
  if (options.isPostInstall) {
74
74
  // Send analytics
75
75
  await sendAnalytics(thisPackageJSON, theirPackageJSON)
76
- .then(() => {
77
- console.log(chalk.green(`[prepare-package]: Sent analytics...`));
76
+ .then((r) => {
77
+ console.log(chalk.green(`[prepare-package]: Sent analytics code=${r.status}`));
78
78
  })
79
79
  .catch(e => {
80
- console.log(chalk.red(`[prepare-package]: Failed to send analytics...`, e));
80
+ console.log(chalk.red(`[prepare-package]: Failed to send analytics`, e));
81
81
  });
82
82
  }
83
83
 
@@ -92,10 +92,10 @@ module.exports = async function (options) {
92
92
  tries: 3,
93
93
  })
94
94
  .then(result => {
95
- console.log(chalk.green(`[prepare-package]: Purged... ${theirPackageJSON.name}`));
95
+ console.log(chalk.green(`[prepare-package]: Purged ${theirPackageJSON.name}`));
96
96
  })
97
97
  .catch(e => {
98
- console.log(chalk.red(`[prepare-package]: Failed to purge... ${theirPackageJSON.name}`, e));
98
+ console.log(chalk.red(`[prepare-package]: Failed to purge ${theirPackageJSON.name}`, e));
99
99
  })
100
100
  }
101
101
 
@@ -113,19 +113,28 @@ function sendAnalytics(thisPackageJSON, theirPackageJSON) {
113
113
  const mac = getDeviceUniqueId();
114
114
  const uuid = uuidv5(mac, '4caf995a-3d43-451b-b34d-e535d2663bc1');
115
115
  const simpleOS = getSimpleOS(os.platform());
116
+ const name = (theirPackageJSON.name || 'unknown')
117
+ // Replace anything not a letter, number, or underscore with an underscore
118
+ .replace(/[^a-zA-Z0-9_]/g, '_')
119
+ // Remove leading and trailing underscores
120
+ .replace(/^_+|_+$/g, '')
121
+ // Remove multiple underscores
122
+ .replace(/_+/g, '_');
123
+
124
+ // Build body
116
125
  const body = {
117
126
  client_id: uuid,
118
127
  user_id: uuid,
119
128
  // timestamp_micros: new Date().getTime() * 1000,
120
129
  user_properties: {
121
- operating_system: simpleOS,
130
+ // operating_system: simpleOS, // CAUSES EVENT TO NOT BE SENT
122
131
  },
123
132
  user_data: {
124
133
  },
125
134
  // consent: {},
126
135
  // non_personalized_ads: false,
127
136
  events: [{
128
- name: theirPackageJSON.name,
137
+ name: name,
129
138
  params: {
130
139
  packageName: theirPackageJSON.name,
131
140
  packageVersion: theirPackageJSON.version,
@@ -148,13 +157,27 @@ function sendAnalytics(thisPackageJSON, theirPackageJSON) {
148
157
  }
149
158
 
150
159
  // Get the user's location
160
+ const savePath = path.resolve(os.tmpdir(), 'prepare-package-geolocation-cache.json');
151
161
  const geolocation = await fetch('https://ipapi.co/json/', {
152
162
  response: 'json',
153
163
  tries: 2,
154
164
  timeout: 30000,
155
165
  })
156
- .catch(e => {
157
- console.log(chalk.red(`[prepare-package]: Failed to get geolocation...`, e));
166
+ .then((r) => {
167
+ // Save to tmpdir
168
+ jetpack.write(savePath, JSON.stringify(r, null, 2));
169
+
170
+ return r;
171
+ })
172
+ .catch((e) => {
173
+ console.log(chalk.red(`[prepare-package]: Failed to get geolocation`, e));
174
+
175
+ // Try to get from cache
176
+ if (jetpack.exists(savePath)) {
177
+ console.log(chalk.blue(`[prepare-package]: Used cached geolocation`));
178
+
179
+ return JSON.parse(jetpack.read(savePath));
180
+ }
158
181
  });
159
182
 
160
183
  // Add the geolocation to the body
@@ -167,13 +190,13 @@ function sendAnalytics(thisPackageJSON, theirPackageJSON) {
167
190
  }
168
191
 
169
192
  // Log the options
170
- // console.log(chalk.blue(`[prepare-package]: Sending analytics...`, mac, uuid), body, body.events[0].params);
171
- console.log(chalk.blue(`[prepare-package]: Sending analytics...`, mac, uuid));
193
+ // console.log(chalk.blue(`[prepare-package]: Sending analytics mac=${mac}, uuid=${uuid}...`), body, body.events[0].params);
194
+ console.log(chalk.blue(`[prepare-package]: Sending analytics mac=${mac}, uuid=${uuid}...`));
172
195
 
173
196
  // Send event
174
197
  fetch(url, {
175
198
  method: 'post',
176
- response: 'text',
199
+ response: 'raw',
177
200
  tries: 2,
178
201
  timeout: 30000,
179
202
  body: body,