@putout/operator-filesystem 6.1.0 → 6.3.0
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/README.md +45 -0
- package/lib/filesystem.js +72 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,6 +48,37 @@ const {
|
|
|
48
48
|
} = operator;
|
|
49
49
|
|
|
50
50
|
const [dirPath] = findFile(ast, '/bin');
|
|
51
|
+
|
|
52
|
+
readDirectory(dirPath);
|
|
53
|
+
// returns list of files
|
|
54
|
+
[];
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### `createNestedDirectory(path: FilePath|DirectoryPath, name: string): DirectoryPath`
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
const {operator} = require('putout');
|
|
61
|
+
const {
|
|
62
|
+
createDirectory,
|
|
63
|
+
findFile,
|
|
64
|
+
createNestedDirectory,
|
|
65
|
+
} = operator;
|
|
66
|
+
|
|
67
|
+
const newDirectoryPath = createNestedDirectory(ast, '/hello/world');
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### `readDirectory(directoryPath: DirectoryPath): (FilePath | DirectoryPath)[]`
|
|
71
|
+
|
|
72
|
+
```js
|
|
73
|
+
const {operator} = require('putout');
|
|
74
|
+
const {
|
|
75
|
+
finedFiles,
|
|
76
|
+
findFile,
|
|
77
|
+
readDirectory,
|
|
78
|
+
} = operator;
|
|
79
|
+
|
|
80
|
+
const [dirPath] = findFile(ast, '/bin');
|
|
81
|
+
|
|
51
82
|
readDirectory(dirPath);
|
|
52
83
|
// returns list of files
|
|
53
84
|
[];
|
|
@@ -109,6 +140,20 @@ dirPath === getParentDirectory(newDirectoryPath);
|
|
|
109
140
|
true;
|
|
110
141
|
```
|
|
111
142
|
|
|
143
|
+
### `getRootDirectory(path: FilePath | DirectoryPath): DrectoryPath`
|
|
144
|
+
|
|
145
|
+
```js
|
|
146
|
+
const {operator} = require('putout');
|
|
147
|
+
const {
|
|
148
|
+
findFile,
|
|
149
|
+
getRootDirectory,
|
|
150
|
+
} = operator;
|
|
151
|
+
|
|
152
|
+
const [filePath] = findFile(ast, 'hello');
|
|
153
|
+
|
|
154
|
+
getRootDirectory(dirPath);
|
|
155
|
+
```
|
|
156
|
+
|
|
112
157
|
### `getFilename(path: FilePath | DirectoryPath): string`
|
|
113
158
|
|
|
114
159
|
```js
|
package/lib/filesystem.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const {
|
|
3
|
+
const {
|
|
4
|
+
join,
|
|
5
|
+
basename,
|
|
6
|
+
dirname,
|
|
7
|
+
} = require('node:path');
|
|
8
|
+
|
|
4
9
|
const {types} = require('@putout/babel');
|
|
5
10
|
const tryCatch = require('try-catch');
|
|
6
11
|
|
|
@@ -51,7 +56,9 @@ const getRegExp = (wildcard) => {
|
|
|
51
56
|
return RegExp(`^${escaped}$`);
|
|
52
57
|
};
|
|
53
58
|
|
|
54
|
-
module.exports.getParentDirectory =
|
|
59
|
+
module.exports.getParentDirectory = getParentDirectory;
|
|
60
|
+
|
|
61
|
+
function getParentDirectory(filePath) {
|
|
55
62
|
if (!filePath.parentPath)
|
|
56
63
|
return null;
|
|
57
64
|
|
|
@@ -61,7 +68,7 @@ module.exports.getParentDirectory = (filePath) => {
|
|
|
61
68
|
return null;
|
|
62
69
|
|
|
63
70
|
return parentPath;
|
|
64
|
-
}
|
|
71
|
+
}
|
|
65
72
|
|
|
66
73
|
module.exports.findFile = findFile;
|
|
67
74
|
|
|
@@ -283,7 +290,9 @@ module.exports.readDirectory = (dirPath) => {
|
|
|
283
290
|
return getFiles(dirPath).get('value.elements');
|
|
284
291
|
};
|
|
285
292
|
|
|
286
|
-
module.exports.createDirectory =
|
|
293
|
+
module.exports.createDirectory = createDirectory;
|
|
294
|
+
|
|
295
|
+
function createDirectory(dirPath, name) {
|
|
287
296
|
const dirPathFiles = getFiles(dirPath);
|
|
288
297
|
const parentFilename = getFilename(dirPath);
|
|
289
298
|
const filename = join(parentFilename, name);
|
|
@@ -301,7 +310,7 @@ module.exports.createDirectory = (dirPath, name) => {
|
|
|
301
310
|
maybeFS.createDirectory(filename);
|
|
302
311
|
|
|
303
312
|
return dirPathFiles.get('value.elements').at(-1);
|
|
304
|
-
}
|
|
313
|
+
}
|
|
305
314
|
|
|
306
315
|
module.exports.readFileContent = (filePath) => {
|
|
307
316
|
const fileType = getFileType(filePath);
|
|
@@ -347,7 +356,64 @@ function writeFileContent(filePath, content) {
|
|
|
347
356
|
filePath.node.properties.push(property);
|
|
348
357
|
}
|
|
349
358
|
|
|
350
|
-
module.exports.
|
|
359
|
+
module.exports.createNestedDirectory = (path, name) => {
|
|
360
|
+
const rootPath = getRootDirectory(path);
|
|
361
|
+
const dir = dirname(name);
|
|
362
|
+
|
|
363
|
+
if (dir === getFilename(path))
|
|
364
|
+
return createDirectory(path, basename(name));
|
|
365
|
+
|
|
366
|
+
let currentDir = name;
|
|
367
|
+
|
|
368
|
+
const rootDir = getFilename(rootPath);
|
|
369
|
+
const directories = [];
|
|
370
|
+
let prevDir = currentDir;
|
|
371
|
+
|
|
372
|
+
while (currentDir !== rootDir) {
|
|
373
|
+
directories.unshift(currentDir);
|
|
374
|
+
prevDir = currentDir;
|
|
375
|
+
currentDir = dirname(currentDir);
|
|
376
|
+
|
|
377
|
+
if (currentDir === prevDir) {
|
|
378
|
+
currentDir = rootDir;
|
|
379
|
+
|
|
380
|
+
for (const [i, dir] of directories.entries()) {
|
|
381
|
+
directories[i] = join(rootDir, dir);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
directories.shift();
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
let lastDirectoryPath = findFile(rootPath, directories).at(-1) || rootPath;
|
|
390
|
+
const lastDirectoryName = getFilename(lastDirectoryPath);
|
|
391
|
+
|
|
392
|
+
const n = directories.length;
|
|
393
|
+
|
|
394
|
+
for (let i = directories.indexOf(lastDirectoryName) + 1; i < n; i++) {
|
|
395
|
+
const name = basename(directories[i]);
|
|
396
|
+
lastDirectoryPath = createDirectory(lastDirectoryPath, name);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
return lastDirectoryPath;
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
module.exports.getRootDirectory = getRootDirectory;
|
|
403
|
+
function getRootDirectory(path) {
|
|
404
|
+
let currentDirPath = getParentDirectory(path);
|
|
405
|
+
|
|
406
|
+
if (!currentDirPath)
|
|
407
|
+
return path;
|
|
408
|
+
|
|
409
|
+
let prevPath = currentDirPath;
|
|
410
|
+
|
|
411
|
+
while (currentDirPath = getParentDirectory(currentDirPath)) {
|
|
412
|
+
prevPath = currentDirPath;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
return prevPath;
|
|
416
|
+
}module.exports.init = maybeFS.init;
|
|
351
417
|
module.exports.deinit = maybeFS.deinit;
|
|
352
418
|
|
|
353
419
|
module.exports.pause = maybeFS.pause;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@putout/operator-filesystem",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.3.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"author": "coderaiser <mnemonic.enemy@gmail.com> (https://github.com/coderaiser)",
|
|
6
6
|
"description": "🐊Putout operator adds ability to filesystem referenced variables that was not defined",
|