@vanillaes/esmtk 0.6.4 → 0.6.6

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.
@@ -36,6 +36,42 @@
36
36
  "cwd": "${workspaceFolder}",
37
37
  "args": ["cp", "-r", "./test/cp/", "./test/cp2"],
38
38
  "console": "integratedTerminal",
39
+ },
40
+ {
41
+ "name": "Lint",
42
+ "type": "node",
43
+ "request": "launch",
44
+ "skipFiles": [
45
+ "<node_internals>/**"
46
+ ],
47
+ "program": "${workspaceFolder}/bin/esmtk.js",
48
+ "cwd": "${workspaceFolder}",
49
+ "args": ["lint"],
50
+ "console": "integratedTerminal",
51
+ },
52
+ {
53
+ "name": "RemoveFile",
54
+ "type": "node",
55
+ "request": "launch",
56
+ "skipFiles": [
57
+ "<node_internals>/**"
58
+ ],
59
+ "program": "${workspaceFolder}/bin/esmtk.js",
60
+ "cwd": "${workspaceFolder}",
61
+ "args": ["rm", "./test/cp2/test1.txt"],
62
+ "console": "integratedTerminal",
63
+ },
64
+ {
65
+ "name": "RemoveDirectory",
66
+ "type": "node",
67
+ "request": "launch",
68
+ "skipFiles": [
69
+ "<node_internals>/**"
70
+ ],
71
+ "program": "${workspaceFolder}/bin/esmtk.js",
72
+ "cwd": "${workspaceFolder}",
73
+ "args": ["rm", "-r", "./test/cp2/"],
74
+ "console": "integratedTerminal",
39
75
  }
40
76
  ]
41
77
  }
package/README.md CHANGED
@@ -78,7 +78,7 @@ esmtk commonjs src/sample.js bundle.cjs
78
78
 
79
79
  ## Copy
80
80
 
81
- Copy us a cross-platform clone of the `cp` command in Linux
81
+ Copy is a cross-platform clone of the `cp` command in Linux
82
82
 
83
83
  ### Arguments
84
84
 
@@ -86,11 +86,29 @@ Copy us a cross-platform clone of the `cp` command in Linux
86
86
 
87
87
  - `[source]` - the source file/glob
88
88
  - `[destination]` - the destination file/directory
89
+ - `-f --force` - ignore exceptions if the file doesn't exist
89
90
  - `-r, --recursive` - copy files/directories recursively
90
- - `-f --force` - force overwrite existing files
91
91
 
92
92
  ### Usage
93
93
 
94
94
  ```sh
95
95
  esmtk cp -r src/ dest/
96
96
  ```
97
+
98
+ ## Remove
99
+
100
+ Remove is a cross-platform clone of the `rm` command in Linux
101
+
102
+ ### Arguments
103
+
104
+ `esmtk rm [-rf] [file|directory]`
105
+
106
+ - `[file|directory]` - the source file/glob
107
+ - `-f --force` - force remove existing file(s)
108
+ - `-r, --recursive` - remove files/directories recursively
109
+
110
+ ### Usage
111
+
112
+ ```sh
113
+ esmtk rm -r src/
114
+ ```
@@ -1,97 +1,11 @@
1
- import { basename, dirname } from 'node:path'
2
- import { access, constants, cp as nodeCp, stat } from 'node:fs/promises'
1
+ import { copyAsync, copyRecursiveAsync } from '../../src/cp.js'
3
2
 
