Skip to content

Commit

Permalink
Merge pull request #18 from ferrislucas/fix-folder-creation
Browse files Browse the repository at this point in the history
Create folders when necessary during processing of model instructions
  • Loading branch information
ferrislucas authored Apr 12, 2023
2 parents 8744a62 + b5cd5a8 commit 694f00b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
6 changes: 6 additions & 0 deletions refactorResultProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ export default class RefactorResultProcessor {
const filePath = operation.filePath;
const fileContents = operation.fileContents;
const absolutePath = path.resolve(filePath);
const dirPath = path.dirname(absolutePath);

if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath, { recursive: true });
}

fs.writeFileSync(absolutePath, fileContents, 'utf-8');
});
}
Expand Down
29 changes: 29 additions & 0 deletions test/refactorResultProcessor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import fs from 'fs';
import path from 'path';
import assert from 'assert';
import RefactorResultProcessor from '../refactorResultProcessor.js'

describe('RefactorResultProcessor', () => {
describe('.call()', () => {
afterEach(() => {
fs.unlinkSync(path.resolve('test/testDir/testFile.txt'));
fs.rmdirSync(path.resolve('test/testDir'), { recursive: true });
});

it('should create file at arbitrary directory tree depth', () => {
const data = {
operations: [
{
crudOperation: 'create',
filePath: 'test/testDir/testFile.txt',
fileContents: 'This is a test file'
}
]
};

RefactorResultProcessor.call(data);

assert(fs.existsSync(path.resolve('test/testDir/testFile.txt')));
});
});
});

0 comments on commit 694f00b

Please sign in to comment.