prepare-package 1.1.14 → 1.2.1

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
 
@@ -11,6 +12,7 @@ module.exports = async function (options) {
11
12
  options.purge = typeof options.purge === 'undefined' ? true : options.purge;
12
13
  options.cwd = options.cwd || process.cwd();
13
14
  options.isPostInstall = typeof options.isPostInstall === 'undefined' ? false : options.isPostInstall;
15
+ options.singleFile = options.singleFile || null; // For watch mode - single file to process
14
16
 
15
17
  // Set the paths
16
18
  const theirPackageJSONPath = path.resolve(options.cwd, 'package.json');
@@ -39,7 +41,7 @@ module.exports = async function (options) {
39
41
  // theirPackageJSON.scripts['prepare:watch'] = theirPackageJSON.scripts['prepare:watch']
40
42
  // || `nodemon -w ./src -e '*' --exec 'npm run prepare'`
41
43
  theirPackageJSON.scripts.prepare = `node -e \"require('prepare-package')()\"`;
42
- theirPackageJSON.scripts['prepare:watch'] = `nodemon -w ./src -e '*' --exec 'npm run prepare'`
44
+ theirPackageJSON.scripts['prepare:watch'] = `node -e \"require('prepare-package/watch')()\"`
43
45
 
44
46
  // Log the options
45
47
  // console.log(chalk.blue(`[prepare-package]: Options purge=${options.purge}`));
@@ -64,33 +66,58 @@ module.exports = async function (options) {
64
66
  const outputPathExists = jetpack.exists(outputPath);
65
67
  const inputPathExists = jetpack.exists(inputPath);
66
68
 
67
- // Remove the output folder if it exists (input must exist too)
68
- if (outputPathExists && inputPathExists) {
69
- jetpack.remove(outputPath);
70
- }
69
+ // Handle single file mode (for watch)
70
+ if (options.singleFile) {
71
+ const relativePath = path.relative(inputPath, options.singleFile);
72
+ const destPath = path.join(outputPath, relativePath);
73
+
74
+ // Copy single file
75
+ if (jetpack.exists(options.singleFile)) {
76
+ jetpack.copy(options.singleFile, destPath, { overwrite: true });
77
+ logger.log(`Updated: ${relativePath}`);
78
+ }
79
+ } else {
80
+ // Normal mode - full copy
81
+ // Remove the output folder if it exists (input must exist too)
82
+ if (outputPathExists && inputPathExists) {
83
+ jetpack.remove(outputPath);
84
+ }
71
85
 
72
- // Copy the input folder to the output folder if it exists
73
- if (inputPathExists) {
74
- jetpack.copy(inputPath, outputPath);
86
+ // Copy the input folder to the output folder if it exists
87
+ if (inputPathExists) {
88
+ jetpack.copy(inputPath, outputPath);
89
+ }
75
90
  }
76
91
 
77
92
  // Only do this part on the actual package that is using THIS package because we dont't want to replace THIS {version}
78
93
  if (isLivePreparation) {
79
- // Replace the main file
80
- if (mainPathExists) {
81
- jetpack.write(
82
- mainPath,
83
- jetpack.read(mainPath)
84
- .replace(/{version}/igm, theirPackageJSON.version),
85
- );
86
- }
87
-
88
- // Replace the package.json
89
- if (theirPackageJSONExists) {
90
- jetpack.write(
91
- theirPackageJSONPath,
92
- `${JSON.stringify(theirPackageJSON, null, 2)}\n`
93
- );
94
+ // In single file mode, only process if it's the main file
95
+ if (options.singleFile) {
96
+ const destPath = path.join(outputPath, path.relative(inputPath, options.singleFile));
97
+ if (destPath === mainPath && jetpack.exists(destPath)) {
98
+ jetpack.write(
99
+ destPath,
100
+ jetpack.read(destPath).replace(/{version}/igm, theirPackageJSON.version)
101
+ );
102
+ }
103
+ } else {
104
+ // Normal mode - process main file and package.json
105
+ // Replace the main file
106
+ if (mainPathExists) {
107
+ jetpack.write(
108
+ mainPath,
109
+ jetpack.read(mainPath)
110
+ .replace(/{version}/igm, theirPackageJSON.version),
111
+ );
112
+ }
113
+
114
+ // Replace the package.json
115
+ if (theirPackageJSONExists) {
116
+ jetpack.write(
117
+ theirPackageJSONPath,
118
+ `${JSON.stringify(theirPackageJSON, null, 2)}\n`
119
+ );
120
+ }
94
121
  }
95
122
  }
96
123
 
@@ -120,46 +147,3 @@ module.exports = async function (options) {
120
147
  })
121
148
  }
122
149
 
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,81 @@
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
+
20
+ // Run initial prepare (full copy)
21
+ logger.log('Running initial prepare...');
22
+ await prepare({ purge: false });
23
+ logger.log('Initial prepare complete!');
24
+
25
+ // Set up watcher
26
+ logger.log('Watching for changes...');
27
+
28
+ const watcher = chokidar.watch(inputPath, {
29
+ persistent: true,
30
+ ignoreInitial: true,
31
+ awaitWriteFinish: {
32
+ stabilityThreshold: 100,
33
+ pollInterval: 100
34
+ }
35
+ });
36
+
37
+ // Helper function to process a single file
38
+ const processSingleFile = async (filePath, eventType) => {
39
+ const relativePath = path.relative(inputPath, filePath);
40
+ const destPath = path.join(outputPath, relativePath);
41
+
42
+ try {
43
+ if (eventType === 'unlink' || eventType === 'unlinkDir') {
44
+ // Remove the file/directory from output
45
+ if (jetpack.exists(destPath)) {
46
+ jetpack.remove(destPath);
47
+ logger.log(`Removed: ${relativePath}`);
48
+ }
49
+ } else if (eventType === 'addDir') {
50
+ // Create directory in output
51
+ jetpack.dir(destPath);
52
+ logger.log(`Created dir: ${relativePath}`);
53
+ } else if (eventType === 'add' || eventType === 'change') {
54
+ // Use the main prepare function with singleFile option
55
+ await prepare({
56
+ purge: false,
57
+ singleFile: filePath
58
+ });
59
+ }
60
+ } catch (error) {
61
+ logger.error(`Error processing ${relativePath}: ${error.message}`);
62
+ }
63
+ };
64
+
65
+ // Set up event handlers
66
+ watcher
67
+ .on('add', path => processSingleFile(path, 'add'))
68
+ .on('change', path => processSingleFile(path, 'change'))
69
+ .on('addDir', path => processSingleFile(path, 'addDir'))
70
+ .on('unlink', path => processSingleFile(path, 'unlink'))
71
+ .on('unlinkDir', path => processSingleFile(path, 'unlinkDir'))
72
+ .on('error', error => logger.error(`Watcher error: ${error}`))
73
+ .on('ready', () => logger.log('Ready for changes!'));
74
+
75
+ // Handle process termination
76
+ process.on('SIGINT', () => {
77
+ logger.log('Stopping watcher...');
78
+ watcher.close();
79
+ process.exit(0);
80
+ });
81
+ }
package/package.json CHANGED
@@ -1,13 +1,17 @@
1
1
  {
2
2
  "name": "prepare-package",
3
- "version": "1.1.14",
3
+ "version": "1.2.1",
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,10 +40,11 @@
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
  },
42
47
  "devDependencies": {
43
48
  "mocha": "^8.4.0"
44
49
  }
45
- }
50
+ }