Skip to content

Commit

Permalink
refactor: cleanup schematics
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitry-stepanenko committed Sep 1, 2023
1 parent 1a9445b commit d8a7450
Show file tree
Hide file tree
Showing 75 changed files with 543 additions and 713 deletions.
1 change: 1 addition & 0 deletions integrations/tempreponx
Submodule tempreponx added at ba31de
22 changes: 11 additions & 11 deletions packages/store/schematics/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
"store": {
"factory": "./factories/store/store.factory#store",
"factory": "./src/store/store.factory#store",
"description": "Create a NGXS full store",
"aliases": ["ngxs-store"],
"schema": "./factories/store/schema.json"
"schema": "./src/store/schema.json"
},
"state": {
"factory": "./factories/state/state.factory#state",
"factory": "./src/state/state.factory#state",
"description": "Create a NGXS state",
"aliases": ["ngxs-state"],
"schema": "./factories/state/schema.json"
"schema": "./src/state/schema.json"
},
"actions": {
"factory": "./factories/actions/actions.factory#actions",
"factory": "./src/actions/actions.factory#actions",
"description": "Create a NGXS actions",
"aliases": ["ngxs-actions"],
"schema": "./factories/actions/schema.json"
"schema": "./src/actions/schema.json"
},
"starter-kit": {
"factory": "./factories/starter-kit/starter-kit.factory#starterKit",
"description": "Create a NGXS starter kit with simple or best practise type",
"factory": "./src/starter-kit/starter-kit.factory#starterKit",
"description": "Create a NGXS starter kit with simple or best practice type",
"aliases": ["ngxs-sk"],
"schema": "./factories/starter-kit/schema.json"
"schema": "./src/starter-kit/schema.json"
},
"ng-add": {
"factory": "./factories/ng-add/ng-add.factory#ngAdd",
"factory": "./src/ng-add/ng-add.factory#ngAdd",
"description": "Add Ngxs Store and plugins in package.json",
"aliases": ["ngxs-init"],
"schema": "./factories/ng-add/schema.json"
"schema": "./src/ng-add/schema.json"
}
}
}

This file was deleted.

76 changes: 0 additions & 76 deletions packages/store/schematics/factories/ng-add/ng-add.factory.spec.ts

This file was deleted.

This file was deleted.

8 changes: 0 additions & 8 deletions packages/store/schematics/factories/state/state.factory.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/store/schematics/factories/store/store.factory.ts

This file was deleted.

30 changes: 30 additions & 0 deletions packages/store/schematics/src/actions/actions.factory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { workspaceRoot } from '@nrwl/devkit';

import * as path from 'path';

import { ActionsSchema } from './actions.schema';

describe('NGXS Actions', () => {
const runner: SchematicTestRunner = new SchematicTestRunner(
'.',
path.join(workspaceRoot, 'packages/store/schematics/collection.json')
);
it('should create action in a folder by default', async () => {
const options: ActionsSchema = {
name: 'todos'
};
const tree: UnitTestTree = await runner.runSchematicAsync('actions', options).toPromise();
const files: string[] = tree.files;
expect(files).toEqual(['/todos/todos.actions.ts']);
});
it('should create action without folder if "flat" is set to "true"', async () => {
const options: ActionsSchema = {
name: 'todos',
flat: true
};
const tree: UnitTestTree = await runner.runSchematicAsync('actions', options).toPromise();
const files: string[] = tree.files;
expect(files).toEqual(['/todos.actions.ts']);
});
});
21 changes: 21 additions & 0 deletions packages/store/schematics/src/actions/actions.factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Rule, SchematicsException, url } from '@angular-devkit/schematics';
import { ActionsSchema } from './actions.schema';
import { generateFiles } from '../utils/generate-utils';
import { isEmpty } from '../utils/common/properties';
import { normalizeBaseOptions } from '../utils/normalize-options';
import { join } from 'path';

