1
+ import * as fs from "fs" ;
2
+ import * as path from "path" ;
1
3
import * as vscode from "vscode" ;
2
- import { activeFileName , getSelectedText , getSelectionOffsetRange , importMissingDependencies , } from "../editor" ;
3
- import { getAllTargets , } from "../template-parser" ;
4
- import { showFilePicker } from "../file-picker" ;
5
- import { createFileIfDoesntExist , persistFileSystemChanges , replaceTextInFile , } from "../file-system" ;
6
- import { pascalCase } from "change-case" ;
7
- import { appendSelectedTextToFile , replaceSelectionWith , } from "../code-actions" ;
8
- import { showDirectoryPicker } from "../directories-picker" ;
9
- import { getComponentInstance , getComponentText , getSpecText , } from "./extract-to-folder-template" ;
10
-
11
- const fs = require ( "fs" ) ;
12
- const path = require ( "path" ) ;
4
+ import {
5
+ activeFileName ,
6
+ getSelectedText ,
7
+ getSelectionOffsetRange ,
8
+ importMissingDependencies ,
9
+ } from "../editor" ;
10
+ import { getAllTargets } from "../template-parser" ;
11
+ import { showFilePicker } from "../file-picker" ;
12
+ import {
13
+ createFileIfDoesntExist ,
14
+ persistFileSystemChanges ,
15
+ replaceTextInFile ,
16
+ } from "../file-system" ;
17
+ import { pascalCase } from "change-case" ;
18
+ import {
19
+ appendSelectedTextToFile ,
20
+ replaceSelectionWith ,
21
+ } from "../code-actions" ;
22
+ import { showDirectoryPicker } from "../directories-picker" ;
23
+ import {
24
+ getComponentInstance ,
25
+ getComponentText ,
26
+ getSpecText ,
27
+ } from "./extract-to-folder-template" ;
13
28
14
29
export async function extractToFolder ( ) {
15
- const { start, end} = getSelectionOffsetRange ( ) ;
30
+ const { start, end } = getSelectionOffsetRange ( ) ;
16
31
17
32
if ( start && end ) {
18
33
try {
@@ -31,9 +46,10 @@ export async function extractToFolder() {
31
46
32
47
const componentName = parts [ parts . length - 1 ] ;
33
48
49
+ const styleExt = await getStyleExt ( ) ;
34
50
35
51
const htmlFilePath = `${ filePath } /${ componentName } .component.html` ;
36
- const cssFilePath = `${ filePath } /${ componentName } .component.css ` ;
52
+ const cssFilePath = `${ filePath } /${ componentName } .component.${ styleExt } ` ;
37
53
const tsFilePath = `${ filePath } /${ componentName } .component.ts` ;
38
54
const specFilePath = `${ filePath } /${ componentName } .component.spec.ts` ;
39
55
@@ -42,14 +58,14 @@ export async function extractToFolder() {
42
58
await createFileIfDoesntExist ( tsFilePath ) ;
43
59
await createFileIfDoesntExist ( specFilePath ) ;
44
60
45
- await appendSelectedTextToFile ( { text} , htmlFilePath ) ;
46
- await appendSelectedTextToFile ( { text : `` } , cssFilePath ) ;
61
+ await appendSelectedTextToFile ( { text } , htmlFilePath ) ;
62
+ await appendSelectedTextToFile ( { text : `` } , cssFilePath ) ;
47
63
await appendSelectedTextToFile (
48
- { text : getComponentText ( componentName , targets ) } ,
64
+ { text : getComponentText ( componentName , targets ) } ,
49
65
tsFilePath
50
66
) ;
51
67
await appendSelectedTextToFile (
52
- { text : getSpecText ( componentName ) } ,
68
+ { text : getSpecText ( componentName ) } ,
53
69
specFilePath
54
70
) ;
55
71
@@ -120,3 +136,13 @@ async function getComponentNameFromHtmlFile(filePath) {
120
136
return ( tsContent . match ( / e x p o r t c l a s s \s + ( [ \w _ ] + ) / ) || [ ] ) [ 1 ] ;
121
137
}
122
138
139
+ async function getStyleExt ( ) {
140
+ try {
141
+ const [ angularJsonPath ] = await vscode . workspace . findFiles ( "angular.json" ) ;
142
+ const config = JSON . parse ( fs . readFileSync ( angularJsonPath . path , "utf-8" ) ) ;
143
+
144
+ return config . schematics [ "@schematics/angular:component" ] . styleext ;
145
+ } catch ( e ) {
146
+ return "css" ;
147
+ }
148
+ }
0 commit comments