generate / generate-install

Generator that automatically detects the dependencies or devDependencies to install based on the templates or includes used. This can be used as a sub-generator or plugin in your own generator.
MIT License
7 stars 0 forks source link

Does not work correctly, maybe #1

Closed tunnckoCore closed 7 years ago

tunnckoCore commented 7 years ago

There's something wrong. It seems it not works as expected, maybe I'm missing something.

But let's say we have directory woo/. In it we have templates/ in which we have package.json and foo.js.

foo.js contains


---
rename:
  basename: test.js
install: ['mukla']

---

var test = require('mukla')

test('foo', function (done) {
  done()
})

package.json contains (intentionally called package.json not $package.json, even if it is prepend with $ things still not working)

{
  "name": "foo-bar",
  "private": true
}

the woo/generator.js

'use strict'

var isValid = require('is-valid-app')

module.exports = function gen (app) {
  if (!isValid(app, 'gen-foo')) return

  app.use(require('generate-install'))
  app.task('default', ['install'])
}

Running $ gen errors with that there's no package.json, okey I thought that it should be in the dir where the generator.js is, so when I added package.json to woo/package.json and running $ gen now does not fail with error and exit with 0, but it seems it did nothing (didn't install the mukla).

It's not actual example. Actual is https://github.com/tunnckoCore/generate-charlike-templates, which I adapted from generate-project where the things seems working.

tunnckoCore commented 7 years ago

I expect to have templates dir, in which I'll have a few templates and $package.json then running $ gen or $ gen dest charlike to scaffold project and install deps to that new project's $package.json.

doowb commented 7 years ago

You need a task that will "process" the templates so the install task knows what to install.

Add something like

app.task('foo', function() {
  return app.src('**/*', {cwd: 'templates'})
    .pipe(app.dest(app.cwd));
});
tunnckoCore commented 7 years ago

I thought, but it still not works here generate-charlike-templates/generator.js#L39-L47, there I have the processing.

doowb commented 7 years ago

Add the 'install' task to the charlike task's dependencies after default. They need to be run in the same process.

... sorry typing on my phone

tunnckoCore commented 7 years ago

doh, yea... forgot that. but actually it still not works.

2016-09-02-16 36 25_1280x1024_scrot 2016-09-02-16 39 52_1280x1024_scrot

btw, side questions: can i hide the timings? as verb's times on/off (if i remember); and how to change template delims - tried with app.option('delims', [..]) and app.option('engineOpts.delims', ...)?

doowb commented 7 years ago

The install task needs to run after the default task. You put it in the dependencies array of the default task.

Try it like this:

  app.task('charlike', ['default', 'install'])
  task(app, 'default', ['**/*'], [
    'editorconfig',
    'license-mit',
    'travis'
  ])

or you can chain tasks together at the command line so you don't have to always run the install task:

$ gen charlike charlike:install

Basically... generate-install sets up middleware that will execute after a file is written (with app.dest). If the file has a .data.install property, then the values are added to the app.cache.install array.

Now that the dependencies are cached on the app.cache.install, you can run the install task through the api in your generator.js file or through the cli. The install task will check the app.cache.install properties and determine if it needs to install dependencies.

The main thing is that install needs to be run in the same process as the task writing the files with app.dest and after that task is run.

btw, side questions: can i hide the timings?

Pass an options object to the ask you want to "silence" with silence: true.

tunnckoCore commented 7 years ago

Try it like this:

it's the same for me. Because, it will run install task after the default task ends and it ends when travis task ends. And written in your way it won't run install task when just run $ gen.

Pass an options object to the ask you want to "silence" with silence: true.

I'm not asking for the questions, but for timings - the time, starting taskName, finished taskName etc. It would be much clear and good if it only outputs the questions :)

doowb commented 7 years ago

I tracked down what's happening...

There are a few configuration issues in your project and there are some bugs that I'll open up issues for in a minute:

On another note, you should change your charlike task to be doing something like what default is doing and have default run the charlike task. This will let you run the generator from another folder when it's globally installed (at least it'll be easier):

$ gen charlike-templates

Right now, I'd have to run the following command to get the charlike task to run:

$ gen charlike-templates:charlike

I hope all this makes sense. Thanks for opening the issue since it helped track down a couple of those other issues.

doowb commented 7 years ago

You should add a dependencies or devDependencies property to the install property.

I doubled checked this and you can do install: ['mukla'] and it'll default to devDependencies. The real issue is that generate-install checking the wrong package.json to see if it's already installed. I got a false-positive because I used dependencies instead of devDependencies when I tested it.

tunnckoCore commented 7 years ago

@doowb @jonschlinkert seems to work tonight. I was sure I reinstalled and cleaned cache few times before opening issues... yesterday didn't work, tonight yes. Let's stay open for few days?


Now I have another issue/thought/suggest and will open it.

jonschlinkert commented 7 years ago

seems to work tonight. I was sure I reinstalled and cleaned cache few times before opening issues... yesterday didn't work, tonight yes. Let's stay open for few days?

how about if we close instead and just reopen if it happens again. or we can start a new issue? that sound ok? thanks for the update.

tunnckoCore commented 7 years ago

:+1:

jonschlinkert commented 7 years ago

thanks!