UNPKG

generator-begcode

Version:

Spring Boot + Angular/React/Vue in one handy generator

51 lines (50 loc) 2.02 kB
import { writeFile } from 'fs/promises'; import path from 'path'; import { inspect } from 'util'; import axios from 'axios'; import { packageJson } from '../lib/index.js'; import { logger } from './utils.mjs'; const downloadFile = async (url, filename) => { logger.verboseInfo(`Downloading file: ${url}`); const response = await axios.get(url); if (response.status !== 200) { throw new Error(`Error downloading ${url}: ${response.status} - ${response.statusText}`); } logger.debug(`Creating file: ${path.join(filename)}`); await writeFile(filename, response.data, 'utf8'); return filename; }; export const downloadJdlFile = async (jdlFile, { skipSampleRepository } = {}) => { let url; try { const urlObject = new URL(jdlFile); url = jdlFile; jdlFile = path.basename(urlObject.pathname); } catch (_error) { if (skipSampleRepository) { throw new Error(`Could not find ${jdlFile}, make sure the path is correct.`); } url = new URL(jdlFile, `https://raw.githubusercontent.com/jhipster/jdl-samples/v${packageJson.version}/`).toString(); jdlFile = path.basename(jdlFile); } try { return await downloadFile(url, jdlFile); } catch (error) { logger.verboseInfo(error.message); const branchName = 'v8'; url = new URL(jdlFile, `https://raw.githubusercontent.com/jhipster/jdl-samples/${branchName}/`).toString(); return downloadFile(url, jdlFile); } }; export const downloadJdlFiles = async (jdlFiles, options = {}) => { logger.debug('cmd: download'); logger.debug(`jdlFiles: ${inspect(jdlFiles)}`); if (!jdlFiles || jdlFiles.length === 0) { throw new Error('\nAt least one jdl file is required.\n'); } return Promise.all(jdlFiles.map(filename => downloadJdlFile(filename, options))); }; const downloadJdlCommand = (positionalArgs, options) => downloadJdlFiles(positionalArgs[0], options); export default downloadJdlCommand;