Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add File Existence Check and Confirmation Prompt to createFigmadocFromUrl Function #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 25 additions & 3 deletions react/src/connect/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as prettier from 'prettier'
import { FigmaRestApi, getApiUrl } from './figma_rest_api'
import { logger } from '../common/logging'
import { camelCase } from 'lodash'

import readline from 'readline'
interface GenerateDocsArgs {
accessToken: string
figmaNodeUrl: string
Expand Down Expand Up @@ -134,8 +134,9 @@ figma.connect(${componentName}, "${figmaNodeUrl}", {
})
const fileName =
outFile ?? `${process.env.INIT_CWD ?? process.cwd()}/${componentName}.figma.tsx`
fs.writeFileSync(fileName, formatted)
logger.info(`Created ${fileName}`)

// Check if a file with the name 'fileName' already exists and handle the situation with user input
checkFileOverwrite(fileName, formatted);
} else {
logger.error(`Failed to get node information from Figma with status: ${response.status}`)
logger.debug('Failed to get node information from Figma with Body:', response.data)
Expand All @@ -156,3 +157,24 @@ figma.connect(${componentName}, "${figmaNodeUrl}", {
process.exit(1)
}
}

function checkFileOverwrite(fileName: string, content: string) {
if (fs.existsSync(fileName)) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(`Code Connect file '${fileName}' already exists. Do you want to overwrite it? (y/n): `, (answer) => {
if (answer.toLowerCase() === 'y' || answer.toLowerCase() === 'yes') {
fs.writeFileSync(fileName, content);
logger.info(`Overwrote the file: ${fileName}`);
} else {
logger.info('Canceled: No existing file was overwritten.');
}
rl.close();
});
} else {
fs.writeFileSync(fileName, content);
logger.info(`Created ${fileName}`);
}
}