简体   繁体   中英

How to generate a file with Node.js from a predefined template?

I am building an app with React Native and when I need a new component, I go through a few steps (lets assume I need a Button component) :

  1. Create a new folder: /src/components/Button
  2. Create Button.js
  3. Create Button.styles.js
  4. Create index.js , which exports both the component and its styles

That's takes some time, so I want to create a script that does the following things:

  1. Prompts a new component name
  2. Checks if that component already exists. If so, displays error
  3. Creates files listed above with predefined templates, for example:

     import $component from './$component'; import styles from './$component.styles'; export { $component, styles }; 

    where $component is a name, provided via cli.

I can create folder and files with node, but how can I write a template and provide a variable to those files?

Create a template file manually. While running your script, read the contents of the file and then replace predetermined variable ($component for example) with the name from the console.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM