@procoderx/copy-file 1.0.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 ProCoderX
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,510 @@
1
+ # ProCoderX Copy CLI
2
+
3
+ A lightweight Node.js CLI utility for copying files from one location to another.
4
+
5
+ `pcx-copy` provides a simple command-line interface for copying files while demonstrating the fundamentals of building and publishing an npm-based Node.js CLI package.
6
+
7
+ ---
8
+
9
+ ## Features
10
+
11
+ - Copy files from the command line
12
+ - Supports relative and absolute file paths
13
+ - Uses Node.js `fs/promises`
14
+ - Supports binary files through `Buffer`
15
+ - Provides CLI argument validation
16
+ - Displays clear usage and error messages
17
+ - Returns appropriate process exit codes
18
+ - Exposes a custom npm CLI command
19
+ - Supports global npm installation
20
+ - Can be executed through `npx`
21
+ - Separates CLI handling from file-copy logic
22
+
23
+ ---
24
+
25
+ ## Tech Stack
26
+
27
+ - **Node.js**
28
+ - **JavaScript**
29
+ - **ES Modules**
30
+ - **npm**
31
+ - **Node.js `fs/promises`**
32
+ - **Node.js `path`**
33
+
34
+ ---
35
+
36
+ ## Requirements
37
+
38
+ - Node.js `18+`
39
+ - npm `9+`
40
+
41
+ Check your installed versions:
42
+
43
+ ```bash
44
+ node -v
45
+ npm -v
46
+ ```
47
+
48
+ ---
49
+
50
+ ## Installation
51
+
52
+ ### Global Installation
53
+
54
+ Install the CLI globally with npm:
55
+
56
+ ```bash
57
+ npm install -g pcx-copy
58
+ ```
59
+
60
+ After installation, the `pcx-copy` command is available globally.
61
+
62
+ Verify the installation:
63
+
64
+ ```bash
65
+ pcx-copy
66
+ ```
67
+
68
+ If no arguments are provided, the CLI displays its usage information:
69
+
70
+ ```text
71
+ Usage: pcx-copy <source-file> <destination-file>
72
+ ```
73
+
74
+ ---
75
+
76
+ ## Using with npx
77
+
78
+ The package can also be executed with `npx` without manually installing it globally:
79
+
80
+ ```bash
81
+ npx pcx-copy <source-file> <destination-file>
82
+ ```
83
+
84
+ Example:
85
+
86
+ ```bash
87
+ npx pcx-copy ./money.png ./buffer.png
88
+ ```
89
+
90
+ This is useful when you want to run the CLI without adding it as a global command.
91
+
92
+ ---
93
+
94
+ ## Local Development
95
+
96
+ Clone the repository:
97
+
98
+ ```bash
99
+ git clone https://github.com/theprocoderx/pcx-copy.git
100
+ ```
101
+
102
+ Navigate into the project:
103
+
104
+ ```bash
105
+ cd pcx-copy
106
+ ```
107
+
108
+ Install the project dependencies:
109
+
110
+ ```bash
111
+ npm install
112
+ ```
113
+
114
+ Link the package locally:
115
+
116
+ ```bash
117
+ npm link
118
+ ```
119
+
120
+ After linking, the `pcx-copy` command becomes available in your terminal.
121
+
122
+ ---
123
+
124
+ ## Usage
125
+
126
+ ### Basic Syntax
127
+
128
+ ```bash
129
+ pcx-copy <source-file> <destination-file>
130
+ ```
131
+
132
+ ### Example
133
+
134
+ ```bash
135
+ pcx-copy ./money.png ./buffer.png
136
+ ```
137
+
138
+ The command copies:
139
+
140
+ ```text
141
+ ./money.png
142
+ ```
143
+
144
+ to:
145
+
146
+ ```text
147
+ ./buffer.png
148
+ ```
149
+
150
+ Successful output:
151
+
152
+ ```text
153
+ Successfully copied "./money.png" to "./buffer.png"
154
+ ```
155
+
156
+ ---
157
+
158
+ ## Copying to Another Location
159
+
160
+ The CLI supports relative and absolute destination paths.
161
+
162
+ ### Relative Path
163
+
164
+ ```bash
165
+ pcx-copy ./money.png ./backup/money.png
166
+ ```
167
+
168
+ ### Absolute Path
169
+
170
+ ```bash
171
+ pcx-copy ./money.png /c/Users/user/Desktop/buffer.png
172
+ ```
173
+
174
+ The source and destination paths are resolved before the file operation is performed.
175
+
176
+ ---
177
+
178
+ ## Supported Files
179
+
180
+ The utility works with regular files, including binary files such as:
181
+
182
+ - Images
183
+ - PDFs
184
+ - ZIP files
185
+ - Videos
186
+ - Documents
187
+ - Other binary files
188
+
189
+ The file contents are handled as a Node.js `Buffer`.
190
+
191
+ ---
192
+
193
+ ## How It Works
194
+
195
+ The CLI receives arguments from the terminal through `process.argv`.
196
+
197
+ For example:
198
+
199
+ ```bash
200
+ pcx-copy ./money.png ./buffer.png
201
+ ```
202
+
203
+ The arguments are read using:
204
+
205
+ ```js
206
+ const [, , source, destination] = process.argv;
207
+ ```
208
+
209
+ The values become:
210
+
211
+ ```text
212
+ source → ./money.png
213
+ destination → ./buffer.png
214
+ ```
215
+
216
+ The CLI then passes these values to the file-copy logic.
217
+
218
+ ```text
219
+ Terminal
220
+
221
+ │ pcx-copy <source> <destination>
222
+
223
+ process.argv
224
+
225
+ CLI Argument Validation
226
+
227
+ copyFile()
228
+
229
+ readFile()
230
+
231
+ Buffer
232
+
233
+ writeFile()
234
+
235
+ Destination File
236
+ ```
237
+
238
+ ---
239
+
240
+ ## File Copy Implementation
241
+
242
+ The core file operation uses Node.js `fs/promises`:
243
+
244
+ ```js
245
+ const content = await readFile(sourcePath);
246
+
247
+ await writeFile(destinationPath, content);
248
+ ```
249
+
250
+ The process is asynchronous and uses promises, allowing the CLI to work with Node.js's asynchronous filesystem APIs.
251
+
252
+ ---
253
+
254
+ ## CLI Entry Point
255
+
256
+ The executable entry point is:
257
+
258
+ ```text
259
+ bin/pcx-copy.js
260
+ ```
261
+
262
+ It starts with the Node.js shebang:
263
+
264
+ ```js
265
+ #!/usr/bin/env node
266
+ ```
267
+
268
+ The shebang allows the file to be executed directly as a command when npm links the package's `bin` entry.
269
+
270
+ The CLI entry point is responsible for:
271
+
272
+ - Reading command-line arguments
273
+ - Validating arguments
274
+ - Calling the copy operation
275
+ - Displaying CLI messages
276
+ - Handling process exit codes
277
+
278
+ ---
279
+
280
+ ## npm `bin` Configuration
281
+
282
+ The executable command is registered in `package.json`:
283
+
284
+ ```json
285
+ {
286
+ "bin": {
287
+ "pcx-copy": "./bin/pcx-copy.js"
288
+ }
289
+ }
290
+ ```
291
+
292
+ This creates the relationship:
293
+
294
+ ```text
295
+ pcx-copy
296
+
297
+ bin/pcx-copy.js
298
+
299
+ Node.js
300
+ ```
301
+
302
+ Therefore, users can run:
303
+
304
+ ```bash
305
+ pcx-copy ./source.txt ./destination.txt
306
+ ```
307
+
308
+ instead of:
309
+
310
+ ```bash
311
+ node bin/pcx-copy.js ./source.txt ./destination.txt
312
+ ```
313
+
314
+ ---
315
+
316
+ ## Project Structure
317
+
318
+ ```text
319
+ pcx-copy/
320
+ ├── bin/
321
+ │ └── pcx-copy.js ← CLI entry point (JavaScript)
322
+ ├── src/
323
+ │ └── copyFile.js ← file-copy logic (JavaScript)
324
+ ├── package.json ← npm configuration
325
+ ├── README.md ← documentation
326
+ ├── LICENSE ← MIT license
327
+ ├── .gitignore
328
+ └── .npmignore
329
+ ```
330
+
331
+ ---
332
+
333
+ ## Architecture
334
+
335
+ ```text
336
+ Terminal
337
+
338
+ │ CLI command
339
+
340
+ bin/pcx-copy.js
341
+
342
+ ├── process.argv
343
+ ├── argument validation
344
+ └── CLI output
345
+
346
+
347
+ src/copyFile.js
348
+
349
+ ├── path resolution
350
+ ├── readFile()
351
+ └── writeFile()
352
+
353
+
354
+ Destination File
355
+ ```
356
+
357
+ The CLI layer and application layer have separate responsibilities.
358
+
359
+ ### `bin/pcx-copy.js`
360
+
361
+ Responsible for:
362
+
363
+ - CLI arguments
364
+ - Input validation
365
+ - CLI output
366
+ - Process exit codes
367
+
368
+ ### `src/copyFile.js`
369
+
370
+ Responsible for:
371
+
372
+ - Resolving paths
373
+ - Reading the source file
374
+ - Writing the destination file
375
+ - Performing the file-copy operation
376
+
377
+ This separation keeps the package easier to maintain and test.
378
+
379
+ ---
380
+
381
+ ## Error Handling
382
+
383
+ The CLI validates the required arguments before attempting the copy operation.
384
+
385
+ Running:
386
+
387
+ ```bash
388
+ pcx-copy
389
+ ```
390
+
391
+ displays:
392
+
393
+ ```text
394
+ Usage: pcx-copy <source-file> <destination-file>
395
+ ```
396
+
397
+ File-system errors are handled using `try...catch`:
398
+
399
+ ```js
400
+ try {
401
+ // Copy operation
402
+ } catch (error) {
403
+ console.error(`Failed to copy file: ${error.message}`);
404
+ process.exit(1);
405
+ }
406
+ ```
407
+
408
+ A successful operation exits normally.
409
+
410
+ A failed operation returns a non-zero process exit code.
411
+
412
+ ---
413
+
414
+ ## Development
415
+
416
+ Run the CLI directly during development:
417
+
418
+ ```bash
419
+ node bin/pcx-copy.js ./source.txt ./destination.txt
420
+ ```
421
+
422
+ Or link the package locally:
423
+
424
+ ```bash
425
+ npm link
426
+ ```
427
+
428
+ Then use:
429
+
430
+ ```bash
431
+ pcx-copy ./source.txt ./destination.txt
432
+ ```
433
+
434
+ ---
435
+
436
+ ## Package Configuration
437
+
438
+ The npm package uses the `bin` field to expose the CLI command.
439
+
440
+ Example:
441
+
442
+ ```json
443
+ {
444
+ "name": "pcx-copy",
445
+ "version": "1.0.0",
446
+ "type": "module",
447
+ "bin": {
448
+ "pcx-copy": "./bin/pcx-copy.js"
449
+ }
450
+ }
451
+ ```
452
+
453
+ The important relationship is:
454
+
455
+ ```text
456
+ npm package
457
+
458
+ package.json
459
+
460
+ bin
461
+
462
+ pcx-copy command
463
+
464
+ bin/pcx-copy.js
465
+ ```
466
+
467
+ ---
468
+
469
+ ## Future Improvements
470
+
471
+ Possible future versions may include:
472
+
473
+ - `--help` option
474
+ - `--version` option
475
+ - Directory copying
476
+ - Recursive directory copying
477
+ - Multiple source files
478
+ - File-existence checks
479
+ - Overwrite confirmation
480
+ - Progress indicators
481
+ - Better cross-platform path handling
482
+ - Improved error codes
483
+ - Automated tests
484
+ - npm package versioning and release automation
485
+
486
+ ---
487
+
488
+ ## Learning Objectives
489
+
490
+ This project demonstrates the fundamentals of Node.js CLI development:
491
+
492
+ - `process.argv`
493
+ - Node.js `fs/promises`
494
+ - `Buffer`
495
+ - Node.js `path`
496
+ - ES Modules
497
+ - Shebangs
498
+ - npm `bin` configuration
499
+ - CLI argument validation
500
+ - Process exit codes
501
+ - npm package structure
502
+ - Global CLI installation
503
+ - `npx` execution
504
+ - Separation of CLI and application logic
505
+
506
+ ---
507
+
508
+ ## License
509
+
510
+ This project is licensed under the **MIT License**.
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env node
2
+
3
+ import { copyFile } from '../src/copyFile.js';
4
+
5
+ const [, , source, destination] = process.argv;
6
+
7
+ if (!source || !destination) {
8
+ console.error(
9
+ 'Usage: pcx-copy <source-file> <destination-file>'
10
+ );
11
+ process.exit(1);
12
+ }
13
+
14
+ await copyFile(source, destination);
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@procoderx/copy-file",
3
+ "version": "1.0.0",
4
+ "description": "A lightweight Node.js CLI tool for copying files from one location to another.",
5
+ "type": "module",
6
+ "bin": {
7
+ "pcx-copy": "./bin/pcx-copy.js"
8
+ },
9
+ "files": [
10
+ "bin",
11
+ "src",
12
+ "README.md",
13
+ "LICENSE"
14
+ ],
15
+ "keywords": [
16
+ "copy",
17
+ "copy-file",
18
+ "file-copy",
19
+ "copy-cli",
20
+ "nodejs-cli",
21
+ "cli-tool"
22
+ ],
23
+ "author": {
24
+ "name": "ProCoderX",
25
+ "url": "https://procoderx.com"
26
+ },
27
+ "license": "MIT",
28
+ "engines": {
29
+ "node": ">=18"
30
+ }
31
+ }
@@ -0,0 +1,20 @@
1
+ import { readFile, writeFile } from 'node:fs/promises';
2
+ import { resolve } from 'node:path';
3
+
4
+ export async function copyFile(source, destination) {
5
+ try {
6
+ const sourcePath = resolve(source);
7
+ const destinationPath = resolve(destination);
8
+
9
+ const content = await readFile(sourcePath);
10
+
11
+ await writeFile(destinationPath, content);
12
+
13
+ console.log(
14
+ `Successfully copied "${source}" to "${destination}"`
15
+ );
16
+ } catch (error) {
17
+ console.error(`Failed to copy file: ${error.message}`);
18
+ process.exit(1);
19
+ }
20
+ }