Skip to content

Commit

Permalink
chore: Add logger for command
Browse files Browse the repository at this point in the history
  • Loading branch information
Jungwoo-An committed Dec 24, 2020
1 parent e69e50a commit 0efa0e8
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 14 deletions.
Binary file not shown.
Binary file not shown.
Binary file modified .yarn/install-state.gz
Binary file not shown.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@
"@typescript-eslint/eslint-plugin": "^4.3.0",
"@typescript-eslint/parser": "^4.3.0",
"@vue/compiler-sfc": "^3.0.4",
"chalk": "^4.1.0",
"eslint": "^7.10.0",
"eslint-config-airbnb-base": "^14.2.0",
"eslint-config-prettier": "^6.12.0",
"eslint-plugin-import": "^2.22.1",
"glob": "^7.1.6",
"glob-promise": "^3.4.0",
"readline": "^1.3.0",
"rollup": "^2.32.1",
"rollup-plugin-analyzer": "^4.0.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.28.0",
"rollup-plugin-vue": "^6.0.0",
Expand Down
31 changes: 25 additions & 6 deletions scripts/commands/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const packageJSON = require('../../package.json');

const { getWorkspacePackages } = require('../utils');
const { createRollupConfig } = require('../config');
const { error, done, info } = require('../logger');

(async () => {
const packages = await getWorkspacePackages(packageJSON.workspaces || []);
Expand All @@ -16,11 +17,29 @@ const { createRollupConfig } = require('../config');
})
.map(createRollupConfig);

await Promise.all(
configs.map(async (config) => {
const compiler = await rollup(config);

return Promise.all(config.output.map(compiler.write));
})
info(
`bundles ${packages.join(', ')}${configs
.map((config) => config.output.map(({ file }) => file).join(', '))
.join(', ')}`
);

try {
const startMs = Date.now();

await Promise.all(
configs.map(async (config) => {
const compiler = await rollup(config);

return Promise.all(config.output.map(compiler.write));
})
);

done(
`created ${configs
.map((config) => config.output.map(({ file }) => file).join(', '))
.join(', ')} in ${Date.now() - startMs}ms`
);
} catch (e) {
error(e.message);
}
})();
53 changes: 46 additions & 7 deletions scripts/commands/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const packageJSON = require('../../package.json');

const { getWorkspacePackages } = require('../utils');
const { createRollupConfig } = require('../config');
const { error, done, info } = require('../logger');

(async () => {
const packages = await getWorkspacePackages(packageJSON.workspaces || []);
Expand All @@ -16,13 +17,51 @@ const { createRollupConfig } = require('../config');
})
.map(createRollupConfig);

await Promise.all(
configs.map(async (config) => {
const compiler = await rollup(config);

return Promise.all(config.output.map(compiler.write));
})
info(
`bundles ${packages.join(', ')}${configs
.map((config) => config.output.map(({ file }) => file).join(', '))
.join(', ')}`
);

watch(configs);
try {
const startMs = Date.now();

await Promise.all(
configs.map(async (config) => {
const compiler = await rollup(config);

return Promise.all(config.output.map(compiler.write));
})
);

done(
`created ${configs
.map((config) => config.output.map(({ file }) => file).join(', '))
.join(', ')} in ${Date.now() - startMs}ms`
);
} catch (e) {
error(e.message);
}

const watcher = watch(configs);
watcher.on('event', (e) => {
switch (e.code) {
case 'ERROR':
error(e.error.message);
// eslint-disable-next-line no-console
console.log(e.error.stack);
break;

case 'BUNDLE_START':
info(`bundles ${e.input}${e.output.join(', ')}`);
break;

case 'END':
done('waiting for changes...');
break;

default:
break;
}
});
})();
47 changes: 47 additions & 0 deletions scripts/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* eslint-disable no-console */
const readline = require('readline');

const chalk = require('chalk');

function clearConsole() {
if (!process.stdout.isTTY) {
return;
}

const blank = '\n'.repeat(process.stdout.rows);
console.log(blank);
readline.cursorTo(process.stdout, 0, 0);
readline.clearScreenDown(process.stdout);
}

function info(...args) {
clearConsole();

console.log(`${chalk.bgBlue.black(' INFO ')} ${chalk.white(...args)}`);
}

function done(...args) {
clearConsole();

console.log(`${chalk.bgGreen.black(' DONE ')} ${chalk.green(...args)}`);
}

function warn(...args) {
clearConsole();

console.log(`${chalk.bgYellow.black(' WARN ')} ${chalk.yellow(...args)}`);
}

function error(...args) {
clearConsole();

console.log(`${chalk.bgRed(' ERROR ')} ${chalk.red(...args)}`);
}

module.exports = {
info,
done,
warn,
error,
clearConsole,
};
19 changes: 18 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ __metadata:
languageName: node
linkType: hard

"chalk@npm:^4.0.0":
"chalk@npm:^4.0.0, chalk@npm:^4.1.0":
version: 4.1.0
resolution: "chalk@npm:4.1.0"
dependencies:
Expand Down Expand Up @@ -3175,6 +3175,13 @@ fsevents@~2.1.2:
languageName: node
linkType: hard

"readline@npm:^1.3.0":
version: 1.3.0
resolution: "readline@npm:1.3.0"
checksum: 1638f45209e339c90891d609f999748d2113b8c2e259c19c7e0ea14d0c7439fee0d74c52b09ff17372e52387f8cca9f94650c03396696ac3874d72efc8804219
languageName: node
linkType: hard

"regexpp@npm:^3.0.0, regexpp@npm:^3.1.0":
version: 3.1.0
resolution: "regexpp@npm:3.1.0"
Expand Down Expand Up @@ -3280,6 +3287,13 @@ [email protected]:
languageName: node
linkType: hard

"rollup-plugin-analyzer@npm:^4.0.0":
version: 4.0.0
resolution: "rollup-plugin-analyzer@npm:4.0.0"
checksum: 3d72bd5027cfea692d4bc625aac0599f90d7ea81c7fe40c25178d3c42292becb219418697852eb5e2c36e7068fcdf0236f1e423fb57bb2a24d0eccb6c032ac22
languageName: node
linkType: hard

"rollup-plugin-terser@npm:^7.0.2":
version: 7.0.2
resolution: "rollup-plugin-terser@npm:7.0.2"
Expand Down Expand Up @@ -3468,13 +3482,16 @@ [email protected]:
"@typescript-eslint/eslint-plugin": ^4.3.0
"@typescript-eslint/parser": ^4.3.0
"@vue/compiler-sfc": ^3.0.4
chalk: ^4.1.0
eslint: ^7.10.0
eslint-config-airbnb-base: ^14.2.0
eslint-config-prettier: ^6.12.0
eslint-plugin-import: ^2.22.1
glob: ^7.1.6
glob-promise: ^3.4.0
readline: ^1.3.0
rollup: ^2.32.1
rollup-plugin-analyzer: ^4.0.0
rollup-plugin-terser: ^7.0.2
rollup-plugin-typescript2: ^0.28.0
rollup-plugin-vue: ^6.0.0
Expand Down

0 comments on commit 0efa0e8

Please sign in to comment.