@putout/operator-filesystem 6.0.3 → 6.2.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 CHANGED
@@ -37,6 +37,52 @@ const [dirPath] = findFile(ast, 'hello');
37
37
  const newDirectoryPath = createDirectory(dirPath, 'world');
38
38
  ```
39
39
 
40
+ ### `readDirectory(directoryPath: DirectoryPath): (FilePath | DirectoryPath)[]`
41
+
42
+ ```js
43
+ const {operator} = require('putout');
44
+ const {
45
+ finedFiles,
46
+ findFile,
47
+ readDirectory,
48
+ } = operator;
49
+
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
+ } = operator;
65
+
66
+ const newDirectoryPath = createDirectory(ast, '/hello/world');
67
+ ```
68
+
69
+ ### `readDirectory(directoryPath: DirectoryPath): (FilePath | DirectoryPath)[]`
70
+
71
+ ```js
72
+ const {operator} = require('putout');
73
+ const {
74
+ finedFiles,
75
+ findFile,
76
+ readDirectory,
77
+ } = operator;
78
+
79
+ const [dirPath] = findFile(ast, '/bin');
80
+
81
+ readDirectory(dirPath);
82
+ // returns list of files
83
+ [];
84
+ ```
85
+
40
86
  ### `findFile(directoryPath: DirectoryPath, name: string | string[], exclude?: string[]): (FilePath | DirectoryPath)[]`
41
87
 
42
88
  ```js
package/lib/filesystem.js CHANGED
@@ -1,8 +1,13 @@
1
1
  'use strict';
2
2
 
3
- const {join, basename} = require('node:path');
4
- const tryCatch = require('try-catch');
3
+ const {
4
+ join,
5
+ basename,
6
+ dirname,
7
+ } = require('node:path');
8
+
5
9
  const {types} = require('@putout/babel');
10
+ const tryCatch = require('try-catch');
6
11
 
