Skip to content

Commit

Permalink
feat: removed "schematics-utilities" package and dependent code (#91)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
patelvimal authored Jan 6, 2022
1 parent 4dff6e6 commit b445981
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 45 deletions.
2 changes: 1 addition & 1 deletion collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"schematics": {
"ng-add": {
"description": "Builder to analyze package using source-map-explorer",
"factory": "./ng-add/index",
"factory": "./ng-add/index#ngAdd",
"schema": "./ng-add/schema.json",
"aliases": [
"install"
Expand Down
64 changes: 25 additions & 39 deletions ng-add/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { Rule, SchematicContext, SchematicsException, Tree } from '@angular-devkit/schematics';
import { getWorkspace, getWorkspacePath, ProjectType, WorkspaceProject } from 'schematics-utilities';
import { TargetDefinition } from '@angular-devkit/core/src/workspace';
import { chain, Rule, SchematicsException, Tree } from '@angular-devkit/schematics';
import { NgAddOptions } from './schema';
import { getWorkspace, updateWorkspace } from './workspace';


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

// getting project name
// Get project name
if (!options.project) {
if (workspaceSchema && workspaceSchema.defaultProject) {
options.project = workspaceSchema.defaultProject;
if (workspace.extensions.defaultProject) {
options.project = workspace.extensions.defaultProject as string;
} else {
throw new SchematicsException(
'No Angular project selected and no default project in the workspace'
Expand All @@ -21,45 +20,32 @@ export function sourceMapBuilder(options: NgAddOptions): Rule {
}

// Validating project name
const project: WorkspaceProject<ProjectType.Application> = workspaceSchema.projects[options.project];
const project = workspace.projects.get(options.project);
if (!project) {
throw new SchematicsException(
'The specified Angular project is not defined in this workspace'
);
throw new SchematicsException(`The specified Angular project is not defined in this workspace`);
}

// Checking if it is application
if (project.projectType !== 'application') {
throw new SchematicsException(
`source-map-analyzer requires an Angular project type of "application" in angular.json`
);
if (project.extensions['projectType'] !== 'application') {
throw new SchematicsException(`source-map-analyzer requires an Angular project type of "application" in angular.json`);
}

const outputPath: string | undefined = project.targets.get('build')?.options?.outputPath as string;

// Getting output path from Angular.json
if (
!project.architect ||
!project.architect.build ||
!project.architect.build.options ||
!project.architect.build.options.outputPath
) {
throw new SchematicsException(
`Cannot read the output path(architect.build.options.outputPath) of the Angular project "${options.project}" in angular.json`
);
if (!outputPath) {
const message: string = `Cannot read the output path(architect.build.options.outputPath) of the Angular project "${options.project}" in angular.json`;
throw new SchematicsException(message);
}

// adding deploy statement for builder
project.architect['analyze'] = {
"builder": "@ngx-builders/analyze:analyze",
"options": {
"outputPath": project.architect.build.options.outputPath
var targetDefinition: TargetDefinition = {
builder: "@ngx-builders/analyze:analyze",
options: {
outputPath: outputPath
}
}

tree.overwrite(workspacePath, JSON.stringify(workspaceSchema, null, 2));
return tree;
};
}
project.targets.add({ name: 'analyze', ...targetDefinition });

export default function (options: NgAddOptions): Rule {
return sourceMapBuilder(options)
return chain([updateWorkspace(workspace)]);
};
}
2 changes: 0 additions & 2 deletions ng-add/schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export interface NgAddOptions {
project: string | undefined;
siteID: string;
netlifyToken: string;
}
64 changes: 64 additions & 0 deletions ng-add/workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { virtualFs, workspaces } from '@angular-devkit/core';
import { noop, Rule, SchematicsException, Tree } from '@angular-devkit/schematics';

/* Below code reference is taken from Angular CLI project.
https://github.com/angular/angular-cli/blob/master/packages/schematics/angular/utility/workspace.ts
These methods are not part of public APIs so we should not be referencing those methods.
that's why added below method here.
*/

function createHost(tree: Tree): workspaces.WorkspaceHost {
return {
async readFile(path: string): Promise<string> {
const data = tree.read(path);
if (!data) {
throw new SchematicsException('File not found.');
}
return virtualFs.fileBufferToString(data);
},
async writeFile(path: string, data: string): Promise<void> {
return tree.overwrite(path, data);
},
async isDirectory(path: string): Promise<boolean> {
return !tree.exists(path) && tree.getDir(path).subfiles.length > 0;
},
async isFile(path: string): Promise<boolean> {
return tree.exists(path);
},
};
}

export async function getWorkspace(tree: Tree, path = '/') : Promise<workspaces.WorkspaceDefinition> {
const host = createHost(tree);
const { workspace } = await workspaces.readWorkspace(path, host);
return workspace;
}

export function updateWorkspace(
updater: (workspace: workspaces.WorkspaceDefinition) => void | Rule | PromiseLike<void | Rule>,
): Rule;
export function updateWorkspace(workspace: workspaces.WorkspaceDefinition): Rule;
export function updateWorkspace(
updaterOrWorkspace:
| workspaces.WorkspaceDefinition
| ((workspace: workspaces.WorkspaceDefinition) => void | Rule | PromiseLike<void | Rule>),
): Rule {
return async (tree: Tree) => {
const host = createHost(tree);

if (typeof updaterOrWorkspace === 'function') {
const { workspace } = await workspaces.readWorkspace('/', host);

const result = await updaterOrWorkspace(workspace);

await workspaces.writeWorkspace(workspace, host);

return result || noop;
} else {
await workspaces.writeWorkspace(updaterOrWorkspace, host);

return noop;
}
};
}
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@ngx-builders/analyze",
"version": "3.0.0",
"description": "",
"version": "3.0.1",
"description": "Angular Builder To Run Source Map Explorer",
"main": "index.js",
"builders": "./builders.json",
"schematics": "./collection.json",
Expand Down Expand Up @@ -43,6 +43,6 @@
"dependencies": {
"@angular-devkit/architect": "0.1300.0",
"@angular-devkit/core": "13.0.0",
"schematics-utilities": "2.0.3"
"@angular-devkit/schematics": "^13.0.0"
}
}

0 comments on commit b445981

Please sign in to comment.