angulartypescriptangular-schematics

Angular schematics error when using external schematic ng-new, version required


Im trying to create a schematic that uses the angular ng-new as first call and my other schematic that adds a prettier file to the new project. When i execute the command i get the error

  Data path "" must have required property 'version'.

So i have to add --version="x" to the command. Is there a way of using this external schematic without having to add the version so it takes automatically the latest version as ng new does?

This is my schematic

import {
  chain,
  externalSchematic,
  Rule,
  schematic,
  SchematicContext,
  Tree,
} from "@angular-devkit/schematics";

import { Schema as NgNewOptions } from "@schematics/angular/ng-new/schema";

export function newProject(options: NgNewOptions): Rule {
  return async (_tree: Tree, _context: SchematicContext) => {
    const rule = externalSchematic("@schematics/angular", "ng-new", options);
    const rule2 = schematic("add-prettier-configuration", options);

    return chain([rule, rule2]);
  };
}

and this my the collection

{
  "$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
  "extends": ["@schematics/angular"],
  "schematics": {
    "hello-world": {
      "description": "A blank schematic.",
      "factory": "./hello-world/index#helloWorld"
    },
    "goodbye-world": {
      "description": "A blank schematic.",
      "factory": "./goodbye-world/index#goodbyeWorld"
    },
    "add-prettier-configuration": {
      "description": "add a prettier configuration file.",
      "factory": "./add-prettier-configuration/index#addPrettierConfiguration",
      "schema": "./add-prettier-configuration/schema.json"
    },
    "new-project": {
      "description": "A blank schematic.",
      "factory": "./new-project/index#newProject",
      "schema": "./new-project/schema.json"
    }
  }
}

when i execute the new-project schematic it starts asking the name of the project but when the "new" schematic should start it doesnt giving the next error:

 schematics ./hello-world:new-project
Debug mode enabled by default for local collections.
? What name would you like to use for the new workspace and initial project? iuhiuh+
An error occured:
Error: Schematic input does not validate against the Schema: {"name":"iuhiuh+"}
Errors:

  Data path "" must have required property 'version'.
    at MapSubscriber.project (C:\Users\ijimenlu\AppData\Roaming\npm\node_modules\@angular-devkit\schematics-cli\node_modules\@angular-devkit\schematics\tools\schema-option-transform.js:30:27)
    at MapSubscriber._next (C:\Users\ijimenlu\AppData\Roaming\npm\node_modules\@angular-devkit\schematics-cli\node_modules\rxjs\internal\operators\map.js:49:35)
    at MapSubscriber.Subscriber.next (C:\Users\ijimenlu\AppData\Roaming\npm\node_modules\@angular-devkit\schematics-cli\node_modules\rxjs\internal\Subscriber.js:66:18)
    at ThrowIfEmptySubscriber._next (C:\Users\ijimenlu\AppData\Roaming\npm\node_modules\@angular-devkit\schematics-cli\node_modules\rxjs\internal\operators\throwIfEmpty.js:44:26)
    at ThrowIfEmptySubscriber.Subscriber.next (C:\Users\ijimenlu\AppData\Roaming\npm\node_modules\@angular-devkit\schematics-cli\node_modules\rxjs\internal\Subscriber.js:66:18)
    at TakeSubscriber._next (C:\Users\ijimenlu\AppData\Roaming\npm\node_modules\@angular-devkit\schematics-cli\node_modules\rxjs\internal\operators\take.js:54:30)
    at TakeSubscriber.Subscriber.next (C:\Users\ijimenlu\AppData\Roaming\npm\node_modules\@angular-devkit\schematics-cli\node_modules\rxjs\internal\Subscriber.js:66:18)
    at MergeMapSubscriber.notifyNext (C:\Users\ijimenlu\AppData\Roaming\npm\node_modules\@angular-devkit\schematics-cli\node_modules\rxjs\internal\operators\mergeMap.js:93:26)
    at SimpleInnerSubscriber._next (C:\Users\ijimenlu\AppData\Roaming\npm\node_modules\@angular-devkit\schematics-cli\node_modules\rxjs\internal\innerSubscribe.js:27:21)
    at SimpleInnerSubscriber.Subscriber.next (C:\Users\ijimenlu\AppData\Roaming\npm\node_modules\@angular-devkit\schematics-cli\node_modules\rxjs\internal\Subscriber.js:66:18)

Solution

  • I finally find a solution, yo have to use it as an ng command. I changed new-project to ng-new and i used it like this:

    ng new --collection ./hello-world