Skip to content

Commit

Permalink
Merge pull request #14 from ng2react/11-tree-view-doesnt-limit-search…
Browse files Browse the repository at this point in the history
…-to-angular-source-folder

#11 Fixed issue where tree view includes files outside of angularjs source
  • Loading branch information
maxbilbow authored May 28, 2023
2 parents ab79186 + edb8c32 commit 1fa35f6
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 19 deletions.
10 changes: 2 additions & 8 deletions src/Config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import * as fs from 'fs';
import * as path from 'path';
import * as vscode from 'vscode';
const KEY = {
Expand Down Expand Up @@ -59,13 +58,8 @@ export default class Config {
}

export function getSourceRoot(key: 'angular' | 'react' | 'test') {
const sourceRoot = Config.get(`source.${key}Root`) || 'src';
const absoluteSourceRoot = path.join(vscode.workspace.workspaceFolders![0].uri.fsPath, sourceRoot);
if (!fs.existsSync(absoluteSourceRoot)) {
fs.mkdirSync(absoluteSourceRoot, { recursive: true });
return undefined;
}
return absoluteSourceRoot;
const sourceRoot = Config.get(`source.${key}Root`);
return path.join(vscode.workspace.workspaceFolders![0].uri.fsPath, sourceRoot);
}

export function getCustomPromptPath() {
Expand Down
17 changes: 17 additions & 0 deletions src/commands/convertToReact.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscode from 'vscode';
import Config, { getSourceRoot } from '../Config';
import convertToReact from '../lib/angularToReact';
export async function convertToReactCmd(
filePath: vscode.Uri | undefined,
Expand All @@ -20,6 +21,22 @@ export async function convertToReactCmd(
if (response !== 'Yes') {
return;
}

if (!filePath.fsPath.includes(getSourceRoot('angular'))) {
if (
'Yes' !==
(await vscode.window.showWarningMessage(
`${shortFileName} was not found under ${Config.get(
'source.angularRoot'
)}; ths may cause problems. Proceed anyway?`,
{ modal: true },
'Yes',
'No'
))
) {
return;
}
}
try {
await convertToReact(filePath, componentName, context);
} catch (e) {
Expand Down
13 changes: 10 additions & 3 deletions src/commands/generateReactTest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { generateReactTest } from '@ng2react/core';
import * as fs from 'fs';
import * as vscode from 'vscode';
import Config, { getSourceRoot } from '../Config';
import displayMarkdownResult, { displayPrompt } from '../lib/displayMarkdownResult';
Expand Down Expand Up @@ -102,15 +103,21 @@ async function doApiCall(

function getNewFilePath(sourceFile: vscode.Uri, newFileName: string) {
const newFilePath = vscode.Uri.file(sourceFile.path.replace(/[^\/]+$/, newFileName));
const reactRoot = getSourceRoot('react') ?? getSourceRoot('angular');
const reactRoot = getSourceRoot('react');
const testRoot = getSourceRoot('test');
if (!(testRoot && reactRoot)) {

if (reactRoot === testRoot) {
return newFilePath;
}
if (reactRoot === testRoot) {

if (!newFilePath.fsPath.includes(reactRoot)) {
return newFilePath;
}

if (!fs.existsSync(testRoot)) {
fs.mkdirSync(testRoot, { recursive: true });
}

const testFilePath = newFilePath.fsPath.replace(reactRoot, testRoot);
return vscode.Uri.file(testFilePath);
}
14 changes: 13 additions & 1 deletion src/lib/findAllAngularFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,26 @@ import * as vscode from 'vscode';
import { getSourceRoot } from '../Config';

export default async function findAllAngularFiles() {
const workspaceRoot = getSourceRoot('angular') ?? vscode.workspace.workspaceFolders?.[0].uri.fsPath;
const workspaceRoot = vscode.workspace.workspaceFolders?.[0].uri.fsPath;
if (!workspaceRoot) {
return [];
}

const angularRoot = getSourceRoot('angular');
if (!fs.existsSync(angularRoot)) {
vscode.window.showErrorMessage('AngularJS source folder incorrectly configured.');
return [];
}

const ignore = extractGlobsFromGitignore(workspaceRoot)?.join(',');

const files = await vscode.workspace.findFiles('**/*.{js,ts}', `{${ignore}}`, 1000);

return files
.filter((f) => {
if (!f.fsPath.includes(angularRoot)) {
return false;
}
const content = fs.readFileSync(f.fsPath, 'utf-8');
return search(content).length > 0;
})
Expand Down
10 changes: 4 additions & 6 deletions src/lib/searchDocument.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export default function searchDocument(document: vscode.TextDocument) {
* @param componentName name of angular component
*/
export function findExistingConversions(componentFile: vscode.Uri, componentName: string) {
const componentFileDir = getReactFolder(componentFile);
const componentFileDir = getFolder(componentFile, 'react');
const reactElementName = startCase(componentName).replace(/ /g, '');
const convertedComponentFiles = ['jsx', 'tsx', 'jsx.md', 'tsx.md'].map((ext) => ({
name: `${reactElementName}.${ext}`,
Expand All @@ -42,16 +42,14 @@ export function findExistingTests(componentFile: vscode.Uri, componentName: stri

return testFiles.filter(({ uri }) => fs.existsSync(uri.fsPath));
}
function getReactFolder(angularFile: vscode.Uri) {
return getFolder(angularFile, 'react');
}
function getFolder(angularFile: vscode.Uri, folderName = 'react' as 'react' | 'test') {

function getFolder(angularFile: vscode.Uri, folderName: 'react' | 'test') {
const angularRoot = getSourceRoot('angular');
const folderRoot = getSourceRoot(folderName);

const angularFileDir = path.dirname(angularFile.path);

if (!(angularRoot && folderRoot) || angularRoot === folderRoot) {
if (angularRoot === folderRoot) {
return angularFileDir;
}

Expand Down
5 changes: 4 additions & 1 deletion src/lib/writeToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ function getNewFilePath(sourceFile: vscode.Uri, angularName: string) {

const newFilePath = sourceFile.path.replace(/[^\/]+$/, newFileName);

if (!(angularRoot && reactRoot) || angularRoot === reactRoot) {
if (angularRoot === reactRoot) {
return { newFileName, newFilePath };
}
if (!newFilePath.includes(angularRoot)) {
return { newFileName, newFilePath };
}
if (!fs.existsSync(reactRoot)) {
fs.mkdirSync(reactRoot, { recursive: true });
}
return { newFileName, newFilePath: newFilePath.replace(angularRoot, reactRoot) };
}

0 comments on commit 1fa35f6

Please sign in to comment.