-
Notifications
You must be signed in to change notification settings - Fork 6.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ng-add): set up gestures in CLI projects (#12734)
Introduces a new option for the `ng-add` schematic that enables automatic set up of HammerJS in the CLI project. By default, gestures will be set up if someone runs `ng add @angular/material`.
- Loading branch information
1 parent
dad0ed0
commit b919a48
Showing
8 changed files
with
112 additions
and
4 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,41 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {Rule, Tree} from '@angular-devkit/schematics'; | ||
import {getWorkspace} from '@schematics/angular/utility/config'; | ||
import {getProjectFromWorkspace} from '../../utils/get-project'; | ||
import {Schema} from '../schema'; | ||
import {getProjectMainFile} from './project-main-file'; | ||
|
||
const hammerjsImportStatement = `import 'hammerjs';`; | ||
|
||
/** Adds HammerJS to the main file of the specified Angular CLI project. */ | ||
export function addHammerJsToMain(options: Schema): Rule { | ||
return (host: Tree) => { | ||
const workspace = getWorkspace(host); | ||
const project = getProjectFromWorkspace(workspace, options.project); | ||
const mainFile = getProjectMainFile(project); | ||
|
||
const recorder = host.beginUpdate(mainFile); | ||
const buffer = host.read(mainFile); | ||
|
||
if (!buffer) { | ||
return console.error(`Could not read the project main file (${mainFile}). Please manually ` + | ||
`import HammerJS in your main TypeScript file.`); | ||
} | ||
|
||
const fileContent = buffer.toString('utf8'); | ||
|
||
if (fileContent.includes(hammerjsImportStatement)) { | ||
return console.log(`HammerJS is already imported in the project main file (${mainFile}).`); | ||
} | ||
|
||
recorder.insertRight(0, `${hammerjsImportStatement}\n`); | ||
host.commitUpdate(recorder); | ||
}; | ||
} |
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,22 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.io/license | ||
*/ | ||
|
||
import {SchematicsException} from '@angular-devkit/schematics'; | ||
import {WorkspaceProject} from '@schematics/angular/utility/config'; | ||
|
||
/** Looks for the main TypeScript file in the given project and returns its path. */ | ||
export function getProjectMainFile(project: WorkspaceProject): string { | ||
const buildTarget = project.architect.build.options; | ||
|
||
if (buildTarget.main) { | ||
return buildTarget.main; | ||
} | ||
|
||
throw new SchematicsException( | ||
'Could not find the project main file inside of the workspace config.'); | ||
} |
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
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
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