loadPresetFromDir.js 777 B

12345678910111213141516171819202122
  1. const path = require('path')
  2. const fs = require('fs-extra')
  3. module.exports = async function loadPresetFromDir (dir) {
  4. const presetPath = path.join(dir, 'preset.json')
  5. if (!fs.existsSync(presetPath)) {
  6. throw new Error('remote / local preset does not contain preset.json!')
  7. }
  8. const preset = await fs.readJson(presetPath)
  9. // if the preset dir contains generator.js or generator/index.js, we will inject it as a hidden
  10. // plugin so it will be invoked by the generator.
  11. const hasGenerator = fs.existsSync(path.join(dir, 'generator.js')) || fs.existsSync(path.join(dir, 'generator/index.js'))
  12. if (hasGenerator) {
  13. (preset.plugins || (preset.plugins = {}))[dir.replace(/[\/]$/, '')] = {
  14. _isPreset: true,
  15. prompts: true
  16. }
  17. }
  18. return preset
  19. }