7
12
  const {
8
13
  setLiteralValue,
@@ -12,6 +17,14 @@ const {
12
17
 
13
18
  const maybeFS = require('./maybe-fs');
14
19
 
20
+ const {
21
+ arrayExpression,
22
+ stringLiteral,
23
+ objectProperty,
24
+ isProgram,
25
+ objectExpression,
26
+ } = types;
27
+
15
28
  const isString = (a) => typeof a === 'string';
16
29
  const {isArray} = Array;
17
30
  const maybeArray = (a) => isArray(a) ? a : [a];
@@ -34,14 +47,6 @@ const fromBase64 = (content) => {
34
47
  return content;
35
48
  };
36
49
 
37
- const {
38
- ObjectExpression,
39
- ArrayExpression,
40
- StringLiteral,
41
- ObjectProperty,
42
- isProgram,
43
- } = types;
44
-
45
50
  const getRegExp = (wildcard) => {
46
51
  const escaped = wildcard
47
52
  .replace(/\./g, '\\.')
@@ -51,7 +56,9 @@ const getRegExp = (wildcard) => {
51
56
  return RegExp(`^${escaped}$`);
52
57
  };
53
58
 
54
- module.exports.getParentDirectory = (filePath) => {
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
 
@@ -169,7 +176,7 @@ module.exports.moveFile = (filePath, dirPath) => {
169
176
 
170
177
  const dirname = getFilename(dirPath);
171
178
  const filename = getFilename(filePath);
172
- const dirPathFiles = getProperty(dirPath, 'files');
179
+ const dirPathFiles = getFiles(dirPath);
173
180
  const filenamePath = getProperty(filePath, 'filename');
174
181
 
175
182
  const basename = filename
@@ -198,7 +205,7 @@ module.exports.copyFile = (filePath, dirPath) => {
198
205
  const newFilename = join(dirname, basename);
199
206
  const [hasContent, content] = getFileContent(filePath);
200
207
 
201
- const copiedFile = ObjectExpression([
208
+ const copiedFile = objectExpression([
202
209
  createTypeProperty('file'),
203
210
  createFilenameProperty(newFilename),
204
211
  hasContent && createContentProperty(content),
@@ -230,19 +237,19 @@ function maybeRemoveFile(dirPath, filename) {
230
237
  fileToOverwrite.remove();
231
238
  }
232
239
 
233
- const createTypeProperty = (type) => ObjectProperty(StringLiteral('type'), StringLiteral(type));
240
+ const createTypeProperty = (type) => objectProperty(stringLiteral('type'), stringLiteral(type));
234
241
 
235
242
  module.exports.createTypeProperty = createTypeProperty;
236
243
 
237
- const createFilesProperty = (files) => ObjectProperty(StringLiteral('files'), ArrayExpression(files));
244
+ const createFilesProperty = (files) => objectProperty(stringLiteral('files'), arrayExpression(files));
238
245
 
239
246
  module.exports.createFilesProperty = createFilesProperty;
240
247
 
241
- const createFilenameProperty = (filename) => ObjectProperty(StringLiteral('filename'), StringLiteral(filename));
248
+ const createFilenameProperty = (filename) => objectProperty(stringLiteral('filename'), stringLiteral(filename));
242
249
 
243
250
  module.exports.createFilenameProperty = createFilenameProperty;
244
251
 
245
- const createContentProperty = (content) => ObjectProperty(StringLiteral('content'), StringLiteral(content));
252
+ const createContentProperty = (content) => objectProperty(stringLiteral('content'), stringLiteral(content));
246
253
 
247
254
  module.exports.createContentProperty = createContentProperty;
248
255
 
@@ -262,7 +269,7 @@ module.exports.createFile = (dirPath, name, content) => {
262
269
  content && createContentProperty(content),
263
270
  ].filter(Boolean);
264
271
 
265
- dirPathFiles.node.value.elements.push(ObjectExpression(properties));
272
+ dirPathFiles.node.value.elements.push(objectExpression(properties));
266
273
 
267
274
  const filePath = dirPathFiles.get('value.elements').at(-1);
268
275
 
@@ -274,7 +281,18 @@ module.exports.createFile = (dirPath, name, content) => {
274
281
 
275
282
  const getFiles = (dirPath) => getProperty(dirPath, 'files');
276
283
 
277
- module.exports.createDirectory = (dirPath, name) => {
284
+ module.exports.readDirectory = (dirPath) => {
285
+ const fileType = getFileType(dirPath);
286
+
287
+ if (fileType !== 'directory')
288
+ return [];
289
+
290
+ return getFiles(dirPath).get('value.elements');
291
+ };
292
+
293
+ module.exports.createDirectory = createDirectory;
294
+
295
+ function createDirectory(dirPath, name) {
278
296
  const dirPathFiles = getFiles(dirPath);
279
297
  const parentFilename = getFilename(dirPath);
280
298
  const filename = join(parentFilename, name);
@@ -283,7 +301,7 @@ module.exports.createDirectory = (dirPath, name) => {
283
301
  const filesProperty = createFilesProperty([]);
284
302
  const filenameProperty = createFilenameProperty(filename);
285
303
 
286
- dirPathFiles.node.value.elements.push(ObjectExpression([
304
+ dirPathFiles.node.value.elements.push(objectExpression([
287
305
  typeProperty,
288
306
  filenameProperty,
289
307
  filesProperty,
@@ -292,7 +310,7 @@ module.exports.createDirectory = (dirPath, name) => {
292
310
  maybeFS.createDirectory(filename);
293
311
 
294
312
  return dirPathFiles.get('value.elements').at(-1);
295
- };
313
+ }
296
314
 
297
315
  module.exports.readFileContent = (filePath) => {
298
316
  const fileType = getFileType(filePath);
@@ -338,6 +356,64 @@ function writeFileContent(filePath, content) {
338
356
  filePath.node.properties.push(property);
339
357
  }
340
358
 
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
+ function getRootDirectory(path) {
403
+ let currentDirPath = getParentDirectory(path);
404
+
405
+ if (!currentDirPath)
406
+ return path;
407
+
408
+ let prevPath = path;
409
+
410
+ while (currentDirPath = getParentDirectory(currentDirPath)) {
411
+ prevPath = currentDirPath;
412
+ }
413
+
414
+ return prevPath;
415
+ }
416
+
341
417
  module.exports.init = maybeFS.init;
342
418
  module.exports.deinit = maybeFS.deinit;
343
419
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "6.0.3",
3
+ "version": "6.2.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",
@@ -39,12 +39,11 @@
39
39
  "@putout/engine-parser": "^12.0.0",
40
40
  "@putout/eslint-flat": "^2.0.0",
41
41
  "@putout/operator-json": "^2.0.0",
42
- "@putout/test": "^11.0.0",
42
+ "@putout/test": "^12.0.0",
43
43
  "c8": "^10.0.0",
44
44
  "eslint": "^9.0.0",
45
45
  "eslint-plugin-n": "^17.0.0",
46
- "eslint-plugin-putout": "^24.0.0",
47
- "lerna": "^6.0.1",
46
+ "eslint-plugin-putout": "^25.0.1",
48
47
  "madrun": "^10.0.0",
49
48
  "montag": "^1.2.1",
50
49
  "nodemon": "^3.0.1",