const glob = require('glob');
const fs = require('fs');
const filenames = [];
var entryFiles = glob.sync('./**/*.vue');
entryFiles.forEach(filePath => {
  filenames.push(filePath);
});
const getImportItemStr = path => {
  const filename = path.substring(
    path.lastIndexOf('/') + 1,
    path.lastIndexOf('.')
  );
  return `import ${filename} from '${path}';`;
};
const getRegistItemStr = path => {
  const filename = path.substring(
    path.lastIndexOf('/') + 1,
    path.lastIndexOf('.')
  );
  return `Vue.component('${filename}', ${filename});`;
};

const content = [
  `import Vue from 'vue';`,
  '\r\n',
  filenames.map(x => getImportItemStr(x)).join('\r\n'),
  '\r\n\r\n',
  filenames.map(x => getRegistItemStr(x)).join('\r\n'),
  '\r\n'
];

fs.writeFile('./index.js', content.join(''), function(err) {
  if (err) console.log(err);
  else console.log('写文件操作成功');
});
