prepare-package 1.1.14 → 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,7 +40,7 @@ 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
46
  // console.log(chalk.blue(`[prepare-package]: Options purge=${options.purge}`));
@@ -120,46 +121,3 @@ module.exports = async function (options) {
120
121
  })
121
122
  }
122
123
 
123
- // Setup logger
124
- const logger = {};
125
-
126
- // Loop through log, error, warn, and info and make methods that log to console with the name and time [xx:xx:xx] name: message
127
- ['log', 'error', 'warn', 'info'].forEach((method) => {
128
- logger[method] = function () {
129
- // Get time
130
- const time = new Date().toLocaleTimeString('en-US', {
131
- hour12: false,
132
- hour: '2-digit',
133
- minute: '2-digit',
134
- second: '2-digit'
135
- });
136
-
137
- // Determine color based on method
138
- let color;
139
- switch (method) {
140
- case 'warn':
141
- color = chalk.yellow;
142
- break;
143
- case 'error':
144
- color = chalk.red;
145
- break;
146
- default:
147
- color = (text) => text; // No color
148
- }
149
-
150
- // Convert arguments to array and prepend time and name
151
- const args = [`[${chalk.magenta(time)}] '${chalk.cyan('prepare-package')}':`, ...Array.from(arguments).map(arg => {
152
- return typeof arg === 'string'
153
- ? color(arg)
154
- : (
155
- arg instanceof Error
156
- ? color(arg.stack)
157
- : arg
158
- );
159
- })];
160
-
161
- // Log
162
- console[method].apply(console, args);
163
- };
164
- });
165
-
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.14",
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,6 +40,7 @@
36
40
  },
37
41
  "dependencies": {
38
42
  "chalk": "^4.1.2",
43
+ "chokidar": "^3.5.3",
39
44
  "fs-jetpack": "^4.3.1",
40
45
  "wonderful-fetch": "^1.3.2"
41
46
  },