nitayneeman / schematics-utilities Goto Github PK
View Code? Open in Web Editor NEW๐ ๏ธ Useful exported utilities for working with Schematics
Home Page: https://nitayneeman.github.io/schematics-utilities
License: MIT License
๐ ๏ธ Useful exported utilities for working with Schematics
Home Page: https://nitayneeman.github.io/schematics-utilities
License: MIT License
I have written the following code by peeking into the angular/schematics/component/index.js code
const declarationChanges = addDeclarationToModule(sourceFile, newLocal, `List${strings.classify(_options.name)}Component`, `./components/general-configuration/${dasherize(_options.name)}/list/list-${dasherize(_options.name)}.component`);
const declarationRecorder = tree.beginUpdate(newLocal);
for (const change of declarationChanges) {
if (change instanceof InsertChange) {
_context.logger.info(change.toAdd);
declarationRecorder.insertLeft(change.pos, change.toAdd);
}
}
tree.commitUpdate(declarationRecorder);
I get the following output
UPDATE src/app/app.module.ts (1959 bytes)
but the file still has no changes in it. Any idea what's going wrong here ?
I just wanted to give you a heads up that depending upon the Angular CDK
schematics-utilities/package.json
Line 40 in 96bf7f4
I think that it would be safer to copy the schematic code from the CDK over into this repo just like you do with Angular Material.
We're going to try to put some checks in place in Angular Material's schematics, so that this conflict doesn't break someone's project. But the reality is that projects shouldn't be pulling in @angular/cdk
in order to use the private schematic helpers. They need to copy them into their own project (and take over maintenance of them in their project) instead.
The version for @angular-devkit/schematics is currently fixed, which pulls in a version of @angular-devkit/core that is broken and doesn't compile due to a typing error. It would be good to update the dependency and republish.
I use the the function addModuleImportToRootModule
in the following way:
addModuleImportToRootModule(
host,
`MyModule.forRoot(
{
option1: '<value of option1>',
option2: '<value of option2>',
})`,
'@mycompany/my-module',
project
);
As result, I get an invalid import statement in my app.module.ts. The module will be added correctly to the NgModule, but create an invalid import statement:
import { MyModule.forRoot(
{
option1: '<value of option1>',
option2: '<value of option2>',
}) } from '@mycompany/my-module';
Is there a way to to solve this problem?
Thanks!
When the providers
key of NgModule
ends with a provider object (like {provide: '', useValue: ''}
) the method addProviderToModule
has an unexpected result :
@NgModule({
providers: [
{ provide: '', useValue: '', providers: [{ provide: TOKEN, useValue: 'someValue' }] }
]
})
I've run some tests :
providers: [SomeProvider]
providers: [{ provide: '', useValue: '' } as Provider]
providers: [{ provide: '', useValue: '' }, SomeProvider]
providers: [{ provide: '', useValue: '' }]
providers: [SomeProvider, { provide: '', useValue: '' }]
I'm using this library as part of @oktadev/schematics. Today, I tried upgrading to Angular 10 and now I get the following stack trace when I try to install my schematic. It seems like the error is caused by this project, but I'm not sure.
Cannot find module '@schematics/angular/utility/project'
Require stack:
- /Users/mraible/ng10/node_modules/@angular/cdk/schematics/utils/build-component.js
- /Users/mraible/ng10/node_modules/@angular/cdk/schematics/utils/index.js
- /Users/mraible/ng10/node_modules/@angular/cdk/schematics/index.js
- /Users/mraible/ng10/node_modules/schematics-utilities/dist/material/ast/ng-module-imports.js
- /Users/mraible/ng10/node_modules/schematics-utilities/dist/material/index.js
- /Users/mraible/ng10/node_modules/schematics-utilities/dist/index.js
- /Users/mraible/ng10/node_modules/@oktadev/schematics/src/add-auth/index.js
- /Users/mraible/ng10/node_modules/@angular-devkit/schematics/tools/export-ref.js
- /Users/mraible/ng10/node_modules/@angular-devkit/schematics/tools/index.js
- /Users/mraible/ng10/node_modules/@angular/cli/utilities/json-schema.js
- /Users/mraible/ng10/node_modules/@angular/cli/models/command-runner.js
- /Users/mraible/ng10/node_modules/@angular/cli/lib/cli/index.js
- /usr/local/lib/node_modules/@angular/cli/lib/init.js
- /usr/local/lib/node_modules/@angular/cli/bin/ng
The current version of @schematics/update
still uses init 1.3.5 but this contains a vulnerability.
for eg
if this is my code
import { NodeDependency, NodeDependencyType } from 'schematics-utilities';
const dependencies: NodeDependency[] = [
{ type: NodeDependencyType.Dev, version: '^23.6.0', name: 'jest' },
{ type: NodeDependencyType.Dev, version: '^3.0.0', name: '@angular/material' }
];
can I add the registry for that dependency like this?
{ type: NodeDependencyType.Dev, version: '^3.0.0', name: '@angular/material' , registry: 'www.google.com/material'}
Or is there any other way to do this?
Hi, @nitayneeman, I'd like to report a vulnerability introduced by package [email protected]:
I noticed that a vulnerability is introduced in [email protected] _:
Vulnerability CVE-2020-7788 (high severity) affects package ini (versions:<1.3.6): https://snyk.io/vuln/SNYK-JS-INI-1048974
The above vulnerable package is referenced by [email protected] via:
[email protected] โ @schematics/[email protected] โ [email protected]
Since [email protected] (20,943 downloads per week) is referenced by 37 downstream projects (e.g., @datorama/akita 6.2.3 (latest version), @scullyio/init 1.1.4 (latest version), @sentinel-one/mgmt-schematics 2.0.10 (latest version), @flarehr/apollo-benefits 2.2.19 (latest version), @flarehr/cars-calculator 2.2.19 (latest version)), the vulnerability CVE-2020-7788 can be propagated into these downstream projects and expose security threats to them via the following package dependency paths:
(1)@datorama/[email protected] โ [email protected] โ @schematics/[email protected] โ [email protected]
(2)@flarehr/[email protected] โ @datorama/[email protected] โ [email protected] โ @schematics/[email protected] โ [email protected]
......
If [email protected].* removes the vulnerable package from the above version, then its fixed version can help downstream users decrease their pain.
Given the large number of downstream users, could you help update your package to remove the vulnerability from [email protected] ?
In [email protected], maybe you can kindly try to perform the following upgrade(not crossing major version) :
@schematics/update ^0.803.8 โ ^0.1100.5
;
Note:
@schematics/[email protected](>=0.1100.5) directly depends on [email protected] which has fixed the vulnerability CVE-2020-7788.
Of course, you are welcome to share other ways to resolve the issue.
Thank you for your attention to this issue.
Best regards,
^_^
Hi dude,
Can you update @angular/cdk (@angular/components) package and release new version?
There are some nice changes there
Would it be possible to switch from using dependencies to peerDependencies? The dependency graph for schematics-utilities is pretty big: http://npm.broofa.com/[email protected]
When trying to edit file '/tsconfig.json', there found no suitable functions.
Considering add the follow function(from here):
export function getJSON(host: Tree, jsonFile: string, type?: string): any {
if (!host.exists(jsonFile)) return null;
const sourceText = host.read(jsonFile)!.toString('utf-8');
const json = JSON.parse(sourceText);
if (type && !json[type]) {
json[type] = {};
}
return json;
}
Maybe it's should be classified as 'third-lib'
I've recently added ng add
support to angular-datatables
project and my PR for it had been merged recently but I'm getting the following error when trying to install the package to clean Angular project.
Error:
An unhandled exception occurred: Cannot find module 'schematics-utilities'
Stack Trace:
[error] Error: Cannot find module 'schematics-utilities'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (/home/suryateja/myproject/node_modules/angular-datatables/schematics/src/ng-add/index.js:4:32)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at new ExportStringRef (/home/suryateja/myproject/node_modules/@angular-devkit/schematics/tools/export-ref.js:18:25)
at NodeModulesEngineHost._resolveReferenceString (/home/suryateja/myproject/node_modules/@angular-devkit/schematics/tools/node-module-engine-host.js:94:21)
at NodeModulesEngineHost.createSchematicDescription (/home/suryateja/myproject/node_modules/@angular-devkit/schematics/tools/file-system-engine-host-base.js:179:34)
at SchematicEngine.createSchematic (/home/suryateja/myproject/node_modules/@angular-devkit/schematics/src/engine/engine.js:219:38)
Steps to Reproduce:
ng new myproject
cd myproject
ng add angular-datatables
If I try to call this function or use the mentioned type, I get this error:
Cannot find module 'chalk'
Error: Cannot find module 'chalk'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:668:15)
Even if the function doesn't get called but it's part of the code, I'm getting the same error. It only works if I comment the function call.
I tried
npm cache verify
npm ci
npm i chalk
No change. Is this a bug? It seems the error is generated by @angular/cdk/schematics/ng-update/upgrade-rules/class-inheritance-rule.js:10:17
Is it possible to fix this?
See builds in this PR with an upgrade to 2.0.1 (fails) and subsequent downgrade to 2.0.0 (passes).
KO in 2.0.0 and 2.0.1
OK in 1.1.3
Here is my rule factory function
import {
Rule, Tree/*, SchematicContext*/
} from '@angular-devkit/schematics';
import { addModuleImportToRootModule, getProjectFromWorkspace } from 'schematics-utilities';
import { getWorkspace } from '@schematics/angular/utility/config';
export function ngAdd(options: any): Rule {
return (host: Tree/*, context: SchematicContext*/) => {
// get the workspace config of the consuming project
// i.e. angular.json file
const workspace = getWorkspace(host);
// identify the project config which is using our library
// or default to the default project in consumer workspace
const project = getProjectFromWorkspace(
workspace,
options.project || workspace.defaultProject
);
// inject our module into the current main module of the selected project
addModuleImportToRootModule(
// tree to modify
host,
// Module name to insert
'AngularCoreModule',
// project name for import statement
'@corp/angular-core',
// project to be modified
project
);
// return updated tree
return host;
};
}
Here is what I got in console
Installing packages for tooling via npm.
Installed packages for tooling via npm.
UPDATE src/app/app.module.ts (1333 bytes)
But my app.module.ts is absolutely the same as before.
I could downgrade to 1.1.3 but I need to set configuration for my modules, but then I got this issue:
What am I supposed to do?
P.S. Where I can find a comprehensive list of the API your package expoes? Thank you?
After I upgrade from "typescript": "~3.5.3" to "typescript": "~3.8.2" I start to get
the following error:
src/schematics-utils/schematics.utils.ts:39:33 - error TS2345: Argument of type 'import("/../node_modules/typescript/lib/typescript").SourceFile' is not assignable to parameter of type 'import("/../node_modules/schematics-utilities/node_modules/@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript").SourceFile'.
Types of property 'kind' are incompatible.
Type 'import("/../node_modules/typescript/lib/typescript").SyntaxKind.SourceFile' is not assignable to type 'import("/../node_modules/schematics-utilities/node_modules/@schematics/angular/third_party/github.com/Microsoft/TypeScript/lib/typescript").SyntaxKind.SourceFile'.
39 const change = insertImport(sourceFile, pathFileToEdit, importSymbolName, importPath, isDefault) as InsertChange;
~~~~~~~~~~
I have a utils imports in my code
import * as ts from 'typescript';
import { InsertChange, insertImport } from 'schematics-utilities';
}
in case I'm replace the type ts.SourceFile to any the build work but I want to keep the type
I think it happen since the
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.