Skip to content

Commit

Permalink
feat: New schematics installer mechanism (#15459)
Browse files Browse the repository at this point in the history

closes #15380
  • Loading branch information
znikola committed Jun 8, 2022
1 parent 8bd7fcc commit 60188a8
Show file tree
Hide file tree
Showing 213 changed files with 7,967 additions and 4,759 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@
"name": "Debug schematics",
"skipFiles": ["<node_internals>/**"],
"program": "${workspaceFolder}/node_modules/@angular/cli/bin/ng",
"args": ["add", "@spartacus/schematics@latest"],
"args": ["add", "--skip-confirmation", "@spartacus/schematics@latest"],
"console": "integratedTerminal",
"outFiles": ["${workspaceFolder}/**/*.js"]
"outFiles": ["${workspaceFolder}/node_modules/@spartacus/**/*.js"]
},

// to debug a schematics Jest test, make sure that a spec.ts file is currently opened.
Expand Down
24 changes: 14 additions & 10 deletions docs/libs/creating-lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ This document can also serve as the guideline for the future schematic that can

## Table of contents

- [Naming conventions](#Naming-conventions)
- [Generating a library](#Generating-a-library)
- [Aligning with the other libs](#Aligning-with-the-other-libs)
- [Modifying the generated files](#Modifying-the-generated-files)
- [Additional changes to existing files](#Additional-changes-to-existing-files)
- [Multi-entry point library](#multi-entry-point-library)
- [Testing](#Testing)
- [Schematics](#Schematics)
- [Configuring Schematics](#Configuring Schematics)
- [Testing Schematics](#Testing Schematics)
- [Creating a Spartacus library](#creating-a-spartacus-library)
- [Table of contents](#table-of-contents)
- [Naming conventions](#naming-conventions)
- [Generating a library](#generating-a-library)
- [Aligning with the other libs](#aligning-with-the-other-libs)
- [Modifying the generated files](#modifying-the-generated-files)
- [Additional changes to existing files](#additional-changes-to-existing-files)
- [Multi-entry point library](#multi-entry-point-library)
- [Process](#process)
- [Testing](#testing)
- [Schematics](#schematics)
- [Configuring Schematics](#configuring-schematics)
- [Testing Schematics](#testing-schematics)

## Naming conventions

Expand Down Expand Up @@ -384,6 +387,7 @@ There are couple of required changes to make sure schematics will work properly
- add new feature lib schema.json elements in schematics folder - `feature-libs\<lib-name>\schematics\add-<lib-name>\schema.json` where the `lib-name` is the name of the new library
- add new feature chain method to 'shouldAddFeature' and function to add it - `feature-libs\<lib-name>\schematics\add-<lib-name>\index.ts` where the `lib-name` is the name of the new library
- create new feature lib module in - `projects/storefrontapp/src/app/spartacus/features`
- create your schematics configuration in e.g. `projects/schematics/src/shared/lib-configs/asm-schematics-config.ts` and add it to the `projects/schematics/src/shared/schematics-config-mappings.ts` file.


### Testing Schematics
Expand Down
4 changes: 4 additions & 0 deletions docs/migration/5_0.md
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,7 @@ Due to i18next migration, certain set of keys have been migrated (from `_plural`
#### Template Changes

- Changed `<ng-container *ngIf="(typeSelected$ | async) && !(isUpdating$ | async); else loading">` to `<ng-container *ngIf="!!paymentTypes.length && (typeSelected$ | async) && !(isUpdating$ | async); else loading">`

## Schematics

`Account` and `Profile` CLI names have been changed to `User-Account` and `User-Profile`, respectively, to better reflect their purpose.
60 changes: 12 additions & 48 deletions feature-libs/asm/schematics/add-asm/index.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,37 @@
import {
chain,
noop,
Rule,
SchematicContext,
Tree,
} from '@angular-devkit/schematics';
import {
addLibraryFeature,
addFeatures,
addPackageJsonDependenciesForLibrary,
ASM_MODULE,
ASM_ROOT_MODULE,
CLI_ASM_FEATURE,
analyzeApplication,
analyzeCrossFeatureDependencies,
finalizeInstallation,
LibraryOptions as SpartacusAsmOptions,
readPackageJson,
shouldAddFeature,
SPARTACUS_ASM,
validateSpartacusInstallation,
} from '@spartacus/schematics';
import { peerDependencies } from '../../package.json';
import {
ASM_FEATURE_NAME_CONSTANT,
ASM_FOLDER_NAME,
ASM_MODULE_NAME,
ASM_TRANSLATIONS,
ASM_TRANSLATION_CHUNKS_CONFIG,
SPARTACUS_ASM_ASSETS,
SPARTACUS_ASM_ROOT,
SCSS_FILE_NAME,
} from '../constants';

export function addAsmFeatures(options: SpartacusAsmOptions): Rule {
return (tree: Tree, _context: SchematicContext): Rule => {
const packageJson = readPackageJson(tree);
validateSpartacusInstallation(packageJson);

const features = analyzeCrossFeatureDependencies(
options.features as string[]
);

return chain([
analyzeApplication(options, features),

addFeatures(options, features),
addPackageJsonDependenciesForLibrary(peerDependencies, options),

shouldAddFeature(CLI_ASM_FEATURE, options.features)
? addAsmFeature(options)
: noop(),
finalizeInstallation(options, features),
]);
};
}

function addAsmFeature(options: SpartacusAsmOptions): Rule {
return addLibraryFeature(options, {
folderName: ASM_FOLDER_NAME,
moduleName: ASM_MODULE_NAME,
featureModule: {
name: ASM_MODULE,
importPath: SPARTACUS_ASM,
},
rootModule: {
name: ASM_ROOT_MODULE,
importPath: SPARTACUS_ASM_ROOT,
},
lazyLoadingChunk: {
moduleSpecifier: SPARTACUS_ASM_ROOT,
namedImports: [ASM_FEATURE_NAME_CONSTANT],
},
styles: {
scssFileName: SCSS_FILE_NAME,
importStyle: SPARTACUS_ASM,
},
i18n: {
resources: ASM_TRANSLATIONS,
chunks: ASM_TRANSLATION_CHUNKS_CONFIG,
importPath: SPARTACUS_ASM_ASSETS,
},
});
}
25 changes: 17 additions & 8 deletions feature-libs/asm/schematics/add-asm/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,25 @@ import {
} from '@schematics/angular/application/schema';
import { Schema as WorkspaceOptions } from '@schematics/angular/workspace/schema';
import {
CLI_ASM_FEATURE,
asmFeatureModulePath,
ASM_FEATURE_NAME,
LibraryOptions as SpartacusAsmOptions,
SpartacusOptions,
SPARTACUS_ASM,
SPARTACUS_SCHEMATICS,
userFeatureModulePath,
} from '@spartacus/schematics';
import * as path from 'path';
import { peerDependencies } from '../../package.json';

const collectionPath = path.join(__dirname, '../collection.json');
const featureModulePath =
'src/app/spartacus/features/asm/asm-feature.module.ts';
const scssFilePath = 'src/styles/spartacus/asm.scss';

describe('Spartacus Asm schematics: ng-add', () => {
const schematicRunner = new SchematicTestRunner('schematics', collectionPath);
const schematicRunner = new SchematicTestRunner(
SPARTACUS_ASM,
collectionPath
);

let appTree: UnitTestTree;

Expand Down Expand Up @@ -57,7 +61,7 @@ describe('Spartacus Asm schematics: ng-add', () => {

const asmFeatureOptions: SpartacusAsmOptions = {
...libraryNoFeaturesOptions,
features: [CLI_ASM_FEATURE],
features: [ASM_FEATURE_NAME],
};

beforeEach(async () => {
Expand Down Expand Up @@ -99,7 +103,7 @@ describe('Spartacus Asm schematics: ng-add', () => {
});

it('should not create any of the feature modules', () => {
expect(appTree.exists(featureModulePath)).toBeFalsy();
expect(appTree.exists(asmFeatureModulePath)).toBeFalsy();
});

it('should install necessary Spartacus libraries', () => {
Expand Down Expand Up @@ -137,10 +141,15 @@ describe('Spartacus Asm schematics: ng-add', () => {
});

it('should add the feature using the lazy loading syntax', async () => {
const module = appTree.readContent(featureModulePath);
const module = appTree.readContent(asmFeatureModulePath);
expect(module).toMatchSnapshot();
});

it('should NOT install the required feature dependencies', async () => {
const userFeatureModule = appTree.readContent(userFeatureModulePath);
expect(userFeatureModule).toBeFalsy();
});

describe('styling', () => {
it('should create a proper scss file', () => {
const scssContent = appTree.readContent(scssFilePath);
Expand Down Expand Up @@ -169,7 +178,7 @@ describe('Spartacus Asm schematics: ng-add', () => {
});

it('should import appropriate modules', async () => {
const module = appTree.readContent(featureModulePath);
const module = appTree.readContent(asmFeatureModulePath);
expect(module).toMatchSnapshot();
});
});
Expand Down
12 changes: 0 additions & 12 deletions feature-libs/asm/schematics/add-library/index.ts

This file was deleted.

6 changes: 0 additions & 6 deletions feature-libs/asm/schematics/collection.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
"hidden": true,
"aliases": ["install"]
},
"add-spartacus-library": {
"description": "Install a spartacus library",
"factory": "./add-library/index#addSpartacusLibrary",
"private": true,
"hidden": true
},
"add": {
"factory": "./add-asm/index#addAsmFeatures",
"description": "Add and configure Spartacus' Asm features",
Expand Down
11 changes: 0 additions & 11 deletions feature-libs/asm/schematics/constants.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,20 @@ import { CmsConfig, I18nConfig, provideConfig } from \\"@spartacus/core\\";
[CART_BASE_FEATURE]: {
module: () =>
import('@spartacus/cart/base').then((m) => m.CartBaseModule),
}, [MINI_CART_FEATURE]: {
},
}
}),
provideConfig(<CmsConfig>{
featureModules: {
[MINI_CART_FEATURE]: {
module: () =>
import('@spartacus/cart/base/components/mini-cart').then((m) => m.MiniCartModule),
}, [ADD_TO_CART_FEATURE]: {
},
}
}),
provideConfig(<CmsConfig>{
featureModules: {
[ADD_TO_CART_FEATURE]: {
module: () =>
import('@spartacus/cart/base/components/add-to-cart').then((m) => m.AddToCartModule),
},
Expand Down Expand Up @@ -193,7 +203,7 @@ exports[`Spartacus Cart schematics: ng-add Cart Base feature general setup styli
}"
`;
exports[`Spartacus Cart schematics: ng-add Quick Order feature Cart Import Export feature eager loading should import appropriate modules 1`] = `
exports[`Spartacus Cart schematics: ng-add Cart Import Export feature eager loading should import appropriate modules 1`] = `
"import { NgModule } from '@angular/core';
import { ImportExportModule } from \\"@spartacus/cart/import-export\\";
import { importExportTranslationChunksConfig, importExportTranslations } from \\"@spartacus/cart/import-export/assets\\";
Expand All @@ -217,7 +227,7 @@ export class CartImportExportFeatureModule { }
"
`;
exports[`Spartacus Cart schematics: ng-add Quick Order feature Cart Import Export feature general setup should add the feature using the lazy loading syntax 1`] = `
exports[`Spartacus Cart schematics: ng-add Cart Import Export feature general setup should add the feature using the lazy loading syntax 1`] = `
"import { NgModule } from '@angular/core';
import { importExportTranslationChunksConfig, importExportTranslations } from \\"@spartacus/cart/import-export/assets\\";
import { CART_IMPORT_EXPORT_FEATURE, ImportExportRootModule } from \\"@spartacus/cart/import-export/root\\";
Expand Down Expand Up @@ -248,9 +258,9 @@ export class CartImportExportFeatureModule { }
"
`;
exports[`Spartacus Cart schematics: ng-add Quick Order feature Cart Import Export feature general setup styling should create a proper scss file 1`] = `"@import \\"@spartacus/cart\\";"`;
exports[`Spartacus Cart schematics: ng-add Cart Import Export feature general setup styling should create a proper scss file 1`] = `"@import \\"@spartacus/cart\\";"`;
exports[`Spartacus Cart schematics: ng-add Quick Order feature Cart Import Export feature general setup styling should update angular.json 1`] = `
exports[`Spartacus Cart schematics: ng-add Cart Import Export feature general setup styling should update angular.json 1`] = `
"{
\\"$schema\\": \\"./node_modules/@angular/cli/lib/config/schema.json\\",
\\"version\\": 1,
Expand Down Expand Up @@ -784,7 +794,12 @@ import { CmsConfig, I18nConfig, provideConfig } from \\"@spartacus/core\\";
[CART_WISH_LIST_FEATURE]: {
module: () =>
import('@spartacus/cart/wish-list').then((m) => m.WishListModule),
}, [ADD_TO_WISHLIST_FEATURE]: {
},
}
}),
provideConfig(<CmsConfig>{
featureModules: {
[ADD_TO_WISHLIST_FEATURE]: {
module: () =>
import('@spartacus/cart/wish-list/components/add-to-wishlist').then((m) => m.AddToWishListModule),
},
Expand Down
Loading

0 comments on commit 60188a8

Please sign in to comment.