@brianbuie/node-kit 0.2.3 → 0.2.5

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/Dir.d.ts ADDED
@@ -0,0 +1,49 @@
1
+ /**
2
+ * Reference to a specific directory with helpful methods for resolving filepaths,
3
+ * sanitizing filenames, and saving files
4
+ */
5
+ export declare class Dir {
6
+ path: string;
7
+ /**
8
+ * @param path can be relative to workspace or absolute
9
+ */
10
+ constructor(_path: string);
11
+ make(): void;
12
+ /**
13
+ * Create a new Dir inside the current Dir
14
+ * @param subPath to create in current Dir
15
+ * @example
16
+ * const folder = new Dir('example');
17
+ * // folder.path = './example'
18
+ * const child = folder.subDir('path/to/dir');
19
+ * // child.path = './example/path/to/dir'
20
+ */
21
+ subDir(subPath: string): Dir;
22
+ sanitize(name: string): string;
23
+ /**
24
+ * @param base - The file name with extension
25
+ * @example
26
+ * const folder = new Dir('example');
27
+ * const filepath = folder.resolve('file.json');
28
+ * // 'example/file.json'
29
+ */
30
+ resolve(base: string): string;
31
+ /**
32
+ * Save something in this Dir
33
+ * @param base filename with extension. `.json` will be used if base doesn't include an ext.
34
+ * @param contents `string`, or `any` if it's a json file
35
+ * @returns the filepath of the saved file
36
+ */
37
+ save(base: string, contents: any): string;
38
+ }
39
+ /**
40
+ * Extends Dir class with method to `clear()` contents
41
+ */
42
+ export declare class TempDir extends Dir {
43
+ subDir(subPath: string): TempDir;
44
+ clear(): void;
45
+ }
46
+ /**
47
+ * Common temp dir location
48
+ */
49
+ export declare const temp: TempDir;
package/.dist/Dir.js ADDED
@@ -0,0 +1,77 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import sanitizeFilename from 'sanitize-filename';
4
+ import { snapshot } from './snapshot.js';
5
+ /**
6
+ * Reference to a specific directory with helpful methods for resolving filepaths,
7
+ * sanitizing filenames, and saving files
8
+ */
9
+ export class Dir {
10
+ path;
11
+ /**
12
+ * @param path can be relative to workspace or absolute
13
+ */
14
+ constructor(_path) {
15
+ this.path = _path;
16
+ }
17
+ make() {
18
+ fs.mkdirSync(this.path, { recursive: true });
19
+ }
20
+ /**
21
+ * Create a new Dir inside the current Dir
22
+ * @param subPath to create in current Dir
23
+ * @example
24
+ * const folder = new Dir('example');
25
+ * // folder.path = './example'
26
+ * const child = folder.subDir('path/to/dir');
27
+ * // child.path = './example/path/to/dir'
28
+ */
29
+ subDir(subPath) {
30
+ return new Dir(path.resolve(this.path, subPath));
31
+ }
32
+ sanitize(name) {
33
+ return sanitizeFilename(name.replace('https://', '').replace('www.', ''), { replacement: '_' });
34
+ }
35
+ /**
36
+ * @param base - The file name with extension
37
+ * @example
38
+ * const folder = new Dir('example');
39
+ * const filepath = folder.resolve('file.json');
40
+ * // 'example/file.json'
41
+ */
42
+ resolve(base) {
43
+ return path.resolve(this.path, this.sanitize(base));
44
+ }
45
+ /**
46
+ * Save something in this Dir
47
+ * @param base filename with extension. `.json` will be used if base doesn't include an ext.
48
+ * @param contents `string`, or `any` if it's a json file
49
+ * @returns the filepath of the saved file
50
+ */
51
+ save(base, contents) {
52
+ if (typeof contents !== 'string') {
53
+ if (!/\.json$/.test(base))
54
+ base += '.json';
55
+ contents = JSON.stringify(snapshot(contents), null, 2);
56
+ }
57
+ const filepath = this.resolve(base);
58
+ this.make();
59
+ fs.writeFileSync(filepath, contents);
60
+ return filepath;
61
+ }
62
+ }
63
+ /**
64
+ * Extends Dir class with method to `clear()` contents
65
+ */
66
+ export class TempDir extends Dir {
67
+ subDir(subPath) {
68
+ return new TempDir(path.resolve(this.path, subPath));
69
+ }
70
+ clear() {
71
+ fs.rmSync(this.path, { recursive: true, force: true });
72
+ }
73
+ }
74
+ /**
75
+ * Common temp dir location
76
+ */
77
+ export const temp = new TempDir('.temp');
package/.dist/_index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
+ export { Dir, TempDir, temp } from './Dir.js';
1
2
  export { Fetcher, type Route, Query, FetchOptions } from './Fetcher.js';
2
3
  export { Jwt } from './Jwt.js';
3
- export { snapshot } from './snapshot.js';
4
4
  export { Log } from './Log.js';
5
+ export { snapshot } from './snapshot.js';
6
+ export { TypeWriter } from './TypeWriter.js';
package/.dist/_index.js CHANGED
@@ -1,4 +1,6 @@
1
+ export { Dir, TempDir, temp } from './Dir.js';
1
2
  export { Fetcher } from './Fetcher.js';
