Skip to content
Snippets Groups Projects
watchdocs.js 1.68 KiB
Newer Older
  • Learn to ignore specific revisions
  • Markus Kaarto's avatar
    Markus Kaarto committed
    var argv = require("minimist")(process.argv.slice(2));
    
    const fs = require("fs");
    const folder = "./src/components/";
    
    const target = argv["target"] || process.env.DOCS_LOCATION;
    console.log("env: ", JSON.stringify(process.env.DOCS_LOCATION));
    if (!target) {
    
    Markus Kaarto's avatar
    Markus Kaarto committed
      console.error(
    
        "please provide Vuepress folder address as an argument. i.e '--target ../documentation/docs', OR as an environment variable DOCS_LOCATION"
    
    Markus Kaarto's avatar
    Markus Kaarto committed
      );
      process.exit();
    }
    
    function readFolder(folder, subName) {
      const files = fs.readdirSync(folder);
      var list = [];
      files.forEach(file => {
        const stat = fs.statSync(folder + "/" + file);
        if (stat.isDirectory()) {
          const dir = `${folder}/${file}/`;
          list = list.concat(readFolder(dir, file));
        } else {
          if (file.includes(".md") && !!subName) {
            list.push({
              file: fs.readFileSync(`${folder}/${file}`),
              name: subName
            });
          }
        }
      });
      return list;
    }
    
    function watchFolder(folder, cb) {
      const files = fs.readdirSync(folder);
      files.forEach(file => {
        const stat = fs.statSync(folder + "/" + file);
        if (stat.isDirectory()) {
          const dir = `${folder}${file}/`;
          debouncedWatch(dir, cb);
        }
      });
    }
    
    function debouncedWatch(dir, cb) {
      let fsWait = false;
      fs.watch(dir, (_, filename) => {
        if (filename) {
          if (fsWait) return;
          fsWait = setTimeout(() => {
            fsWait = false;
          }, 100);
          console.log(`${filename} file Changed`);
          cb();
        }
      });
    }
    
    watchFolder(folder, () => {
      const files = readFolder(folder);
      files.forEach(file => {
    
        console.log("writing docs at: ", `${target}/${file.name}.md`);
        fs.writeFileSync(`${target}/${file.name}.md`, file.file);
    
    Markus Kaarto's avatar
    Markus Kaarto committed
      });
    });