4
3
  export async function cp (source, target, options) {
5
- if (!options.recursive) {
6
- await copyAsync(source, target, options.force)
4
+ if (!options?.recursive) {
5
+ await copyAsync(source, target, options?.force)
7
6
  }
8
7
 
9
- if (options.recursive) {
10
- await copyRecursiveAsync(source, target, options.force)
11
- }
12
- }
13
-
14
- // copy a single file
15
- async function copyAsync (source, target, force = false) {
16
- const sExists = await fileExists(source)
17
- if (!sExists) {
18
- console.error(`cp: source ${source} does not exist`)
19
- process.exit(1)
20
- }
21
- const sStats = await stat(source)
22
- if (sStats.isSymbolicLink()) {
23
- console.error(`cp: source ${source} is a sybolic link`)
24
- process.exit(1)
25
- }
26
-
27
- const tDir = dirname(target)
28
- const tDirExists = await fileExists(tDir)
29
- if (!tDirExists) {
30
- console.error(`cp: target directory ${tDir} does not exist`)
31
- process.exit(1)
32
- }
33
-
34
- const tExists = await fileExists(target)
35
- if (tExists) {
36
- const tStats = await stat(target)
37
- if (tStats.isSymbolicLink()) {
38
- console.error(`cp: target ${target} is a sybolic link`)
39
- process.exit(1)
40
- }
41
- if (tStats.isDirectory()) {
42
- const sourceFile = basename(source)
43
- target = target.endsWith('/') ? target.slice(0, -1) : target
44
- target = `${target}/${sourceFile}`
45
- }
46
- }
47
-
48
- try {
49
- await nodeCp(source, target, { force })
50
- } catch (err) {
51
- console.error(`cp: error ${err.message}`)
52
- }
53
- }
54
-
55
- // copy a directory recursively
56
- async function copyRecursiveAsync (source, target, force = false) {
57
- const sExists = await fileExists(source)
58
- if (!sExists) {
59
- console.error(`cp: source ${source} does not exist`)
60
- process.exit(1)
61
- }
62
- const sStats = await stat(source)
63
- if (sStats.isSymbolicLink()) {
64
- console.error(`cp: source ${source} is a sybolic link`)
65
- process.exit(1)
66
- }
67
-
68
- const tExists = await fileExists(target)
69
- if (!tExists) {
70
- console.error(`cp: target directory ${target} does not exist`)
71
- process.exit(1)
72
- }
73
- const tStats = await stat(target)
74
- if (tStats.isSymbolicLink()) {
75
- console.error(`cp: target ${target} is a sybolic link`)
76
- process.exit(1)
77
- }
78
- if (!tStats.isDirectory()) {
79
- console.error(`cp: target ${target} is not a directory`)
80
- process.exit(1)
81
- }
82
-
83
- try {
84
- await nodeCp(source, target, { force, recursive: true })
85
- } catch (err) {
86
- console.error(`cp": error ${err.message}`)
87
- }
88
- }
89
-
90
- async function fileExists (file) {
91
- try {
92
- await access(file, constants.F_OK)
93
- return true
94
- } catch (error) {
95
- return false
8
+ if (options?.recursive) {
9
+ await copyRecursiveAsync(source, target, options?.force)
96
10
  }
97
11
  }
@@ -3,3 +3,4 @@ export { commonjs } from './commonjs.js'
3
3
  export { cp } from './cp.js'
4
4
  export { lint } from './lint.js'
5
5
  export { minify } from './minify.js'
6
+ export { rm } from './rm.js'
@@ -4,10 +4,22 @@ import { join } from 'path'
4
4
  const BIN_PATH = join(process.cwd(), 'node_modules', '.bin', 'standard')
5
5
 
6
6
  export async function lint (flags) {
7
- spawn(BIN_PATH, flags, {
7
+ const child = spawn(BIN_PATH, flags, {
8
8
  cwd: process.cwd(),
9
- stdio: ['pipe', process.stdout, process.stderr]
9
+ stdio: ['pipe', 'pipe', 'pipe']
10
10
  }).on('error', err => {
11
- console.error(err)
11
+ console.error(`test${err}`)
12
+ })
13
+
14
+ child.stdout.on('data', (data) => {
15
+ process.stdout.write(`${data}`)
16
+ })
17
+
18
+ child.stderr.on('data', (data) => {
19
+ if (data.toString() === 'standard: Run `standard --fix` to automatically fix some problems.\n') {
20
+ process.stderr.write('standard: Run `esmtk lint --fix` to automatically fix some problems.\n')
21
+ } else {
22
+ process.stderr.write(`${data}`)
23
+ }
12
24
  })
13
25
  }
@@ -0,0 +1,11 @@
1
+ import { removeAsync, removeRecursiveAsync } from '../../src/rm.js'
2
+
3
+ export async function rm (path, options) {
4
+ if (!options?.recursive) {
5
+ await removeAsync(path, options?.force)
6
+ }
7
+
8
+ if (options?.recursive) {
9
+ await removeRecursiveAsync(path, options?.force)
10
+ }
11
+ }
package/bin/esmtk.js CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import { bundle, cp, commonjs, lint, minify } from './commands/index.js'
2
+ import { bundle, cp, commonjs, lint, minify, rm } from './commands/index.js'
3
3
  import { Command } from 'commander'
4
4
  import { createRequire } from 'module'
5
5
  const program = new Command()
@@ -37,10 +37,19 @@ program.command('minify <input> <output>')
37
37
  program.command('cp <source> <target>')
38
38
  .usage('[-rf] source target')
39
39
  .description('Copy files from the source to the target')
40
- .option('-f, --force', 'do not prompt before overwriting')
41
- .option('-r, --recursive', 'copy directories recursively')
40
+ .option('-f, --force', 'do not prompt before overwriting', false)
41
+ .option('-r, --recursive', 'copy directories recursively', false)
42
42
  .action((source, target, options) => {
43
43
  cp(source, target, options)
44
44
  })
45
45
 
46
+ program.command('rm <path>')
47
+ .usage('[-rf] file|directory')
48
+ .description('Remove a file|directory')
49
+ .option('-f, --force', 'do not prompt before overwriting', false)
50
+ .option('-r, --recursive', 'remove directories recursively', false)
51
+ .action((path, options) => {
52
+ rm(path, options)
53
+ })
54
+
46
55
  program.parse(process.argv)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vanillaes/esmtk",
3
- "version": "0.6.4",
3
+ "version": "0.6.6",
4
4
  "description": "ES Module Toolkit",
5
5
  "keywords": [
6
6
  "esm",
@@ -13,6 +13,11 @@
13
13
  "author": "Evan Plaice <evanplaice@gmail.com> (https://evanplaice.com/)",
14
14
  "license": "MIT",
15
15
  "type": "module",
16
+ "exports": {
17
+ ".": "./src/index.js",
18
+ "./cp": "./src/cp.js",
19
+ "./rm": "./src/rm.js"
20
+ },
16
21
  "bin": {
17
22
  "esmtk": "bin/esmtk.js"
18
23
  },
package/src/cp.js ADDED
@@ -0,0 +1,97 @@
1
+ import { basename, dirname } from 'node:path'
2
+ import { access, constants, cp, stat } from 'node:fs/promises'
3
+
4
+ /**
5
+ * Copy a single file asynchronously
6
+ * @param {string} source The source file
7
+ * @param {string} target The target file
8
+ * @param {boolean} force If the file already exists, overwrite it (default false)
9
+ */
10
+ export async function copyAsync (source, target, force = false) {
11
+ const sExists = await fileExists(source)
12
+ if (!sExists) {
13
+ console.error(`cp: source ${source} does not exist`)
14
+ process.exit(1)
15
+ }
16
+ const sStats = await stat(source)
17
+ if (sStats.isSymbolicLink()) {
18
+ console.error(`cp: source ${source} is a sybolic link`)
19
+ process.exit(1)
20
+ }
21
+
22
+ const tDir = dirname(target)
23
+ const tDirExists = await fileExists(tDir)
24
+ if (!tDirExists) {
25
+ console.error(`cp: target directory ${tDir} does not exist`)
26
+ process.exit(1)
27
+ }
28
+
29
+ const tExists = await fileExists(target)
30
+ if (tExists) {
31
+ const tStats = await stat(target)
32
+ if (tStats.isSymbolicLink()) {
33
+ console.error(`cp: target ${target} is a sybolic link`)
34
+ process.exit(1)
35
+ }
36
+ if (tStats.isDirectory()) {
37
+ const sourceFile = basename(source)
38
+ target = target.endsWith('/') ? target.slice(0, -1) : target
39
+ target = `${target}/${sourceFile}`
40
+ }
41
+ }
42
+
43
+ try {
44
+ await cp(source, target, { force })
45
+ } catch (err) {
46
+ console.error(`cp: error ${err.message}`)
47
+ }
48
+ }
49
+
50
+ /**
51
+ * Recursively copy a directory asynchronously
52
+ * @param {string} source The source directory
53
+ * @param {string} target The target directory
54
+ * @param {bool} force If the file already exists, overwrite it (default false)
55
+ */
56
+ export async function copyRecursiveAsync (source, target, force = false) {
57
+ const sExists = await fileExists(source)
58
+ if (!sExists) {
59
+ console.error(`cp: source ${source} does not exist`)
60
+ process.exit(1)
61
+ }
62
+ const sStats = await stat(source)
63
+ if (sStats.isSymbolicLink()) {
64
+ console.error(`cp: source ${source} is a sybolic link`)
65
+ process.exit(1)
66
+ }
67
+
68
+ const tExists = await fileExists(target)
69
+ if (!tExists) {
70
+ console.error(`cp: target directory ${target} does not exist`)
71
+ process.exit(1)
72
+ }
73
+ const tStats = await stat(target)
74
+ if (tStats.isSymbolicLink()) {
75
+ console.error(`cp: target ${target} is a sybolic link`)
76
+ process.exit(1)
77
+ }
78
+ if (!tStats.isDirectory()) {
79
+ console.error(`cp: target ${target} is not a directory`)
80
+ process.exit(1)
81
+ }
82
+
83
+ try {
84
+ await cp(source, target, { force, recursive: true })
85
+ } catch (err) {
86
+ console.error(`cp": error ${err.message}`)
87
+ }
88
+ }
89
+
90
+ async function fileExists (file) {
91
+ try {
92
+ await access(file, constants.F_OK)
93
+ return true
94
+ } catch (error) {
95
+ return false
96
+ }
97
+ }
package/src/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { copyAsync, copyRecursiveAsync } from './cp.js'
2
+ export { removeAsync, removeRecursiveAsync } from './rm.js'
package/src/rm.js ADDED
@@ -0,0 +1,58 @@
1
+ import { access, constants, rm, stat } from 'node:fs/promises'
2
+
3
+ /**
4
+ * Remove a file asynchronously
5
+ * @param {string} path The file to delete
6
+ * @param {boolean} force Ignore exceptions if the file doesn't exist (default false)
7
+ */
8
+ export async function removeAsync (path, force = false) {
9
+ const exists = await fileExists(path)
10
+ if (!exists) {
11
+ console.error(`rm: path ${path} does not exist`)
12
+ process.exit(1)
13
+ }
14
+ const stats = await stat(path)
15
+ if (stats.isSymbolicLink()) {
16
+ console.error(`rm: path ${path} is a sybolic link`)
17
+ process.exit(1)
18
+ }
19
+
20
+ try {
21
+ await rm(path, { force })
22
+ } catch (err) {
23
+ console.error(`rm: error ${err.message}`)
24
+ }
25
+ }
26
+
27
+ /**
28
+ * Recursively remove a file|directory asynchronously
29
+ * @param {string} path The file|directory to remove
30
+ * @param {bool} force Ignore exceptions if the file|directory doesn't exist (default false)
31
+ */
32
+ export async function removeRecursiveAsync (path, force = false) {
33
+ const exists = await fileExists(path)
34
+ if (!exists) {
35
+ console.error(`rm: path ${path} does not exist`)
36
+ process.exit(1)
37
+ }
38
+ const stats = await stat(path)
39
+ if (stats.isSymbolicLink()) {
40
+ console.error(`rm: path ${path} is a sybolic link`)
41
+ process.exit(1)
42
+ }
43
+
44
+ try {
45
+ await rm(path, { force, recursive: true })
46
+ } catch (err) {
47
+ console.error(`rm": error ${err.message}`)
48
+ }
49
+ }
50
+
51
+ async function fileExists (file) {
52
+ try {
53
+ await access(file, constants.F_OK)
54
+ return true
55
+ } catch (error) {
56
+ return false
57
+ }
58
+ }