2
3
  export { Jwt } from './Jwt.js';
3
- export { snapshot } from './snapshot.js';
4
4
  export { Log } from './Log.js';
5
+ export { snapshot } from './snapshot.js';
6
+ export { TypeWriter } from './TypeWriter.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@brianbuie/node-kit",
3
- "version": "0.2.3",
3
+ "version": "0.2.5",
4
4
  "license": "ISC",
5
5
  "repository": {
6
6
  "type": "git",
@@ -32,7 +32,8 @@
32
32
  "extract-domain": "^5.0.2",
33
33
  "jsonwebtoken": "^9.0.2",
34
34
  "lodash-es": "^4.17.21",
35
- "quicktype-core": "^23.2.6"
35
+ "quicktype-core": "^23.2.6",
36
+ "sanitize-filename": "^1.6.3"
36
37
  },
37
38
  "devDependencies": {
38
39
  "typescript": "^5.9.3"
@@ -0,0 +1,25 @@
1
+ import { describe, it } from 'node:test';
2
+ import assert from 'node:assert';
3
+ import { Dir, TempDir, temp } from './Dir.js';
4
+
5
+ describe('Dir', () => {
6
+ const test = new Dir('test');
7
+
8
+ it('Sanitizes filenames', () => {
9
+ const name = test.sanitize(':/something/else.json');
10
+ assert(!name.includes('/'));
11
+ assert(!name.includes(':'));
12
+ });
13
+
14
+ it('Creates sub directories', () => {
15
+ const subPath = 'sub/dir';
16
+ const sub = test.subDir(subPath);
17
+ assert(sub.path.includes(test.path));
18
+ assert(sub.path.includes(subPath));
19
+ });
20
+
21
+ it('TempDir.subDir returns instance of TempDir', () => {
22
+ const sub = temp.subDir('example');
23
+ assert(sub instanceof TempDir);
24
+ });
25
+ });
package/src/Dir.ts ADDED
@@ -0,0 +1,86 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import sanitizeFilename from 'sanitize-filename';
4
+ import { snapshot } from './snapshot.js';
5
+
6
+ /**
7
+ * Reference to a specific directory with helpful methods for resolving filepaths,
8
+ * sanitizing filenames, and saving files
9
+ */
10
+ export class Dir {
11
+ path;
12
+
13
+ /**
14
+ * @param path can be relative to workspace or absolute
15
+ */
16
+ constructor(_path: string) {
17
+ this.path = _path;
18
+ }
19
+
20
+ make() {
21
+ fs.mkdirSync(this.path, { recursive: true });
22
+ }
23
+
24
+ /**
25
+ * Create a new Dir inside the current Dir
26
+ * @param subPath to create in current Dir
27
+ * @example
28
+ * const folder = new Dir('example');
29
+ * // folder.path = './example'
30
+ * const child = folder.subDir('path/to/dir');
31
+ * // child.path = './example/path/to/dir'
32
+ */
33
+ subDir(subPath: string) {
34
+ return new Dir(path.resolve(this.path, subPath));
35
+ }
36
+
37
+ sanitize(name: string) {
38
+ return sanitizeFilename(name.replace('https://', '').replace('www.', ''), { replacement: '_' });
39
+ }
40
+
41
+ /**
42
+ * @param base - The file name with extension
43
+ * @example
44
+ * const folder = new Dir('example');
45
+ * const filepath = folder.resolve('file.json');
46
+ * // 'example/file.json'
47
+ */
48
+ resolve(base: string) {
49
+ return path.resolve(this.path, this.sanitize(base));
50
+ }
51
+
52
+ /**
53
+ * Save something in this Dir
54
+ * @param base filename with extension. `.json` will be used if base doesn't include an ext.
55
+ * @param contents `string`, or `any` if it's a json file
56
+ * @returns the filepath of the saved file
57
+ */
58
+ save(base: string, contents: any) {
59
+ if (typeof contents !== 'string') {
60
+ if (!/\.json$/.test(base)) base += '.json';
61
+ contents = JSON.stringify(snapshot(contents), null, 2);
62
+ }
63
+ const filepath = this.resolve(base);
64
+ this.make();
65
+ fs.writeFileSync(filepath, contents);
66
+ return filepath;
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Extends Dir class with method to `clear()` contents
72
+ */
73
+ export class TempDir extends Dir {
74
+ subDir(subPath: string) {
75
+ return new TempDir(path.resolve(this.path, subPath));
76
+ }
77
+
78
+ clear() {
79
+ fs.rmSync(this.path, { recursive: true, force: true });
80
+ }
81
+ }
82
+
83
+ /**
84
+ * Common temp dir location
85
+ */
86
+ export const temp = new TempDir('.temp');
package/src/_index.ts CHANGED
@@ -1,4 +1,6 @@
1
+ export { Dir, TempDir, temp } from './Dir.js';
1
2
  export { Fetcher, type Route, Query, FetchOptions } from './Fetcher.js';
2
3
  export { Jwt } from './Jwt.js';
3
- export { snapshot } from './snapshot.js';
4
4
  export { Log } from './Log.js';
5
+ export { snapshot } from './snapshot.js';
6
+ export { TypeWriter } from './TypeWriter.js';