Skip to content

Commit

Permalink
Generate .gitignore on dev command (#1705)
Browse files Browse the repository at this point in the history
Co-authored-by: Jan Potoms <[email protected]>
Co-authored-by: Bharat Kashyap <[email protected]>
  • Loading branch information
3 people authored Mar 24, 2023
1 parent 01c5525 commit 8172526
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
41 changes: 35 additions & 6 deletions packages/toolpad-app/cli/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -32,6 +33,8 @@ function* getPreferredPorts(port: number = DEFAULT_PORT): Iterable<number> {
}
}

const TOOLPAD_DIR_PATH = path.resolve(__dirname, '../..'); // from ./dist/server

interface RunCommandArgs {
// Whether Toolpad editor is running in debug mode
devMode?: boolean;
Expand All @@ -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) {
Expand All @@ -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: {
Expand All @@ -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}`);
}
Expand All @@ -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<void> {
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);
});
Expand All @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore

0 comments on commit 8172526

Please sign in to comment.