scss-variable-extractor 1.0.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +304 -12
- package/bin/cli.js +399 -13
- package/package.json +8 -4
- package/src/angular-parser.js +381 -0
- package/src/bootstrap-migrator.js +602 -0
- package/src/config.js +11 -2
- package/src/index.js +11 -1
- package/src/ng-refactorer.js +578 -0
- package/src/scanner.js +5 -4
- package/test/angular-parser.test.js +230 -0
- package/test/bootstrap-migrator.test.js +175 -0
- package/test/fixtures/angular.json +123 -0
- package/test/fixtures/apps/subapp/src/app/bootstrap-component/bootstrap-component.component.html +36 -0
- package/test/fixtures/apps/subapp/src/app/component-c/component-c.component.scss +47 -0
- package/test/ng-refactorer.test.js +184 -0
package/README.md
CHANGED
|
@@ -9,7 +9,11 @@ A standalone, reusable CLI tool that analyzes Angular project SCSS files, identi
|
|
|
9
9
|
🔍 **Smart Pattern Recognition** - Detects colors, spacing, fonts, shadows, and more
|
|
10
10
|
📊 **Multiple Report Formats** - Table, JSON, or Markdown output
|
|
11
11
|
🛡️ **Safe Refactoring** - Preserves existing variables and avoids unsafe replacements
|
|
12
|
-
⚙️ **Highly Configurable** - Customize via `.scssextractrc.json`
|
|
12
|
+
⚙️ **Highly Configurable** - Customize via `.scssextractrc.json`
|
|
13
|
+
🔧 **Modernization Tools** - Removes `::ng-deep` and `!important` anti-patterns
|
|
14
|
+
🚀 **Global Scope Scanning** - Analyze any folder in your project
|
|
15
|
+
📐 **Angular.json Integration** - Automatically detects project structure and settings
|
|
16
|
+
🔄 **Bootstrap to Material Migration** - Converts Bootstrap classes to Angular Material MDC components and utilities
|
|
13
17
|
|
|
14
18
|
## Installation
|
|
15
19
|
|
|
@@ -37,12 +41,18 @@ npm install
|
|
|
37
41
|
|
|
38
42
|
## Quick Start
|
|
39
43
|
|
|
44
|
+
> **💡 Tip:** If you have an `angular.json` file, you can omit the source path - the tool will auto-detect your project structure!
|
|
45
|
+
|
|
40
46
|
### 1. Analyze (Dry Run)
|
|
41
47
|
|
|
42
48
|
See what values would be extracted without modifying any files:
|
|
43
49
|
|
|
44
50
|
```bash
|
|
45
|
-
|
|
51
|
+
# With angular.json (auto-detects project)
|
|
52
|
+
npx scss-extract analyze
|
|
53
|
+
|
|
54
|
+
# Or specify path manually
|
|
55
|
+
npx scss-extract analyze ./apps/subapp/src --threshold 2
|
|
46
56
|
```
|
|
47
57
|
|
|
48
58
|
### 2. Generate Variables File
|
|
@@ -50,7 +60,11 @@ npx scss-extract analyze --src ./apps/subapp/src --threshold 2
|
|
|
50
60
|
Create a `_variables.scss` file with extracted variables:
|
|
51
61
|
|
|
52
62
|
```bash
|
|
53
|
-
|
|
63
|
+
# With angular.json
|
|
64
|
+
npx scss-extract generate
|
|
65
|
+
|
|
66
|
+
# Or specify paths manually
|
|
67
|
+
npx scss-extract generate ./apps/subapp/src --output ./libs/styles/_variables.scss
|
|
54
68
|
```
|
|
55
69
|
|
|
56
70
|
### 3. Full Refactoring
|
|
@@ -58,7 +72,19 @@ npx scss-extract generate --src ./apps/subapp/src --output ./libs/styles/_variab
|
|
|
58
72
|
Generate variables file AND replace hardcoded values in all SCSS files:
|
|
59
73
|
|
|
60
74
|
```bash
|
|
61
|
-
npx scss-extract refactor
|
|
75
|
+
npx scss-extract refactor ./apps/subapp/src --output ./libs/styles/_variables.scss
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### 4. Modernize Code (Remove Anti-Patterns)
|
|
79
|
+
|
|
80
|
+
Remove `::ng-deep` and `!important` following Angular Material v15+ best practices:
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
# Preview changes first
|
|
84
|
+
npx scss-extract modernize ./src --dry-run
|
|
85
|
+
|
|
86
|
+
# Apply modernization
|
|
87
|
+
npx scss-extract modernize ./src
|
|
62
88
|
```
|
|
63
89
|
|
|
64
90
|
## CLI Commands
|
|
@@ -68,18 +94,20 @@ npx scss-extract refactor --src ./apps/subapp/src --output ./libs/styles/_variab
|
|
|
68
94
|
Scans SCSS files and reports repeated values without modifying anything.
|
|
69
95
|
|
|
70
96
|
```bash
|
|
71
|
-
npx scss-extract analyze [options]
|
|
97
|
+
npx scss-extract analyze <src> [options]
|
|
72
98
|
```
|
|
73
99
|
|
|
100
|
+
**Arguments:**
|
|
101
|
+
- `<src>` - Source directory to scan (required)
|
|
102
|
+
|
|
74
103
|
**Options:**
|
|
75
|
-
- `--src <path>` - Source directory to scan (default: `./apps/subapp/src`)
|
|
76
104
|
- `--threshold <number>` - Minimum repeat count (default: `2`)
|
|
77
105
|
- `--format <format>` - Report format: `table`, `json`, `markdown` (default: `table`)
|
|
78
106
|
- `--config <path>` - Path to custom config file
|
|
79
107
|
|
|
80
108
|
**Example:**
|
|
81
109
|
```bash
|
|
82
|
-
npx scss-extract analyze
|
|
110
|
+
npx scss-extract analyze ./src --threshold 3 --format markdown
|
|
83
111
|
```
|
|
84
112
|
|
|
85
113
|
### `generate` - Generate Variables File
|
|
@@ -87,18 +115,20 @@ npx scss-extract analyze --src ./src --threshold 3 --format markdown
|
|
|
87
115
|
Creates a `_variables.scss` file with extracted variables.
|
|
88
116
|
|
|
89
117
|
```bash
|
|
90
|
-
npx scss-extract generate [options]
|
|
118
|
+
npx scss-extract generate <src> [options]
|
|
91
119
|
```
|
|
92
120
|
|
|
121
|
+
**Arguments:**
|
|
122
|
+
- `<src>` - Source directory to scan (required)
|
|
123
|
+
|
|
93
124
|
**Options:**
|
|
94
|
-
- `--src <path>` - Source directory to scan
|
|
95
125
|
- `--output <path>` - Output path for variables file (default: `./libs/styles/_variables.scss`)
|
|
96
126
|
- `--threshold <number>` - Minimum repeat count
|
|
97
127
|
- `--config <path>` - Path to custom config file
|
|
98
128
|
|
|
99
129
|
**Example:**
|
|
100
130
|
```bash
|
|
101
|
-
npx scss-extract generate
|
|
131
|
+
npx scss-extract generate ./apps/myapp/src --output ./styles/_variables.scss
|
|
102
132
|
```
|
|
103
133
|
|
|
104
134
|
### `refactor` - Full Extraction + Replacement
|
|
@@ -106,11 +136,13 @@ npx scss-extract generate --src ./apps/myapp/src --output ./styles/_variables.sc
|
|
|
106
136
|
Generates variables file AND refactors SCSS files to use variables.
|
|
107
137
|
|
|
108
138
|
```bash
|
|
109
|
-
npx scss-extract refactor [options]
|
|
139
|
+
npx scss-extract refactor <src> [options]
|
|
110
140
|
```
|
|
111
141
|
|
|
142
|
+
**Arguments:**
|
|
143
|
+
- `<src>` - Source directory to scan (required)
|
|
144
|
+
|
|
112
145
|
**Options:**
|
|
113
|
-
- `--src <path>` - Source directory to scan
|
|
114
146
|
- `--output <path>` - Output path for variables file
|
|
115
147
|
- `--threshold <number>` - Minimum repeat count
|
|
116
148
|
- `--config <path>` - Path to custom config file
|
|
@@ -120,6 +152,266 @@ npx scss-extract refactor [options]
|
|
|
120
152
|
npx scss-extract refactor --src ./apps/subapp/src --output ./libs/styles/_variables.scss
|
|
121
153
|
```
|
|
122
154
|
|
|
155
|
+
### `analyze-patterns` - Analyze Angular Anti-Patterns
|
|
156
|
+
|
|
157
|
+
Scans SCSS files for Angular Material v15+ anti-patterns like `::ng-deep` and `!important` usage.
|
|
158
|
+
|
|
159
|
+
```bash
|
|
160
|
+
npx scss-extract analyze-patterns <src> [options]
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
**Arguments:**
|
|
164
|
+
- `<src>` - Source directory to scan (required)
|
|
165
|
+
|
|
166
|
+
**Options:**
|
|
167
|
+
- `--format <format>` - Report format: `table`, `json`, `markdown` (default: `table`)
|
|
168
|
+
- `--config <path>` - Path to custom config file
|
|
169
|
+
|
|
170
|
+
**Example:**
|
|
171
|
+
```bash
|
|
172
|
+
npx scss-extract analyze-patterns ./src --format table
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
### `modernize` - Refactor to Angular Material v15+ Best Practices
|
|
176
|
+
|
|
177
|
+
Automatically refactors SCSS files by removing `::ng-deep`, `/deep/`, `>>>`, and `!important` declarations, following Angular Material v15+ best practices.
|
|
178
|
+
|
|
179
|
+
```bash
|
|
180
|
+
npx scss-extract modernize <src> [options]
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
**Arguments:**
|
|
184
|
+
- `<src>` - Source directory to scan (required)
|
|
185
|
+
|
|
186
|
+
**Options:**
|
|
187
|
+
- `--global-styles <path>` - Path to global styles file (default: `./src/styles.scss`)
|
|
188
|
+
- `--no-ng-deep` - Skip ::ng-deep refactoring
|
|
189
|
+
- `--no-important` - Skip !important refactoring
|
|
190
|
+
- `--dry-run` - Preview changes without modifying files
|
|
191
|
+
- `--config <path>` - Path to custom config file
|
|
192
|
+
|
|
193
|
+
**Example:**
|
|
194
|
+
```bash
|
|
195
|
+
# Preview changes
|
|
196
|
+
npx scss-extract modernize ./src --dry-run
|
|
197
|
+
|
|
198
|
+
# Apply changes
|
|
199
|
+
npx scss-extract modernize ./src --global-styles ./src/styles.scss
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
**What it does:**
|
|
203
|
+
- ✅ Removes `::ng-deep`, `/deep/`, and `>>>` (deprecated shadow-piercing selectors)
|
|
204
|
+
- ✅ Removes `!important` declarations (suggests increasing specificity instead)
|
|
205
|
+
- ✅ Extracts styles that were using `::ng-deep` to global styles file
|
|
206
|
+
- ✅ Provides warnings and suggestions for manual review
|
|
207
|
+
|
|
208
|
+
**Angular Material v15+ Best Practices:**
|
|
209
|
+
- Use CSS custom properties for theming
|
|
210
|
+
- Use Angular Material theme mixins for component customization
|
|
211
|
+
- Place global styles in `styles.scss` instead of using `::ng-deep`
|
|
212
|
+
- Increase selector specificity instead of using `!important`
|
|
213
|
+
- Consider `ViewEncapsulation.None` for components that need global styles
|
|
214
|
+
|
|
215
|
+
## Angular.json Integration
|
|
216
|
+
|
|
217
|
+
The tool can automatically read your `angular.json` file to detect project structure and apply the correct schematics for style refactoring.
|
|
218
|
+
|
|
219
|
+
### Auto-detection
|
|
220
|
+
|
|
221
|
+
By default, all commands will look for `angular.json` in the current directory and use it to configure:
|
|
222
|
+
- Source directory (`sourceRoot`)
|
|
223
|
+
- Output path for variables (based on global styles)
|
|
224
|
+
- Style preprocessor (scss, sass, less, css)
|
|
225
|
+
- Component prefix
|
|
226
|
+
- Ignore patterns (`.angular`, `dist`, etc.)
|
|
227
|
+
|
|
228
|
+
### Usage
|
|
229
|
+
|
|
230
|
+
**Without angular.json (manual configuration):**
|
|
231
|
+
```bash
|
|
232
|
+
scss-extract analyze ./apps/myapp/src
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
**With angular.json (auto-configured):**
|
|
236
|
+
```bash
|
|
237
|
+
# Uses default project from angular.json
|
|
238
|
+
scss-extract analyze
|
|
239
|
+
|
|
240
|
+
# Use specific project
|
|
241
|
+
scss-extract analyze --project myapp
|
|
242
|
+
|
|
243
|
+
# Specify angular.json location
|
|
244
|
+
scss-extract analyze --angular-json ./path/to/angular.json
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
**Disable angular.json integration:**
|
|
248
|
+
```bash
|
|
249
|
+
scss-extract analyze ./src --no-angular
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### All Commands Support Angular.json
|
|
253
|
+
|
|
254
|
+
Every command supports these options:
|
|
255
|
+
- `--angular-json <path>` - Path to angular.json file
|
|
256
|
+
- `--project <name>` - Specify which project to use
|
|
257
|
+
- `--no-angular` - Disable angular.json integration
|
|
258
|
+
|
|
259
|
+
**Examples:**
|
|
260
|
+
```bash
|
|
261
|
+
# Analyze with angular.json
|
|
262
|
+
scss-extract analyze --project frontend
|
|
263
|
+
|
|
264
|
+
# Generate with specific project
|
|
265
|
+
scss-extract generate --project admin-app
|
|
266
|
+
|
|
267
|
+
# Modernize with angular.json auto-detection
|
|
268
|
+
scss-extract modernize --dry-run
|
|
269
|
+
|
|
270
|
+
# Refactor specific project
|
|
271
|
+
scss-extract refactor --project mylib --output ./libs/mylib/styles/_variables.scss
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
### What Gets Auto-Configured
|
|
275
|
+
|
|
276
|
+
When angular.json is detected, the tool automatically sets:
|
|
277
|
+
|
|
278
|
+
| Config | Source |
|
|
279
|
+
|--------|--------|
|
|
280
|
+
| **src** | `project.sourceRoot` (e.g., `apps/myapp/src`) |
|
|
281
|
+
| **output** | Derived from global styles location |
|
|
282
|
+
| **styleExt** | From project schematics (scss, sass, etc.) |
|
|
283
|
+
| **prefix** | Component prefix (e.g., `app`, `lib`) |
|
|
284
|
+
| **ignore** | Adds `.angular/`, `dist/`, `out-tsc/` |
|
|
285
|
+
|
|
286
|
+
The tool will display the detected Angular project info:
|
|
287
|
+
```
|
|
288
|
+
Angular Project: myapp
|
|
289
|
+
Prefix: app
|
|
290
|
+
Style: scss
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
## Bootstrap to Angular Material Migration
|
|
294
|
+
|
|
295
|
+
Automatically detect and migrate Bootstrap classes to Angular Material MDC-based components and utilities. For flex/grid/spacing classes, the tool generates custom utilities.
|
|
296
|
+
|
|
297
|
+
### Detect Bootstrap Usage
|
|
298
|
+
|
|
299
|
+
Analyze your codebase for Bootstrap classes and get a comprehensive report:
|
|
300
|
+
|
|
301
|
+
```bash
|
|
302
|
+
npx scss-extract detect-bootstrap [src]
|
|
303
|
+
|
|
304
|
+
# With output format
|
|
305
|
+
npx scss-extract detect-bootstrap ./src --format table
|
|
306
|
+
npx scss-extract detect-bootstrap ./src --format json
|
|
307
|
+
npx scss-extract detect-bootstrap ./src --format markdown
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
**Arguments:**
|
|
311
|
+
- `[src]` - Source directory to scan (optional, auto-detected from angular.json)
|
|
312
|
+
|
|
313
|
+
**Options:**
|
|
314
|
+
- `--format <type>` - Output format: table, json, markdown (default: table)
|
|
315
|
+
- `--angular-json <path>` - Path to angular.json file
|
|
316
|
+
- `--project <name>` - Specify which project to use
|
|
317
|
+
- `--no-angular` - Disable angular.json integration
|
|
318
|
+
|
|
319
|
+
**Example Output:**
|
|
320
|
+
```
|
|
321
|
+
Bootstrap Usage Report
|
|
322
|
+
┌────────────┬───────┬─────────────────────────────────────────┐
|
|
323
|
+
│ Category │ Count │ Classes │
|
|
324
|
+
├────────────┼───────┼─────────────────────────────────────────┤
|
|
325
|
+
│ Components │ 5 │ btn, card, form-control, navbar, modal │
|
|
326
|
+
│ Utilities │ 12 │ d-flex, m-3, p-2, text-center, etc. │
|
|
327
|
+
└────────────┴───────┴─────────────────────────────────────────┘
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
### Migrate Bootstrap to Material
|
|
331
|
+
|
|
332
|
+
Convert Bootstrap classes to Angular Material equivalents:
|
|
333
|
+
|
|
334
|
+
```bash
|
|
335
|
+
npx scss-extract migrate-bootstrap [src]
|
|
336
|
+
|
|
337
|
+
# With options
|
|
338
|
+
npx scss-extract migrate-bootstrap ./src --custom-utilities ./src/utilities.scss --dry-run
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
**Arguments:**
|
|
342
|
+
- `[src]` - Source directory to scan (optional, auto-detected from angular.json)
|
|
343
|
+
|
|
344
|
+
**Options:**
|
|
345
|
+
- `--custom-utilities <path>` - Path for custom utilities file (default: `./src/utilities.scss`)
|
|
346
|
+
- `--dry-run` - Preview changes without modifying files
|
|
347
|
+
- `--angular-json <path>` - Path to angular.json file
|
|
348
|
+
- `--project <name>` - Specify which project to use
|
|
349
|
+
- `--no-angular` - Disable angular.json integration
|
|
350
|
+
|
|
351
|
+
**What it does:**
|
|
352
|
+
|
|
353
|
+
✅ **Component Migrations:**
|
|
354
|
+
- `btn` → `mat-button` / `mat-raised-button` / `mat-stroked-button`
|
|
355
|
+
- `card` → `mat-card` with `mat-card-header`, `mat-card-content`, `mat-card-actions`
|
|
356
|
+
- `form-control` → `mat-form-field` with `matInput`
|
|
357
|
+
- `navbar` → `mat-toolbar`
|
|
358
|
+
- `modal` → `mat-dialog`
|
|
359
|
+
- And 50+ more component mappings
|
|
360
|
+
|
|
361
|
+
✅ **Utility Migrations:**
|
|
362
|
+
- `d-flex` → Custom flex utilities
|
|
363
|
+
- `justify-content-*` → Custom flexbox utilities
|
|
364
|
+
- `align-items-*` → Custom flexbox utilities
|
|
365
|
+
- `m-*`, `p-*` → Custom spacing utilities (0-5 scale)
|
|
366
|
+
- `text-*` → Custom text utilities
|
|
367
|
+
- `w-*`, `h-*` → Custom sizing utilities
|
|
368
|
+
|
|
369
|
+
✅ **Generated Custom Utilities:**
|
|
370
|
+
Creates a `utilities.scss` file with Material Design-aligned spacing:
|
|
371
|
+
```scss
|
|
372
|
+
// Spacing utilities (0, 4px, 8px, 16px, 24px, 48px)
|
|
373
|
+
.m-0 { margin: 0; }
|
|
374
|
+
.m-1 { margin: 4px; }
|
|
375
|
+
.m-2 { margin: 8px; }
|
|
376
|
+
.m-3 { margin: 16px; }
|
|
377
|
+
.p-t-2 { padding-top: 8px; }
|
|
378
|
+
|
|
379
|
+
// Flexbox utilities
|
|
380
|
+
.d-flex { display: flex; }
|
|
381
|
+
.justify-content-center { justify-content: center; }
|
|
382
|
+
|
|
383
|
+
// Display utilities
|
|
384
|
+
.d-block { display: block; }
|
|
385
|
+
.d-none { display: none; }
|
|
386
|
+
|
|
387
|
+
// Text utilities
|
|
388
|
+
.text-center { text-align: center; }
|
|
389
|
+
.fw-bold { font-weight: 700; }
|
|
390
|
+
```
|
|
391
|
+
|
|
392
|
+
**Migration Warnings:**
|
|
393
|
+
|
|
394
|
+
The tool provides warnings for manual review items:
|
|
395
|
+
- ⚠️ Bootstrap grid system → Material Layout or CSS Grid
|
|
396
|
+
- ⚠️ Bootstrap forms → Material form fields (requires template changes)
|
|
397
|
+
- ⚠️ Bootstrap modals → Material dialogs (requires component refactoring)
|
|
398
|
+
- ⚠️ Complex component compositions → Manual Material implementation
|
|
399
|
+
|
|
400
|
+
**Example Workflow:**
|
|
401
|
+
```bash
|
|
402
|
+
# 1. Detect Bootstrap usage
|
|
403
|
+
npx scss-extract detect-bootstrap --format table
|
|
404
|
+
|
|
405
|
+
# 2. Preview migration
|
|
406
|
+
npx scss-extract migrate-bootstrap --dry-run
|
|
407
|
+
|
|
408
|
+
# 3. Apply migration
|
|
409
|
+
npx scss-extract migrate-bootstrap
|
|
410
|
+
|
|
411
|
+
# 4. Review changes and test components
|
|
412
|
+
# Manual review of generated utilities.scss and component templates
|
|
413
|
+
```
|
|
414
|
+
|
|
123
415
|
## Configuration
|
|
124
416
|
|
|
125
417
|
Create a `.scssextractrc.json` file in your project root:
|