Skip to content

Commit

Permalink
fix(generate): au generate should only create folder when needed
Browse files Browse the repository at this point in the history
closes #475
  • Loading branch information
3cp committed Jun 2, 2022
1 parent 47d0b0f commit 238f68b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/project-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ exports.ProjectItem = class {
create(relativeTo) {
let fullPath = relativeTo ? this.calculateRelativePath(relativeTo) : this.name;

if (this.isDirectory) {
// Skip empty folder
if (this.isDirectory && this.children.length) {
return fs.stat(fullPath)
.then(result => result)
.catch(() => fs.mkdir(fullPath))
Expand Down
13 changes: 12 additions & 1 deletion spec/lib/project-item.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('The ProjectItem module', () => {
expect(fs.readFileSync(path.join('folder', 'deepFolder', 'file4.js'))).toBe('file4');
});

it('Overwrites existing file', async() => {
it('overwrites existing file', async() => {
mockfs({
'folder': {
'file1.js': 'oldfile1',
Expand All @@ -64,5 +64,16 @@ describe('The ProjectItem module', () => {
expect(fs.readdirSync(path.join('folder', 'deepFolder')).sort()).toEqual(['file4.js']);
expect(fs.readFileSync(path.join('folder', 'deepFolder', 'file4.js'))).toBe('file4');
});

it('skips empty folder', async() => {
folder.add(ProjectItem.directory('empty-folder'));
await folder.create('root');
expect(fs.readdirSync('.')).toEqual(['folder']);
expect(fs.readdirSync('folder').sort()).toEqual(['deepFolder', 'file1.js', 'file2.js']);
expect(fs.readFileSync(path.join('folder', 'file1.js'))).toBe('file1');
expect(fs.readFileSync(path.join('folder', 'file2.js'))).toBe('file2');
expect(fs.readdirSync(path.join('folder', 'deepFolder')).sort()).toEqual(['file4.js']);
expect(fs.readFileSync(path.join('folder', 'deepFolder', 'file4.js'))).toBe('file4');
});
});
});

0 comments on commit 238f68b

Please sign in to comment.