diff --git a/packages/toolpad-app/cli/index.ts b/packages/toolpad-app/cli/index.ts index 9fc5fb5cdbf..14707078fc7 100644 --- a/packages/toolpad-app/cli/index.ts +++ b/packages/toolpad-app/cli/index.ts @@ -1,6 +1,7 @@ import 'dotenv/config'; import arg from 'arg'; import path from 'path'; +import * as fs from 'fs/promises'; import invariant from 'invariant'; import { Readable } from 'stream'; import * as readline from 'readline'; @@ -32,6 +33,8 @@ function* getPreferredPorts(port: number = DEFAULT_PORT): Iterable { } } +const TOOLPAD_DIR_PATH = path.resolve(__dirname, '../..'); // from ./dist/server + interface RunCommandArgs { // Whether Toolpad editor is running in debug mode devMode?: boolean; @@ -42,7 +45,7 @@ async function runApp(cmd: 'dev' | 'start', { devMode = false, port }: RunComman const { execa } = await import('execa'); const { default: chalk } = await import('chalk'); const { default: getPort } = await import('get-port'); - const toolpadDir = path.resolve(__dirname, '../..'); // from ./dist/server + const nextCommand = devMode ? 'dev' : 'start'; if (!port) { @@ -57,7 +60,7 @@ async function runApp(cmd: 'dev' | 'start', { devMode = false, port }: RunComman } const cp = execa('next', [nextCommand, `--port=${port}`], { - cwd: toolpadDir, + cwd: TOOLPAD_DIR_PATH, preferLocal: true, stdio: 'pipe', env: { @@ -80,7 +83,7 @@ async function runApp(cmd: 'dev' | 'start', { devMode = false, port }: RunComman invariant(detectedPort, 'Could not find port in stdout'); const toolpadBaseUrl = `http://localhost:${detectedPort}/`; try { - await openBrowser(toolpadBaseUrl); + openBrowser(toolpadBaseUrl); } catch (err: any) { console.error(`${chalk.red('error')} - Failed to open browser: ${err.message}`); } @@ -93,17 +96,43 @@ async function runApp(cmd: 'dev' | 'start', { devMode = false, port }: RunComman }); } +const PROJECT_FILES_PATH = path.resolve(TOOLPAD_DIR_PATH, './cli/projectFiles'); + +const projectFiles = [ + { + source: 'toolpad-generated-gitignore', + destination: './.toolpad-generated/.gitignore', + }, +]; + +async function writeProjectFiles(): Promise { + await Promise.all( + projectFiles.map(async ({ source, destination }) => { + const filePath = path.resolve(PROJECT_FILES_PATH, source); + const fileContent = await fs.readFile(filePath); + + await fs.writeFile(path.join(process.cwd(), destination), fileContent, { + encoding: 'utf-8', + }); + }), + ); +} + async function devCommand(args: RunCommandArgs) { const { default: chalk } = await import('chalk'); // eslint-disable-next-line no-console - console.log(`${chalk.blue('info')} - starting Toolpad application in dev mode...`); + console.log(`${chalk.blue('info')} - generating project files…`); + await writeProjectFiles(); + + // eslint-disable-next-line no-console + console.log(`${chalk.blue('info')} - starting Toolpad application in dev mode…`); await runApp('dev', args); } async function buildCommand() { const { default: chalk } = await import('chalk'); // eslint-disable-next-line no-console - console.log(`${chalk.blue('info')} - building Toolpad application...`); + console.log(`${chalk.blue('info')} - building Toolpad application…`); await new Promise((resolve) => { setTimeout(resolve, 1000); }); @@ -114,7 +143,7 @@ async function buildCommand() { async function startCommand(args: RunCommandArgs) { const { default: chalk } = await import('chalk'); // eslint-disable-next-line no-console - console.log(`${chalk.blue('info')} - Starting Toolpad application...`); + console.log(`${chalk.blue('info')} - Starting Toolpad application…`); await runApp('start', args); } diff --git a/packages/toolpad-app/cli/projectFiles/toolpad-generated-gitignore b/packages/toolpad-app/cli/projectFiles/toolpad-generated-gitignore new file mode 100644 index 00000000000..c96a04f008e --- /dev/null +++ b/packages/toolpad-app/cli/projectFiles/toolpad-generated-gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file