Skip to content

Commit

Permalink
Detect/resolve ambiguous script names (redwoodjs#9249)
Browse files Browse the repository at this point in the history
  • Loading branch information
codersmith committed Jan 18, 2024
1 parent 5fba571 commit 6515b98
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
15 changes: 11 additions & 4 deletions packages/cli/src/commands/execHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,18 @@ export const handler = async (args) => {
],
})

try {
require.resolve(scriptPath)
} catch {
// check the script exists, and that there are no ambiguous conflicts like [foo.js, foo.ts]
const tgtScriptName = path.parse(scriptPath)?.name
const tgtMatch = findScripts()
.map((p) => path.parse(p)?.name)
.filter((n) => tgtScriptName && n == tgtScriptName)
if (tgtMatch.length != 1) {
console.error(
c.error(`\nNo script called ${c.underline(name)} in ./scripts folder.\n`)
c.error(
`\n${
!tgtMatch.length ? 'No script' : 'Multiple scripts'
} called ${c.underline(name)}.{js,jsx,ts,tsx} in ./scripts folder.\n`
)
)

printAvailableScriptsToConsole()
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/lib/exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import {
getWebSideDefaultBabelConfig,
registerApiSideBabelHook,
} from '@redwoodjs/babel-config'
import { resolveSourcePath } from '@redwoodjs/internal/dist/files'
import { getPaths } from '@redwoodjs/project-config'

export async function runScriptFunction({
path: scriptPath,
functionName,
args,
}) {
const script = require(scriptPath)
const script = require(resolveSourcePath(scriptPath))
const returnValue = await script[functionName](args)

try {
Expand Down
25 changes: 25 additions & 0 deletions packages/internal/src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,31 @@ export const findScripts = (cwd: string = getPaths().scripts) => {
})
}

/**
* Resolves a path like `path = /my/dir/foo` to a source if the given path matches exactly one file
* with an extension of {.js, .jsx, .ts, .tsx}.
*
* @param {string} pathNoExt - The absolute path to the file, minus extension.
* @returns {boolean} - Returns the absolute path to the file including extension if exactly one such matching file exists, or false otherwise.
*
* @example
* resolveSourcePath('/Users/user/myproject/scripts/foo'); // assuming that file `foo.js` exists
* // Returns: '/Users/user/myproject/scripts/foo.js'
*
* resolveSourcePath('/Users/user/myproject/scripts/fooDoesNotExist');
* // Returns: false
*/
export const resolveSourcePath = (pathNoExt: string) => {
const files = fg.sync(`${pathNoExt}.{js,jsx,ts,tsx}`, {
absolute: true,
ignore: ['node_modules'],
})
if (files.length == 1) {
return files[0]
}
return false
}

export const isPageFile = (p: string) => {
const { name } = path.parse(p)

Expand Down

0 comments on commit 6515b98

Please sign in to comment.