@voxframeworks/linetools 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/main.js ADDED
@@ -0,0 +1,14 @@
1
+ const mkdir = require('./tools/mkdir.js');
2
+ const cp = require('./tools/cp.js');
3
+ const touch = require('./tools/touch.js');
4
+ const mv = require('./tools/mv.js');
5
+ const tar = require('./tools/tar.js');
6
+ const rmdir = require('./tools/rmdir.js');
7
+ module.exports = {
8
+ mkdir : mkdir,
9
+ cp : cp,
10
+ touch : touch,
11
+ mv : mv,
12
+ tar : tar,
13
+ rmdir : rmdir
14
+ }
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@voxframeworks/linetools",
3
+ "version": "1.0.0",
4
+ "description": "command line tools for js",
5
+ "main": "main.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": ["line tools", "command line", "cli"],
10
+ "author": "VoxFrameworks",
11
+ "license": "ISC",
12
+ "type": "commonjs",
13
+ "dependencies": {
14
+ "fstream": "^1.0.12",
15
+ "tar": "^7.5.3"
16
+ }
17
+ }
package/readme.md ADDED
@@ -0,0 +1,27 @@
1
+ ```
2
+ _ _ _____ _
3
+ | | (_)_ __ __|_ _|__ ___ | |___
4
+ | | | | '_ \ / _ \| |/ _ \ / _ \| / __|
5
+ | |___| | | | | __/| | (_) | (_) | \__ \
6
+ |_____|_|_| |_|\___||_|\___/ \___/|_|___/
7
+
8
+
9
+ Linetools is a npm package that lets you use command line tools in a js file.
10
+
11
+ An Example usage is:
12
+ linetools.mkdir('testDir');
13
+
14
+ This will create a directory named testDir in the current directory.
15
+
16
+ We have many commands available.
17
+ The list of commands we have is:
18
+ • mkdir
19
+ • rmdir
20
+ • cp
21
+ • mv
22
+ • touch
23
+ • tar
24
+
25
+ We will be adding more soon!
26
+
27
+ By VoxFrameworks
package/test.js ADDED
@@ -0,0 +1,2 @@
1
+ const linetools = require('linetools');
2
+ linetools.tar('testOUT', 'testDir', 'rezip')
package/tools/cp.js ADDED
@@ -0,0 +1,19 @@
1
+ const fs = require('fs').promises;
2
+
3
+ /**
4
+ * Asynchronously copies the file or directory from the source path to the destination path.
5
+ *
6
+ * @param {string} sourcePath - The source file or directory path.
7
+ * @param {string} destinationPath - The destination file or directory path.
8
+ */
9
+ async function cp(sourcePath, destinationPath) {
10
+ try {
11
+ // The 'recursive: true' option is necessary to copy directories and their contents.
12
+ await fs.cp(sourcePath, destinationPath, { recursive: true });
13
+ console.log(`Contents of '${sourcePath}' copied successfully to '${destinationPath}'!`);
14
+ } catch (error) {
15
+ console.error('Failed to copy files or directory:', error.message);
16
+ }
17
+ }
18
+
19
+ module.exports = cp;
package/tools/mkdir.js ADDED
@@ -0,0 +1,11 @@
1
+ const fs = require('fs').promises;
2
+
3
+ async function mkdir(directoryPath) {
4
+ try {
5
+ await fs.mkdir(directoryPath, { recursive: true });
6
+ console.log(`Directory '${directoryPath}' created successfully!`);
7
+ } catch (error) {
8
+ console.error('Failed to create directory:', error);
9
+ }
10
+ }
11
+ module.exports = mkdir;
package/tools/mv.js ADDED
@@ -0,0 +1,22 @@
1
+ const fs = require('fs').promises;
2
+ const path = require('path');
3
+
4
+ async function mv(oldPath, newPath) {
5
+ try {
6
+ // Ensure the destination directory exists (optional, but robust)
7
+ const destinationDir = path.dirname(newPath);
8
+ await fs.mkdir(destinationDir, { recursive: true });
9
+
10
+ // Move the file or directory
11
+ await fs.rename(oldPath, newPath);
12
+ console.log(`Moved from '${oldPath}' to '${newPath}' successfully!`);
13
+ } catch (error) {
14
+ console.error('Failed to move:', error);
15
+ // Handle specific errors like moving across different file systems (EXDEV)
16
+ if (error.code === 'EXDEV') {
17
+ console.error('Operation failed across different devices. Consider using fs-extra for cross-device moves.');
18
+ }
19
+ }
20
+ }
21
+
22
+ module.exports = mv;
package/tools/rmdir.js ADDED
@@ -0,0 +1,22 @@
1
+ const fs = require('fs').promises;
2
+
3
+ /**
4
+ * Asynchronously removes a directory.
5
+ * @param {string} directoryPath - The path to the directory to remove.
6
+ */
7
+ async function rmdir(directoryPath) {
8
+ try {
9
+ // The 'recursive: true' option is crucial for removing non-empty directories.
10
+ await fs.rm(directoryPath, { recursive: true, force: true });
11
+ console.log(`Directory '${directoryPath}' removed successfully!`);
12
+ } catch (error) {
13
+ // Check if the error indicates the directory wasn't found to provide a clearer message
14
+ if (error.code === 'ENOENT') {
15
+ console.warn(`Directory '${directoryPath}' does not exist, skipping removal.`);
16
+ } else {
17
+ console.error('Failed to remove directory:', error);
18
+ }
19
+ }
20
+ }
21
+
22
+ module.exports = rmdir;
package/tools/tar.js ADDED
@@ -0,0 +1,69 @@
1
+ const tarModule = require('tar');
2
+ const fs = require('fs').promises;
3
+ const path = require('path');
4
+
5
+ /**
6
+ * Handles tar archive operations (unzip/rezip) for files and folders.
7
+ * @param {string} targetPath - The path to the tarball file (output for rezip, input for unzip).
8
+ * @param {string} sourcePath - The source directory/files for rezip, or the destination directory for unzip.
9
+ * @param {'unzip' | 'rezip'} type - The operation type.
10
+ */
11
+ async function tar(targetPath, sourcePath, type) {
12
+ try {
13
+ if (type === 'unzip') {
14
+ // Ensure the destination directory exists
15
+ await fs.mkdir(sourcePath, { recursive: true });
16
+ console.log(`Starting extraction of '${targetPath}' to '${sourcePath}'...`);
17
+ // Extracting from a tarball (handles .tar, .tar.gz automatically with modern 'tar')
18
+ await tarModule.extract({
19
+ file: targetPath,
20
+ cwd: sourcePath, // Extract *into* this directory
21
+ });
22
+ console.log('Extraction completed successfully!');
23
+ } else if (type === 'rezip') {
24
+ const tarballDir = path.dirname(targetPath);
25
+ await fs.mkdir(tarballDir, { recursive: true }); // Ensure output dir exists
26
+
27
+ // For zipping folders, use tar.pack() with streams for better control & recursion
28
+ // or tar.create() if you prefer the object API and ensure 'gzip: true' for .tar.gz
29
+ console.log(`Starting creation of tarball '${targetPath}' from '${sourcePath}'...`);
30
+ // Use tar.create or tar.c for creating streams
31
+ const archiveStream = tarModule.create({ gzip: targetPath.endsWith('.gz') }, sourcePath);
32
+
33
+ fileHandle = await fs.open(targetPath, 'w');
34
+
35
+ // Create the write stream from the file handle
36
+ const writeStream = fileHandle.createWriteStream();
37
+
38
+ // If you need .tar.gz, pipe through gzip stream
39
+ if (targetPath.endsWith('.gz')) {
40
+ const zlib = require('zlib');
41
+ await new Promise((resolve, reject) => {
42
+ archiveStream
43
+ .pipe(zlib.createGzip()) // Add gzip compression
44
+ .pipe(outputStream)
45
+ .on('finish', resolve)
46
+ .on('error', reject);
47
+ });
48
+ } else {
49
+ // For plain .tar
50
+ await new Promise((resolve, reject) => {
51
+ archiveStream
52
+ .pipe(outputStream)
53
+ .on('finish', resolve)
54
+ .on('error', reject);
55
+ });
56
+ }
57
+
58
+ console.log('Tarball creation completed successfully!');
59
+
60
+ } else {
61
+ throw new Error('Invalid type specified. Use "unzip" or "rezip".');
62
+ }
63
+ } catch (error) {
64
+ console.error(`Failed to perform tar operation (${type}):`, error.message);
65
+ throw error; // Rethrow to allow the calling function to handle the error
66
+ }
67
+ }
68
+
69
+ module.exports = tar;
package/tools/touch.js ADDED
@@ -0,0 +1,21 @@
1
+ const fs = require('fs').promises;
2
+
3
+ async function touch(filePath) {
4
+ try {
5
+ const now = new Date();
6
+ // Try to update access and modification times (like 'touch' on existing files)
7
+ await fs.utimes(filePath, now, now);
8
+ console.log(`Updated timestamp for '${filePath}'`);
9
+ } catch (error) {
10
+ if (error.code === 'ENOENT') {
11
+ // If file doesn't exist, create an empty one
12
+ // Using 'a' (append) ensures we don't overwrite if it was just created
13
+ const fileHandle = await fs.open(filePath, 'a');
14
+ await fileHandle.close();
15
+ console.log(`File '${filePath}' created successfully!`);
16
+ } else {
17
+ console.error('Failed to touch file:', error);
18
+ }
19
+ }
20
+ }
21
+ module.exports = touch;