From e7fd6b1e04f3f3387a91c53a7ac479fe72bdd72e Mon Sep 17 00:00:00 2001 From: James Henry Date: Mon, 4 Dec 2017 18:45:28 +0000 Subject: [PATCH] fix(schematics): only begin converting to workspace once files have been checked --- .../src/collection/workspace/index.ts | 20 ++++++++ .../collection/workspace/workspace.spec.ts | 49 +++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 packages/schematics/src/collection/workspace/workspace.spec.ts diff --git a/packages/schematics/src/collection/workspace/index.ts b/packages/schematics/src/collection/workspace/index.ts index bc544ad22a4f8..3ba9441d2eeba 100644 --- a/packages/schematics/src/collection/workspace/index.ts +++ b/packages/schematics/src/collection/workspace/index.ts @@ -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(), diff --git a/packages/schematics/src/collection/workspace/workspace.spec.ts b/packages/schematics/src/collection/workspace/workspace.spec.ts new file mode 100644 index 0000000000000..1dc9cbf9fb3fc --- /dev/null +++ b/packages/schematics/src/collection/workspace/workspace.spec.ts @@ -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'); + }); +});