How can I run an atom editor script to automate meteor.js code based on the filename?

I am currently using the Meteor.js framework and atom editor. I would like to automate the output of the meteor “hooks.”

Here is a use case: I create a new javascript file let’s say example.js. I would like to run a quick script inside atom. The only input so to speak is filename, the script would then write to the current file the following:

Template.example.onCreated(function(){
});
Template.example.onRendered(function(){
});
Template.example.onDestroyed(function(){
});
Template.example.helpers({
})
Template.example.events({
})

I did take some time to read over the atom script page but quite honestly the documentation wasn’t clear enough for me.

Does anyone have any ideas here ? You help is much appreciated. Thank you. Alex

You should consider using Atom Snippets instead (installed by default).

a) In Atom, select “Atom > Open Your Snippets”.

b) In your snippets file add something like:

'.source.js':
  'Meteor Template':
    'prefix': 'mtmpl'
    'body': 'Template.example.onCreated(function(){\n});\nTemplate.example.onRendered(function(){\n});\nTemplate.example.onDestroyed(function(){\n});\nTemplate.example.helpers({\n});\nTemplate.example.events({\n})\n'

c) In any open .js file, start typing “mtmpl” and hit tab to complete.

Hey thanks for the reply! Appreciate it. It doesn’t appear atom snippets supports getting and passing the current filename ? I would want to output Template.FILENAME.onCreated etc etc as opposed to Template.example.onCreated etc etc . Am I missing something here ?

Thank you.
Alex

Just use ${#} variables - when you use these and create a snippet, the focus is then put automatically on the defined variable. Try the following to see what I mean:

'.source.js':
  'Meteor Template':
    'prefix': 'mtmpl'
    'body': 'Template.${1:"example"}.onCreated(function(){\n});\nTemplate.${1:"example"}.onRendered(function(){\n});\nTemplate.${1:"example"}.onDestroyed(function(){\n});\nTemplate.${1:"example"}.helpers({\n});\nTemplate.${1:"example"}.events({\n})\n'

You can then type the template name right after running the snippet, and have it updated in all areas.

1 Like

You are awesome :slight_smile: Thanks for your help. Keep in touch fellow meteorite