Issue with collection cache name when using FileSystemEngineHost #285
Description
Bug Report or Feature Request (mark with an x
)
- [x] bug report -> please search issues before submitting
- [ ] feature request
Area
- [ ] devkit
- [x] schematics
Versions
"@angular-devkit/schematics": "0.0.36",
Repro steps
FileSystemEngineHost
cannot resolve collection.json
in subfolder. For example, with @nrwl/schematics
it's in @nrwl/schematics/src/collection.json
. I extended it and used this;
import { sync as globSync } from 'glob';
export class MyEngineHost extends FileSystemEngineHost {
constructor(protected _root: string) { super(_root); }
protected _resolveCollectionPath(name: string): string {
// Allow `${_root}/${name}.json` as a collection.
if (existsSync(join(this._root, name + '.json'))) {
return join(this._root, name + '.json');
}
// Allow `${_root}/${name}/collection.json.
if (existsSync(join(this._root, name, 'collection.json'))) {
return join(this._root, name, 'collection.json');
}
// Allow `${_root}/ ** /${name}/collection.json.
const collectionJsonPath = globSync(`${ name }/**/collection.json`, {
cwd: this._root
})[ 0 ];
if (collectionJsonPath) {
return join(this._root, collectionJsonPath);
}
throw new CollectionCannotBeResolvedException(name);
}
}
Next, I created host
, engine
and collection
:
const host = new MyEngineHost('my_root');
host.registerOptionsTransform(/* ... */)
const engine = new SchematicEngine(host);
const collection = engine.createCollection('@nrwl/schematics');
So far everything works:
host.listSchematics(collection); /* OK */
but when I try to create schematics:
const schematic = collection.createSchematic('app');
I get an error Error: Unknown collection "nx".
The log given by the failure
It turns out that createSchematic()
is using name
from @nrwl/schematics/src/collection.json
which is set to nx
, but is caching it using @nrwl/schematics
;
Desired functionality
Since name
in collection.json
is optional (it's not set in @schematics/angular
) I believe collection name should be used and not name
from collection.json
.
Mention any other details that might be useful
Chaning FileSystemEngineHost _transformCollectionDescription()
to match NodeModulesEngineHost _transformCollectionDescription()
should solve the issue
// return desc as FileSystemCollectionDesc;
return {
...desc,
name,
} as FileSystemCollectionDesc;