diff --git a/src/commands/generateCodeInFolder/handlers/genFolderSelectionList.ts b/src/commands/generateCodeInFolder/handlers/genFolderSelectionList.ts index 5a6657e..42d56ae 100644 --- a/src/commands/generateCodeInFolder/handlers/genFolderSelectionList.ts +++ b/src/commands/generateCodeInFolder/handlers/genFolderSelectionList.ts @@ -4,13 +4,15 @@ import getFoldersInWorkspace from '../../../../utils/folderFiles/getFoldersInWor import getWorkspacePath from '../../../../utils/workspace/getWorkspacePath' export default async function genFolderSelectionList() { - const workspacePath = getWorkspacePath() + const workspacePath = getWorkspacePath({}) const workplaceFiles = await getFoldersInWorkspace() const folderSelectionList = workplaceFiles.map((wsFile) => ({ label: wsFile.replace(workspacePath.path, '') === '/' ? '/' - : removeFirstCharacter(removeLastCharacter(wsFile.replace(workspacePath.path, ''))), + : removeFirstCharacter( + removeLastCharacter(wsFile.replace(workspacePath.path, '')), + ), path: wsFile, })) diff --git a/src/common/generateCode/handlers/create.ts b/src/common/generateCode/handlers/create.ts index 116e102..f7dd80a 100644 --- a/src/common/generateCode/handlers/create.ts +++ b/src/common/generateCode/handlers/create.ts @@ -41,7 +41,7 @@ export default async function create(props: { type: props.componentConfig.type, } let parentFolderName = - file?.parentFolderName?.(fileProperties) || props.name || ''; + file?.parentFolderName?.(fileProperties) || props.name || '' // Format parentFolderName (optional) if (props.componentConfig?.options?.formatParentFolderName) { @@ -60,24 +60,23 @@ export default async function create(props: { outputPath: props.componentOutputPath, })?.newName } - + const outputPath = path.join( - !outputInRootFolder ? props.componentOutputPath : getWorkspacePath().path, + !outputInRootFolder ? props.componentOutputPath : getWorkspacePath({}).path, createNamedFolder ? parentFolderName : '', file.path(fileProperties), ) - + const content = await prettifyFile({ content: file.template(fileProperties), prettierConfig: props.prettierConfig, }) - if (doesFolderOrFileExist(outputPath)) logError(`${props.name} already exists`, { silent: true }) - + if (doesFolderOrFileExist(outputPath)) + logError(`${props.name} already exists`, { silent: true }) + await createFile(outputPath, content) if (openOnCreate) openFile(outputPath) - - }), ) } catch (error: unknown) { diff --git a/src/common/generateCode/handlers/importPrettierConfig.ts b/src/common/generateCode/handlers/importPrettierConfig.ts index 524ca99..93ad366 100644 --- a/src/common/generateCode/handlers/importPrettierConfig.ts +++ b/src/common/generateCode/handlers/importPrettierConfig.ts @@ -4,8 +4,12 @@ export type importPrettierConfigProps = { prettierConfigPath: string } -export default async function importPrettierConfig({ prettierConfigPath }: importPrettierConfigProps) { - const workspacePath = getWorkspacePath(prettierConfigPath) +export default async function importPrettierConfig({ + prettierConfigPath, +}: importPrettierConfigProps) { + const workspacePath = getWorkspacePath({ + add: prettierConfigPath, + }) let prettierConfig = false if (workspacePath.hasExtension('.js')) { @@ -14,6 +18,5 @@ export default async function importPrettierConfig({ prettierConfigPath }: impor console.warn('Quick Component Create: Prettier config should be a JS file') } - return prettierConfig } diff --git a/utils/folderFiles/getFoldersInWorkspace.ts b/utils/folderFiles/getFoldersInWorkspace.ts index 33ef47a..7f0b39c 100644 --- a/utils/folderFiles/getFoldersInWorkspace.ts +++ b/utils/folderFiles/getFoldersInWorkspace.ts @@ -3,7 +3,7 @@ import getWorkspacePath from '../workspace/getWorkspacePath' export default async function getFoldersInWorkspace() { // get the path of the workspace - const workspacePath = getWorkspacePath().path + const workspacePath = getWorkspacePath({}).path // get all the folders in the workspace, excluding node_modules const foldersInWorkspace = glob.sync(workspacePath + '/**/', { diff --git a/utils/folderFiles/importFileInWorkspace.ts b/utils/folderFiles/importFileInWorkspace.ts index 5288ca5..9cf57d4 100644 --- a/utils/folderFiles/importFileInWorkspace.ts +++ b/utils/folderFiles/importFileInWorkspace.ts @@ -4,20 +4,24 @@ import importTs from '../importTsFile/importTsFile' import getWorkspacePath from '../workspace/getWorkspacePath' export type importFileInWorkspaceProps = { - uri: string; + uri: string } -export default async function importFileInWorkspace({uri}: importFileInWorkspaceProps) { +export default async function importFileInWorkspace({ uri }: importFileInWorkspaceProps) { // get the path of the file - const uriPath = getWorkspacePath(uri); + const uriPath = getWorkspacePath({ + add: uri, + }) // check if the file exists - const uriPathExists = doesFileExist(uriPath.path); + const uriPathExists = doesFileExist(uriPath.path) // assert that the file exists assert(uriPathExists, `Schema not found at ${uriPath}`) // return the file imported/required - return uriPath.hasExtension('.ts') ? await importTs({filePath: uriPath.path}) : require(uriPath.path) + return uriPath.hasExtension('.ts') + ? await importTs({ filePath: uriPath.path }) + : require(uriPath.path) } export type ImportFileInWorkspaceReturn = ReturnType diff --git a/utils/workspace/getWorkspacePath.ts b/utils/workspace/getWorkspacePath.ts index e4e03e3..2a8a764 100644 --- a/utils/workspace/getWorkspacePath.ts +++ b/utils/workspace/getWorkspacePath.ts @@ -4,20 +4,18 @@ import { platform } from 'process' import { StartsWithSlashRegex } from '../splitPath' import removeFirstCharacter from '../folderFiles/removeFirstCharacter' -export type FilePath = { - path: string - getFileUri(): string - getExtension(): string - hasExtension(ext: string): boolean - addFile(dir: string): void +export type GetWorkspacePathProps = { + add?: string } -export default function getWorkspacePath(add = ''): FilePath { +export default function getWorkspacePath( + props: GetWorkspacePathProps, +): GetWorkspacePathReturn { // Get the workspace folder uri const folderUri = vscode.workspace.workspaceFolders![0].uri // add the path to the workspace folder - let filePath = path.join(folderUri.path, add) + let filePath = path.join(folderUri.path, props.add || '') // If the platform is windows and the path starts with a slash, remove the slash if (platform == 'win32' && StartsWithSlashRegex.test(filePath)) { @@ -41,3 +39,11 @@ export default function getWorkspacePath(add = ''): FilePath { }, } } + +export type GetWorkspacePathReturn = { + path: string + getFileUri(): string + getExtension(): string + hasExtension(ext: string): boolean + addFile(dir: string): void +}