-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Usage: `npm install @angular-extensions/svg-icons-builder` or `ng add @angular-extensions/svg-icons-builder` - adds `svg-to-ts` as a dev dependency
- Loading branch information
1 parent
8b1d52a
commit 0988b19
Showing
5 changed files
with
72 additions
and
9 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
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 |
---|---|---|
@@ -1,14 +1,32 @@ | ||
import { Tree } from '@angular-devkit/schematics'; | ||
import { SchematicTestRunner } from '@angular-devkit/schematics/testing'; | ||
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing'; | ||
import * as path from 'path'; | ||
|
||
const collectionPath = path.join(__dirname, '../collection.json'); | ||
const runner = new SchematicTestRunner('schematics', collectionPath); | ||
|
||
describe('ng-add', () => { | ||
it('works', async () => { | ||
const tree = await runner.runSchematicAsync('ng-add', {}, Tree.empty()).toPromise(); | ||
let appTree: UnitTestTree; | ||
|
||
expect(tree.files).toEqual([]); | ||
beforeEach(() => { | ||
appTree = new UnitTestTree(Tree.empty()); | ||
appTree.create('/package.json', JSON.stringify({ devDependencies: {} })); | ||
}); | ||
|
||
it('should update package.json', async () => { | ||
const tree = await runner.runSchematicAsync('ng-add', { preserveAngularCLILayout: true }, appTree).toPromise(); | ||
|
||
const result = JSON.parse(tree.readContent('/package.json')).devDependencies; | ||
expect(result['svg-to-ts']).toBeDefined(); | ||
}); | ||
|
||
it('should error if no package.json is present', async () => { | ||
appTree.delete('package.json'); | ||
try { | ||
await runner.runSchematicAsync('ng-add', { name: 'myApp' }, appTree).toPromise(); | ||
fail('should throw'); | ||
} catch (e) { | ||
expect(e.message).toContain('Cannot find package.json'); | ||
} | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,9 +1,44 @@ | ||
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; | ||
import { NodePackageInstallTask } from '@angular-devkit/schematics/tasks'; | ||
|
||
const svgToTsVersion = '^5.7.0'; | ||
|
||
// You don't have to export the function as default. You can also have more than one rule factory | ||
// per file. | ||
export function ngAdd(_options: any): Rule { | ||
return (tree: Tree, _context: SchematicContext) => { | ||
addPackageToPackageJson(tree, 'svg-to-ts', `${svgToTsVersion}`); | ||
_context.logger.log('info', `Installing added packages...`); | ||
_context.addTask(new NodePackageInstallTask()); | ||
return tree; | ||
}; | ||
} | ||
|
||
export function addPackageToPackageJson(host: Tree, pkg: string, version: string): Tree { | ||
if (host.exists('package.json')) { | ||
const sourceText = host.read('package.json')!.toString('utf-8'); | ||
const json = JSON.parse(sourceText); | ||
|
||
if (!json.devDependencies) { | ||
json.devDependencies = {}; | ||
} | ||
|
||
if (!json.devDependencies[pkg]) { | ||
json.devDependencies[pkg] = version; | ||
json.devDependencies = sortObjectByKeys(json.devDependencies); | ||
} | ||
|
||
host.overwrite('package.json', JSON.stringify(json, null, 2)); | ||
} else { | ||
throw new Error(`Cannot find package.json`); | ||
} | ||
|
||
return host; | ||
} | ||
|
||
function sortObjectByKeys(obj: any) { | ||
return Object.keys(obj) | ||
.sort() | ||
.reduce((result: any, key) => { | ||
result[key] = obj[key]; | ||
return result; | ||
}, {}); | ||
} |