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

subgenerator that cleans up files created in app generator. #1066

Closed
benmonro opened this issue Apr 13, 2018 · 6 comments
Closed

subgenerator that cleans up files created in app generator. #1066

benmonro opened this issue Apr 13, 2018 · 6 comments
Labels

Comments

@benmonro
Copy link

I have a subgenerator that removes sample code created by the app generator. I'd like to compose this in the app generator itself, so the user has the choice to include the sample code or not. In a nutshell, the app generator is creating the file, but then the sub generator is deleting it. However, when i try this, the file is still created. is there any way to accomplish this in the "yeoman" way (i.e. not using shell commands to delete the files)

@SBoudrias
Copy link
Member

I think this issue might have to do with ordering (see the run loop documentation). Is your generator removing the files really running after the one creating them?

@benmonro
Copy link
Author

No it uses the writing function on both. I can verify that it does the create in the app generator and the delete in the child. I'll put together an example when I get back home

@SBoudrias
Copy link
Member

@benmonro did you console.log in each function to make sure they're called in the order you expect?

@benmonro
Copy link
Author

benmonro commented Apr 15, 2018 via email

@regevbr
Copy link
Contributor

regevbr commented Jul 17, 2018

I have written a test that shows it should work (it was located in the base.js test file under the '#composeWith()' cateogry

    describe('share the same fs', () => {
      const tmpdir = path.join(os.tmpdir(), 'yeoman-base-storage');
      const tmpfile = path.join(tmpdir, 'tempFile');

      const DummyWriter = class extends Base {
        writing() {
          this.fs.write(tmpfile, 'test');
        }
      };
      beforeEach(function() {
        fs.mkdirSync(tmpdir);
        this.dummyWriter = new DummyWriter([], {
          resolved: resolveddir,
          namespace: 'dummy',
          env: this.env,
          'skip-install': true
        });
        this.dummyWriterCompose = class extends Base {
          writing() {
            this.fs.delete(tmpfile);
          }
        };
        this.env.registerStub(this.dummyWriterCompose, 'composed:genwriter');
      });

      afterEach(function() {
        rimraf.sync(tmpdir);
      });

      it('shares fs between the composed generators', function() {
        this.dummyWriter.composeWith('composed:genwriter');
        return this.dummyWriter.run().then(() => {
          assert.equal(fs.existsSync(tmpfile), false);
        });
      });
    });

@benmonro are you sure you are calling the proper delete method like I did in the test?
If you are using 'real' fs delete call it will not work as the writing of the files only happens after the writing phase is over by committing the mem-fs to the disk.
Test to prove my point:

    describe('share the same fs', () => {
      const tmpdir = path.join(os.tmpdir(), 'yeoman-base-storage');
      const tmpfile = path.join(tmpdir, 'tempFile');

      const DummyWriter = class extends Base {
        writing() {
          this.fs.write(tmpfile, 'test');
        }
      };
      beforeEach(function() {
        fs.mkdirSync(tmpdir);
        this.dummyWriter = new DummyWriter([], {
          resolved: resolveddir,
          namespace: 'dummy',
          env: this.env,
          'skip-install': true
        });
        this.dummyWriterCompose = class extends Base {
          writing() {
            try {
              fs.unlinkSync(tmpfile);
            } catch (e){}
          }
        };
        this.env.registerStub(this.dummyWriterCompose, 'composed:genwriter');
      });

      afterEach(function() {
        rimraf.sync(tmpdir);
      });

      it('delete of files should now work on mem-fs written files', function() {
        this.dummyWriter.composeWith('composed:genwriter');
        return this.dummyWriter.run().then(() => {
          assert.equal(fs.existsSync(tmpfile), true);
        });
      });
    });

@github-actions
Copy link
Contributor

This issue is stale because it has been open 15 days with no activity. Remove stale label or comment or this will be closed in 5 days

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

No branches or pull requests

3 participants