Skip to content

Commit

Permalink
Merge pull request #599 from JeroenVinke/fix/generator-root-dir
Browse files Browse the repository at this point in the history
fix(generators): create elements/attributes in correct location
  • Loading branch information
JeroenVinke committed May 22, 2017
2 parents 9bfe9bc + 7400e71 commit fd07344
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/project-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ exports.ProjectItem = class {
}

create(ui, relativeTo) {
let fullPath = relativeTo ? path.posix.join(relativeTo, this.name) : this.name;
let fullPath = relativeTo ? this.calculateRelativePath(relativeTo) : this.name;

if (this.isDirectory) {
return fs.stat(fullPath)
Expand Down
11 changes: 10 additions & 1 deletion lib/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ exports.Project = class {
this.generatorDirectory = path.join(directory, 'aurelia_project/generators');
this.aureliaJSONPath = path.join(directory, 'aurelia_project', 'aurelia.json');

this.locations = Object.keys(model.paths).map(key => this[key] = ProjectItem.directory(model.paths[key]));
this.locations = Object.keys(model.paths).map(key => {
this[key] = ProjectItem.directory(model.paths[key]);

if (key !== 'root') {
this[key] = ProjectItem.directory(model.paths[key]);
this[key].parent = this.root;
}

return this[key];
});
this.locations.push(this.generators = ProjectItem.directory('aurelia_project/generators'));
this.locations.push(this.tasks = ProjectItem.directory('aurelia_project/tasks'));
}
Expand Down
66 changes: 45 additions & 21 deletions spec/lib/project-item.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('The project-item module', () => {
let fs;

let ProjectItem;
let project;
let projectItem;

beforeEach(() => {
mockfs = require('mock-fs');
Expand All @@ -24,59 +24,81 @@ describe('The project-item module', () => {

describe('The create() function', () => {
beforeEach(() => {
project = new ProjectItem();
projectItem = new ProjectItem();
});

describe('isDirectory = true', () => {
beforeEach(() => {
project.isDirectory = true;
projectItem.isDirectory = true;
});

it('takes parent directory into consideration', done => {
let parent = ProjectItem.directory('src');
projectItem.name = 'resources';
projectItem.parent = parent;

parent.create()
.then(() => {
projectItem.create(null, '.')
.then(() => fs.readdir('src'))
.then(files => {
expect(files[0]).toBe('resources');
})
.then(done);
})
.catch(e => done.fail(e));
});

it('creates a directory if it is missing', done => {
project.name = 'cli-app';
project.create()
.then(() => fs.readdir(project.name))
projectItem.name = 'cli-app';
projectItem.create()
.then(() => fs.readdir(projectItem.name))
.then(files => {
expect(files).toBeDefined();
}).catch(fail).then(done);
})
.then(done)
.catch(e => done.fail(e));
});

it('creates the childs', done => {
const ui = {};
const child = { create: () => { } };
project.children.push(child);
projectItem.children.push(child);
spyOn(child, 'create');

project.name = 'cli-app';
project.create(ui)
projectItem.name = 'cli-app';
projectItem.create(ui)
.then(() => {
expect(child.create).toHaveBeenCalledWith(ui, project.name);
expect(child.create).toHaveBeenCalledWith(ui, projectItem.name);
})
.catch(fail).then(done);
.then(done)
.catch(e => done.fail(e));
});
});
});

describe('The _write() function', () => {
beforeEach(() => {
project = new ProjectItem();
projectItem = new ProjectItem();
});
it('creates non-existing files', done => {
const file = {
path: 'index.html',
content: '<html></html>',
content: '<html></html>'
};

project._write(file.path, file.content)
projectItem._write(file.path, file.content)
.then(() => fs.readFile(file.path))
.then(content => {
expect(content).toBe(file.content);
}).catch(fail).then(done);
})
.then(done)
.catch(e => done.fail(e));
});

describe('in `skip` strategy', () => {
beforeEach(() => {
project._fileExistsStrategy = 'skip';
projectItem._fileExistsStrategy = 'skip';
});

it('does not override an existing file', done => {
Expand All @@ -86,12 +108,14 @@ describe('The project-item module', () => {
};

fs.writeFile(file.path, file.content)
.then(() => project._write(file.path, 'evil'))
.then(() => projectItem._write(file.path, 'evil'))
.then(() => fs.readFile(file.path))
.then(content => {
expect(content).toBe(file.content);
}).catch(fail).then(done);
})
.then(done)
.catch(e => done.fail(e));
});
});
});
});
});
34 changes: 33 additions & 1 deletion spec/lib/project.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,22 @@ describe('The project module', () => {
mockfs.restore();
});

it('creates project items for all paths in aurelia.json, using the root path as the parent directory', () => {
let model = {
paths: {
'root': 'src',
'resources': 'resources',
'elements': 'resources/elements'
}
};

project = new Project(ui, '', model);

expect(hasProjectItem(project.locations, 'src', null)).toBe(true);
expect(hasProjectItem(project.locations, 'resources', 'src')).toBe(true);
expect(hasProjectItem(project.locations, 'resources/elements', 'src')).toBe(true);
});

describe('The resolveGenerator() function', () => {
it('resolves to teh generators location', done => {
fs.writeFile('aurelia_project/generators/test.js', '')
Expand Down Expand Up @@ -66,4 +82,20 @@ describe('The project module', () => {
}).catch(fail).then(done);
});
});
});
});

function hasProjectItem(locations, name, parent) {
for (let i = 0; i < locations.length; i++) {
if (locations[i].name === name) {
if (!parent && !locations[i].parent) {
return true;
}

if (locations[i].parent && locations[i].parent.name === parent) {
return true;
}
}
}

return false;
}

0 comments on commit fd07344

Please sign in to comment.