@putout/operator-filesystem 2.8.0 → 2.9.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.
Files changed (2) hide show
  1. package/lib/filesystem.js +10 -18
  2. package/package.json +1 -1
package/lib/filesystem.js CHANGED
@@ -1,5 +1,6 @@
1
1
  'use strict';
2
2
 
3
+ const {join} = require('node:path');
3
4
  const tryCatch = require('try-catch');
4
5
  const {types} = require('putout');
5
6
 
@@ -102,15 +103,6 @@ module.exports.removeFile = (filePath) => {
102
103
  maybeFS.removeFile(filename);
103
104
  };
104
105
 
105
- function getFile(dirPathFiles, name) {
106
- for (const file of dirPathFiles.get('value.elements')) {
107
- if (name === getFilename(file))
108
- return file;
109
- }
110
-
111
- return null;
112
- }
113
-
114
106
  module.exports.moveFile = (filePath, dirPath) => {
115
107
  const dirname = getFilename(dirPath);
116
108
  const filename = getFilename(filePath);
@@ -121,7 +113,7 @@ module.exports.moveFile = (filePath, dirPath) => {
121
113
  .split('/')
122
114
  .pop();
123
115
 
124
- const newFilename = `${dirname}/${basename}`;
116
+ const newFilename = join(dirname, basename);
125
117
 
126
118
  maybeRemoveFile(dirPath, newFilename);
127
119
 
@@ -140,7 +132,7 @@ module.exports.copyFile = (filePath, dirPath) => {
140
132
  .split('/')
141
133
  .pop();
142
134
 
143
- const newFilename = `${dirname}/${basename}`;
135
+ const newFilename = join(dirname, basename);
144
136
  const [hasContent, content] = getFileContent(filePath);
145
137
 
146
138
  const copiedFile = ObjectExpression([
@@ -159,8 +151,7 @@ module.exports.copyFile = (filePath, dirPath) => {
159
151
 
160
152
  function maybeRemoveFile(dirPath, filename) {
161
153
  const dirPathFiles = getProperty(dirPath, 'files');
162
-
163
- const fileToOverwrite = getFile(dirPathFiles, filename);
154
+ const [fileToOverwrite] = findFile(dirPathFiles, filename);
164
155
 
165
156
  if (!fileToOverwrite)
166
157
  return;
@@ -173,7 +164,7 @@ const createFiles = (files) => ObjectProperty(StringLiteral('files'), ArrayExpre
173
164
  const createFilename = (filename) => ObjectProperty(StringLiteral('filename'), StringLiteral(filename));
174
165
  const createContent = (content) => ObjectProperty(StringLiteral('content'), StringLiteral(content));
175
166
 
176
- module.exports.createFile = (dirPath, name, content) => {
167
+ module.exports.createFile = (dirPath, name, content = '') => {
177
168
  maybeRemoveFile(dirPath, name);
178
169
 
179
170
  const dirPathFiles = getFiles(dirPath);
@@ -183,14 +174,13 @@ module.exports.createFile = (dirPath, name, content) => {
183
174
  const typeProperty = createType('file');
184
175
  const filenameProperty = createFilename(filename);
185
176
 
186
- dirPathFiles.node.value.elements.push(ObjectExpression([typeProperty, filenameProperty]));
177
+ dirPathFiles.node.value.elements.push(ObjectExpression([typeProperty, filenameProperty, createContent(content)]));
187
178
 
188
179
  const filePath = dirPathFiles
189
180
  .get('value.elements')
190
181
  .at(-1);
191
182
 
192
- if (content)
193
- writeFileContent(filePath, content);
183
+ writeFileContent(filePath, content);
194
184
 
195
185
  return filePath;
196
186
  };
@@ -265,5 +255,7 @@ function writeFileContent(filePath, content) {
265
255
 
266
256
  const property = createContentProperty(content);
267
257
  filePath.node.properties.push(property);
268
- }module.exports.init = maybeFS.init;
258
+ }
259
+
260
+ module.exports.init = maybeFS.init;
269
261
  module.exports.deinit = maybeFS.deinit;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@putout/operator-filesystem",
3
- "version": "2.8.0",
3
+ "version": "2.9.1",
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",