Skip to content

Commit

Permalink
feat(store): register eslint-plugin-ngrx with ng add (#3014)
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed May 10, 2021
1 parent df760f8 commit 5259890
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 0 deletions.
42 changes: 42 additions & 0 deletions modules/store/schematics/ng-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('Store ng-add Schematic', () => {
);
const defaultOptions: RootStoreOptions = {
skipPackageJson: false,
skipESLintPlugin: false,
project: 'bar',
module: 'app',
minimal: false,
Expand Down Expand Up @@ -146,4 +147,45 @@ describe('Store ng-add Schematic', () => {

expect(content).toMatch(/export interface AppState {/);
});

it('should register the NgRx ESLint Plugin', async () => {
const options = { ...defaultOptions };

const initialConfig = {};
appTree.create('.eslintrc.json', JSON.stringify(initialConfig, null, 2));

const tree = await schematicRunner
.runSchematicAsync('ng-add', options, appTree)
.toPromise();

const packageContent = tree.readContent('package.json');
const packageJson = JSON.parse(packageContent);
expect(packageJson.devDependencies['eslint-plugin-ngrx']).toBeDefined();

const eslintContent = tree.readContent(`.eslintrc.json`);
const eslintJson = JSON.parse(eslintContent);
expect(eslintJson).toEqual({
extends: ['plugin:ngrx/recommended'],
plugins: ['ngrx'],
});
});

it('should not register the NgRx ESLint Plugin when skipped', async () => {
const options = { ...defaultOptions, skipESLintPlugin: true };

const initialConfig = {};
appTree.create('.eslintrc.json', JSON.stringify(initialConfig, null, 2));

const tree = await schematicRunner
.runSchematicAsync('ng-add', options, appTree)
.toPromise();

const packageContent = tree.readContent('package.json');
const packageJson = JSON.parse(packageContent);
expect(packageJson.devDependencies['eslint-plugin-ngrx']).not.toBeDefined();

const eslintContent = tree.readContent(`.eslintrc.json`);
const eslintJson = JSON.parse(eslintContent);
expect(eslintJson).toEqual(initialConfig);
});
});
48 changes: 48 additions & 0 deletions modules/store/schematics/ng-add/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,53 @@ function addNgRxStoreToPackageJson() {
};
}

function addNgRxESLintPlugin() {
return (host: Tree, context: SchematicContext) => {
const eslintConfigPath = '.eslintrc.json';
const docs =
'https://github.com/timdeschryver/eslint-plugin-ngrx/#eslint-plugin-ngrx';

const eslint = host.read(eslintConfigPath)?.toString('utf-8');
if (!eslint) {
return host;
}

addPackageToPackageJson(
host,
'devDependencies',
'eslint-plugin-ngrx',
'^1.0.0'
);
context.addTask(new NodePackageInstallTask());

try {
const json = JSON.parse(eslint);
json.plugins = [...(json.plugins || []), 'ngrx'];
json.extends = [...(json.extends || []), 'plugin:ngrx/recommended'];
host.overwrite(eslintConfigPath, JSON.stringify(json, null, 2));

context.logger.info(`
The NgRx ESLint Plugin is installed and configured with the recommended config.
If you want to change the configuration, please see ${docs}.
`);
return host;
} catch (err) {
context.logger.warn(`
Something went wrong while adding the NgRx ESLint Plugin.
The NgRx ESLint Plugin is installed but not configured.
Please see ${docs} to configure the NgRx ESLint Plugin.
Details:
${err.message}
`);
}

return host;
};
}

export default function (options: RootStoreOptions): Rule {
return (host: Tree, context: SchematicContext) => {
options.path = getProjectPath(host, options);
Expand Down Expand Up @@ -156,6 +203,7 @@ export default function (options: RootStoreOptions): Rule {
chain([addImportToNgModule(options), mergeWith(templateSource)])
),
options && options.skipPackageJson ? noop() : addNgRxStoreToPackageJson(),
options && options.skipESLintPlugin ? noop() : addNgRxESLintPlugin(),
])(host, context);
};
}
5 changes: 5 additions & 0 deletions modules/store/schematics/ng-add/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
"type": "boolean",
"default": true,
"description": "Setup state management without registering initial reducers."
},
"skipESLintPlugin": {
"type": "boolean",
"default": false,
"description": "Do not register the NgRx ESLint Plugin."
}
},
"required": []
Expand Down
1 change: 1 addition & 0 deletions modules/store/schematics/ng-add/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ export interface Schema {
* Setup state management without registering initial reducers.
*/
minimal?: boolean;
skipESLintPlugin?: boolean;
}

0 comments on commit 5259890

Please sign in to comment.