Skip to content

Commit b445981

Browse files
authored
feat: removed "schematics-utilities" package and dependent code (#91)
* chore: update github action to publish package updated github action so that whenever we create a new release then only it will be triggered and publish the package to npm * feat: removed "schematics-utilities" package and dependent code and replaced it with native workspace methods
1 parent 4dff6e6 commit b445981

File tree

5 files changed

+93
-45
lines changed

5 files changed

+93
-45
lines changed

collection.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"schematics": {
44
"ng-add": {
55
"description": "Builder to analyze package using source-map-explorer",
6-
"factory": "./ng-add/index",
6+
"factory": "./ng-add/index#ngAdd",
77
"schema": "./ng-add/schema.json",
88
"aliases": [
99
"install"

ng-add/index.ts

+25-39
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
import { Rule, SchematicContext, SchematicsException, Tree } from '@angular-devkit/schematics';
2-
import { getWorkspace, getWorkspacePath, ProjectType, WorkspaceProject } from 'schematics-utilities';
1+
import { TargetDefinition } from '@angular-devkit/core/src/workspace';
2+
import { chain, Rule, SchematicsException, Tree } from '@angular-devkit/schematics';
33
import { NgAddOptions } from './schema';
4+
import { getWorkspace, updateWorkspace } from './workspace';
45

56

6-
export function sourceMapBuilder(options: NgAddOptions): Rule {
7-
return (tree: Tree, _context: SchematicContext) => {
8-
// get the workspace details
9-
const workspaceSchema = getWorkspace(tree);
10-
const workspacePath: string = getWorkspacePath(tree);
7+
export function ngAdd(options: NgAddOptions): Rule {
8+
return async (host: Tree) => {
9+
const workspace = await getWorkspace(host);
1110

12-
// getting project name
11+
// Get project name
1312
if (!options.project) {
14-
if (workspaceSchema && workspaceSchema.defaultProject) {
15-
options.project = workspaceSchema.defaultProject;
13+
if (workspace.extensions.defaultProject) {
14+
options.project = workspace.extensions.defaultProject as string;
1615
} else {
1716
throw new SchematicsException(
1817
'No Angular project selected and no default project in the workspace'
@@ -21,45 +20,32 @@ export function sourceMapBuilder(options: NgAddOptions): Rule {
2120
}
2221

2322
// Validating project name
24-
const project: WorkspaceProject<ProjectType.Application> = workspaceSchema.projects[options.project];
23+
const project = workspace.projects.get(options.project);
2524
if (!project) {
26-
throw new SchematicsException(
27-
'The specified Angular project is not defined in this workspace'
28-
);
25+
throw new SchematicsException(`The specified Angular project is not defined in this workspace`);
2926
}
3027

3128
// Checking if it is application
32-
if (project.projectType !== 'application') {
33-
throw new SchematicsException(
34-
`source-map-analyzer requires an Angular project type of "application" in angular.json`
35-
);
29+
if (project.extensions['projectType'] !== 'application') {
30+
throw new SchematicsException(`source-map-analyzer requires an Angular project type of "application" in angular.json`);
3631
}
32+
33+
const outputPath: string | undefined = project.targets.get('build')?.options?.outputPath as string;
3734

38-
// Getting output path from Angular.json
39-
if (
40-
!project.architect ||
41-
!project.architect.build ||
42-
!project.architect.build.options ||
43-
!project.architect.build.options.outputPath
44-
) {
45-
throw new SchematicsException(
46-
`Cannot read the output path(architect.build.options.outputPath) of the Angular project "${options.project}" in angular.json`
47-
);
35+
if (!outputPath) {
36+
const message: string = `Cannot read the output path(architect.build.options.outputPath) of the Angular project "${options.project}" in angular.json`;
37+
throw new SchematicsException(message);
4838
}
4939

50-
// adding deploy statement for builder
51-
project.architect['analyze'] = {
52-
"builder": "@ngx-builders/analyze:analyze",
53-
"options": {
54-
"outputPath": project.architect.build.options.outputPath
40+
var targetDefinition: TargetDefinition = {
41+
builder: "@ngx-builders/analyze:analyze",
42+
options: {
43+
outputPath: outputPath
5544
}
5645
}
5746

58-
tree.overwrite(workspacePath, JSON.stringify(workspaceSchema, null, 2));
59-
return tree;
60-
};
61-
}
47+
project.targets.add({ name: 'analyze', ...targetDefinition });
6248

63-
export default function (options: NgAddOptions): Rule {
64-
return sourceMapBuilder(options)
49+
return chain([updateWorkspace(workspace)]);
50+
};
6551
}

ng-add/schema.ts

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
export interface NgAddOptions {
22
project: string | undefined;
3-
siteID: string;
4-
netlifyToken: string;
53
}

ng-add/workspace.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { virtualFs, workspaces } from '@angular-devkit/core';
2+
import { noop, Rule, SchematicsException, Tree } from '@angular-devkit/schematics';
3+
4+
/* Below code reference is taken from Angular CLI project.
5+
https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/utility/workspace.ts
6+
7+
These methods are not part of public APIs so we should not be referencing those methods.
8+
that's why added below method here.
9+
*/
10+
11+
function createHost(tree: Tree): workspaces.WorkspaceHost {
12+
return {
13+
async readFile(path: string): Promise<string> {
14+
const data = tree.read(path);
15+
if (!data) {
16+
throw new SchematicsException('File not found.');
17+
}
18+
return virtualFs.fileBufferToString(data);
19+
},
20+
async writeFile(path: string, data: string): Promise<void> {
21+
return tree.overwrite(path, data);
22+
},
23+
async isDirectory(path: string): Promise<boolean> {
24+
return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
25+
},
26+
async isFile(path: string): Promise<boolean> {
27+
return tree.exists(path);
28+
},
29+
};
30+
}
31+
32+
export async function getWorkspace(tree: Tree, path = '/') : Promise<workspaces.WorkspaceDefinition> {
33+
const host = createHost(tree);
34+
const { workspace } = await workspaces.readWorkspace(path, host);
35+
return workspace;
36+
}
37+
38+
export function updateWorkspace(
39+
updater: (workspace: workspaces.WorkspaceDefinition) => void | Rule | PromiseLike<void | Rule>,
40+
): Rule;
41+
export function updateWorkspace(workspace: workspaces.WorkspaceDefinition): Rule;
42+
export function updateWorkspace(
43+
updaterOrWorkspace:
44+
| workspaces.WorkspaceDefinition
45+
| ((workspace: workspaces.WorkspaceDefinition) => void | Rule | PromiseLike<void | Rule>),
46+
): Rule {
47+
return async (tree: Tree) => {
48+
const host = createHost(tree);
49+
50+
if (typeof updaterOrWorkspace === 'function') {
51+
const { workspace } = await workspaces.readWorkspace('/', host);
52+
53+
const result = await updaterOrWorkspace(workspace);
54+
55+
await workspaces.writeWorkspace(workspace, host);
56+
57+
return result || noop;
58+
} else {
59+
await workspaces.writeWorkspace(updaterOrWorkspace, host);
60+
61+
return noop;
62+
}
63+
};
64+
}

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@ngx-builders/analyze",
3-
"version": "3.0.0",
4-
"description": "",
3+
"version": "3.0.1",
4+
"description": "Angular Builder To Run Source Map Explorer",
55
"main": "index.js",
66
"builders": "./builders.json",
77
"schematics": "./collection.json",
@@ -43,6 +43,6 @@
4343
"dependencies": {
4444
"@angular-devkit/architect": "0.1300.0",
4545
"@angular-devkit/core": "13.0.0",
46-
"schematics-utilities": "2.0.3"
46+
"@angular-devkit/schematics": "^13.0.0"
4747
}
4848
}

0 commit comments

Comments
 (0)