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

feat: bail out when initializing new project to pre-existing directories #1036

Merged
merged 5 commits into from
Mar 4, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 11 additions & 28 deletions packages/cli/src/commands/init/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,37 +42,21 @@ interface TemplateOptions {
projectTitle?: string;
}

function doesDirectoryExist(dir: string) {
return fs.existsSync(dir);
function doesDirectoryExist(path: string) {
return fs.existsSync(path);
}

async function setProjectDirectory(directory: string) {
const directoryExists = doesDirectoryExist(directory);
if (directoryExists) {
const {shouldReplaceprojectDirectory} = await inquirer.prompt([
thymikee marked this conversation as resolved.
Show resolved Hide resolved
{
type: 'confirm',
name: 'shouldReplaceprojectDirectory',
message: `Directory "${directory}" already exists, do you want to replace it?`,
default: false,
},
]);

if (!shouldReplaceprojectDirectory) {
throw new DirectoryAlreadyExistsError(directory);
}

fs.emptyDirSync(directory);
async function setProjectDirectory(projectPath: string) {
if (doesDirectoryExist(projectPath)) {
throw new DirectoryAlreadyExistsError(projectPath);
}

try {
mkdirp.sync(directory);
process.chdir(directory);
mkdirp.sync(projectPath);
process.chdir(projectPath);
} catch (error) {
throw new CLIError(
`Error occurred while trying to ${
directoryExists ? 'replace' : 'create'
} project directory.`,
`Error occurred while trying to create project directory.`,
error,
);
}
Expand Down Expand Up @@ -224,13 +208,12 @@ export default (async function initialize(
*/
const version: string = minimist(process.argv).version || DEFAULT_VERSION;

const directoryName = path.relative(root, options.directory || projectName);
const projectPath = path.resolve(root, options.directory || projectName);

try {
await createProject(projectName, directoryName, version, options);
await createProject(projectName, projectPath, version, options);

const projectFolder = path.join(root, directoryName);
thymikee marked this conversation as resolved.
Show resolved Hide resolved
printRunInstructions(projectFolder, projectName);
printRunInstructions(projectPath, projectName);
} catch (e) {
logger.error(e.message);
}
Expand Down