Skip to content

Commit 3e1a9d5

Browse files
committed
feat: add specs schematic
1 parent 8a5535e commit 3e1a9d5

22 files changed

+676
-14
lines changed

.editorconfig

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Editor configuration, see https://editorconfig.org
2+
root = true
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
insert_final_newline = true
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
max_line_length = off
13+
trim_trailing_whitespace = false

README.md

+25-3
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,18 @@ npm i -D ngx-spec
1111
```
1212

1313
* version 2.* for Angular 7
14-
* version 1.* for Angular 6
14+
* version 1.* for Angular 6 (no `specs` support)
1515

16-
## Usage
16+
## Supported types
1717

1818
Supported types are
1919

2020
```ts
21-
const supportedTypes = ['component', 'directive', 'guard', 'service', 'pipe'];
21+
const SupportedTypes = ['component', 'directive', 'guard', 'service', 'pipe'];
2222
```
2323

24+
## ng g ngx:spec
25+
2426
Run:
2527

2628
```sh
@@ -35,6 +37,26 @@ ng g ngx-spec:spec path/my.service.ts
3537

3638
> Please note that in a standard Angular CLI project the path will start at `src/app`. That's why if you use the file paths then it would be comfortable to `cd src/app` first and then safely use the shell autocompletion to produce the proper path.
3739
40+
## ng g ngx-spec:specs
41+
42+
Batch specs generator. Supports [minimatch](https://github.com/isaacs/minimatch) globs. Does not override existing spec files.
43+
44+
Examples:
45+
46+
```sh
47+
ng g ngx-spec:spec path/*.service.ts
48+
```
49+
50+
```sh
51+
ng g ngx-spec:spec **/*.service.ts
52+
```
53+
54+
```sh
55+
ng g ngx-spec:spec **/*
56+
```
57+
58+
etc.
59+
3860
## Usage in Visual Studio Code
3961

4062
It could be easier to use the schematic with [vscode-angular-schematics extension](https://github.com/cyrilletuzi/vscode-angular-schematics) in Visual Studio Code.

package-lock.json

+94
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
"@angular-devkit/schematics": "^7.3.3",
2525
"@schematics/angular": "^7.3.3",
2626
"@types/jasmine": "^2.8.16",
27-
"@types/node": "^8.10.40"
27+
"@types/minimatch": "^3.0.3",
28+
"@types/node": "^8.10.40",
29+
"jasmine": "^3.3.1"
30+
},
31+
"dependencies": {
32+
"minimatch": "^3.0.4"
2833
}
2934
}

src/collection.json

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
"description": "Creates a spec",
66
"factory": "./spec/index",
77
"schema": "./spec/schema.json"
8+
},
9+
"specs": {
10+
"description": "Creates spec files for each matching file by a provided GLOB",
11+
"factory": "./specs/index",
12+
"schema": "./specs/schema.json"
813
}
914
}
1015
}

src/spec/index.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/spec/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/spec/index.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ import { basename, dirname, normalize, Path, strings } from '@angular-devkit/cor
22
import { WorkspaceSchema } from '@angular-devkit/core/src/workspace';
33
import { apply, applyTemplates, filter, mergeWith, move, Rule, SchematicContext, SchematicsException, Tree, url } from '@angular-devkit/schematics';
44
import * as nodePath from 'path';
5+
import { SupportedTypes } from '../utils/supported-types';
56
import { Schema as Options } from './schema';
67

7-
const supportedTypes = ['component', 'directive', 'guard', 'service', 'pipe'];
8-
98
export function getWorkspacePath(host: Tree): string {
109
const possibleFiles = ['/angular.json', '/.angular.json'];
1110
const path = possibleFiles.filter(path => host.exists(path))[0];
@@ -67,9 +66,9 @@ export default function (options: Options): Rule {
6766
);
6867
}
6968

70-
if (!supportedTypes.includes(type)) {
69+
if (!SupportedTypes.includes(type)) {
7170
const ex = `The type "${type}" is not supported. Please use one of [${
72-
supportedTypes.join(', ')
71+
SupportedTypes.join(', ')
7372
}].`;
7473

7574
throw new SchematicsException(ex);
@@ -80,10 +79,10 @@ export default function (options: Options): Rule {
8079

8180
const schematicsPath = require.resolve(`@schematics/angular/${type}/index.js`).replace(/index\.js$/, 'files');
8281

83-
const targetPath = `${ parsedPath.path }/${ name }.${ type }.ts`;
82+
const targetPath = `${parsedPath.path}/${name}.${type}.ts`;
8483

8584
if (!host.exists(targetPath) && !options.ignoreTargetNotFound) {
86-
throw new SchematicsException(`Target file ${ targetPath } is not existing`);
85+
throw new SchematicsException(`Target file ${targetPath} is not existing`);
8786
}
8887

8988
// important for windows to get the relative path, otherwise schematics becomes crazy when sees C:\bla\bla things

src/specs/index.js

+61
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)