background

In normal project development, most people are not used to writing comments or component explanations as they write components. This can lead to unfriendly use of work handover or component promotion.

Pain points

With this in mind, it is important to write a complete code comment as the project iterations get faster and larger.

Therefore, I choose to record in the form of MD files. However, due to the large number of project directories, I need to open one folder one by one, and then create one by one. Finally, I find an MD file in the corresponding file directory to write the MD file….. A whole set of operations down, boy! It took 10 minutes just to create the folder and start writing, which led the programmer to close the computer and write a DER. “, so this led to the huge (excrement) stone (mountain) project to the back responsible for the handover of the students confused.

How to solve

Good programmers like to be lazy, and lazy here is lazy but not totally lazy. With a good script, we can save a lot of wasted time moving more bricks. = = @!

The body of the

Nonsense don’t say, directly on the talent! (EGM,~

const fs = require('fs');
/** * Config file */
const configs = {
  auto: false.fileContent: ## here is the title.// When auto is true, the field is automatically filled in
  template: [{path: '/pscWorld'.fileContent: ## here is the title}, {path: '/template'.fileContent: ## here is the title,}]};const fileName = 'index.md';
function isDir(file) {
  return `${process.cwd()}/${file}`.split('. ').length ! = =2 && !(/ /..test(`${process.cwd()}/${file}`));
}
/** * write the function */
function writeFile(path, content) {
  fs.writeFile(`${path}/${fileName}`, content, (err) = > {
    if (err) {
      console.log('Creation failed');
    } else {
      console.log('Created successfully'); }}); }/** * reads the directory function */
function readDir() {
  const dir = [];
  const files = fs.readdirSync(process.cwd());
  files.forEach((file) = > {
    if(isDir(file)) { dir.push(file); }});return dir;
}
/** * main function */
function run() {
  if (configs.auto) {
    const dirs = readDir();
    for (let i = 0; i < dirs.length; i++) {
      if (dirs.length === configs.template) {
        const { fileContent } = configs.template[i];
        writeFile(`${process.cwd()}/${dirs[i]}`, fileContent);
      } else {
        writeFile(`${process.cwd()}/${dirs[i]}`, configs.fileContent); }}}else {
    for (let i = 0; i < configs.template; i++) {
      const { fileContent, path } = configs.template[i];
      writeFile(`${process.cwd()}/${path}`, fileContent);
    }
  }
}

run();
Copy the code

explain

Configs: When auto is true, md files are automatically created in all folders in the script run directory. Otherwise, if the value is false, the path in template will be read and filled. FileContent will be filled.

Afterword.

There is still room for improvement and expansion in this script, for example, can you automatically create template files with templates in the specified directory? (Similar to the type file generated by the editor)

Or is it possible to add a component or page to the current project with one click? (Add router and page to corresponding directory, similar to vue-CLI function)

There are so many ideas. How do you do that? Based on this article, we will continue to discuss how to implement a VUe-CLI from 0 to 1