-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(generate): add guard generation (#4055)
- Loading branch information
1 parent
1655e51
commit 2c1e877
Showing
10 changed files
with
388 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# ng generate guard | ||
|
||
## Overview | ||
`ng generate guard [name]` generates a guard | ||
|
||
## Options | ||
`--flat` flag to indicate if a dir is created | ||
|
||
`--spec` specifies if a spec file is generated | ||
|
||
`--module` (`-m`) allows specification of the declaring module |
16 changes: 16 additions & 0 deletions
16
packages/@angular/cli/blueprints/guard/files/__path__/__name__.guard.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* tslint:disable:no-unused-variable */ | ||
|
||
import { TestBed, async, inject } from '@angular/core/testing'; | ||
import { <%= classifiedModuleName %>Guard } from './<%= dasherizedModuleName %>.guard'; | ||
|
||
describe('<%= classifiedModuleName %>Guard', () => { | ||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
providers: [<%= classifiedModuleName %>Guard] | ||
}); | ||
}); | ||
|
||
it('should ...', inject([<%= classifiedModuleName %>Guard], (guard: <%= classifiedModuleName %>Guard) => { | ||
expect(guard).toBeTruthy(); | ||
})); | ||
}); |
12 changes: 12 additions & 0 deletions
12
packages/@angular/cli/blueprints/guard/files/__path__/__name__.guard.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; | ||
import { Observable } from 'rxjs/Observable'; | ||
|
||
@Injectable() | ||
export class <%= classifiedModuleName %>Guard implements CanActivate { | ||
canActivate( | ||
next: ActivatedRouteSnapshot, | ||
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import {NodeHost} from '../../lib/ast-tools'; | ||
import { oneLine } from 'common-tags'; | ||
|
||
const path = require('path'); | ||
const fs = require('fs'); | ||
const chalk = require('chalk'); | ||
const dynamicPathParser = require('../../utilities/dynamic-path-parser'); | ||
const Blueprint = require('../../ember-cli/lib/models/blueprint'); | ||
const stringUtils = require('ember-cli-string-utils'); | ||
const astUtils = require('../../utilities/ast-utils'); | ||
const getFiles = Blueprint.prototype.files; | ||
|
||
export default Blueprint.extend({ | ||
description: '', | ||
|
||
availableOptions: [ | ||
{ name: 'flat', type: Boolean }, | ||
{ name: 'spec', type: Boolean }, | ||
{ name: 'module', type: String, aliases: ['m'] } | ||
], | ||
|
||
beforeInstall: function(options: any) { | ||
if (options.module) { | ||
// Resolve path to module | ||
const modulePath = options.module.endsWith('.ts') ? options.module : `${options.module}.ts`; | ||
const parsedPath = dynamicPathParser(this.project, modulePath); | ||
this.pathToModule = path.join(this.project.root, parsedPath.dir, parsedPath.base); | ||
|
||
if (!fs.existsSync(this.pathToModule)) { | ||
throw 'Module specified does not exist'; | ||
} | ||
} | ||
}, | ||
|
||
normalizeEntityName: function (entityName: string) { | ||
const parsedPath = dynamicPathParser(this.project, entityName); | ||
|
||
this.dynamicPath = parsedPath; | ||
return parsedPath.name; | ||
}, | ||
|
||
locals: function (options: any) { | ||
options.flat = options.flat !== undefined ? | ||
options.flat : | ||
this.project.ngConfigObj.get('defaults.guard.flat'); | ||
|
||
options.spec = options.spec !== undefined ? | ||
options.spec : | ||
this.project.ngConfigObj.get('defaults.guard.spec'); | ||
|
||
return { | ||
dynamicPath: this.dynamicPath.dir, | ||
flat: options.flat | ||
}; | ||
}, | ||
|
||
files: function() { | ||
let fileList = getFiles.call(this) as Array<string>; | ||
|
||
if (this.options && !this.options.spec) { | ||
fileList = fileList.filter(p => p.indexOf('__name__.guard.spec.ts') < 0); | ||
} | ||
|
||
return fileList; | ||
}, | ||
|
||
fileMapTokens: function (options: any) { | ||
// Return custom template variables here. | ||
return { | ||
__path__: () => { | ||
let dir = this.dynamicPath.dir; | ||
if (!options.locals.flat) { | ||
dir += path.sep + options.dasherizedModuleName; | ||
} | ||
this.generatePath = dir; | ||
return dir; | ||
} | ||
}; | ||
}, | ||
|
||
afterInstall(options: any) { | ||
const returns: Array<any> = []; | ||
|
||
if (!this.pathToModule) { | ||
const warningMessage = oneLine` | ||
Guard is generated but not provided, | ||
it must be provided to be used | ||
`; | ||
this._writeStatusToUI(chalk.yellow, 'WARNING', warningMessage); | ||
} else { | ||
const className = stringUtils.classify(`${options.entity.name}Guard`); | ||
const fileName = stringUtils.dasherize(`${options.entity.name}.guard`); | ||
const fullGeneratePath = path.join(this.project.root, this.generatePath); | ||
const moduleDir = path.parse(this.pathToModule).dir; | ||
const relativeDir = path.relative(moduleDir, fullGeneratePath); | ||
const importPath = relativeDir ? `./${relativeDir}/${fileName}` : `./${fileName}`; | ||
returns.push( | ||
astUtils.addProviderToModule(this.pathToModule, className, importPath) | ||
.then((change: any) => change.apply(NodeHost))); | ||
this._writeStatusToUI(chalk.yellow, | ||
'update', | ||
path.relative(this.project.root, this.pathToModule)); | ||
} | ||
|
||
return Promise.all(returns); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
'use strict'; | ||
|
||
var fs = require('fs-extra'); | ||
var ng = require('../helpers/ng'); | ||
var existsSync = require('exists-sync'); | ||
var expect = require('chai').expect; | ||
var path = require('path'); | ||
var tmp = require('../helpers/tmp'); | ||
var root = process.cwd(); | ||
var Promise = require('@angular/cli/ember-cli/lib/ext/promise'); | ||
var SilentError = require('silent-error'); | ||
const denodeify = require('denodeify'); | ||
|
||
const readFile = denodeify(fs.readFile); | ||
|
||
describe('Acceptance: ng generate guard', function () { | ||
beforeEach(function () { | ||
return tmp.setup('./tmp').then(function () { | ||
process.chdir('./tmp'); | ||
}).then(function () { | ||
return ng(['new', 'foo', '--skip-install']); | ||
}); | ||
}); | ||
|
||
afterEach(function () { | ||
this.timeout(10000); | ||
|
||
return tmp.teardown('./tmp'); | ||
}); | ||
|
||
it('ng generate guard my-guard', function () { | ||
const appRoot = path.join(root, 'tmp/foo'); | ||
const testPath = path.join(appRoot, 'src/app/my-guard.guard.ts'); | ||
const testSpecPath = path.join(appRoot, 'src/app/my-guard.guard.spec.ts'); | ||
const appModulePath = path.join(appRoot, 'src/app/app.module.ts'); | ||
|
||
return ng(['generate', 'guard', 'my-guard']) | ||
.then(() => { | ||
expect(existsSync(testPath)).to.equal(true); | ||
expect(existsSync(testSpecPath)).to.equal(true); | ||
}) | ||
.then(() => readFile(appModulePath, 'utf-8')) | ||
.then(content => { | ||
expect(content).not.to.matches(/import.*\MyGuardGuard\b.*from '.\/my-guard.guard';/); | ||
expect(content).not.to.matches(/providers:\s*\[MyGuardGuard\]/m); | ||
}); | ||
}); | ||
|
||
it('ng generate guard my-guard --no-spec', function () { | ||
const appRoot = path.join(root, 'tmp/foo'); | ||
const testPath = path.join(appRoot, 'src/app/my-guard.guard.ts'); | ||
const testSpecPath = path.join(appRoot, 'src/app/my-guard.guard.spec.ts'); | ||
const appModulePath = path.join(appRoot, 'src/app/app.module.ts'); | ||
|
||
return ng(['generate', 'guard', 'my-guard', '--no-spec']) | ||
.then(() => { | ||
expect(existsSync(testPath)).to.equal(true); | ||
expect(existsSync(testSpecPath)).to.equal(false); | ||
}) | ||
.then(() => readFile(appModulePath, 'utf-8')) | ||
.then(content => { | ||
expect(content).not.to.matches(/import.*\MyGuardGuard\b.*from '.\/my-guard.guard';/); | ||
expect(content).not.to.matches(/providers:\s*\[MyGuardGuard\]/m); | ||
}); | ||
}); | ||
|
||
it('ng generate guard my-guard --module=app.module.ts', function () { | ||
const appRoot = path.join(root, 'tmp/foo'); | ||
const testPath = path.join(appRoot, 'src/app/my-guard.guard.ts'); | ||
const testSpecPath = path.join(appRoot, 'src/app/my-guard.guard.spec.ts'); | ||
const appModulePath = path.join(appRoot, 'src/app/app.module.ts'); | ||
|
||
return ng(['generate', 'guard', 'my-guard', '--module', 'app.module.ts']) | ||
.then(() => { | ||
expect(existsSync(testPath)).to.equal(true); | ||
expect(existsSync(testSpecPath)).to.equal(true); | ||
}) | ||
.then(() => readFile(appModulePath, 'utf-8')) | ||
.then(content => { | ||
expect(content).to.matches(/import.*\MyGuardGuard\b.*from '.\/my-guard.guard';/); | ||
expect(content).to.matches(/providers:\s*\[MyGuardGuard\]/m); | ||
}); | ||
}); | ||
|
||
it('ng generate guard test' + path.sep + 'my-guard', function () { | ||
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', 'test')); | ||
return ng(['generate', 'guard', 'test' + path.sep + 'my-guard']).then(() => { | ||
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'test', 'my-guard.guard.ts'); | ||
expect(existsSync(testPath)).to.equal(true); | ||
}); | ||
}); | ||
|
||
it('ng generate guard test' + path.sep + '..' + path.sep + 'my-guard', function () { | ||
return ng(['generate', 'guard', 'test' + path.sep + '..' + path.sep + 'my-guard']).then(() => { | ||
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'my-guard.guard.ts'); | ||
expect(existsSync(testPath)).to.equal(true); | ||
}); | ||
}); | ||
|
||
it('ng generate guard my-guard from a child dir', () => { | ||
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', '1')); | ||
return new Promise(function (resolve) { | ||
process.chdir('./src'); | ||
resolve(); | ||
}) | ||
.then(() => process.chdir('./app')) | ||
.then(() => process.chdir('./1')) | ||
.then(() => { | ||
process.env.CWD = process.cwd(); | ||
return ng(['generate', 'guard', 'my-guard']) | ||
}) | ||
.then(() => { | ||
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', '1', 'my-guard.guard.ts'); | ||
expect(existsSync(testPath)).to.equal(true); | ||
}); | ||
}); | ||
|
||
it('ng generate guard child-dir' + path.sep + 'my-guard from a child dir', () => { | ||
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', '1', 'child-dir')); | ||
return new Promise(function (resolve) { | ||
process.chdir('./src'); | ||
resolve(); | ||
}) | ||
.then(() => process.chdir('./app')) | ||
.then(() => process.chdir('./1')) | ||
.then(() => { | ||
process.env.CWD = process.cwd(); | ||
return ng(['generate', 'guard', 'child-dir' + path.sep + 'my-guard']) | ||
}) | ||
.then(() => { | ||
var testPath = path.join( | ||
root, 'tmp', 'foo', 'src', 'app', '1', 'child-dir', 'my-guard.guard.ts'); | ||
expect(existsSync(testPath)).to.equal(true); | ||
}); | ||
}); | ||
|
||
it('ng generate guard child-dir' + path.sep + '..' + path.sep + 'my-guard from a child dir', | ||
() => { | ||
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', '1')); | ||
return new Promise(function (resolve) { | ||
process.chdir('./src'); | ||
resolve(); | ||
}) | ||
.then(() => process.chdir('./app')) | ||
.then(() => process.chdir('./1')) | ||
.then(() => { | ||
process.env.CWD = process.cwd(); | ||
return ng( | ||
['generate', 'guard', 'child-dir' + path.sep + '..' + path.sep + 'my-guard']) | ||
}) | ||
.then(() => { | ||
var testPath = | ||
path.join(root, 'tmp', 'foo', 'src', 'app', '1', 'my-guard.guard.ts'); | ||
expect(existsSync(testPath)).to.equal(true); | ||
}); | ||
}); | ||
|
||
it('ng generate guard ' + path.sep + 'my-guard from a child dir, gens under ' + | ||
path.join('src', 'app'), | ||
() => { | ||
fs.mkdirsSync(path.join(root, 'tmp', 'foo', 'src', 'app', '1')); | ||
return new Promise(function (resolve) { | ||
process.chdir('./src'); | ||
resolve(); | ||
}) | ||
.then(() => process.chdir('./app')) | ||
.then(() => process.chdir('./1')) | ||
.then(() => { | ||
process.env.CWD = process.cwd(); | ||
return ng(['generate', 'guard', path.sep + 'my-guard']) | ||
}) | ||
.then(() => { | ||
var testPath = path.join(root, 'tmp', 'foo', 'src', 'app', 'my-guard.guard.ts'); | ||
expect(existsSync(testPath)).to.equal(true); | ||
}); | ||
}); | ||
|
||
it('ng generate guard ..' + path.sep + 'my-guard from root dir will fail', () => { | ||
return ng(['generate', 'guard', '..' + path.sep + 'my-guard']).then(() => { | ||
throw new SilentError(`ng generate guard ..${path.sep}my-guard from root dir should fail.`); | ||
}, (err) => { | ||
expect(err).to.equal(`Invalid path: "..${path.sep}my-guard" cannot be above the "src${path.sep}app" directory`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import {join} from 'path'; | ||
import {ng} from '../../../utils/process'; | ||
import {expectFileToExist} from '../../../utils/fs'; | ||
|
||
|
||
export default function() { | ||
// Does not create a sub directory. | ||
const guardDir = join('src', 'app'); | ||
|
||
return ng('generate', 'guard', 'test-guard') | ||
.then(() => expectFileToExist(guardDir)) | ||
.then(() => expectFileToExist(join(guardDir, 'test-guard.guard.ts'))) | ||
.then(() => expectFileToExist(join(guardDir, 'test-guard.guard.spec.ts'))) | ||
|
||
// Try to run the unit tests. | ||
.then(() => ng('test', '--single-run')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import {ng} from '../../../utils/process'; | ||
import {expectToFail} from '../../../utils/utils'; | ||
|
||
export default function() { | ||
return Promise.resolve() | ||
.then(() => expectToFail(() => | ||
ng('generate', 'guard', 'test-guard', '--module', 'app.moduleXXX.ts'))); | ||
} |
Oops, something went wrong.