<commit_before>import fs from 'fs';
import path from 'path';
import ejs from 'ejs';
import tmp from 'tmp';
import { getNpmLayer } from './layer';
import pkg from './pkg';

tmp.setGracefulCleanup();

export const getTemplate = (templateName) => {
  const templateSearchPaths = [
    process.cwd(),
    path.join(process.cwd(), 'share'),
    __dirname,
    path.join(__dirname, 'share'),
    path.join(__dirname, '..', 'share'),
  ];

  let templatePath = null;

  for (const templateDir of templateSearchPaths) {
    templatePath = path.join(templateDir, templateName);
    if (fs.existsSync(templatePath)) {
      break;
    }
  }

  try {
    return fs.readFileSync(templatePath).toString('utf-8');
  } catch (error) {
    console.error(` * error: no such file or read error: ${error.message}`);
    return false;
  }
};

export const renderContent = (templateContent, vars = {}, options = {}) => {
  try {
    return ejs.render(templateContent, vars, options);
  } catch (renderError) {
    console.error(`Rendering error: ${renderError.message}`);
    return '';
  }
};

export const writeTempFile = (content) => {
  const tmpDir = process.cwd();
  const tf = tmp.fileSync({
    mode: '0644',
    dir: tmpDir,
    prefix: '.tmp-containr-',
    postfix: '',
  });
  fs.writeFileSync(tf.fd, content);
  return tf.name;
};

export const renderTemplate = (templateContent, vars = {}) => {
  const pkgLocal = pkg.getLocalPkg();
  const labels = pkg.getPackageLabels(pkg);

  const layer = Object.assign({
    npm: baseLayer => getNpmLayer(baseLayer, pkg),
  }, vars.layer || {});

  const containr = Object.assign({
    imageName: pkg.parsePackageName(pkg.name),
  }, vars.containr || {});

  // const templateVars = Object.assign({
  // })

  const dockerFileContent = renderContent(templateContent, {
    pkg: pkgLocal,
    labels,
    layer,
    containr,
  }, {
    // debug: true,
  });

  // console.log(dockerFileContent);
  const dockerFilePath = writeTempFile(dockerFileContent);
  return dockerFilePath;
};

export const renderFile = (fileName, vars = {}) => {
  const templateContent = getTemplate(fileName);
  return renderTemplate(templateContent, vars);
};

export const getExtension = (filename = '') => {
  if (filename.indexOf('.') === -1) {
    return '';
  }
  return filename.split('.').pop();
};

export const isEjs = (filename = '') => getExtension(filename) === 'ejs';

export default {
  renderFile,
  writeTempFile,
  getExtension,
  isEjs,
};
<commit_msg>fix diff between pkg and pkgLocal<commit_after>import fs from 'fs';
import path from 'path';
import ejs from 'ejs';
import tmp from 'tmp';
import { getNpmLayer } from './layer';
import l from './logger';
import pkg from './pkg';

tmp.setGracefulCleanup();

export const getTemplate = (templateName) => {
  const templateSearchPaths = [
    process.cwd(),
    path.join(process.cwd(), 'share'),
    __dirname,
    path.join(__dirname, 'share'),
    path.join(__dirname, '..', 'share'),
  ];

  let templatePath = null;

  for (const templateDir of templateSearchPaths) {
    templatePath = path.join(templateDir, templateName);
    if (fs.existsSync(templatePath)) {
      break;
    }
  }

  try {
    return fs.readFileSync(templatePath).toString('utf-8');
  } catch (error) {
    console.error(` * error: no such file or read error: ${error.message}`);
    return false;
  }
};

export const renderContent = (templateContent, vars = {}, options = {}) => {
  try {
    return ejs.render(templateContent, vars, options);
  } catch (renderError) {
    console.error(`Rendering error: ${renderError.message}`);
    return '';
  }
};

export const writeTempFile = (content) => {
  const tmpDir = process.cwd();
  const tf = tmp.fileSync({
    mode: '0644',
    dir: tmpDir,
    prefix: '.tmp-containr-',
    postfix: '',
  });
  fs.writeFileSync(tf.fd, content);
  return tf.name;
};

export const renderTemplate = (templateContent, vars = {}) => {
  // @TODO cleanup vars in templates
  const pkgLocal = pkg.getLocalPkg();
  const labels = pkg.getPackageLabels(pkgLocal);

  const layer = Object.assign({
    npm: baseLayer => getNpmLayer(baseLayer, pkgLocal),
  }, vars.layer || {});

  const containr = Object.assign({
    imageName: pkg.parsePackageName(pkgLocal.name),
  }, vars.containr || {});

  // const templateVars = Object.assign({
  // })

  const dockerFileContent = renderContent(templateContent, {
    pkg: pkgLocal,
    labels,
    layer,
    containr,
  }, {
    // debug: true,
  });

  // console.log(dockerFileContent);
  const dockerFilePath = writeTempFile(dockerFileContent);
  return dockerFilePath;
};

export const renderFile = (fileName, vars = {}) => {
  const templateContent = getTemplate(fileName);
  return renderTemplate(templateContent, vars);
};

export const getExtension = (filename = '') => {
  if (filename.indexOf('.') === -1) {
    return '';
  }
  return filename.split('.').pop();
};

export const isEjs = (filename = '') => getExtension(filename) === 'ejs';

export default {
  renderFile,
  writeTempFile,
  getExtension,
  isEjs,
};

{'fraction_non_alphanumeric': 0.15822784810126583, 'fraction_numerical': 0.0023734177215189874, 'mean_word_length': 3.605646630236794, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6493454', 'n_tokens_mistral': 1629, 'n_tokens_neox': 1600, 'n_words': 382}