starship-butler-utils 0.0.0 → 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/LICENSE.md +21 -0
- package/dist/index.d.mts +76 -1
- package/dist/index.mjs +116 -1
- package/package.json +7 -6
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025-PRESENT LUMIRELLE <https://github.com/lumirelle>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,80 @@
|
|
|
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
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Check if a file or directory exists. Does not dereference symlinks.
|
|
12
|
+
* @param path - The path to check
|
|
13
|
+
* @returns `true` if the file or directory exists, `false` otherwise
|
|
14
|
+
*/
|
|
15
|
+
declare function existsSync(path: string): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Check if a path is a directory. If path exists, we check the stats, if not, we check if the path ends with `/` or `\`.
|
|
18
|
+
* @param path - The path to check
|
|
19
|
+
* @returns `true` if the path is a directory, `false` otherwise
|
|
20
|
+
*/
|
|
21
|
+
declare function isDirectory(path: string): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Ensure a directory exists.
|
|
24
|
+
* If the path is not a directory, we will try to create it.
|
|
25
|
+
* If the path is a file, we will create the parent directory.
|
|
26
|
+
* @param dirPath - The path to ensure
|
|
27
|
+
* @returns `true` if the directory is created, `false` otherwise
|
|
28
|
+
*/
|
|
29
|
+
declare function ensureDir(dirPath: 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
|
+
*/
|
|
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
|
+
*/
|
|
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;
|
|
58
|
+
|
|
59
|
+
declare const fs_copyFile: typeof copyFile;
|
|
60
|
+
declare const fs_createSymlink: typeof createSymlink;
|
|
61
|
+
declare const fs_ensureDir: typeof ensureDir;
|
|
62
|
+
declare const fs_existsSync: typeof existsSync;
|
|
63
|
+
declare const fs_isDirectory: typeof isDirectory;
|
|
64
|
+
declare const fs_removeFile: typeof removeFile;
|
|
65
|
+
declare const fs_removeSymlink: typeof removeSymlink;
|
|
66
|
+
declare namespace fs {
|
|
67
|
+
export {
|
|
68
|
+
fs_copyFile as copyFile,
|
|
69
|
+
fs_createSymlink as createSymlink,
|
|
70
|
+
fs_ensureDir as ensureDir,
|
|
71
|
+
fs_existsSync as existsSync,
|
|
72
|
+
fs_isDirectory as isDirectory,
|
|
73
|
+
fs_removeFile as removeFile,
|
|
74
|
+
fs_removeSymlink as removeSymlink,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
3
78
|
declare const highlight: {
|
|
4
79
|
green: (text: string) => string;
|
|
5
80
|
red: (text: string) => string;
|
|
@@ -8,4 +83,4 @@ declare const highlight: {
|
|
|
8
83
|
reset: ansis.Ansis;
|
|
9
84
|
};
|
|
10
85
|
|
|
11
|
-
export { highlight };
|
|
86
|
+
export { fs, highlight, upsertUserRc };
|
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
|
+
import { readUser, writeUser, updateUser } from 'rc9';
|
|
2
|
+
import { lstatSync, mkdirSync, renameSync, copyFileSync, constants, promises, unlinkSync } from 'node:fs';
|
|
3
|
+
import { dirname } from 'node:path';
|
|
4
|
+
import consola from 'consola';
|
|
1
5
|
import { reset, bold, magenta, cyan, red, green } from 'ansis';
|
|
2
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
|
+
|
|
3
16
|
const highlight = {
|
|
4
17
|
green: (text) => {
|
|
5
18
|
return green(text);
|
|
@@ -16,4 +29,106 @@ const highlight = {
|
|
|
16
29
|
reset
|
|
17
30
|
};
|
|
18
31
|
|
|
19
|
-
|
|
32
|
+
function existsSync(path) {
|
|
33
|
+
try {
|
|
34
|
+
lstatSync(path);
|
|
35
|
+
return true;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
if (error instanceof Error && error.message.includes("ENOENT")) {
|
|
38
|
+
return false;
|
|
39
|
+
} else {
|
|
40
|
+
throw error;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
function isDirectory(path) {
|
|
45
|
+
return existsSync(path) && lstatSync(path).isDirectory() || path.match(/\/$|\\$/) !== null;
|
|
46
|
+
}
|
|
47
|
+
function ensureDir(dirPath) {
|
|
48
|
+
if (!isDirectory(dirPath)) {
|
|
49
|
+
dirPath = dirname(dirPath);
|
|
50
|
+
}
|
|
51
|
+
if (!existsSync(dirPath)) {
|
|
52
|
+
mkdirSync(dirPath, { recursive: true });
|
|
53
|
+
return true;
|
|
54
|
+
}
|
|
55
|
+
return false;
|
|
56
|
+
}
|
|
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`);
|
|
62
|
+
return false;
|
|
63
|
+
} else {
|
|
64
|
+
renameSync(targetPath, `${targetPath}.bak`);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
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);
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
return true;
|
|
80
|
+
}
|
|
81
|
+
async function createSymlink(sourcePath, targetPath, force = false) {
|
|
82
|
+
const isExist = existsSync(targetPath);
|
|
83
|
+
if (isExist) {
|
|
84
|
+
if (!force) {
|
|
85
|
+
consola.warn(`LINK: File already exists: ${highlight.info(targetPath)}, skip`);
|
|
86
|
+
return false;
|
|
87
|
+
} else {
|
|
88
|
+
renameSync(targetPath, `${targetPath}.bak`);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
try {
|
|
92
|
+
await promises.symlink(sourcePath, targetPath, "file");
|
|
93
|
+
if (isExist) {
|
|
94
|
+
removeFile(`${targetPath}.bak`);
|
|
95
|
+
}
|
|
96
|
+
} catch (error) {
|
|
97
|
+
if (isExist) {
|
|
98
|
+
renameSync(`${targetPath}.bak`, targetPath);
|
|
99
|
+
}
|
|
100
|
+
consola.error(error);
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
return true;
|
|
104
|
+
}
|
|
105
|
+
function removeFile(targetPath) {
|
|
106
|
+
if (!existsSync(targetPath)) {
|
|
107
|
+
consola.warn(`REMOVE: Target file not found: ${highlight.info(targetPath)}, skip`);
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
unlinkSync(targetPath);
|
|
111
|
+
return true;
|
|
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
|
+
}
|
|
122
|
+
|
|
123
|
+
const fs = {
|
|
124
|
+
__proto__: null,
|
|
125
|
+
copyFile: copyFile,
|
|
126
|
+
createSymlink: createSymlink,
|
|
127
|
+
ensureDir: ensureDir,
|
|
128
|
+
existsSync: existsSync,
|
|
129
|
+
isDirectory: isDirectory,
|
|
130
|
+
removeFile: removeFile,
|
|
131
|
+
removeSymlink: removeSymlink
|
|
132
|
+
};
|
|
133
|
+
|
|
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.
|
|
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",
|
|
@@ -23,13 +23,14 @@
|
|
|
23
23
|
"files": [
|
|
24
24
|
"dist"
|
|
25
25
|
],
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"ansis": "^4.1.0",
|
|
28
|
+
"consola": "^3.4.2",
|
|
29
|
+
"rc9": "^2.1.2"
|
|
30
|
+
},
|
|
26
31
|
"scripts": {
|
|
27
32
|
"build": "unbuild",
|
|
28
33
|
"dev": "unbuild --stub",
|
|
29
|
-
"prepublishOnly": "nr build",
|
|
30
34
|
"start": "tsx src/index.ts"
|
|
31
|
-
},
|
|
32
|
-
"dependencies": {
|
|
33
|
-
"ansis": "catalog:utils/prod"
|
|
34
35
|
}
|
|
35
|
-
}
|
|
36
|
+
}
|