prepare-package 1.1.2 → 1.1.4

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 -20
  2. package/package.json +1 -1
  3. package/src/index.js +36 -20
package/dist/index.js CHANGED
@@ -35,26 +35,28 @@ 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}`));
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
47
 
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
- )
48
+ // Remove the output folder if it exists
49
+ if (jetpack.exists(outputPath)) {
50
+ jetpack.remove(outputPath);
51
+ }
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,
@@ -74,10 +76,10 @@ module.exports = async function (options) {
74
76
  // Send analytics
75
77
  await sendAnalytics(thisPackageJSON, theirPackageJSON)
76
78
  .then((r) => {
77
- console.log(chalk.green(`[prepare-package]: Sent analytics code=${r.status}...`));
79
+ console.log(chalk.green(`[prepare-package]: Sent analytics code=${r.status}`));
78
80
  })
79
81
  .catch(e => {
80
- console.log(chalk.red(`[prepare-package]: Failed to send analytics...`, e));
82
+ console.log(chalk.red(`[prepare-package]: Failed to send analytics`, e));
81
83
  });
82
84
  }
83
85
 
@@ -92,10 +94,10 @@ module.exports = async function (options) {
92
94
  tries: 3,
93
95
  })
94
96
  .then(result => {
95
- console.log(chalk.green(`[prepare-package]: Purged... ${theirPackageJSON.name}`));
97
+ console.log(chalk.green(`[prepare-package]: Purged ${theirPackageJSON.name}`));
96
98
  })
97
99
  .catch(e => {
98
- console.log(chalk.red(`[prepare-package]: Failed to purge... ${theirPackageJSON.name}`, e));
100
+ console.log(chalk.red(`[prepare-package]: Failed to purge ${theirPackageJSON.name}`, e));
99
101
  })
100
102
  }
101
103
 
@@ -157,13 +159,27 @@ function sendAnalytics(thisPackageJSON, theirPackageJSON) {
157
159
  }
158
160
 
159
161
  // Get the user's location
162
+ const savePath = path.resolve(os.tmpdir(), 'prepare-package-geolocation-cache.json');
160
163
  const geolocation = await fetch('https://ipapi.co/json/', {
161
164
  response: 'json',
162
165
  tries: 2,
163
166
  timeout: 30000,
164
167
  })
165
- .catch(e => {
166
- console.log(chalk.red(`[prepare-package]: Failed to get geolocation...`, e));
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
+ }
167
183
  });
168
184
 
169
185
  // Add the geolocation to the body
@@ -176,8 +192,8 @@ function sendAnalytics(thisPackageJSON, theirPackageJSON) {
176
192
  }
177
193
 
178
194
  // Log the options
179
- console.log(chalk.blue(`[prepare-package]: Sending analytics...`, mac, uuid), body, body.events[0].params);
180
- // console.log(chalk.blue(`[prepare-package]: Sending analytics...`, mac, uuid));
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}...`));
181
197
 
182
198
  // Send event
183
199
  fetch(url, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prepare-package",
3
- "version": "1.1.2",
3
+ "version": "1.1.4",
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,26 +35,28 @@ 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}`));
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
47
 
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
- )
48
+ // Remove the output folder if it exists
49
+ if (jetpack.exists(outputPath)) {
50
+ jetpack.remove(outputPath);
51
+ }
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,
@@ -74,10 +76,10 @@ module.exports = async function (options) {
74
76
  // Send analytics
75
77
  await sendAnalytics(thisPackageJSON, theirPackageJSON)
76
78
  .then((r) => {
77
- console.log(chalk.green(`[prepare-package]: Sent analytics code=${r.status}...`));
79
+ console.log(chalk.green(`[prepare-package]: Sent analytics code=${r.status}`));
78
80
  })
79
81
  .catch(e => {
80
- console.log(chalk.red(`[prepare-package]: Failed to send analytics...`, e));
82
+ console.log(chalk.red(`[prepare-package]: Failed to send analytics`, e));
81
83
  });
82
84
  }
83
85
 
@@ -92,10 +94,10 @@ module.exports = async function (options) {
92
94
  tries: 3,
93
95
  })
94
96
  .then(result => {
95
- console.log(chalk.green(`[prepare-package]: Purged... ${theirPackageJSON.name}`));
97
+ console.log(chalk.green(`[prepare-package]: Purged ${theirPackageJSON.name}`));
96
98
  })
97
99
  .catch(e => {
98
- console.log(chalk.red(`[prepare-package]: Failed to purge... ${theirPackageJSON.name}`, e));
100
+ console.log(chalk.red(`[prepare-package]: Failed to purge ${theirPackageJSON.name}`, e));
99
101
  })
100
102
  }
101
103
 
@@ -157,13 +159,27 @@ function sendAnalytics(thisPackageJSON, theirPackageJSON) {
157
159
  }
158
160
 
159
161
  // Get the user's location
162
+ const savePath = path.resolve(os.tmpdir(), 'prepare-package-geolocation-cache.json');
160
163
  const geolocation = await fetch('https://ipapi.co/json/', {
161
164
  response: 'json',
162
165
  tries: 2,
163
166
  timeout: 30000,
164
167
  })
165
- .catch(e => {
166
- console.log(chalk.red(`[prepare-package]: Failed to get geolocation...`, e));
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
+ }
167
183
  });
168
184
 
169
185
  // Add the geolocation to the body
@@ -176,8 +192,8 @@ function sendAnalytics(thisPackageJSON, theirPackageJSON) {
176
192
  }
177
193
 
178
194
  // Log the options
179
- console.log(chalk.blue(`[prepare-package]: Sending analytics...`, mac, uuid), body, body.events[0].params);
180
- // console.log(chalk.blue(`[prepare-package]: Sending analytics...`, mac, uuid));
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}...`));
181
197
 
182
198
  // Send event
183
199
  fetch(url, {