Skip to content

Commit

Permalink
Refactor getWorkspacePath usage for improved consistency and readability
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremytenjo committed Jan 31, 2025
1 parent fe459a4 commit 0ee94bf
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}))

Expand Down
15 changes: 7 additions & 8 deletions src/common/generateCode/handlers/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
9 changes: 6 additions & 3 deletions src/common/generateCode/handlers/importPrettierConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -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
}
2 changes: 1 addition & 1 deletion utils/folderFiles/getFoldersInWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '/**/', {
Expand Down
14 changes: 9 additions & 5 deletions utils/folderFiles/importFileInWorkspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<typeof importFileInWorkspace>
22 changes: 14 additions & 8 deletions utils/workspace/getWorkspacePath.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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
}

0 comments on commit 0ee94bf

Please sign in to comment.