Skip to content

Commit

Permalink
feat(scripts): Args type for script arguments (#11946)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobbe authored Feb 5, 2025
1 parent 68798f7 commit 33bfda8
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 26 deletions.
4 changes: 4 additions & 0 deletions .changesets/11946.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- feat(scripts): Args type for script arguments (#11946) by @Tobbe

Generated scripts will now come with an `Args` interface that you can add your
arguments to for better TS support.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`creates a JavaScript function to execute 1`] = `
"// To access your database
// Append api/* to import from api and web/* to import from web
import { db } from 'api/src/lib/db'
"// Append api/* to import from api and web/* to import from web
// To access your database uncomment the line below
// import { db } from 'api/src/lib/db'
export default async ({ args }) => {
// Your script here...
Expand All @@ -15,12 +15,17 @@ export default async ({ args }) => {
`;

exports[`creates a TypeScript function to execute 1`] = `
"// To access your database
// Append api/* to import from api and web/* to import from web
import { db } from 'api/src/lib/db'
"// Append api/* to import from api and web/* to import from web
// To access your database uncomment the line below
// import { db } from 'api/src/lib/db'
export default async ({ args }) => {
interface Args {
_: string[]
[key: string]: unknown
}
export default async ({ args }: Args) => {
// Your script here...
console.log(':: Executing script with args ::')
console.log(args)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import path from 'path'
import { test, expect } from 'vitest'
import yargs from 'yargs'

import * as script from '../script'
import * as script from '../script.js'

test('creates a JavaScript function to execute', () => {
const output = script.files({
test('creates a JavaScript function to execute', async () => {
const output = await script.files({
name: 'scriptyMcScript',
typescript: false,
})
Expand All @@ -23,8 +23,8 @@ test('creates a JavaScript function to execute', () => {
expect(output[expectedOutputPath]).toMatchSnapshot()
})

test('creates a TypeScript function to execute', () => {
const output = script.files({
test('creates a TypeScript function to execute', async () => {
const output = await script.files({
name: 'typescriptyTypescript',
typescript: true,
})
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/src/commands/generate/script/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,30 @@ import terminalLink from 'terminal-link'
import { recordTelemetryAttributes } from '@redwoodjs/cli-helpers'
import { errorTelemetry } from '@redwoodjs/telemetry'

import { getPaths, writeFilesTask } from '../../../lib'
import { getPaths, writeFilesTask, transformTSToJS } from '../../../lib'
import c from '../../../lib/colors'
import { prepareForRollback } from '../../../lib/rollback'
import { validateName, yargsDefaults } from '../helpers'

const TEMPLATE_PATH = path.resolve(__dirname, 'templates', 'script.js.template')
const TEMPLATE_PATH = path.resolve(__dirname, 'templates', 'script.ts.template')
const TSCONFIG_TEMPLATE = path.resolve(
__dirname,
'templates',
'tsconfig.json.template',
)

export const files = ({ name, typescript = false }) => {
export const files = async ({ name, typescript = false }) => {
const outputFilename = `${name}.${typescript ? 'ts' : 'js'}`
const outputPath = path.join(getPaths().scripts, outputFilename)

const scriptTsConfigPath = path.join(getPaths().scripts, 'tsconfig.json')

const template = fs.readFileSync(TEMPLATE_PATH, 'utf-8')

return {
[outputPath]: fs.readFileSync(TEMPLATE_PATH, 'utf-8'),
[outputPath]: typescript
? template
: await transformTSToJS(outputPath, template),

// Add tsconfig for type and cmd+click support if project is TS
...(typescript &&
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Append api/* to import from api and web/* to import from web

// To access your database uncomment the line below
// import { db } from 'api/src/lib/db'

interface Args {
_: string[]
[key: string]: unknown
}

export default async ({ args }: Args) => {
// Your script here...
console.log(':: Executing script with args ::')
console.log(args)
}

0 comments on commit 33bfda8

Please sign in to comment.