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

New application with collection already containing a .gitignore fails #1628

Closed
2 of 4 tasks
maxgaurav opened this issue Apr 28, 2022 · 7 comments
Closed
2 of 4 tasks

Comments

@maxgaurav
Copy link
Contributor

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

When setting up a new application using a collection that already contains a .gitignore file. The git init process would fail.

image

Minimum reproduction code

https://gist.github.com/maxgaurav/c6260d956d5fb7d124f565959406e957

Steps to reproduce

npm install -g @nestjs/cli
npm install -g @maxgaurav/nestjs-template-schematics

nest new sample-content

Expected behavior

When a .gitignore file already exists it should not try to overwrite with default git content. This actions leads to

Package version

8.2.5

NestJS version

No response

Node.js version

v16.14.2

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

It looks like the action where .gitignore is being initialized is not considering the fact a .gitignore file can already be there

@jmcdo29
Copy link
Member

jmcdo29 commented Apr 28, 2022

If you're using nest new in a directory that already is using git you can use the --skip-git flag to tell Nest not to worry about the git setup

@maxgaurav
Copy link
Contributor Author

Findings

The main call to init git passes but when the next which is to create gitignore file fails.

    if (!isDryRunEnabled) {
      if (!shouldSkipGit) {
        await initializeGitRepository(projectDirectory); // passes and setups all necessary git init actions
        await createGitIgnoreFile(projectDirectory); // fails as it assumes no .gitignore file would exists
      }

      printCollective();
    }

Referencing file:

The createGitIgnoreFile function does not consider the fact the files copied through schematics may already contain a .gitignore file. I think this is written this way as the default application strucuture in @nestjs/schmatics does not include the .gitignore file. In my collection overide of the schematics I have provided my own gitignore and its causing failure in completion of command.

/**
 * Write a file `.gitignore` in the root of the newly created project.
 * `.gitignore` available in `@nestjs/schematics` cannot be published to
 * NPM (needs to be investigated).
 *
 * @param dir Relative path to the project.
 * @param content (optional) Content written in the `.gitignore`.
 *
 * @return Resolves when succeeds, or rejects with any error from `fn.writeFile`.
 */
const createGitIgnoreFile = (dir: string, content?: string) => {
  const fileContent = content || defaultGitIgnore;
  const filePath = join(process.cwd(), dir, '.gitignore');
  return promisify(fs.writeFile)(filePath, fileContent);
};

@maxgaurav
Copy link
Contributor Author

maxgaurav commented Apr 28, 2022

Also I was creating a new app in a non existing directory

Just FYI: I was looking into the implementation it looks like there is some attempt but never completed, as there is an option of passing custom content but the main caller never does such a thing along with the fact there is a possibility that the file may already exist after copying the structure from schematics provided.

Would it be okay that I provide a pull request for this?

@jmcdo29
Copy link
Member

jmcdo29 commented Apr 28, 2022

If it's non-breaking and easy to maintain, I'd say go for it :)

@maxgaurav
Copy link
Contributor Author

Ok. Will do

@maxgaurav
Copy link
Contributor Author

@jmcdo29
I have linked the issue with the pull request.

@kamilmysliwiec
Copy link
Member

Tracking this here #1630

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants