const transform = require('./src/transform');
const { includeDir } = require('./src/config');
const { collection } = require('./src/collect');
const { resolve } = require('path');
const { upload } = require('./src/upload');

/**
 * 同步文档
 */
async function syncDoc(done) {
  const files = includeDir
    .map(item => collection(resolve(process.cwd(), item.path), item.deep))
    .flat()
    .filter(file => !file.includes('en-US') && !file.includes('en_US'));
  const transformContent = files.map(file => transform(file));
  const uploadList = transformContent.map(item =>
    upload({
      tag: item.id,
      title: item.fileName,
      content: item.content,
    }),
  );

  try {
    await Promise.all(uploadList);
  } catch (error) {
    // eslint-disable-next-line no-console
    console.log('上传文件失败: ', error);
  } finally {
    done();
  }
}

module.exports = syncDoc;
