Skip to content

Commit 545b9bf

Browse files
authored
fix(exec): Run scripts from inside scripts dir (#59)
1 parent 68caa45 commit 545b9bf

File tree

6 files changed

+88
-6
lines changed

6 files changed

+88
-6
lines changed

.github/workflows/ci.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,13 @@ jobs:
438438
YARN_ENABLE_IMMUTABLE_INSTALLS: false
439439

440440
- name: Run `rw info`
441-
run: yarn rw info
441+
run: yarn rw info | tr -d '\n' | grep "npmPackages:.*@redmix/core:"
442442
working-directory: ${{ steps.set-up-test-project.outputs.test-project-path }}
443443

444+
- name: Run in /scripts `rw info`
445+
run: yarn rw info | tr -d '\n' | grep "npmPackages:.*@redmix/core:"
446+
working-directory: ${{ steps.set-up-test-project.outputs.test-project-path }}/scripts
447+
444448
- name: Run `rw lint`
445449
run: yarn rw lint ./api/src --fix
446450
working-directory: ${{ steps.set-up-test-project.outputs.test-project-path }}
@@ -465,6 +469,17 @@ jobs:
465469
run: yarn rw g script testScript && yarn rw exec testScript
466470
working-directory: ${{ steps.set-up-test-project.outputs.test-project-path }}
467471

472+
- name: Nested script | Run "rw exec"
473+
# Using -f to overwrite script that already exists in the test-project
474+
run: yarn rw g script -f i/am/nested && yarn rw exec i/am/nested
475+
working-directory: ${{ steps.set-up-test-project.outputs.test-project-path }}
476+
477+
- name: Nested script | Run in /scripts "rw exec"
478+
# This is probably stretching the definition of "smoke test" a bit too
479+
# much. But we don't have a better place to run a test like this
480+
run: yarn rw g script i/am/nested/too && yarn rw exec i/am/nested/too | grep "Executing script with args"
481+
working-directory: ${{ steps.set-up-test-project.outputs.test-project-path }}/scripts/i/am
482+
468483
- name: Run "prisma generate"
469484
run: yarn rw prisma generate
470485
working-directory: ${{ steps.set-up-test-project.outputs.test-project-path }}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Append api/* to import from api and web/* to import from web
2+
3+
// To access your database uncomment the line below
4+
// import { db } from 'api/src/lib/db'
5+
6+
interface Args {
7+
_: string[]
8+
[key: string]: unknown
9+
}
10+
11+
export default async ({ args }: Args) => {
12+
// Your script here...
13+
console.log(':: Executing script with args ::')
14+
console.log(args)
15+
}

packages/cli/src/__tests__/cwd.test.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@ import { describe, it, expect } from 'vitest'
66
const BASE_DIR = path.resolve(import.meta.dirname, '..', '..', '..', '..')
77
const CLI = path.join(BASE_DIR, 'packages', 'cli', 'dist', 'index.js')
88

9-
console.log('BASE_DIR:', BASE_DIR)
10-
console.log('CLI:', CLI)
11-
129
function rw(args, options) {
1310
const { status, stdout, stderr } = spawnSync('node', [CLI, ...args], {
1411
cwd: BASE_DIR,

packages/cli/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ try {
101101

102102
process.env.RWJS_CWD = cwd
103103

104+
if (process.cwd() !== cwd) {
105+
process.chdir(cwd)
106+
}
107+
104108
// Load .env.* files.
105109
//
106110
// This should be done as early as possible, and the earliest we can do it is after setting `cwd`.

packages/telemetry/src/sendTelemetry.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ const getInfo = async (presets: Args = {}) => {
118118
npmVersion: info.Binaries?.npm?.version,
119119
vsCodeVersion: info.IDEs?.VSCode?.version,
120120
redwoodVersion:
121-
presets.redwoodVersion || info.npmPackages['@redmix/core']?.installed,
121+
presets.redwoodVersion || info.npmPackages?.['@redmix/core']?.installed,
122122
system: `${cpu.physicalCores}.${Math.round(mem.total / 1073741824)}`,
123123
webBundler: 'vite', // Hardcoded as this is now the only supported bundler
124124
experiments,

tasks/test-project/rebuild-test-project-fixture.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ async function runCommand() {
415415
await tuiTask({
416416
step: 9,
417417
title: 'Add scripts',
418-
task: () => {
418+
task: async () => {
419419
const nestedPath = path.join(OUTPUT_PROJECT_PATH, 'scripts', 'one', 'two')
420420

421421
fs.mkdirSync(nestedPath, { recursive: true })
@@ -425,6 +425,57 @@ async function runCommand() {
425425
" console.log('Hello from myNestedScript.ts')\n" +
426426
'}\n\n',
427427
)
428+
429+
await exec(
430+
'yarn rw g script i/am/nested',
431+
[],
432+
getExecaOptions(OUTPUT_PROJECT_PATH),
433+
)
434+
435+
// Verify that the scripts are added and included in the list of
436+
// available scripts
437+
const list = await exec(
438+
'yarn rw exec',
439+
[],
440+
getExecaOptions(OUTPUT_PROJECT_PATH),
441+
)
442+
443+
if (
444+
!list.stdout.includes('seed') ||
445+
!list.stdout.includes('i/am/nested') ||
446+
!list.stdout.includes('one/two/myNestedScript')
447+
) {
448+
console.error('yarn rw exec output', list.stdout, list.stderr)
449+
450+
throw new Error('Scripts not included in list')
451+
}
452+
453+
// Verify that the scripts can be executed
454+
const runFromRoot = await exec(
455+
'yarn rw exec one/two/myNestedScript',
456+
[],
457+
getExecaOptions(OUTPUT_PROJECT_PATH),
458+
)
459+
460+
if (!runFromRoot.stdout.includes('Hello from myNestedScript')) {
461+
console.error('`yarn rw exec one/two/myNestedScript` output')
462+
console.error(runFromRoot.stdout, runFromRoot.stderr)
463+
464+
throw new Error('Script not executed successfully')
465+
}
466+
467+
const runFromScripts = await exec(
468+
'yarn rw exec one/two/myNestedScript',
469+
[],
470+
getExecaOptions(path.join(OUTPUT_PROJECT_PATH, 'scripts', 'one')),
471+
)
472+
473+
if (!runFromScripts.stdout.includes('Hello from myNestedScript')) {
474+
console.error('`yarn rw exec one/two/myNestedScript` output')
475+
console.error(runFromRoot.stdout, runFromRoot.stderr)
476+
477+
throw new Error('Script not executed successfully')
478+
}
428479
},
429480
})
430481

0 commit comments

Comments
 (0)