Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch download command to use axios #25543

Merged
merged 1 commit into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 11 additions & 20 deletions cli/download.mts
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import fs from 'fs';
import { get } from 'https';
import { writeFile } from 'fs/promises';
import path from 'path';
import { inspect } from 'util';
import axios from 'axios';

import { logger } from './utils.mjs';
import { packageJson } from '../lib/index.js';

const downloadFile = (url: string, filename: string): Promise<string> => {
return new Promise((resolve, reject) => {
logger.verboseInfo(`Downloading file: ${url}`);
get(url, response => {
if (response.statusCode !== 200) {
reject(new Error(`Error downloading ${url}: ${response.statusCode} - ${response.statusMessage}`));
return;
}

logger.debug(`Creating file: ${path.join(filename)}`);
const fileStream = fs.createWriteStream(`${filename}`);
fileStream.on('finish', () => fileStream.close());
fileStream.on('close', () => resolve(filename));
response.pipe(fileStream);
}).on('error', e => {
reject(e);
});
});
const downloadFile = async (url: string, filename: string): Promise<string> => {
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 type DownloadJdlOptions = { skipSampleRepository?: boolean };
Expand Down