prepare-package 1.1.13 → 1.2.0

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/dist/index.js CHANGED
@@ -2,6 +2,7 @@ const jetpack = require('fs-jetpack');
2
2
  const fetch = require('wonderful-fetch');
3
3
  const path = require('path');
4
4
  const chalk = require('chalk');
5
+ const logger = require('./logger');
5
6
 
6
7
  // const argv = require('yargs').argv;
7
8
 
@@ -39,13 +40,20 @@ module.exports = async function (options) {
39
40
  // theirPackageJSON.scripts['prepare:watch'] = theirPackageJSON.scripts['prepare:watch']
40
41
  // || `nodemon -w ./src -e '*' --exec 'npm run prepare'`
41
42
  theirPackageJSON.scripts.prepare = `node -e \"require('prepare-package')()\"`;
42
- theirPackageJSON.scripts['prepare:watch'] = `nodemon -w ./src -e '*' --exec 'npm run prepare'`
43
+ theirPackageJSON.scripts['prepare:watch'] = `node -e \"require('prepare-package/watch')()\"`
43
44
 
44
45
  // Log the options
45
- console.log(chalk.blue(`[prepare-package]: Options purge=${options.purge}`));
46
- console.log(chalk.blue(`[prepare-package]: input=${theirPackageJSON.preparePackage.input}`));
47
- console.log(chalk.blue(`[prepare-package]: output=${theirPackageJSON.preparePackage.output}`));
48
- console.log(chalk.blue(`[prepare-package]: main=${theirPackageJSON.main}`));
46
+ // console.log(chalk.blue(`[prepare-package]: Options purge=${options.purge}`));
47
+ // console.log(chalk.blue(`[prepare-package]: input=${theirPackageJSON.preparePackage.input}`));
48
+ // console.log(chalk.blue(`[prepare-package]: output=${theirPackageJSON.preparePackage.output}`));
49
+ // console.log(chalk.blue(`[prepare-package]: main=${theirPackageJSON.main}`));
50
+ logger.log(`Starting...`);
51
+ logger.log({
52
+ purge: options.purge,
53
+ input: theirPackageJSON.preparePackage.input,
54
+ output: theirPackageJSON.preparePackage.output,
55
+ main: theirPackageJSON.main,
56
+ });
49
57
 
50
58
  // Set the paths relative to the cwd
51
59
  const mainPath = path.resolve(options.cwd, theirPackageJSON.main);
@@ -103,11 +111,13 @@ module.exports = async function (options) {
103
111
  response: 'json',
104
112
  tries: 3,
105
113
  })
