Huuums / vscode-folder-templates

Create your own Component structure with a simple click.
MIT License
214 stars 33 forks source link

Add folder and template paths variables #93

Open tmura-ido opened 2 years ago

tmura-ido commented 2 years ago

It would be nice if there was a variable to add inside the template that contains the project template output path like imports.

example:

template_name:
  file.js
  inside_folder:
    datasource.js

if I select the template path inside the project realtive path src/, inside the datasource.js would be two options to relative paths to the project

  1. [FTTemplatePath] template output root path -> src/template_name (or src)
  2. [FTRelativePath] template output file path -> src/template_name/inside_folder

so inside the datasource.js file I could use the relative path it has to import the file.js

import {foo, bar} from '[FTTemplatePath]/file.js';
Huuums commented 2 years ago

Hey, sorry for taking a while to respond.

I think this is kind of tricky to implement.

for [FTTemplatePath] these things come to mind

[FTRelativePath] doesn't make sense to me to be honest. It would be the same as a simple ./ or am I missing something here? Can you maybe give another example so I can understand better?

stanimirt-broadcom commented 2 years ago

That would be nice to be implemented. In the file, we can have a path to file ../../../filename.ts, however, you can create it in a different folder in the solution and the path won't work. You need to change it manually to correct one, e.g. ../../filename.ts.

lucas-labs commented 1 year ago

This would be useful. For example to define golang package name on each file using the name of the parent folder

project
  |- foo
  |      |- bar.go
// bar.go
package foo

...
nickiepucel commented 9 months ago

I'm not sure if this is the same thing or not, but I certainly think it would be useful to have a variable such as FTDirectory which points to the directory in which you are creating your template folder.

Similar to how VSCode snippets offers a TM_DIRECTORY variable.

This would allow me to include imports in my template files:

// Template folder
My Custom Template: // this is the template directory in .fttemplates
  index.ts
  other.ts
// in index.ts
import other from "<FTDirectory>/<FTName>/other";

Additional context: This would be useful in my case since we don't use relative imports (./) in our repo—paths are all relative to the project root.

jblossomweb commented 4 months ago

I think a variable like this would be useful specifically for templating a Storybook file, or a Jest test file, when you or your team have decided to follow a convention of naming stories and/or tests based on their top-down directory paths.

Storybook

As of version 6.4, Storybook explicitly disallows a dynamic value for the default export's title attribute of a story:

// can't do this
const path = (__dirname.split("/src/app/")[1])

export default {
  title: path, // eg: 'components/MyComponent'

// this is ok
export default {
  title: 'components/MyComponent',

Details: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#string-literal-titles

so the ability to output some sort of interpolation of the value of what the outputted file's __dirname variable would be in a template could be very helpful in this situation:

export default {
  title: '<FTDirectory>',

both to get around this limitation, and because a flat string in the outputted code might be easier to read anyway (albeit admittedly less dynamic at runtime, in case you end up moving the file around).

Jest

This could also be used for Jest test files. While Jest still allows this to be dynamic last I checked, eg:

// this is ok
const path = (__dirname.split("/src/")[1])

describe(path, () => {

I'd, again, still prefer to be able to output it from a template:

describe('<FTDirectory>', () => {

For the same reason, because the following outputted code would be easier to read:

describe('app/components/MyComponent', () => {