starship-butler-utils 0.0.1-beta.1 → 0.0.1-beta.10
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 +34 -3
- package/dist/index.mjs +55 -28
- package/package.json +3 -2
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 (rc file under home directory).
|
|
5
|
+
* @param name The name of the rc file.
|
|
6
|
+
* @param config The rc to write.
|
|
7
|
+
*/
|
|
8
|
+
declare function upsertUserRc(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
|
-
|
|
24
|
-
|
|
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, upsertUserRc };
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,18 @@
|
|
|
1
|
-
import {
|
|
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 upsertUserRc(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,52 @@ function ensureDir(dirPath) {
|
|
|
44
54
|
}
|
|
45
55
|
return false;
|
|
46
56
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
consola.warn(`LINK: File already exists: ${highlight.info(targetPath)}, skip`);
|
|
57
|
+
function copyFile(sourcePath, targetPath, force = false) {
|
|
58
|
+
const isExist = existsSync(targetPath);
|
|
59
|
+
if (isExist) {
|
|
60
|
+
if (!force) {
|
|
61
|
+
consola.warn(`COPY: File already exists: ${highlight.info(targetPath)}, skip`);
|
|
53
62
|
return false;
|
|
63
|
+
} else {
|
|
64
|
+
renameSync(targetPath, `${targetPath}.bak`);
|
|
54
65
|
}
|
|
55
66
|
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
consola.warn(`UNLINK: Target file is not a symlink: ${highlight.info(targetPath)}, skip`);
|
|
67
|
+
try {
|
|
68
|
+
copyFileSync(sourcePath, targetPath, force ? constants.COPYFILE_FICLONE : constants.COPYFILE_EXCL);
|
|
69
|
+
if (isExist) {
|
|
70
|
+
removeFile(`${targetPath}.bak`);
|
|
71
|
+
}
|
|
72
|
+
} catch (error) {
|
|
73
|
+
if (isExist) {
|
|
74
|
+
renameSync(`${targetPath}.bak`, targetPath);
|
|
75
|
+
}
|
|
76
|
+
consola.error(error);
|
|
67
77
|
return false;
|
|
68
78
|
}
|
|
69
|
-
unlinkSync(targetPath);
|
|
70
79
|
return true;
|
|
71
80
|
}
|
|
72
|
-
function
|
|
73
|
-
|
|
81
|
+
async function createSymlink(sourcePath, targetPath, force = false) {
|
|
82
|
+
const isExist = existsSync(targetPath);
|
|
83
|
+
if (isExist) {
|
|
74
84
|
if (!force) {
|
|
75
|
-
consola.warn(`
|
|
85
|
+
consola.warn(`LINK: File already exists: ${highlight.info(targetPath)}, skip`);
|
|
76
86
|
return false;
|
|
77
87
|
} else {
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
88
|
+
renameSync(targetPath, `${targetPath}.bak`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
await promises.symlink(sourcePath, targetPath, "file");
|
|
93
|
+
if (isExist) {
|
|
94
|
+
removeFile(`${targetPath}.bak`);
|
|
82
95
|
}
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (isExist) {
|
|
98
|
+
renameSync(`${targetPath}.bak`, targetPath);
|
|
99
|
+
}
|
|
100
|
+
consola.error(error);
|
|
101
|
+
return false;
|
|
83
102
|
}
|
|
84
|
-
copyFileSync(sourcePath, targetPath, force ? constants.COPYFILE_FICLONE : constants.COPYFILE_EXCL);
|
|
85
103
|
return true;
|
|
86
104
|
}
|
|
87
105
|
function removeFile(targetPath) {
|
|
@@ -92,6 +110,15 @@ function removeFile(targetPath) {
|
|
|
92
110
|
unlinkSync(targetPath);
|
|
93
111
|
return true;
|
|
94
112
|
}
|
|
113
|
+
function removeSymlink(targetPath) {
|
|
114
|
+
const stats = lstatSync(targetPath);
|
|
115
|
+
if (!stats.isSymbolicLink()) {
|
|
116
|
+
consola.warn(`REMOVE: Target file is not a symlink: ${highlight.info(targetPath)}, skip`);
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
removeFile(targetPath);
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
95
122
|
|
|
96
123
|
const fs = {
|
|
97
124
|
__proto__: null,
|
|
@@ -104,4 +131,4 @@ const fs = {
|
|
|
104
131
|
removeSymlink: removeSymlink
|
|
105
132
|
};
|
|
106
133
|
|
|
107
|
-
export { fs, highlight };
|
|
134
|
+
export { fs, highlight, upsertUserRc };
|
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.
|
|
4
|
+
"version": "0.0.1-beta.10",
|
|
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",
|