starship-butler-utils 0.0.1-beta.1 → 0.0.1-beta.2

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.d.mts CHANGED
@@ -1,5 +1,12 @@
1
1
  import * as ansis from 'ansis';
2
2
 
3
+ /**
4
+ * Updates or creates a user rc file.
5
+ * @param name The name of the rc file.
6
+ * @param config The rc to write.
7
+ */
8
+ declare function updateOrCreateUserRc(name: string, config: any): void;
9
+
3
10
  /**
4
11
  * Check if a file or directory exists. Does not dereference symlinks.
5
12
  * @param path - The path to check
@@ -20,10 +27,34 @@ declare function isDirectory(path: string): boolean;
20
27
  * @returns `true` if the directory is created, `false` otherwise
21
28
  */
22
29
  declare function ensureDir(dirPath: string): boolean;
23
- declare function createSymlink(sourcePath: string, targetPath: string, force?: boolean): Promise<boolean>;
24
- declare function removeSymlink(targetPath: string): boolean;
30
+ /**
31
+ * Copy a file.
32
+ * @param sourcePath The path to the source file
33
+ * @param targetPath The path to the target file
34
+ * @param force Whether to force overwrite the target file
35
+ * @returns `true` if the file was copied, `false` otherwise
36
+ */
25
37
  declare function copyFile(sourcePath: string, targetPath: string, force?: boolean): boolean;
38
+ /**
39
+ * Create a symbolic link.
40
+ * @param sourcePath The path to the source file
41
+ * @param targetPath The path to the target symlink
42
+ * @param force Whether to force overwrite the target symlink
43
+ * @returns `true` if the symlink was created, `false` otherwise
44
+ */
45
+ declare function createSymlink(sourcePath: string, targetPath: string, force?: boolean): Promise<boolean>;
46
+ /**
47
+ * Remove a file.
48
+ * @param targetPath The path to the target file
49
+ * @returns `true` if the file was removed, `false` otherwise
50
+ */
26
51
  declare function removeFile(targetPath: string): boolean;
52
+ /**
53
+ * Remove a symbolic link.
54
+ * @param targetPath The path to the target symlink
55
+ * @returns `true` if the symlink was removed, `false` otherwise
56
+ */
57
+ declare function removeSymlink(targetPath: string): boolean;
27
58
 
28
59
  declare const fs_copyFile: typeof copyFile;
29
60
  declare const fs_createSymlink: typeof createSymlink;
@@ -52,4 +83,4 @@ declare const highlight: {
52
83
  reset: ansis.Ansis;
53
84
  };
54
85
 
55
- export { fs, highlight };
86
+ export { fs, highlight, updateOrCreateUserRc };
package/dist/index.mjs CHANGED
@@ -1,8 +1,18 @@
1
- import { lstatSync, mkdirSync, unlinkSync, promises, copyFileSync, constants } from 'node:fs';
1
+ import { readUser, writeUser, updateUser } from 'rc9';
2
+ import { lstatSync, mkdirSync, renameSync, copyFileSync, constants, promises, unlinkSync } from 'node:fs';
2
3
  import { dirname } from 'node:path';
3
4
  import consola from 'consola';
4
5
  import { reset, bold, magenta, cyan, red, green } from 'ansis';
5
6
 
