Skip to content

Commit

Permalink
Fix for #3, #5
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksey Okhrimenko committed Aug 24, 2020
1 parent 546c73e commit 056e97f
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 19 deletions.
1 change: 1 addition & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"request": "launch",
"runtimeExecutable": "${execPath}",
"args": [
"--disable-extensions",
"--extensionDevelopmentPath=${workspaceFolder}",
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
],
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## 0.1.0

- Get style file extension from `@schematics/angular:component`
- Fix extraction of structural directives (ngFor etc..)

## 0.0.5

- Fix wrong contructor keyword placement
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "arrr",
"displayName": "arrr",
"description": "The extension provides refactoring tools for your Angular codebase",
"version": "0.0.5",
"version": "0.1.0",
"publisher": "obenjiro",
"engines": {
"vscode": "^1.47.0"
Expand Down
60 changes: 43 additions & 17 deletions src/modules/extract-to-folder.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";
import {activeFileName, getSelectedText, getSelectionOffsetRange, importMissingDependencies,} from "../editor";
import {getAllTargets,} from "../template-parser";
import {showFilePicker} from "../file-picker";
import {createFileIfDoesntExist, persistFileSystemChanges, replaceTextInFile,} from "../file-system";
import {pascalCase} from "change-case";
import {appendSelectedTextToFile, replaceSelectionWith,} from "../code-actions";
import {showDirectoryPicker} from "../directories-picker";
import {getComponentInstance, getComponentText, getSpecText,} from "./extract-to-folder-template";

const fs = require("fs");
const path = require("path");
import {
activeFileName,
getSelectedText,
getSelectionOffsetRange,
importMissingDependencies,
} from "../editor";
import { getAllTargets } from "../template-parser";
import { showFilePicker } from "../file-picker";
import {
createFileIfDoesntExist,
persistFileSystemChanges,
replaceTextInFile,
} from "../file-system";
import { pascalCase } from "change-case";
import {
appendSelectedTextToFile,
replaceSelectionWith,
} from "../code-actions";
import { showDirectoryPicker } from "../directories-picker";
import {
getComponentInstance,
getComponentText,
getSpecText,
} from "./extract-to-folder-template";

export async function extractToFolder() {
const {start, end} = getSelectionOffsetRange();
const { start, end } = getSelectionOffsetRange();

if (start && end) {
try {
Expand All @@ -31,9 +46,10 @@ export async function extractToFolder() {

const componentName = parts[parts.length - 1];

const styleExt = await getStyleExt();

const htmlFilePath = `${filePath}/${componentName}.component.html`;
const cssFilePath = `${filePath}/${componentName}.component.css`;
const cssFilePath = `${filePath}/${componentName}.component.${styleExt}`;
const tsFilePath = `${filePath}/${componentName}.component.ts`;
const specFilePath = `${filePath}/${componentName}.component.spec.ts`;

Expand All @@ -42,14 +58,14 @@ export async function extractToFolder() {
await createFileIfDoesntExist(tsFilePath);
await createFileIfDoesntExist(specFilePath);

await appendSelectedTextToFile({text}, htmlFilePath);
await appendSelectedTextToFile({text: ``}, cssFilePath);
await appendSelectedTextToFile({ text }, htmlFilePath);
await appendSelectedTextToFile({ text: `` }, cssFilePath);
await appendSelectedTextToFile(
{text: getComponentText(componentName, targets)},
{ text: getComponentText(componentName, targets) },
tsFilePath
);
await appendSelectedTextToFile(
{text: getSpecText(componentName)},
{ text: getSpecText(componentName) },
specFilePath
);

Expand Down Expand Up @@ -120,3 +136,13 @@ async function getComponentNameFromHtmlFile(filePath) {
return (tsContent.match(/export class\s+([\w_]+)/) || [])[1];
}

async function getStyleExt() {
try {
const [angularJsonPath] = await vscode.workspace.findFiles("angular.json");
const config = JSON.parse(fs.readFileSync(angularJsonPath.path, "utf-8"));

return config.schematics["@schematics/angular:component"].styleext;
} catch (e) {
return "css";
}
}
14 changes: 14 additions & 0 deletions src/template-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,19 @@ export function getAllTargets(text) {
}
);

// removing variables refrences
visitTarget(
ast,
(value: any) => {
return getNodeCtor(value) === "Variable";
},
(node: any, parent: any) => {
if (targets.indexOf(node.name) > -1) {
targets.splice(targets.indexOf(node.name));
}
return KEEP_VISIT;
}
);

return [...new Set(targets)];
}

0 comments on commit 056e97f

Please sign in to comment.