All files / platform/cli/src/commands/utils createDirectoryContents.js

0% Statements 0/16
0% Branches 0/10
0% Functions 0/2
0% Lines 0/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41                                                                                 
import fs from 'fs';
 
// https://github.dev/leoroese/template-cli/blob/628dd24db7df399ebb520edd0bc301bc7b5e8b66/index.js#L19
const createDirectoryContents = (templatePath, targetDirPath, copyPrettierRules) => {
  const filesToCreate = fs.readdirSync(templatePath);
 
  filesToCreate.forEach(file => {
    if (!copyPrettierRules && file === '.prettierrc') {
      return;
    }
 
    const origFilePath = `${templatePath}/${file}`;
 
    // get stats about the current file
    const stats = fs.statSync(origFilePath);
 
    if (stats.isFile()) {
      const contents = fs.readFileSync(origFilePath, 'utf8');
 
      // Rename
      if (file === '.npmignore') {
        file = '.gitignore';
      }
 
      const writePath = `${targetDirPath}/${file}`;
      fs.writeFileSync(writePath, contents, 'utf8');
    } else if (stats.isDirectory()) {
      fs.mkdirSync(`${targetDirPath}/${file}`);
 
      // recursive call
      createDirectoryContents(
        `${templatePath}/${file}`,
        `${targetDirPath}/${file}`,
        copyPrettierRules
      );
    }
  });
};
 
export default createDirectoryContents;