yeoman / generator

Rails-inspired generator system that provides scaffolding for your apps
http://yeoman.io
BSD 2-Clause "Simplified" License
1.22k stars 299 forks source link

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

Closed benmonro closed 4 years ago

benmonro commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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

benmonro commented 6 years ago

Yes On Sat, Apr 14, 2018 at 10:32 PM Simon Boudrias notifications@github.com wrote:

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

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/yeoman/generator/issues/1066#issuecomment-381381421, or mute the thread https://github.com/notifications/unsubscribe-auth/AAYXhAgBRFGli0n1_a6HG5cf46c1Mjy9ks5tottwgaJpZM4TTwei .

regevbr commented 6 years ago

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[bot] commented 4 years ago

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