template icon indicating copy to clipboard operation
template copied to clipboard

Hooks

Open ahmadawais opened this issue 6 years ago • 10 comments

Thank you for your amazing work on this.

I wish it had before and after hooks.

Here's what I need to do:

  1. CD into the newly created template
  2. Use the data from variables to do stuff e.g. create a GitHub repo based on the {{name}} entered. Sadly, when it runs I don't have the {{name}} data anywhere, I can't cd {{name}} or create a new GitHub repo with that {{name}} without that data.

SEND HALP!

ahmadawais avatar Jul 29 '19 19:07 ahmadawais

I've been thinking about this for a while, I think the best option would be add support for a template.js top-level file which exports a function which will get passed an object containing all the passed variables and an absolute path to the root of the newly instantiated project, so that it can do anything it wants with it.

This would basically be an "after" hook, I'm not sure for what use cases you might want a "before" hook too 🤔

How does that sound?

fabiospampinato avatar Jul 29 '19 20:07 fabiospampinato

I've been thinking about this for a while, I think the best option would be add support for a template.js top-level file which exports a function which will get passed an object containing all the passed variables and an absolute path to the root of the newly instantiated project, so that it can do anything it wants with it.

Perfecto!

This would basically be an "after" hook, I'm not sure for what use cases you might want a "before" hook too 🤔

Add it please, I am sure there will be a use case.

E.g. in my case, I like to clear the screen before a command.

# Init node module.
function inm() {
	clear

	echo "\n${yf}❯ 📟 INITIALIZING…${r}\n"

	template create nm "$1"
	cd "$1"

	echo "\n${yf}❯ 📥 GIT REPOSITORY…${r}"

	sh init.sh &> /dev/null
	git add . >/dev/null 2>&1
	git ci -m "📦 NEW: First commit" >/dev/null 2>&1
	git push >/dev/null 2>&1

	echo "\n${gf}❯ ✅ DONE: https://github.com/ahmadawais/$1 ${r}\n"
}

ahmadawais avatar Jul 29 '19 23:07 ahmadawais

Right now, I have a init.sh script that does a bunch of stuff and deletes itself

#! /usr/bin/env bash

# Create the repo with name and description.
gh re --new "{{name}}" --description "{{description}}"

# Git stuff.
git init
git remote add origin https://github.com/ahmadawais/{{name}}.git

# Delete yourself, bash script.
rm -- "$0"

I want to be able to remove this.

Also an option to use the same name for the dir name. I find myself adding the name for the dir and then the name field — both of which are repetitive.

E.g. template create nm hello-world and then in the name field hello-world

See is that's possible.

ahmadawais avatar Jul 29 '19 23:07 ahmadawais

@fabiospampinato any progress? I would really like to be able to not enter the name twice.

Right now I have to enter:

template create nm NAME

And then I get asked name again and I have to enter it again.

ahmadawais avatar Mar 28 '20 17:03 ahmadawais

@ahmadawais No, I don't think I will have the time to work on this for at least the following 6 months.

If you'd like to submit PRs about this though we can talk about how these issues could be fixed/improved upon 👍

fabiospampinato avatar Mar 28 '20 17:03 fabiospampinato

definitely I would love to help kindly guide me where should I start so it takes a little bit less time than it would if I would go alone

ahmadawais avatar Mar 28 '20 18:03 ahmadawais

@ahmadawais what issue do you want to tackle first, having to write the name twice or hooks?

fabiospampinato avatar Mar 28 '20 19:03 fabiospampinato

The name one. I'd like to take the input that comes from bash and use it in place of the name. Custom argv from process would ndo?

ahmadawais avatar Mar 28 '20 20:03 ahmadawais

I haven't touched the codebase in a while, but I think you should:

  1. Create an object containing placeholders that will be replaced in schemas
  2. Get the project name from here https://github.com/fabiospampinato/template/blob/585af2df9d1d7c2a96611431a69a4dc494361b89/src/index.ts#L65
  3. Add support for the {{project}} schema placeholder
    • We should probably support the triple-brackets variant as well.
  4. Replace all those placeholders with the actual values in schemas, you should probably do that when creating the schema https://github.com/fabiospampinato/template/blob/master/src/middlewares/schema.ts
  5. Update the documentation accordingly
  6. Lastly update your schemas to look somewhat like this:
{
  "variables": {
    "name": {
      "default": "{{project}}"
    }
  }
}

fabiospampinato avatar Mar 28 '20 20:03 fabiospampinato

I also have some thoughts on this, wich on the branch i working on i wanted to see if a could create several hook functions. They would basically be the 3 middleware layers. Onscheme load(to get default value) BeforePrompt (to change what inputed) OnRender(to validate code). Right now we you 3 Middlewares wich seems to me inefficient. It seems to me that three middlewares equals 3 event loops (to goo through all the file) instead of just one. I want just one event loop and have it go by file not list of file , but still have access to them. I must admit have not looked at the code yet to see if this is the case i do more looking later. One of the changes i done on the generator i am working on creating allow using ts-node to launch typescript. If i not mistaken it should alow me to actually name the file to template.ts while still supporting just plain typescript. Right now we have use 3 middleware it looks like this use.(Middlewares.schema ) .use ( Middlewares.prompt ) .use ( Middlewares.render ); I would like to change it to something like the Use.(generateMiddleware) In generate middleware function. Import middleware.hooks.schema Import middleware.hooks.prompt Import middleware.hooks.render

Etc.

frog-o avatar Jul 07 '22 08:07 frog-o

I just implemented the postinstall hook, while rewriting the program. Other hooks could be added potentially if somebody can explain again to me which ones, like what other hook do you want? When should it be called and with what parameters? And what's the use case?

fabiospampinato avatar Jul 21 '23 22:07 fabiospampinato

Closing for housekeeping, as I don't know if you guys still care about this. If you do I'm happy to reopen.

fabiospampinato avatar Jul 21 '23 22:07 fabiospampinato