starship-butler-utils 0.0.1-beta.8 → 0.0.1
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/chunk-CTAAG5j7.js +13 -0
- package/dist/{index.d.mts → index.d.ts} +15 -29
- package/dist/index.js +159 -0
- package/package.json +13 -9
- package/dist/index.mjs +0 -134
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __export = (all) => {
|
|
4
|
+
let target = {};
|
|
5
|
+
for (var name in all) __defProp(target, name, {
|
|
6
|
+
get: all[name],
|
|
7
|
+
enumerable: true
|
|
8
|
+
});
|
|
9
|
+
return target;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
//#endregion
|
|
13
|
+
export { __export };
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as ansis0 from "ansis";
|
|
2
|
+
|
|
3
|
+
//#region src/config.d.ts
|
|
2
4
|
|
|
3
5
|
/**
|
|
4
6
|
* Updates or creates a user rc file (rc file under home directory).
|
|
@@ -6,7 +8,9 @@ import * as ansis from 'ansis';
|
|
|
6
8
|
* @param config The rc to write.
|
|
7
9
|
*/
|
|
8
10
|
declare function upsertUserRc(name: string, config: any): void;
|
|
9
|
-
|
|
11
|
+
declare namespace fs_d_exports {
|
|
12
|
+
export { copyFile, createSymlink, ensureDir, existsSync, isDirectory, removeFile, removeSymlink };
|
|
13
|
+
}
|
|
10
14
|
/**
|
|
11
15
|
* Check if a file or directory exists. Does not dereference symlinks.
|
|
12
16
|
* @param path - The path to check
|
|
@@ -55,32 +59,14 @@ declare function removeFile(targetPath: string): boolean;
|
|
|
55
59
|
* @returns `true` if the symlink was removed, `false` otherwise
|
|
56
60
|
*/
|
|
57
61
|
declare function removeSymlink(targetPath: string): boolean;
|
|
58
|
-
|
|
59
|
-
|
|
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
|
-
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/highlight.d.ts
|
|
78
64
|
declare const highlight: {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
65
|
+
green: (text: string) => string;
|
|
66
|
+
red: (text: string) => string;
|
|
67
|
+
info: (text: string) => string;
|
|
68
|
+
important: (text: string) => string;
|
|
69
|
+
reset: ansis0.Ansis;
|
|
84
70
|
};
|
|
85
|
-
|
|
86
|
-
export { fs, highlight, upsertUserRc };
|
|
71
|
+
//#endregion
|
|
72
|
+
export { fs_d_exports as fs, highlight, upsertUserRc };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import { __export } from "./chunk-CTAAG5j7.js";
|
|
2
|
+
import { readUser, updateUser, writeUser } from "rc9";
|
|
3
|
+
import { constants, copyFileSync, lstatSync, mkdirSync, promises, renameSync, unlinkSync } from "node:fs";
|
|
4
|
+
import { dirname } from "node:path";
|
|
5
|
+
import consola from "consola";
|
|
6
|
+
import { bold, cyan, green, magenta, red, reset } from "ansis";
|
|
7
|
+
|
|
8
|
+
//#region src/config.ts
|
|
9
|
+
/**
|
|
10
|
+
* Updates or creates a user rc file (rc file under home directory).
|
|
11
|
+
* @param name The name of the rc file.
|
|
12
|
+
* @param config The rc to write.
|
|
13
|
+
*/
|
|
14
|
+
function upsertUserRc(name, config) {
|
|
15
|
+
if (!readUser(name)) writeUser(config, name);
|
|
16
|
+
else updateUser(config, name);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
//#region src/highlight.ts
|
|
21
|
+
const highlight = {
|
|
22
|
+
green: (text) => {
|
|
23
|
+
return green(text);
|
|
24
|
+
},
|
|
25
|
+
red: (text) => {
|
|
26
|
+
return red(text);
|
|
27
|
+
},
|
|
28
|
+
info: (text) => {
|
|
29
|
+
return cyan(text);
|
|
30
|
+
},
|
|
31
|
+
important: (text) => {
|
|
32
|
+
return bold(magenta(text));
|
|
33
|
+
},
|
|
34
|
+
reset
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/fs.ts
|
|
39
|
+
var fs_exports = /* @__PURE__ */ __export({
|
|
40
|
+
copyFile: () => copyFile,
|
|
41
|
+
createSymlink: () => createSymlink,
|
|
42
|
+
ensureDir: () => ensureDir,
|
|
43
|
+
existsSync: () => existsSync,
|
|
44
|
+
isDirectory: () => isDirectory,
|
|
45
|
+
removeFile: () => removeFile,
|
|
46
|
+
removeSymlink: () => removeSymlink
|
|
47
|
+
});
|
|
48
|
+
/**
|
|
49
|
+
* Check if a file or directory exists. Does not dereference symlinks.
|
|
50
|
+
* @param path - The path to check
|
|
51
|
+
* @returns `true` if the file or directory exists, `false` otherwise
|
|
52
|
+
*/
|
|
53
|
+
function existsSync(path) {
|
|
54
|
+
try {
|
|
55
|
+
lstatSync(path);
|
|
56
|
+
return true;
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (error instanceof Error && error.message.includes("ENOENT")) return false;
|
|
59
|
+
else throw error;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Check if a path is a directory. If path exists, we check the stats, if not, we check if the path ends with `/` or `\`.
|
|
64
|
+
* @param path - The path to check
|
|
65
|
+
* @returns `true` if the path is a directory, `false` otherwise
|
|
66
|
+
*/
|
|
67
|
+
function isDirectory(path) {
|
|
68
|
+
return existsSync(path) && lstatSync(path).isDirectory() || path.match(/\/$|\\$/) !== null;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Ensure a directory exists.
|
|
72
|
+
* If the path is not a directory, we will try to create it.
|
|
73
|
+
* If the path is a file, we will create the parent directory.
|
|
74
|
+
* @param dirPath - The path to ensure
|
|
75
|
+
* @returns `true` if the directory is created, `false` otherwise
|
|
76
|
+
*/
|
|
77
|
+
function ensureDir(dirPath) {
|
|
78
|
+
if (!isDirectory(dirPath)) dirPath = dirname(dirPath);
|
|
79
|
+
if (!existsSync(dirPath)) {
|
|
80
|
+
mkdirSync(dirPath, { recursive: true });
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Copy a file.
|
|
87
|
+
* @param sourcePath The path to the source file
|
|
88
|
+
* @param targetPath The path to the target file
|
|
89
|
+
* @param force Whether to force overwrite the target file
|
|
90
|
+
* @returns `true` if the file was copied, `false` otherwise
|
|
91
|
+
*/
|
|
92
|
+
function copyFile(sourcePath, targetPath, force = false) {
|
|
93
|
+
const isExist = existsSync(targetPath);
|
|
94
|
+
if (isExist) if (!force) {
|
|
95
|
+
consola.warn(`COPY: File already exists: ${highlight.info(targetPath)}, skip`);
|
|
96
|
+
return false;
|
|
97
|
+
} else renameSync(targetPath, `${targetPath}.bak`);
|
|
98
|
+
try {
|
|
99
|
+
copyFileSync(sourcePath, targetPath, force ? constants.COPYFILE_FICLONE : constants.COPYFILE_EXCL);
|
|
100
|
+
if (isExist) removeFile(`${targetPath}.bak`);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
if (isExist) renameSync(`${targetPath}.bak`, targetPath);
|
|
103
|
+
consola.error(error);
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Create a symbolic link.
|
|
110
|
+
* @param sourcePath The path to the source file
|
|
111
|
+
* @param targetPath The path to the target symlink
|
|
112
|
+
* @param force Whether to force overwrite the target symlink
|
|
113
|
+
* @returns `true` if the symlink was created, `false` otherwise
|
|
114
|
+
*/
|
|
115
|
+
async function createSymlink(sourcePath, targetPath, force = false) {
|
|
116
|
+
const isExist = existsSync(targetPath);
|
|
117
|
+
if (isExist) if (!force) {
|
|
118
|
+
consola.warn(`LINK: File already exists: ${highlight.info(targetPath)}, skip`);
|
|
119
|
+
return false;
|
|
120
|
+
} else renameSync(targetPath, `${targetPath}.bak`);
|
|
121
|
+
try {
|
|
122
|
+
await promises.symlink(sourcePath, targetPath, "file");
|
|
123
|
+
if (isExist) removeFile(`${targetPath}.bak`);
|
|
124
|
+
} catch (error) {
|
|
125
|
+
if (isExist) renameSync(`${targetPath}.bak`, targetPath);
|
|
126
|
+
consola.error(error);
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
return true;
|
|
130
|
+
}
|
|
131
|
+
/**
|
|
132
|
+
* Remove a file.
|
|
133
|
+
* @param targetPath The path to the target file
|
|
134
|
+
* @returns `true` if the file was removed, `false` otherwise
|
|
135
|
+
*/
|
|
136
|
+
function removeFile(targetPath) {
|
|
137
|
+
if (!existsSync(targetPath)) {
|
|
138
|
+
consola.warn(`REMOVE: Target file not found: ${highlight.info(targetPath)}, skip`);
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
unlinkSync(targetPath);
|
|
142
|
+
return true;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Remove a symbolic link.
|
|
146
|
+
* @param targetPath The path to the target symlink
|
|
147
|
+
* @returns `true` if the symlink was removed, `false` otherwise
|
|
148
|
+
*/
|
|
149
|
+
function removeSymlink(targetPath) {
|
|
150
|
+
if (!lstatSync(targetPath).isSymbolicLink()) {
|
|
151
|
+
consola.warn(`REMOVE: Target file is not a symlink: ${highlight.info(targetPath)}, skip`);
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
removeFile(targetPath);
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
//#endregion
|
|
159
|
+
export { fs_exports as 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
|
|
4
|
+
"version": "0.0.1",
|
|
5
5
|
"description": "Your best starship butler.",
|
|
6
6
|
"author": "Lumirelle <shabbyacc@outlook.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -12,25 +12,29 @@
|
|
|
12
12
|
"url": "git+https://github.com/lumirelle/starship-butler.git"
|
|
13
13
|
},
|
|
14
14
|
"bugs": "https://github.com/lumirelle/starship-butler/issues",
|
|
15
|
-
"keywords": [
|
|
15
|
+
"keywords": [
|
|
16
|
+
"config-butler",
|
|
17
|
+
"config-manager",
|
|
18
|
+
"starship-butler"
|
|
19
|
+
],
|
|
16
20
|
"sideEffects": false,
|
|
17
21
|
"exports": {
|
|
18
|
-
".": "./dist/index.
|
|
22
|
+
".": "./dist/index.js"
|
|
19
23
|
},
|
|
20
|
-
"main": "./dist/index.
|
|
21
|
-
"module": "./dist/index.
|
|
22
|
-
"types": "./dist/index.d.
|
|
24
|
+
"main": "./dist/index.js",
|
|
25
|
+
"module": "./dist/index.js",
|
|
26
|
+
"types": "./dist/index.d.ts",
|
|
23
27
|
"files": [
|
|
24
28
|
"dist"
|
|
25
29
|
],
|
|
26
30
|
"dependencies": {
|
|
27
|
-
"ansis": "^4.
|
|
31
|
+
"ansis": "^4.2.0",
|
|
28
32
|
"consola": "^3.4.2",
|
|
29
33
|
"rc9": "^2.1.2"
|
|
30
34
|
},
|
|
31
35
|
"scripts": {
|
|
32
|
-
"build": "
|
|
33
|
-
"dev": "
|
|
36
|
+
"build": "tsdown",
|
|
37
|
+
"dev": "tsdown --watch",
|
|
34
38
|
"start": "tsx src/index.ts"
|
|
35
39
|
}
|
|
36
40
|
}
|
package/dist/index.mjs
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
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';
|
|
5
|
-
import { reset, bold, magenta, cyan, red, green } from 'ansis';
|
|
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
|
-
|
|
16
|
-
const highlight = {
|
|
17
|
-
green: (text) => {
|
|
18
|
-
return green(text);
|
|
19
|
-
},
|
|
20
|
-
red: (text) => {
|
|
21
|
-
return red(text);
|
|
22
|
-
},
|
|
23
|
-
info: (text) => {
|
|
24
|
-
return cyan(text);
|
|
25
|
-
},
|
|
26
|
-
important: (text) => {
|
|
27
|
-
return bold(magenta(text));
|
|
28
|
-
},
|
|
29
|
-
reset
|
|
30
|
-
};
|
|
31
|
-
|
|
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 };
|