Skip to content

Commit

Permalink
fix(schematics): only begin converting to workspace once files have b…
Browse files Browse the repository at this point in the history
…een checked
  • Loading branch information
JamesHenry authored and vsavkin committed Dec 4, 2017
1 parent 3670fd4 commit e7fd6b1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
20 changes: 20 additions & 0 deletions packages/schematics/src/collection/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,29 @@ function dedup(array: any[]): any[] {
return res;
}

function checkCanConvertToWorkspace(options: Schema) {
return (host: Tree) => {
if (!host.exists('package.json')) {
throw new Error('Cannot find package.json');
}
if (!host.exists('protractor.conf.js')) {
throw new Error('Cannot find protractor.conf.js');
}
if (!host.exists('.angular-cli.json')) {
throw new Error('Cannot find .angular-cli.json');
}
const angularCliJson = JSON.parse(host.read('.angular-cli.json')!.toString('utf-8'));
if (angularCliJson.apps.length !== 1) {
throw new Error('Can only convert projects with one app');
}
return host;
};
}

export default function(schema: Schema): Rule {
const options = { ...schema, name: toFileName(schema.name) };
return chain([
checkCanConvertToWorkspace(options),
moveFiles(options),
branchAndMerge(chain([mergeWith(apply(url('./files'), []))])),
updatePackageJson(),
Expand Down
49 changes: 49 additions & 0 deletions packages/schematics/src/collection/workspace/workspace.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { SchematicTestRunner } from '@angular-devkit/schematics/testing';
import * as path from 'path';
import { Tree, VirtualTree } from '@angular-devkit/schematics';
import { getFileContent } from '@schematics/angular/utility/test';

describe('workspace', () => {
const schematicRunner = new SchematicTestRunner('@nrwl/schematics', path.join(__dirname, '../../collection.json'));

let appTree: Tree;

beforeEach(() => {
appTree = new VirtualTree();
});

it('should error if no package.json is present', () => {
expect(() => {
const tree = schematicRunner.runSchematic('workspace', { name: 'myApp' }, appTree);
}).toThrow('Cannot find package.json');
});

it('should error if no protractor.conf.js is present', () => {
expect(() => {
appTree.create('/package.json', JSON.stringify({}));
const tree = schematicRunner.runSchematic('workspace', { name: 'myApp' }, appTree);
}).toThrow('Cannot find protractor.conf.js');
});

it('should error if no .angular-cli.json is present', () => {
expect(() => {
appTree.create('/package.json', JSON.stringify({}));
appTree.create('/protractor.conf.js', '');
const tree = schematicRunner.runSchematic('workspace', { name: 'myApp' }, appTree);
}).toThrow('Cannot find .angular-cli.json');
});

it('should error if the .angular-cli.json specifies more than one app', () => {
expect(() => {
appTree.create('/package.json', JSON.stringify({}));
appTree.create('/protractor.conf.js', '');
appTree.create(
'/.angular-cli.json',
JSON.stringify({
apps: [{}, {}]
})
);
const tree = schematicRunner.runSchematic('workspace', { name: 'myApp' }, appTree);
}).toThrow('Can only convert projects with one app');
});
});

0 comments on commit e7fd6b1

Please sign in to comment.