Skip to content

Commit

Permalink
feat: add local package.json to schematics collection (#298)
Browse files Browse the repository at this point in the history
Check local package.json for the schematics attribute. when available add the path to the collection. Fixed the create of the e2e fixture.
  • Loading branch information
Richard Roozenboom authored and mrmeku committed Nov 9, 2018
1 parent 53c1448 commit 33b85fa
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 5 deletions.
71 changes: 71 additions & 0 deletions apps/angular-console-e2e/src/integration/local-collection.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import {
checkDisplayedCommand,
clickOnTask,
goBack,
goToGenerate,
openProject,
projectPath,
taskListHeaders,
tasks,
texts,
uniqName,
whitelistGraphql
} from './utils';
import { clearRecentTask } from './tasks.utils';

describe('Project with local collection', () => {
beforeEach(() => {
whitelistGraphql();
cy.visit('/workspaces');
openProject(projectPath('proj-local-collection'));
goToGenerate();
cy.get('div.title').contains('Generate Code');
});

it('shows the local schematics', () => {
taskListHeaders($p => {
expect($p.length).to.equal(3);
expect(texts($p)[0]).to.equal('.proj-local-collection');
});

tasks($p => {
const t = texts($p);
expect(t.indexOf('my-full-schematic') > -1).to.equal(true);
});
});

it('runs a local schematic', () => {
clickOnTask('.proj-local-collection', 'my-full-schematic');
cy.get('div.context-title').contains(
'A schematic using a source and a schema to validate options'
);

const name = uniqName('example');
cy.get('input[name="name"]').type(name);

cy.wait(100);

cy.get('button')
.contains('Generate')
.click();

cy.wait(100);

checkDisplayedCommand(`$ ng generate .:my-full-schematic --name=${name}`);
cy.readFile('./../../tmp/proj-local-collection/hola');
cy.readFile('./../../tmp/proj-local-collection/allo');

goBack();

cy.get('div.title').contains('Generate Code');
taskListHeaders($p => {
expect(texts($p)[0]).to.equal('.proj-local-collection');
});
});

after(() => {
cy.visit('/workspaces');
openProject(projectPath('proj-local-collection'));
clearRecentTask();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,11 @@ export class SchematicComponent implements OnInit {
}

getPrefix(schematic: Schematic) {
return [`${schematic.collection}:${schematic.name}`];
return [
`${schematic.collection.startsWith('.') ? '.' : schematic.collection}:${
schematic.name
}`
];
}

path() {
Expand Down
14 changes: 10 additions & 4 deletions server/src/api/read-schematic-collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ interface Schematic {
export function readAllSchematicCollections(basedir: string) {
const nodeModulesDir = path.join(basedir, 'node_modules');
const packages = listOfUnnestedNpmPackages(nodeModulesDir);
const schematicCollections = packages.filter(p => {
const relativeRootPath = './..';
const schematicCollections = [relativeRootPath, ...packages].filter(p => {
try {
return !!readJsonFile(path.join(p, 'package.json'), nodeModulesDir).json
.schematics;
Expand All @@ -47,12 +48,13 @@ export function readAllSchematicCollections(basedir: string) {
}
});
return schematicCollections
.map(c => readSchematicCollections(nodeModulesDir, c))
.map(c => readSchematicCollections(nodeModulesDir, relativeRootPath, c))
.filter(collection => !!collection && collection.schematics.length > 0);
}

function readSchematicCollections(
basedir: string,
rootCollection: string,
collectionName: string
): SchematicCollection {
try {
Expand All @@ -64,8 +66,12 @@ function readSchematicCollections(
packageJson.json.schematics,
path.dirname(packageJson.path)
);
const name =
collectionName === rootCollection
? `.${packageJson.json.name}`
: collectionName;
const schematicCollection = {
name: collectionName,
name,
schematics: [] as Schematic[]
};
Object.entries(collection.json.schematics).forEach(([k, v]: [any, any]) => {
Expand All @@ -77,7 +83,7 @@ function readSchematicCollections(

schematicCollection.schematics.push({
name: k,
collection: collectionName,
collection: name,
schema: normalizeSchema(schematicSchema.json),
description: v.description
});
Expand Down
13 changes: 13 additions & 0 deletions tools/scripts/set-up-e2e-fixtures.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ shell.rm('-rf', path.join(tmp, 'proj-extensions'));
shell.mkdir(path.join(tmp, 'proj-extensions'));
shell.rm('-rf', path.join(tmp, 'proj-no-node-modules'));
shell.mkdir(path.join(tmp, 'proj-no-node-modules'));
shell.rm('-rf', path.join(tmp, 'proj-local-collection'));
shell.mkdir(path.join(tmp, 'proj-local-collection'));

shell.rm('-rf', path.join(tmp, 'ng'));
shell.mkdir(path.join(tmp, 'ng'));
cp.execSync('yarn add @angular/[email protected]', {cwd: path.join(tmp, 'ng')});
cp.execSync('yarn add @angular-devkit/schematics-cli @schematics/schematics', {cwd: path.join(tmp, 'ng')});
cp.execSync('yarn add json', {cwd: path.join(tmp, 'ng')});

cp.execSync('ng config -g cli.packageManager yarn');
cp.execSync(`${path.join(tmp, 'ng')}/node_modules/.bin/ng new proj --collection=@schematics/angular --directory=proj --skip-git --no-interactive`, { cwd: tmp, stdio: [0, 1, 2] });
Expand All @@ -28,3 +32,12 @@ shell.mv(path.join(tmp, 'proj-extensions'), './tmp/proj-extensions');

cp.execSync(`${path.join(tmp, 'ng')}/node_modules/.bin/ng new proj-no-node-modules --collection=@schematics/angular --directory=proj-no-node-modules --skip-install --skip-git --no-interactive`, { cwd: tmp, stdio: [0, 1, 2] });
shell.mv(path.join(tmp, 'proj-no-node-modules'), './tmp/proj-no-node-modules');

cp.execSync(`${path.join(tmp, 'ng')}/node_modules/.bin/ng new proj-local-collection --collection=@schematics/angular --directory=proj-local-collection --skip-git --no-interactive`, { cwd: tmp, stdio: [0, 1, 2] });
shell.mv(path.join(tmp, 'proj-local-collection'), './tmp/proj-local-collection');
shell.cd('./tmp/proj-local-collection');
cp.execSync(`${path.join(tmp, 'ng')}/node_modules/.bin/schematics schematic --name=schematics .`);
cp.execSync(`${path.join(tmp, 'ng')}/node_modules/.bin/json -I -f package.json -e 'this.schematics="./schematics/src/collection.json"'`);
cp.execSync('ng config cli.defaultCollection schematics')
shell.cd('schematics');
cp.execSync(`${path.join(tmp, 'ng')}/node_modules/.bin/tsc`);

0 comments on commit 33b85fa

Please sign in to comment.