Skip to content

Commit 3da8168

Browse files
author
Imran Momin
committed
always add the package under devDependencies
1 parent 64da570 commit 3da8168

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

src/ng-add/index.ts

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { Rule, SchematicContext, SchematicsException, Tree, chain } from '@angular-devkit/schematics';
22
import { experimental, JsonParseMode, parseJson } from '@angular-devkit/core';
3-
import {
4-
addPackageJsonDependency,
5-
NodeDependency,
6-
NodeDependencyType
7-
} from 'schematics-utilities';
3+
import { addPackageJsonDependency, NodeDependency, NodeDependencyType } from 'schematics-utilities';
84

95
function addPackageJsonDependencies(): Rule {
106
return (host: Tree, context: SchematicContext) => {
7+
8+
// always add the package under dev dependencies
119
const dependencies: NodeDependency[] = [
12-
{ type: NodeDependencyType.Default, version: '~3.0.0', name: '@netlify-builder/deploy' }
10+
{ type: NodeDependencyType.Dev, version: '~3.1.0', name: '@netlify-builder/deploy' }
1311
];
14-
12+
1513
dependencies.forEach(dependency => {
1614
addPackageJsonDependency(host, dependency);
1715
context.logger.log('info', `✅️ Added "${dependency.name}" into ${dependency.type}`);
@@ -22,30 +20,28 @@ function addPackageJsonDependencies(): Rule {
2220
}
2321

2422
function getWorkspace(host: Tree): { path: string; workspace: experimental.workspace.WorkspaceSchema } {
25-
const possibleFiles = ['/angular.json', '/.angular.json'];
26-
const path = possibleFiles.filter(path => host.exists(path))[0];
23+
const possibleFiles = ['/angular.json', './angular.json'];
24+
const path = possibleFiles.find(path => host.exists(path));
25+
26+
if (!path) {
27+
throw new SchematicsException(`Could not find angular.json`);
28+
}
2729

2830
const configBuffer = host.read(path);
29-
if (configBuffer === null) {
31+
if (!configBuffer) {
3032
throw new SchematicsException(`Could not find angular.json`);
3133
}
32-
const content = configBuffer.toString();
3334

35+
const content = configBuffer.toString();
3436
let workspace: experimental.workspace.WorkspaceSchema;
37+
3538
try {
36-
workspace = (parseJson(
37-
content,
38-
JsonParseMode.Loose
39-
) as {}) as experimental.workspace.WorkspaceSchema;
39+
workspace = <any>parseJson(content, JsonParseMode.Loose) as experimental.workspace.WorkspaceSchema;
4040
} catch (e) {
41-
throw new SchematicsException(`Could not parse angular.json: ` + e.message);
41+
throw new SchematicsException(`Could not parse angular.json: ${e.message}`);
4242
}
4343

44-
return {
45-
path,
46-
workspace
47-
};
48-
44+
return { path, workspace };
4945
}
5046

5147
interface NgAddOptions {
@@ -56,7 +52,7 @@ interface NgAddOptions {
5652

5753
export function netlifyBuilder(options: NgAddOptions): Rule {
5854
return (tree: Tree, _context: SchematicContext) => {
59-
// Verifying Angular.json
55+
// get the workspace details
6056
const { path: workspacePath, workspace } = getWorkspace(tree);
6157

6258
// getting project name
@@ -93,9 +89,7 @@ export function netlifyBuilder(options: NgAddOptions): Rule {
9389
!project.architect.build.options.outputPath
9490
) {
9591
throw new SchematicsException(
96-
`Cannot read the output path (architect.build.options.outputPath) of the Angular project "${
97-
options.project
98-
}" in angular.json`
92+
`Cannot read the output path(architect.build.options.outputPath) of the Angular project "${options.project}" in angular.json`
9993
);
10094
}
10195

@@ -108,19 +102,15 @@ export function netlifyBuilder(options: NgAddOptions): Rule {
108102
"siteId": options.siteID,
109103
}
110104
}
111-
112-
tree.overwrite(workspacePath, JSON.stringify(workspace, null, 2));
113105

106+
tree.overwrite(workspacePath, JSON.stringify(workspace, null, 2));
114107
return tree;
115-
116108
};
117109
}
118110

119111
export default function (options: NgAddOptions): Rule {
120-
return chain(
121-
[
122-
netlifyBuilder(options),
123-
addPackageJsonDependencies()
124-
]
125-
)
112+
return chain([
113+
netlifyBuilder(options),
114+
addPackageJsonDependencies()
115+
]);
126116
}

0 commit comments

Comments
 (0)