106
- .then(result => {
107
- console.log(chalk.green(`[prepare-package]: Purged ${theirPackageJSON.name}`));
114
+ .then((r) => {
115
+ // console.log(chalk.green(`[prepare-package]: Purged ${theirPackageJSON.name}`));
116
+ logger.log(chalk.green(`Purged ${theirPackageJSON.name}!`));
108
117
  })
109
- .catch(e => {
110
- console.log(chalk.red(`[prepare-package]: Failed to purge ${theirPackageJSON.name}`, e));
118
+ .catch((e) => {
119
+ // console.log(chalk.red(`[prepare-package]: Failed to purge ${theirPackageJSON.name}`, e));
120
+ logger.error(`Failed to purge ${theirPackageJSON.name}!`, e.stack);
111
121
  })
112
122
  }
113
123
 
package/dist/logger.js ADDED
@@ -0,0 +1,46 @@
1
+ const chalk = require('chalk');
2
+
3
+ // Setup logger
4
+ const logger = {};
5
+
6
+ // Loop through log, error, warn, and info and make methods that log to console with the name and time [xx:xx:xx] name: message
7
+ ['log', 'error', 'warn', 'info'].forEach((method) => {
8
+ logger[method] = function () {
9
+ // Get time
10
+ const time = new Date().toLocaleTimeString('en-US', {
11
+ hour12: false,
12
+ hour: '2-digit',
13
+ minute: '2-digit',
14
+ second: '2-digit'
15
+ });
16
+
17
+ // Determine color based on method
18
+ let color;
19
+ switch (method) {
20
+ case 'warn':
21
+ color = chalk.yellow;
22
+ break;
23
+ case 'error':
24
+ color = chalk.red;
25
+ break;
26
+ default:
27
+ color = (text) => text; // No color
28
+ }
29
+
30
+ // Convert arguments to array and prepend time and name
31
+ const args = [`[${chalk.magenta(time)}] '${chalk.cyan('prepare-package')}':`, ...Array.from(arguments).map(arg => {
32
+ return typeof arg === 'string'
33
+ ? color(arg)
34
+ : (
35
+ arg instanceof Error
36
+ ? color(arg.stack)
37
+ : arg
38
+ );
39
+ })];
40
+
41
+ // Log
42
+ console[method].apply(console, args);
43
+ };
44
+ });
45
+
46
+ module.exports = logger;
package/dist/watch.js ADDED
@@ -0,0 +1,90 @@
1
+ const chokidar = require('chokidar');
2
+ const jetpack = require('fs-jetpack');
3
+ const path = require('path');
4
+ const prepare = require('./index');
5
+ const logger = require('./logger');
6
+
7
+ module.exports = async function watch() {
8
+ const cwd = process.cwd();
9
+
10
+ // Get package.json info
11
+ const packageJSONPath = path.resolve(cwd, 'package.json');
12
+ const packageJSONExists = jetpack.exists(packageJSONPath);
13
+ const packageJSON = packageJSONExists ? require(packageJSONPath) : {};
14
+
15
+ // Set up paths
16
+ packageJSON.preparePackage = packageJSON.preparePackage || {};
17
+ const inputPath = path.resolve(cwd, packageJSON.preparePackage.input || './src');
18
+ const outputPath = path.resolve(cwd, packageJSON.preparePackage.output || './dist');
19
+ const mainPath = path.resolve(cwd, packageJSON.main || './dist/index.js');
20
+ const isLivePreparation = packageJSON.name !== 'prepare-package';
21
+
22
+ // Run initial prepare (full copy)
23
+ logger.log('Running initial prepare...');
24
+ await prepare({ purge: false });
25
+ logger.log('Initial prepare complete!');
26
+
27
+ // Set up watcher
28
+ logger.log('Watching for changes...');
29
+
30
+ const watcher = chokidar.watch(inputPath, {
31
+ persistent: true,
32
+ ignoreInitial: true,
33
+ awaitWriteFinish: {
34
+ stabilityThreshold: 100,
35
+ pollInterval: 100
36
+ }
37
+ });
38
+
39
+ // Helper function to process a single file
40
+ const processSingleFile = (filePath, eventType) => {
41
+ const relativePath = path.relative(inputPath, filePath);
42
+ const destPath = path.join(outputPath, relativePath);
43
+
44
+ try {
45
+ if (eventType === 'unlink' || eventType === 'unlinkDir') {
46
+ // Remove the file/directory from output
47
+ if (jetpack.exists(destPath)) {
48
+ jetpack.remove(destPath);
49
+ logger.log(`Removed: ${relativePath}`);
50
+ }
51
+ } else if (eventType === 'addDir') {
52
+ // Create directory in output
53
+ jetpack.dir(destPath);
54
+ logger.log(`Created dir: ${relativePath}`);
55
+ } else if (eventType === 'add' || eventType === 'change') {
56
+ // Copy the file to output
57
+ jetpack.copy(filePath, destPath, { overwrite: true });
58
+
59
+ // Apply version replacement if it's the main file
60
+ if (isLivePreparation && destPath === mainPath) {
61
+ jetpack.write(
62
+ destPath,
63
+ jetpack.read(destPath).replace(/{version}/igm, packageJSON.version)
64
+ );
65
+ }
66
+
67
+ logger.log(`Updated: ${relativePath}`);
68
+ }
69
+ } catch (error) {
70
+ logger.error(`Error processing ${relativePath}: ${error.message}`);
71
+ }
72
+ };
73
+
74
+ // Set up event handlers
75
+ watcher
76
+ .on('add', path => processSingleFile(path, 'add'))
77
+ .on('change', path => processSingleFile(path, 'change'))
78
+ .on('addDir', path => processSingleFile(path, 'addDir'))
79
+ .on('unlink', path => processSingleFile(path, 'unlink'))
80
+ .on('unlinkDir', path => processSingleFile(path, 'unlinkDir'))
81
+ .on('error', error => logger.error(`Watcher error: ${error}`))
82
+ .on('ready', () => logger.log('Ready for changes!'));
83
+
84
+ // Handle process termination
85
+ process.on('SIGINT', () => {
86
+ logger.log('Stopping watcher...');
87
+ watcher.close();
88
+ process.exit(0);
89
+ });
90
+ }
package/package.json CHANGED
@@ -1,13 +1,17 @@
1
1
  {
2
2
  "name": "prepare-package",
3
- "version": "1.1.13",
3
+ "version": "1.2.0",
4
4
  "description": "Prepare a Node.js package before being published",
5
5
  "main": "dist/index.js",
6
+ "exports": {
7
+ ".": "./dist/index.js",
8
+ "./watch": "./dist/watch.js"
9
+ },
6
10
  "scripts": {
7
11
  "test": "./node_modules/mocha/bin/mocha test/ --recursive --timeout=10000",
8
12
  "start": "node -e \"require('./src/index.js')()\"",
9
13
  "prepare": "node -e \"require('./src/index.js')()\"",
10
- "prepare:watch": "nodemon -w ./src -e '*' --exec 'npm run prepare'",
14
+ "prepare:watch": "node -e \"require('./src/watch.js')()\"",
11
15
  "postinstall": "node -e \"require('./dist/index.js')({cwd: process.env.INIT_CWD, isPostInstall: true})\""
12
16
  },
13
17
  "engines": {
@@ -36,8 +40,9 @@
36
40
  },
37
41
  "dependencies": {
38
42
  "chalk": "^4.1.2",
43
+ "chokidar": "^3.5.3",
39
44
  "fs-jetpack": "^4.3.1",
40
- "wonderful-fetch": "^1.1.11"
45
+ "wonderful-fetch": "^1.3.2"
41
46
  },
42
47
  "devDependencies": {
43
48
  "mocha": "^8.4.0"