codify-plugin-lib 1.0.182-beta20 → 1.0.182-beta22
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/utils/file-utils.d.ts +5 -4
- package/dist/utils/file-utils.js +53 -48
- package/package.json +2 -2
- package/src/utils/file-utils.ts +57 -51
|
@@ -9,14 +9,15 @@ export declare class FileUtils {
|
|
|
9
9
|
* @param prepend - Whether to prepend the path to the existing PATH variable.
|
|
10
10
|
*/
|
|
11
11
|
static addPathToShellRc(value: string, prepend: boolean): Promise<void>;
|
|
12
|
+
static removeFromFile(filePath: string, search: string): Promise<void>;
|
|
13
|
+
static removeLineFromFile(filePath: string, search: RegExp | string): Promise<void>;
|
|
14
|
+
static removeLineFromShellRc(search: RegExp | string): Promise<void>;
|
|
15
|
+
static removeAllLinesFromShellRc(searches: Array<RegExp | string>): Promise<void>;
|
|
16
|
+
static appendToFileWithSpacing(file: string, textToInsert: string): string;
|
|
12
17
|
static dirExists(path: string): Promise<boolean>;
|
|
13
18
|
static fileExists(path: string): Promise<boolean>;
|
|
14
19
|
static exists(path: string): Promise<boolean>;
|
|
15
20
|
static checkDirExistsOrThrowIfFile(path: string): Promise<boolean>;
|
|
16
21
|
static createDirIfNotExists(path: string): Promise<void>;
|
|
17
|
-
static removeFromFile(filePath: string, search: string): Promise<void>;
|
|
18
|
-
static removeLineFromFile(filePath: string, search: RegExp | string): Promise<void>;
|
|
19
|
-
static removeLineFromPrimaryShellRc(search: RegExp | string): Promise<void>;
|
|
20
|
-
static appendToFileWithSpacing(file: string, textToInsert: string): string;
|
|
21
22
|
private static calculateEndingNewLines;
|
|
22
23
|
}
|
package/dist/utils/file-utils.js
CHANGED
|
@@ -57,53 +57,6 @@ ${lines.join('\n')}`);
|
|
|
57
57
|
}
|
|
58
58
|
await fs.appendFile(shellRc, `\nexport PATH=${value}:$PATH;`, { encoding: 'utf8' });
|
|
59
59
|
}
|
|
60
|
-
static async dirExists(path) {
|
|
61
|
-
let stat;
|
|
62
|
-
try {
|
|
63
|
-
stat = await fs.stat(path);
|
|
64
|
-
return stat.isDirectory();
|
|
65
|
-
}
|
|
66
|
-
catch {
|
|
67
|
-
return false;
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
static async fileExists(path) {
|
|
71
|
-
let stat;
|
|
72
|
-
try {
|
|
73
|
-
stat = await fs.stat(path);
|
|
74
|
-
return stat.isFile();
|
|
75
|
-
}
|
|
76
|
-
catch {
|
|
77
|
-
return false;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
static async exists(path) {
|
|
81
|
-
try {
|
|
82
|
-
await fs.stat(path);
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
catch {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
static async checkDirExistsOrThrowIfFile(path) {
|
|
90
|
-
let stat;
|
|
91
|
-
try {
|
|
92
|
-
stat = await fs.stat(path);
|
|
93
|
-
}
|
|
94
|
-
catch {
|
|
95
|
-
return false;
|
|
96
|
-
}
|
|
97
|
-
if (stat.isDirectory()) {
|
|
98
|
-
return true;
|
|
99
|
-
}
|
|
100
|
-
throw new Error(`Directory ${path} already exists and is a file`);
|
|
101
|
-
}
|
|
102
|
-
static async createDirIfNotExists(path) {
|
|
103
|
-
if (!fsSync.existsSync(path)) {
|
|
104
|
-
await fs.mkdir(path, { recursive: true });
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
60
|
static async removeFromFile(filePath, search) {
|
|
108
61
|
const contents = await fs.readFile(filePath, 'utf8');
|
|
109
62
|
const newContents = contents.replaceAll(search, '');
|
|
@@ -140,9 +93,14 @@ ${lines.join('\n')}`);
|
|
|
140
93
|
await fs.writeFile(filePath, lines.join('\n'));
|
|
141
94
|
console.log(`Removed line: ${search} from ${filePath}`);
|
|
142
95
|
}
|
|
143
|
-
static async
|
|
96
|
+
static async removeLineFromShellRc(search) {
|
|
144
97
|
return FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
|
|
145
98
|
}
|
|
99
|
+
static async removeAllLinesFromShellRc(searches) {
|
|
100
|
+
for (const search of searches) {
|
|
101
|
+
await FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
146
104
|
// Append the string to the end of a file ensuring at least 1 lines of space between.
|
|
147
105
|
// Ex result:
|
|
148
106
|
// something something;
|
|
@@ -159,6 +117,53 @@ ${lines.join('\n')}`);
|
|
|
159
117
|
: Math.max(0, 2 - endingNewLines);
|
|
160
118
|
return lines.join('\n') + '\n'.repeat(numNewLines) + textToInsert;
|
|
161
119
|
}
|
|
120
|
+
static async dirExists(path) {
|
|
121
|
+
let stat;
|
|
122
|
+
try {
|
|
123
|
+
stat = await fs.stat(path);
|
|
124
|
+
return stat.isDirectory();
|
|
125
|
+
}
|
|
126
|
+
catch {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
static async fileExists(path) {
|
|
131
|
+
let stat;
|
|
132
|
+
try {
|
|
133
|
+
stat = await fs.stat(path);
|
|
134
|
+
return stat.isFile();
|
|
135
|
+
}
|
|
136
|
+
catch {
|
|
137
|
+
return false;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
static async exists(path) {
|
|
141
|
+
try {
|
|
142
|
+
await fs.stat(path);
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
return false;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
static async checkDirExistsOrThrowIfFile(path) {
|
|
150
|
+
let stat;
|
|
151
|
+
try {
|
|
152
|
+
stat = await fs.stat(path);
|
|
153
|
+
}
|
|
154
|
+
catch {
|
|
155
|
+
return false;
|
|
156
|
+
}
|
|
157
|
+
if (stat.isDirectory()) {
|
|
158
|
+
return true;
|
|
159
|
+
}
|
|
160
|
+
throw new Error(`Directory ${path} already exists and is a file`);
|
|
161
|
+
}
|
|
162
|
+
static async createDirIfNotExists(path) {
|
|
163
|
+
if (!fsSync.existsSync(path)) {
|
|
164
|
+
await fs.mkdir(path, { recursive: true });
|
|
165
|
+
}
|
|
166
|
+
}
|
|
162
167
|
// This is overly complicated but it can be used to insert into any
|
|
163
168
|
// position in the future
|
|
164
169
|
static calculateEndingNewLines(lines) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "codify-plugin-lib",
|
|
3
|
-
"version": "1.0.182-
|
|
3
|
+
"version": "1.0.182-beta22",
|
|
4
4
|
"description": "Library plugin library",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"ajv": "^8.12.0",
|
|
23
23
|
"ajv-formats": "^2.1.1",
|
|
24
24
|
"clean-deep": "^3.4.0",
|
|
25
|
-
"codify-schemas": "1.0.86-
|
|
25
|
+
"codify-schemas": "1.0.86-beta7",
|
|
26
26
|
"lodash.isequal": "^4.5.0",
|
|
27
27
|
"nanoid": "^5.0.9",
|
|
28
28
|
"strip-ansi": "^7.1.0",
|
package/src/utils/file-utils.ts
CHANGED
|
@@ -77,56 +77,6 @@ ${lines.join('\n')}`)
|
|
|
77
77
|
await fs.appendFile(shellRc, `\nexport PATH=${value}:$PATH;`, { encoding: 'utf8' });
|
|
78
78
|
}
|
|
79
79
|
|
|
80
|
-
static async dirExists(path: string): Promise<boolean> {
|
|
81
|
-
let stat;
|
|
82
|
-
try {
|
|
83
|
-
stat = await fs.stat(path);
|
|
84
|
-
return stat.isDirectory();
|
|
85
|
-
} catch {
|
|
86
|
-
return false;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
static async fileExists(path: string): Promise<boolean> {
|
|
91
|
-
let stat;
|
|
92
|
-
try {
|
|
93
|
-
stat = await fs.stat(path);
|
|
94
|
-
return stat.isFile();
|
|
95
|
-
} catch {
|
|
96
|
-
return false;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
static async exists(path: string): Promise<boolean> {
|
|
101
|
-
try {
|
|
102
|
-
await fs.stat(path);
|
|
103
|
-
return true;
|
|
104
|
-
} catch {
|
|
105
|
-
return false;
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
static async checkDirExistsOrThrowIfFile(path: string): Promise<boolean> {
|
|
110
|
-
let stat;
|
|
111
|
-
try {
|
|
112
|
-
stat = await fs.stat(path);
|
|
113
|
-
} catch {
|
|
114
|
-
return false;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
if (stat.isDirectory()) {
|
|
118
|
-
return true;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
throw new Error(`Directory ${path} already exists and is a file`);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
static async createDirIfNotExists(path: string): Promise<void> {
|
|
125
|
-
if (!fsSync.existsSync(path)) {
|
|
126
|
-
await fs.mkdir(path, { recursive: true });
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
80
|
static async removeFromFile(filePath: string, search: string): Promise<void> {
|
|
131
81
|
const contents = await fs.readFile(filePath, 'utf8');
|
|
132
82
|
const newContents = contents.replaceAll(search, '');
|
|
@@ -177,10 +127,16 @@ ${lines.join('\n')}`)
|
|
|
177
127
|
console.log(`Removed line: ${search} from ${filePath}`)
|
|
178
128
|
}
|
|
179
129
|
|
|
180
|
-
static async
|
|
130
|
+
static async removeLineFromShellRc(search: RegExp | string): Promise<void> {
|
|
181
131
|
return FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
|
|
182
132
|
}
|
|
183
133
|
|
|
134
|
+
static async removeAllLinesFromShellRc(searches: Array<RegExp | string>): Promise<void> {
|
|
135
|
+
for (const search of searches) {
|
|
136
|
+
await FileUtils.removeLineFromFile(Utils.getPrimaryShellRc(), search);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
184
140
|
// Append the string to the end of a file ensuring at least 1 lines of space between.
|
|
185
141
|
// Ex result:
|
|
186
142
|
// something something;
|
|
@@ -199,6 +155,56 @@ ${lines.join('\n')}`)
|
|
|
199
155
|
return lines.join('\n') + '\n'.repeat(numNewLines) + textToInsert
|
|
200
156
|
}
|
|
201
157
|
|
|
158
|
+
static async dirExists(path: string): Promise<boolean> {
|
|
159
|
+
let stat;
|
|
160
|
+
try {
|
|
161
|
+
stat = await fs.stat(path);
|
|
162
|
+
return stat.isDirectory();
|
|
163
|
+
} catch {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
static async fileExists(path: string): Promise<boolean> {
|
|
169
|
+
let stat;
|
|
170
|
+
try {
|
|
171
|
+
stat = await fs.stat(path);
|
|
172
|
+
return stat.isFile();
|
|
173
|
+
} catch {
|
|
174
|
+
return false;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
static async exists(path: string): Promise<boolean> {
|
|
179
|
+
try {
|
|
180
|
+
await fs.stat(path);
|
|
181
|
+
return true;
|
|
182
|
+
} catch {
|
|
183
|
+
return false;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
static async checkDirExistsOrThrowIfFile(path: string): Promise<boolean> {
|
|
188
|
+
let stat;
|
|
189
|
+
try {
|
|
190
|
+
stat = await fs.stat(path);
|
|
191
|
+
} catch {
|
|
192
|
+
return false;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (stat.isDirectory()) {
|
|
196
|
+
return true;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
throw new Error(`Directory ${path} already exists and is a file`);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
static async createDirIfNotExists(path: string): Promise<void> {
|
|
203
|
+
if (!fsSync.existsSync(path)) {
|
|
204
|
+
await fs.mkdir(path, { recursive: true });
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
202
208
|
// This is overly complicated but it can be used to insert into any
|
|
203
209
|
// position in the future
|
|
204
210
|
private static calculateEndingNewLines(lines: string[]): number {
|