Skip to content

Commit

Permalink
fix(exec): Include nested scripts in --list (#11262)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Aug 15, 2024
1 parent 0c60002 commit b330320
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .changesets/11262.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- fix(exec): Include nested scripts in --list (#11262) by @Tobbe

Now also nested scripts, like `scripts/nested/script.ts`, are included in the
output of `yarn rw exec --list`
3 changes: 3 additions & 0 deletions __fixtures__/test-project/scripts/one/two/myNestedScript.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default async () => {
console.log('Hello from myNestedScript.ts')
}
52 changes: 52 additions & 0 deletions packages/cli/src/commands/__tests__/exec.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import path from 'node:path'

import '../../lib/mockTelemetry'

import { vi, afterEach, beforeEach, describe, it, expect } from 'vitest'

import { handler } from '../execHandler'

vi.mock('envinfo', () => ({ default: { run: () => '' } }))
vi.mock('@redwoodjs/project-config', () => ({
getPaths: () => ({
scripts: path.join(
__dirname,
'../../../../../__fixtures__/test-project/scripts',
),
}),
resolveFile: (path: string) => path,
}))

const mockRedwoodToml = {
fileContents: '',
}

// Before rw tests run, api/ and web/ `jest.config.js` is confirmed via existsSync()
vi.mock('node:fs', async () => ({
default: {
readFileSync: () => {
return mockRedwoodToml.fileContents
},
},
}))

beforeEach(() => {
vi.spyOn(console, 'log').mockImplementation(() => {})
})

afterEach(() => {
vi.mocked(console).log.mockRestore()
})

describe('yarn rw exec --list', () => {
it('includes nested scripts', async () => {
await handler({ list: true })
const scriptPath = path
.join('one', 'two', 'myNestedScript')
// Handle Windows path separators
.replaceAll('\\', '\\\\')
expect(vi.mocked(console).log).toHaveBeenCalledWith(
expect.stringMatching(new RegExp('\\b' + scriptPath + '\\b')),
)
})
})
8 changes: 5 additions & 3 deletions packages/cli/src/commands/execHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import { generatePrismaClient } from '../lib/generatePrismaClient'

const printAvailableScriptsToConsole = () => {
console.log('Available scripts:')
findScripts().forEach((scriptPath) => {
const { name } = path.parse(scriptPath)
console.log(c.info(`- ${name}`))
findScripts(getPaths().scripts).forEach((scriptPath) => {
const relativePath = path.relative(getPaths().scripts, scriptPath)
const { ext } = path.parse(relativePath)
const relativePathWithoutExt = relativePath.slice(0, -ext.length)
console.log(c.info(`- ${relativePathWithoutExt}`))
})
console.log()
}
Expand Down
2 changes: 1 addition & 1 deletion packages/internal/src/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ export const isCellFile = (p: string) => {
}

export const findScripts = (cwd: string = getPaths().scripts) => {
return fg.sync('*.{js,jsx,ts,tsx}', {
return fg.sync('./**/*.{js,jsx,ts,tsx}', {
cwd,
absolute: true,
ignore: ['node_modules'],
Expand Down
29 changes: 25 additions & 4 deletions tasks/test-project/rebuild-test-project-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,8 +386,29 @@ async function runCommand() {
},
})

await tuiTask({
step: 9,
title: 'Add scripts',
task: () => {
const nestedPath = path.join(
OUTPUT_PROJECT_PATH,
'scripts',
'one',
'two'
)

fs.mkdirSync(nestedPath, { recursive: true })
fs.writeFileSync(
path.join(nestedPath, 'myNestedScript.ts'),
'export default async () => {\n' +
" console.log('Hello from myNestedScript.ts')\n" +
'}\n\n'
)
},
})

await tuiTask({
step: 9,
step: 10,
title: 'Running prisma migrate reset',
task: () => {
return exec(
Expand All @@ -399,7 +420,7 @@ async function runCommand() {
})

await tuiTask({
step: 10,
step: 11,
title: 'Lint --fix all the things',
task: async () => {
try {
Expand Down Expand Up @@ -431,7 +452,7 @@ async function runCommand() {
})

await tuiTask({
step: 11,
step: 12,
title: 'Replace and Cleanup Fixture',
task: async () => {
// @TODO: This only works on UNIX, we should use path.join everywhere
Expand Down Expand Up @@ -469,7 +490,7 @@ async function runCommand() {
})

await tuiTask({
step: 12,
step: 13,
title: 'All done!',
task: () => {
console.log('-'.repeat(30))
Expand Down

0 comments on commit b330320

Please sign in to comment.