Skip to content

Commit

Permalink
Add initial version of angular plugin (closes #138)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Sep 24, 2023
1 parent 8ab8992 commit 14f3e98
Show file tree
Hide file tree
Showing 14 changed files with 2,474 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ This is especially useful over time when such configuration files change (and th

Knip contains a growing list of plugins:

- [Angular][plugin-angular]
- [Ava][plugin-ava]
- [Babel][plugin-babel]
- [Capacitor][plugin-capacitor]
Expand Down Expand Up @@ -864,6 +865,7 @@ Special thanks to the wonderful people who have contributed to this project:
[71]: https://www.joshuakgoldberg.com/blog/speeding-up-centered-part-4-unused-code-bloat/
[72]: https://github.com/webpro/knip/graphs/contributors
[73]: https://contrib.rocks/image?repo=webpro/knip
[plugin-angular]: ./src/plugins/angular
[plugin-ava]: ./src/plugins/ava
[plugin-babel]: ./src/plugins/babel
[plugin-capacitor]: ./src/plugins/capacitor
Expand Down
104 changes: 104 additions & 0 deletions fixtures/plugins/angular/angular.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"knip-angular-example": {
"projectType": "application",
"schematics": {
"@schematics/angular:component": {
"style": "scss"
}
},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/knip-angular-example",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": [
"zone.js"
],
"tsConfig": "tsconfig.app.json",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"outputHashing": "all"
},
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"browserTarget": "knip-angular-example:build:production"
},
"development": {
"browserTarget": "knip-angular-example:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "knip-angular-example:build"
}
},
"test": {
"builder": "@angular-devkit/build-angular:karma",
"options": {
"polyfills": [
"zone.js",
"zone.js/testing"
],
"tsConfig": "tsconfig.spec.json",
"inlineStyleLanguage": "scss",
"assets": [
"src/favicon.ico",
"src/assets"
],
"styles": [
"src/styles.scss"
],
"scripts": []
}
}
}
}
}
}
11 changes: 11 additions & 0 deletions fixtures/plugins/angular/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@fixtures/angular",
"version": "*",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development",
"test": "ng test"
}
}
6 changes: 6 additions & 0 deletions fixtures/plugins/angular/tsconfig.app.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"types": []
}
}
12 changes: 12 additions & 0 deletions fixtures/plugins/angular/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"baseUrl": "./",
"lib": ["ES2022", "dom"]
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}
6 changes: 6 additions & 0 deletions fixtures/plugins/angular/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"types": ["jasmine"]
}
}
4 changes: 4 additions & 0 deletions schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@
},
"plugins": {
"properties": {
"angular": {
"title": "angular plugin configuration (https://github.com/webpro/knip/blob/main/src/plugins/angular/README.md)",
"$ref": "#/definitions/plugin"
},
"ava": {
"title": "ava plugin configuration (https://github.com/webpro/knip/blob/main/src/plugins/ava/README.md)",
"$ref": "#/definitions/plugin"
Expand Down
1 change: 1 addition & 0 deletions src/ConfigurationValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const pluginSchema = z.union([
]);

const pluginsSchema = z.object({
angular: pluginSchema,
ava: pluginSchema,
babel: pluginSchema,
capacitor: pluginSchema,
Expand Down
22 changes: 22 additions & 0 deletions src/plugins/angular/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Angular

## Enabled

This plugin is enabled when any of the following package names and/or regular expressions has a match in `dependencies`
or `devDependencies`:

- `@angular/cli`

## Default configuration

```json
{
"angular": {
"config": ["angular.json"]
}
}
```

Also see [Knip plugins][1] for more information about plugins.

[1]: https://github.com/webpro/knip/blob/main/README.md#plugins
47 changes: 47 additions & 0 deletions src/plugins/angular/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { join } from '../../util/path.js';
import { timerify } from '../../util/Performance.js';
import { hasDependency, load } from '../../util/plugin.js';
import { findTypeScriptDependencies } from '../typescript/index.js';
import type { AngularCLIWorkspaceConfiguration } from './types.js';
import type { IsPluginEnabledCallback, GenericPluginCallback } from '../../types/plugins.js';

// link to docs

export const NAME = 'Angular';

/** @public */
export const ENABLERS = ['@angular/cli'];

export const isEnabled: IsPluginEnabledCallback = ({ dependencies }) => hasDependency(dependencies, ENABLERS);

export const CONFIG_FILE_PATTERNS = ['angular.json'];

const findPluginDependencies: GenericPluginCallback = async (configFilePath, opts) => {
const { cwd } = opts;
const config: AngularCLIWorkspaceConfiguration = await load(configFilePath);
if (!config.projects) return [];

const dependencies = new Set<string>();

for (const project of Object.values(config.projects)) {
if (!project.architect) return [];
for (const target of Object.values(project.architect)) {
const { options } = target;
const [packageName] = typeof target.builder === 'string' ? target.builder.split(':') : [];
if (typeof packageName === 'string') dependencies.add(packageName);
if (options) {
if ('main' in options && typeof options?.main === 'string') {
dependencies.add(join(cwd, options.main));
}
if ('tsConfig' in options && typeof options.tsConfig === 'string') {
const tsConfigDependencies = await findTypeScriptDependencies(join(cwd, options.tsConfig), opts);
tsConfigDependencies.forEach(dependency => dependencies.add(dependency));
}
}
}
}

return Array.from(dependencies);
};

export const findDependencies = timerify(findPluginDependencies);
Loading

0 comments on commit 14f3e98

Please sign in to comment.