Skip to content

Commit 83adeff

Browse files
Merge pull request #16 from mediamonks/feature/more-async-in-mm-preview
more async stats, Promise.all, big files load faster
2 parents 90b66f3 + 1f96f4c commit 83adeff

File tree

2 files changed

+23
-31
lines changed

2 files changed

+23
-31
lines changed

Diff for: src/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Filenames = require('./data/Filenames');
77
const fs = require('fs-extra');
88
const inquirer = require('inquirer');
99
const path = require('path');
10+
const chalk = require('chalk')
1011

1112
module.exports = async (options = {}, cli) => {
1213
const filepathRc = `./${Filenames.RC}`;
@@ -156,7 +157,9 @@ module.exports = async (options = {}, cli) => {
156157
await fs.writeJSON(filepathRc, data, {spaces: 2})
157158

158159
// console.log(targetData)
160+
const start = Date.now()
161+
159162
await target.action(targetData);
160163

161-
console.log(`Done, Have a nice day.`);
162-
};
164+
console.log(chalk.green(`Done in ${Date.now() - start}ms, Have a nice day!`));
165+
};

Diff for: src/target/mm-preview.js

+18-29
Original file line numberDiff line numberDiff line change
@@ -69,40 +69,29 @@ const preview = {
6969

7070
const allFiles = await globPromise(`${data.inputDir.replace(/\\/g, '/')}/**/*`);
7171

72-
const filesArray = allFiles
73-
.filter((file) => fs.lstatSync(file).isFile())
74-
.map((file) => {
75-
return {
76-
filePath: file,
77-
s3Path: data.outputDir + path.relative(data.inputDir, file).replace(/\\/g, '/'),
78-
contentType: mime.lookup(file),
79-
content: fs.readFileSync(file),
80-
};
81-
});
72+
const filesArray = (await Promise.all(allFiles.map(async file => {
73+
if ((await fs.promises.lstat(file)).isFile()) {
74+
return new PutObjectCommand({
75+
Bucket: data.bucket,
76+
Key: data.outputDir + path.relative(data.inputDir, file).replace(/\\/g, '/'),
77+
ContentType: mime.lookup(file),
78+
Body: await fs.promises.readFile(file),
79+
})
80+
}
81+
})))
82+
.filter(file => file !== undefined)
8283

8384
const progressBar = new cliProgress.SingleBar({}, cliProgress.Presets.shades_classic);
8485
progressBar.start(filesArray.length, 0);
8586

8687
await Promise.all(
87-
filesArray.map((file) => {
88-
return new Promise(async (resolve) => {
89-
const params = {
90-
Bucket: data.bucket,
91-
Key: file.s3Path,
92-
Body: file.content,
93-
ContentType: file.contentType,
94-
};
95-
96-
try {
97-
await client.send(new PutObjectCommand(params));
98-
progressBar.increment();
99-
resolve(data);
100-
} catch (error) {
101-
console.log(error);
102-
} finally {
103-
// finally.
104-
}
105-
});
88+
filesArray.map(async file => {
89+
try {
90+
await client.send(file);
91+
} catch (e) {
92+
console.log(e);
93+
}
94+
progressBar.increment();
10695
}),
10796
);
10897

0 commit comments

Comments
 (0)