1+ import * as fs from "fs" ;
2+ import * as path from "path" ;
13import * 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" ;
1328
1429export async function extractToFolder ( ) {
15- const { start, end} = getSelectionOffsetRange ( ) ;
30+ const { start, end } = getSelectionOffsetRange ( ) ;
1631
1732 if ( start && end ) {
1833 try {
@@ -31,9 +46,10 @@ export async function extractToFolder() {
3146
3247 const componentName = parts [ parts . length - 1 ] ;
3348
49+ const styleExt = await getStyleExt ( ) ;
3450
3551 const htmlFilePath = `${ filePath } /${ componentName } .component.html` ;
36- const cssFilePath = `${ filePath } /${ componentName } .component.css ` ;
52+ const cssFilePath = `${ filePath } /${ componentName } .component.${ styleExt } ` ;
3753 const tsFilePath = `${ filePath } /${ componentName } .component.ts` ;
3854 const specFilePath = `${ filePath } /${ componentName } .component.spec.ts` ;
3955
@@ -42,14 +58,14 @@ export async function extractToFolder() {
4258 await createFileIfDoesntExist ( tsFilePath ) ;
4359 await createFileIfDoesntExist ( specFilePath ) ;
4460
45- await appendSelectedTextToFile ( { text} , htmlFilePath ) ;
46- await appendSelectedTextToFile ( { text : `` } , cssFilePath ) ;
61+ await appendSelectedTextToFile ( { text } , htmlFilePath ) ;
62+ await appendSelectedTextToFile ( { text : `` } , cssFilePath ) ;
4763 await appendSelectedTextToFile (
48- { text : getComponentText ( componentName , targets ) } ,
64+ { text : getComponentText ( componentName , targets ) } ,
4965 tsFilePath
5066 ) ;
5167 await appendSelectedTextToFile (
52- { text : getSpecText ( componentName ) } ,
68+ { text : getSpecText ( componentName ) } ,
5369 specFilePath
5470 ) ;
5571
@@ -120,3 +136,13 @@ async function getComponentNameFromHtmlFile(filePath) {
120136 return ( tsContent . match ( / e x p o r t c l a s s \s + ( [ \w _ ] + ) / ) || [ ] ) [ 1 ] ;
121137}
122138
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