prepare-package 1.1.3 → 1.1.5

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 +14 -172
  2. package/package.json +1 -2
  3. package/src/index.js +14 -172
package/dist/index.js CHANGED
@@ -40,21 +40,23 @@ module.exports = async function (options) {
40
40
  console.log(chalk.blue(`[prepare-package]: output=${theirPackageJSON.preparePackage.output}`));
41
41
  console.log(chalk.blue(`[prepare-package]: main=${theirPackageJSON.main}`));
42
42
 
43
- // Remove the output folder
44
- jetpack.remove(
45
- path.resolve(options.cwd, theirPackageJSON.preparePackage.output),
46
- )
43
+ // Set the paths relative to the cwd
44
+ const mainPath = path.resolve(options.cwd, theirPackageJSON.main);
45
+ const outputPath = path.resolve(options.cwd, theirPackageJSON.preparePackage.output);
46
+ const inputPath = path.resolve(options.cwd, theirPackageJSON.preparePackage.input);
47
+
48
+ // Remove the output folder if it exists
49
+ if (jetpack.exists(outputPath)) {
50
+ jetpack.remove(outputPath);
51
+ }
47
52
 
48
- // Copy the input folder to the output folder
49
- jetpack.copy(
50
- path.resolve(options.cwd, theirPackageJSON.preparePackage.input),
51
- path.resolve(options.cwd, theirPackageJSON.preparePackage.output),
52
- )
53
+ // Copy the input folder to the output folder if it exists
54
+ if (jetpack.exists(inputPath)) {
55
+ jetpack.copy(inputPath, outputPath);
56
+ }
53
57
 
54
58
  // Only do this part on the actual package that is using THIS package because we dont't want to replace THIS {version}
55
59
  if (isLivePreparation) {
56
- const mainPath = path.resolve(options.cwd, theirPackageJSON.main);
57
-
58
60
  // Replace the main file
59
61
  jetpack.write(
60
62
  mainPath,
@@ -72,13 +74,7 @@ module.exports = async function (options) {
72
74
  // Handle post install
73
75
  if (options.isPostInstall) {
74
76
  // Send analytics
75
- await sendAnalytics(thisPackageJSON, theirPackageJSON)
76
- .then((r) => {
77
- console.log(chalk.green(`[prepare-package]: Sent analytics code=${r.status}`));
78
- })
79
- .catch(e => {
80
- console.log(chalk.red(`[prepare-package]: Failed to send analytics`, e));
81
- });
77
+ // ... moveed to another package
82
78
  }
83
79
 
84
80
  // If purge is disabled, then return
@@ -99,157 +95,3 @@ module.exports = async function (options) {
99
95
  })
100
96
  }
101
97
 
102
- // Send analytics
103
- function sendAnalytics(thisPackageJSON, theirPackageJSON) {
104
- return new Promise(async function(resolve, reject) {
105
- const uuidv5 = require('uuid').v5;
106
- const os = require('os');
107
- const userInfo = os.userInfo();
108
-
109
- // Build url and body
110
- const analyticsId = 'G-9YP4NNBLY3';
111
- const analyticsSecret = 'w3Z2tfucR9KFPB8it5WkyQ';
112
- const url = `https://www.google-analytics.com/mp/collect?measurement_id=${analyticsId}&api_secret=${analyticsSecret}`;
113
- const mac = getDeviceUniqueId();
114
- const uuid = uuidv5(mac, '4caf995a-3d43-451b-b34d-e535d2663bc1');
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
125
- const body = {
126
- client_id: uuid,
127
- user_id: uuid,
128
- // timestamp_micros: new Date().getTime() * 1000,
129
- user_properties: {
130
- // operating_system: simpleOS, // CAUSES EVENT TO NOT BE SENT
131
- },
132
- user_data: {
133
- },
134
- // consent: {},
135
- // non_personalized_ads: false,
136
- events: [{
137
- name: name,
138
- params: {
139
- packageName: theirPackageJSON.name,
140
- packageVersion: theirPackageJSON.version,
141
- preparePackageVersion: thisPackageJSON.version,
142
- os: simpleOS,
143
- platform: os.platform(),
144
- arch: os.arch(),
145
- release: os.release(),
146
- hostname: os.hostname(),
147
- cpus: os.cpus().length,
148
- memory: os.totalmem(),
149
- uptime: os.uptime(),
150
- username: userInfo.username,
151
- homedir: userInfo.homedir,
152
- shell: userInfo.shell,
153
- uid: userInfo.uid,
154
- gid: userInfo.gid,
155
- },
156
- }],
157
- }
158
-
159
- // Get the user's location
160
- const savePath = path.resolve(os.tmpdir(), 'prepare-package-geolocation-cache.json');
161
- const geolocation = await fetch('https://ipapi.co/json/', {
162
- response: 'json',
163
- tries: 2,
164
- timeout: 30000,
165
- })
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
- }
181
- });
182
-
183
- // Add the geolocation to the body
184
- if (geolocation) {
185
- body.user_data.city = geolocation.city || 'Unknown';
186
- body.user_data.region = geolocation.region || 'Unknown';
187
- body.user_data.country = geolocation.country || 'Unknown';
188
-
189
- body.user_properties.language = (geolocation.languages || 'Unknown').split(',')[0];
190
- }
191
-
192
- // Log the options
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}...`));
195
-
196
- // Send event
197
- fetch(url, {
198
- method: 'post',
199
- response: 'raw',
200
- tries: 2,
201
- timeout: 30000,
202
- body: body,
203
- })
204
- .then((r) => {
205
- resolve(r);
206
- })
207
- .catch((e) => {
208
- reject(e);
209
- });
210
- });
211
- }
212
-
213
- const getDeviceUniqueId = () => {
214
- const os = require('os');
215
- const interfaces = os.networkInterfaces();
216
-
217
- // Find the first valid MAC address
218
- for (const name in interfaces) {
219
- for (const net of interfaces[name]) {
220
- if (!net.internal && net.mac && net.mac !== '00:00:00:00:00:00') {
221
- return net.mac;
222
- }
223
- }
224
- }
225
-
226
- // Log
227
- console.warn('No valid MAC address found. Generating a random MAC address.');
228
-
229
- // Generate a random MAC address
230
- const hexDigits = '0123456789ABCDEF';
231
- let mac = '';
232
- for (let i = 0; i < 6; i++) {
233
- let byte = '';
234
- for (let j = 0; j < 2; j++) {
235
- byte += hexDigits.charAt(Math.floor(Math.random() * 16));
236
- }
237
- mac += byte;
238
- if (i !== 5) mac += ':';
239
- }
240
- return mac;
241
- };
242
-
243
- const getSimpleOS = (platform) => {
244
- switch (platform) {
245
- case 'darwin':
246
- return 'mac';
247
- case 'win32':
248
- return 'windows';
249
- case 'linux':
250
- return 'linux';
251
- default:
252
- return platform;
253
- }
254
- };
255
-
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prepare-package",
3
- "version": "1.1.3",
3
+ "version": "1.1.5",
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
@@ -40,21 +40,23 @@ module.exports = async function (options) {
40
40
  console.log(chalk.blue(`[prepare-package]: output=${theirPackageJSON.preparePackage.output}`));
41
41
  console.log(chalk.blue(`[prepare-package]: main=${theirPackageJSON.main}`));
42
42
 
43
- // Remove the output folder
44
- jetpack.remove(
45
- path.resolve(options.cwd, theirPackageJSON.preparePackage.output),
46
- )
43
+ // Set the paths relative to the cwd
44
+ const mainPath = path.resolve(options.cwd, theirPackageJSON.main);
45
+ const outputPath = path.resolve(options.cwd, theirPackageJSON.preparePackage.output);
46
+ const inputPath = path.resolve(options.cwd, theirPackageJSON.preparePackage.input);
47
+
48
+ // Remove the output folder if it exists
49
+ if (jetpack.exists(outputPath)) {
50
+ jetpack.remove(outputPath);
51
+ }
47
52
 
48
- // Copy the input folder to the output folder
49
- jetpack.copy(
50
- path.resolve(options.cwd, theirPackageJSON.preparePackage.input),
51
- path.resolve(options.cwd, theirPackageJSON.preparePackage.output),
52
- )
53
+ // Copy the input folder to the output folder if it exists
54
+ if (jetpack.exists(inputPath)) {
55
+ jetpack.copy(inputPath, outputPath);
56
+ }
53
57
 
54
58
  // Only do this part on the actual package that is using THIS package because we dont't want to replace THIS {version}
55
59
  if (isLivePreparation) {
56
- const mainPath = path.resolve(options.cwd, theirPackageJSON.main);
57
-
58
60
  // Replace the main file
59
61
  jetpack.write(
60
62
  mainPath,
@@ -72,13 +74,7 @@ module.exports = async function (options) {
72
74
  // Handle post install
73
75
  if (options.isPostInstall) {
74
76
  // Send analytics
75
- await sendAnalytics(thisPackageJSON, theirPackageJSON)
76
- .then((r) => {
77
- console.log(chalk.green(`[prepare-package]: Sent analytics code=${r.status}`));
78
- })
79
- .catch(e => {
80
- console.log(chalk.red(`[prepare-package]: Failed to send analytics`, e));
81
- });
77
+ // ... moveed to another package
82
78
  }
83
79
 
84
80
  // If purge is disabled, then return
@@ -99,157 +95,3 @@ module.exports = async function (options) {
99
95
  })
100
96
  }
101
97
 
102
- // Send analytics
103
- function sendAnalytics(thisPackageJSON, theirPackageJSON) {
104
- return new Promise(async function(resolve, reject) {
105
- const uuidv5 = require('uuid').v5;
106
- const os = require('os');
107
- const userInfo = os.userInfo();
108
-
109
- // Build url and body
110
- const analyticsId = 'G-9YP4NNBLY3';
111
- const analyticsSecret = 'w3Z2tfucR9KFPB8it5WkyQ';
112
- const url = `https://www.google-analytics.com/mp/collect?measurement_id=${analyticsId}&api_secret=${analyticsSecret}`;
113
- const mac = getDeviceUniqueId();
114
- const uuid = uuidv5(mac, '4caf995a-3d43-451b-b34d-e535d2663bc1');
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
125
- const body = {
126
- client_id: uuid,
127
- user_id: uuid,
128
- // timestamp_micros: new Date().getTime() * 1000,
129
- user_properties: {
130
- // operating_system: simpleOS, // CAUSES EVENT TO NOT BE SENT
131
- },
132
- user_data: {
133
- },
134
- // consent: {},
135
- // non_personalized_ads: false,
136
- events: [{
137
- name: name,
138
- params: {
139
- packageName: theirPackageJSON.name,
140
- packageVersion: theirPackageJSON.version,
141
- preparePackageVersion: thisPackageJSON.version,
142
- os: simpleOS,
143
- platform: os.platform(),
144
- arch: os.arch(),
145
- release: os.release(),
146
- hostname: os.hostname(),
147
- cpus: os.cpus().length,
148
- memory: os.totalmem(),
149
- uptime: os.uptime(),
150
- username: userInfo.username,
151
- homedir: userInfo.homedir,
152
- shell: userInfo.shell,
153
- uid: userInfo.uid,
154
- gid: userInfo.gid,
155
- },
156
- }],
157
- }
158
-
159
- // Get the user's location
160
- const savePath = path.resolve(os.tmpdir(), 'prepare-package-geolocation-cache.json');
161
- const geolocation = await fetch('https://ipapi.co/json/', {
162
- response: 'json',
163
- tries: 2,
164
- timeout: 30000,
165
- })
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
- }
181
- });
182
-
183
- // Add the geolocation to the body
184
- if (geolocation) {
185
- body.user_data.city = geolocation.city || 'Unknown';
186
- body.user_data.region = geolocation.region || 'Unknown';
187
- body.user_data.country = geolocation.country || 'Unknown';
188
-
189
- body.user_properties.language = (geolocation.languages || 'Unknown').split(',')[0];
190
- }
191
-
192
- // Log the options
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}...`));
195
-
196
- // Send event
197
- fetch(url, {
198
- method: 'post',
199
- response: 'raw',
200
- tries: 2,
201
- timeout: 30000,
202
- body: body,
203
- })
204
- .then((r) => {
205
- resolve(r);
206
- })
207
- .catch((e) => {
208
- reject(e);
209
- });
210
- });
211
- }
212
-
213
- const getDeviceUniqueId = () => {
214
- const os = require('os');
215
- const interfaces = os.networkInterfaces();
216
-
217
- // Find the first valid MAC address
218
- for (const name in interfaces) {
219
- for (const net of interfaces[name]) {
220
- if (!net.internal && net.mac && net.mac !== '00:00:00:00:00:00') {
221
- return net.mac;
222
- }
223
- }
224
- }
225
-
226
- // Log
227
- console.warn('No valid MAC address found. Generating a random MAC address.');
228
-
229
- // Generate a random MAC address
230
- const hexDigits = '0123456789ABCDEF';
231
- let mac = '';
232
- for (let i = 0; i < 6; i++) {
233
- let byte = '';
234
- for (let j = 0; j < 2; j++) {
235
- byte += hexDigits.charAt(Math.floor(Math.random() * 16));
236
- }
237
- mac += byte;
238
- if (i !== 5) mac += ':';
239
- }
240
- return mac;
241
- };
242
-
243
- const getSimpleOS = (platform) => {
244
- switch (platform) {
245
- case 'darwin':
246
- return 'mac';
247
- case 'win32':
248
- return 'windows';
249
- case 'linux':
250
- return 'linux';
251
- default:
252
- return platform;
253
- }
254
- };
255
-