export function actions(options: ActionsSchema): Rule {
if (isEmpty(options.name)) {
throw new SchematicsException('Invalid options, "name" is required.');
}

const normalizedOptions = normalizeBaseOptions(options);
const path = options.flat
? normalizedOptions.path
: join(normalizedOptions.path, normalizedOptions.name);

return generateFiles(url('./files'), path, {
name: normalizedOptions.name
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface ActionsSchema {
*/
path?: string;
/**
* The source root path
* Flag to indicate if a dir is created.
*/
sourceRoot?: string;
flat?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
"format": "path",
"description": "The path to create the actions."
},
"sourceRoot": {
"type": "string",
"format": "path",
"description": "The source root path"
"flat": {
"type": "boolean",
"default": false,
"description": "Flag to indicate if a dir is created."
}
},
"required": ["name"]
Expand Down
111 changes: 111 additions & 0 deletions packages/store/schematics/src/ng-add/ng-add.factory.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
import { workspaceRoot } from '@nrwl/devkit';
import { join } from 'path';
import { Schema as ApplicationOptions } from '@schematics/angular/application/schema';
import { Schema as WorkspaceOptions } from '@schematics/angular/workspace/schema';

import { LIBRARIES } from '../utils/common/lib.config';
import { NgxsPackageSchema } from './ng-add.schema';

describe('Ngxs ng-add Schematic', () => {
const angularSchematicRunner = new SchematicTestRunner(
'@schematics/angular',
join(workspaceRoot, 'node_modules/@schematics/angular/collection.json')
);

const ngxsSchematicRunner = new SchematicTestRunner(
'@ngxs/store/schematics',
join(workspaceRoot, 'packages/store/schematics/collection.json')
);

const defaultOptions: NgxsPackageSchema = {
skipInstall: false,
packages: []
};

const workspaceOptions: WorkspaceOptions = {
name: 'workspace',
newProjectRoot: 'projects',
version: '1.0.0'
};

const appOptions: ApplicationOptions = {
name: 'foo',
inlineStyle: false,
inlineTemplate: false,
routing: true,
skipTests: false,
skipPackageJson: false
};

let appTree: UnitTestTree;
beforeEach(async () => {
appTree = await angularSchematicRunner
.runSchematicAsync('workspace', workspaceOptions)
.toPromise();
appTree = await angularSchematicRunner
.runSchematicAsync('application', appOptions, appTree)
.toPromise();
});

describe('importing the Ngxs module', () => {
test.each`
project
${undefined}
${'foo'}
`('should import the module when project is $project ', async ({ project }) => {
// Arrange
const options: NgxsPackageSchema = { ...defaultOptions, project };
// Act
const tree = await ngxsSchematicRunner
.runSchematicAsync('ngxs-init', options, appTree)
.toPromise();
// Assert
const content = tree.readContent('/projects/foo/src/app/app.module.ts');
expect(content).toMatch(/import { NgxsModule } from '@ngxs\/store'/);
expect(content).toMatch(/imports: \[[^\]]*NgxsModule.forRoot\(\[\],[^\]]*\]/m);
expect(content).toContain(
'NgxsModule.forRoot([], { developmentMode: /** !environment.production */ false, selectorOptions: { suppressErrors: false, injectContainerState: false } })'
);
});
it('should throw if invalid project is specified', async () => {
// Arrange
const options: NgxsPackageSchema = { ...defaultOptions, project: 'hello' };
await expect(
ngxsSchematicRunner.runSchematicAsync('ng-add', options, appTree).toPromise()
).rejects.toThrow(`Project "${options.project}" does not exist.`);
});
});

describe('ng-add package in package.json', () => {
it('should add ngxs store with provided plugins in package.json', async () => {
const packages = [LIBRARIES.DEVTOOLS, LIBRARIES.LOGGER];
const options: NgxsPackageSchema = { packages };
appTree = await ngxsSchematicRunner
.runSchematicAsync('ng-add', options, appTree)
.toPromise();
const packageJsonText = appTree.readContent('/package.json');
const packageJson = JSON.parse(packageJsonText);
expect(packages.every(p => !!packageJson.dependencies[p])).toBeTruthy();
});

it('should add ngxs store with all plugins in package.json', async () => {
const packages = Object.values(LIBRARIES).filter(v => v !== LIBRARIES.STORE);
const options: NgxsPackageSchema = { packages };
appTree = await ngxsSchematicRunner
.runSchematicAsync('ng-add', options, appTree)
.toPromise();
const packageJsonText = appTree.readContent('/package.json');
const packageJson = JSON.parse(packageJsonText);
expect(packages.every(p => !!packageJson.dependencies[p])).toBeTruthy();
});

it('should not attempt to add non-existent package', async () => {
const packages = ['who-am-i'];
const options: NgxsPackageSchema = { packages };
await expect(
ngxsSchematicRunner.runSchematicAsync('ng-add', options, appTree).toPromise()
).rejects.toThrow();
});
});
});
Loading

0 comments on commit d8a7450

Please sign in to comment.