{"size": 1407, "ext": "js", "max_stars_repo_path": "commands/utils.js", "max_stars_repo_name": "Hsins/hackmd-themes", "max_stars_repo_stars_event_min_datetime": "2022-03-07T11:02:37.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-14T03:38:54.000Z", "max_issues_repo_path": "commands/utils.js", "max_issues_repo_name": "Hsins/hackmd-themes", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "commands/utils.js", "max_forks_repo_name": "Hsins/hackmd-themes", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 7, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 28.7142857143, "max_line_length": 104, "alphanum_fraction": 0.6204690832}
import log from 'log-beautify';
import axios from 'axios';
import { writeFile, readFileSync } from 'fs';
import { stylePath, themePath, HACKMD_API_URL, HACKMD_API_TEAM, HACKMD_API_TOKEN } from '../configs.js';

const getMetadata = (theme) => {
  return (
    '---\n' +
    `title: "${theme.metadata.name}"\n` +
    `description: "${theme.metadata.description}"\n` +
    `tags: ${theme.metadata.tags.join(', ')}\n` +
    'breaks: false\n' +
    '---\n'
  );
};

const getStyle = (theme) => {
  const filePath = `${stylePath}/${theme.styleFile}`;
  const minifiedCss = readFileSync(filePath, 'utf-8');

  return `<style>\n` + `${minifiedCss}\n` + `</style>\n`;
};

const updateTheme = (theme, content) => {
  const filePath = `${themePath}/${theme.themeFile}`;

  writeFile(filePath, content, (err) => {
    if (err) throw err;
    log.success(`Theme update: ${filePath} (${theme.slug}).`);
  });
};

const uploadTheme = (theme, content) => {
  const axiosConfig = {
    method: 'patch',
    url: `${HACKMD_API_URL}/teams/${HACKMD_API_TEAM}/notes/${theme.noteId}`,
    headers: {
      'Authorization': HACKMD_API_TOKEN,
      'Content-Type': 'application/json',
    },
    data: JSON.stringify({ content }),
  };

  axios(axiosConfig)
    .then((response) => log.success(`Theme upload: ${theme.slug}.`))
    .catch((error) => log.error(error));
};

export { getMetadata, getStyle, updateTheme, uploadTheme };