All files / src/scripts/createProject copyAgnosticTheme.ts

100% Statements 16/16
100% Branches 6/6
100% Functions 4/4
100% Lines 15/15

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 411x 1x 1x 1x 1x   1x 3x 3x 25x   3x 25x 25x 1x     24x                               25x              
import { getThemePath, buildFileTargetPath } from '../../utils/helpers';
import getAllFilesFromDir from '@vue-storefront/nuxt-theme/scripts/getAllFilesFromDir';
import compileTemplate from '@vue-storefront/nuxt-theme/scripts/compileTemplate';
import * as path from 'path';
import * as fs from 'fs';
 
export default async (integration: string, targetPath: string): Promise<void> => {
  const agnosticThemePath = getThemePath('nuxt-theme/theme');
  const agnosticThemeFiles = getAllFilesFromDir(agnosticThemePath)
    .filter(file => !file.includes(path.sep + 'static' + path.sep));
 
  const compileAgnosticTemplate = (filePath: string, targetPath: string, chopPhrase: string): Promise<void> => {
    const finalPath = buildFileTargetPath(filePath, targetPath, chopPhrase);
    if (fs.existsSync(finalPath)) {
      return;
    }
 
    return compileTemplate(
      path.isAbsolute(filePath)
        ? filePath
        : path.join(agnosticThemePath.replace(/\/theme$/, ''), filePath),
      finalPath.replace('theme', ''),
      {
        generate: {
          replace: {
            apiClient: `@vue-storefront/${integration}-api`,
            composables: `@vue-storefront/${integration}`
          }
        }
      }
    );
  };
 
  await Promise.all(agnosticThemeFiles.map(absoluteDirectoryPath => compileAgnosticTemplate(
    absoluteDirectoryPath,
    path.isAbsolute(targetPath)
      ? targetPath
      : path.join(__dirname, targetPath),
    agnosticThemePath)));
};