7
+ function updateOrCreateUserRc(name, config) {
8
+ const rc = readUser(name);
9
+ if (!rc) {
10
+ writeUser(config, name);
11
+ } else {
12
+ updateUser(config, name);
13
+ }
14
+ }
15
+
6
16
  const highlight = {
7
17
  green: (text) => {
8
18
  return green(text);
@@ -44,44 +54,42 @@ function ensureDir(dirPath) {
44
54
  }
45
55
  return false;
46
56
  }
47
- async function createSymlink(sourcePath, targetPath, force = false) {
57
+ function copyFile(sourcePath, targetPath, force = false) {
48
58
  if (existsSync(targetPath)) {
49
- if (force) {
50
- unlinkSync(targetPath);
51
- } else {
52
- consola.warn(`LINK: File already exists: ${highlight.info(targetPath)}, skip`);
59
+ if (!force) {
60
+ consola.warn(`COPY: File already exists: ${highlight.info(targetPath)}, skip`);
53
61
  return false;
62
+ } else {
63
+ renameSync(targetPath, `${targetPath}.bak`);
54
64
  }
55
65
  }
56
- await promises.symlink(sourcePath, targetPath, "file");
57
- return true;
58
- }
59
- function removeSymlink(targetPath) {
60
- if (!existsSync(targetPath)) {
61
- consola.warn(`UNLINK: Target file not found: ${highlight.info(targetPath)}, skip`);
62
- return false;
63
- }
64
- const stats = lstatSync(targetPath);
65
- if (!stats.isSymbolicLink()) {
66
- consola.warn(`UNLINK: Target file is not a symlink: ${highlight.info(targetPath)}, skip`);
66
+ try {
67
+ copyFileSync(sourcePath, targetPath, force ? constants.COPYFILE_FICLONE : constants.COPYFILE_EXCL);
68
+ removeFile(`${targetPath}.bak`);
69
+ } catch (error) {
70
+ renameSync(`${targetPath}.bak`, targetPath);
71
+ consola.error(error);
67
72
  return false;
68
73
  }
69
- unlinkSync(targetPath);
70
74
  return true;
71
75
  }
72
- function copyFile(sourcePath, targetPath, force = false) {
76
+ async function createSymlink(sourcePath, targetPath, force = false) {
73
77
  if (existsSync(targetPath)) {
74
78
  if (!force) {
75
- consola.warn(`COPY: File already exists: ${highlight.info(targetPath)}, skip`);
79
+ consola.warn(`LINK: File already exists: ${highlight.info(targetPath)}, skip`);
76
80
  return false;
77
81
  } else {
78
- if (lstatSync(targetPath).isSymbolicLink()) {
79
- consola.debug(`Try to force override a symbolic link, will remove it first.`);
80
- removeSymlink(targetPath);
81
- }
82
+ renameSync(targetPath, `${targetPath}.bak`);
82
83
  }
83
84
  }
84
- copyFileSync(sourcePath, targetPath, force ? constants.COPYFILE_FICLONE : constants.COPYFILE_EXCL);
85
+ try {
86
+ await promises.symlink(sourcePath, targetPath, "file");
87
+ removeFile(`${targetPath}.bak`);
88
+ } catch (error) {
89
+ renameSync(`${targetPath}.bak`, targetPath);
90
+ consola.error(error);
91
+ return false;
92
+ }
85
93
  return true;
86
94
  }
87
95
  function removeFile(targetPath) {
@@ -92,6 +100,15 @@ function removeFile(targetPath) {
92
100
  unlinkSync(targetPath);
93
101
  return true;
94
102
  }
103
+ function removeSymlink(targetPath) {
104
+ const stats = lstatSync(targetPath);
105
+ if (!stats.isSymbolicLink()) {
106
+ consola.warn(`REMOVE: Target file is not a symlink: ${highlight.info(targetPath)}, skip`);
107
+ return false;
108
+ }
109
+ removeFile(targetPath);
110
+ return true;
111
+ }
95
112
 
96
113
  const fs = {
97
114
  __proto__: null,
@@ -104,4 +121,4 @@ const fs = {
104
121
  removeSymlink: removeSymlink
105
122
  };
106
123
 
107
- export { fs, highlight };
124
+ export { fs, highlight, updateOrCreateUserRc };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "starship-butler-utils",
3
3
  "type": "module",
4
- "version": "0.0.1-beta.1",
4
+ "version": "0.0.1-beta.2",
5
5
  "description": "Your best starship butler.",
6
6
  "author": "Lumirelle <shabbyacc@outlook.com>",
7
7
  "license": "MIT",
@@ -25,7 +25,8 @@
25
25
  ],
26
26
  "dependencies": {
27
27
  "ansis": "^4.1.0",
28
- "consola": "^3.4.2"
28
+ "consola": "^3.4.2",
29
+ "rc9": "^2.1.2"
29
30
  },
30
31
  "scripts": {
31
32
  "build": "unbuild",