From bb533b0c04ae64cf6513b0b3df84f56194fbdd31 Mon Sep 17 00:00:00 2001 From: seven Date: Mon, 23 Sep 2024 17:25:14 +0800 Subject: [PATCH] refactor: improve & standlize the logger #5 Signed-off-by: seven --- package-lock.json | 199 ++++++++++++++++++++++++-- package.json | 1 + src/commands/deploy.ts | 10 +- src/commands/validate.ts | 5 +- src/common/index.ts | 1 - src/common/logger.ts | 4 + src/common/printer.ts | 9 -- src/common/rosClient.ts | 8 +- src/stack/deploy.ts | 6 +- src/stack/iacSchema.ts | 6 +- tests/fixtures/serverless-insignt.yml | 2 + tests/{ => stack}/validate.test.ts | 4 +- 12 files changed, 219 insertions(+), 36 deletions(-) delete mode 100644 src/common/printer.ts rename tests/{ => stack}/validate.test.ts (96%) diff --git a/package-lock.json b/package-lock.json index 24bfaf5..6fe5641 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "commander": "^11.1.0", "i18n": "^0.15.1", "pino": "^8.17.2", + "pino-pretty": "^11.2.2", "yaml": "^2.5.1" }, "bin": { @@ -3466,6 +3467,11 @@ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, + "node_modules/colorette": { + "version": "2.0.20", + "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.20.tgz", + "integrity": "sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==" + }, "node_modules/commander": { "version": "11.1.0", "resolved": "https://registry.npmjs.org/commander/-/commander-11.1.0.tgz", @@ -3535,6 +3541,14 @@ "node": ">= 8" } }, + "node_modules/dateformat": { + "version": "4.6.3", + "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-4.6.3.tgz", + "integrity": "sha512-2P0p0pFGzHS5EMnhdxQi7aJN+iMheud0UhG4dlE1DLAlvL8JHjJJTX/CSm4JXwV0Ka5nGk3zC5mcb5bUQUxxMA==", + "engines": { + "node": "*" + } + }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -3655,6 +3669,14 @@ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", "dev": true }, + "node_modules/end-of-stream": { + "version": "1.4.4", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", + "integrity": "sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==", + "dependencies": { + "once": "^1.4.0" + } + }, "node_modules/error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", @@ -3968,6 +3990,11 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, + "node_modules/fast-copy": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/fast-copy/-/fast-copy-3.0.2.tgz", + "integrity": "sha512-dl0O9Vhju8IrcLndv2eU4ldt1ftXMqqfgN4H1cpmGV7P6jeB9FwpN9a2c8DPGE1Ys88rNUJVYDHq73CGAGOPfQ==" + }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -4038,6 +4065,11 @@ "node": ">=6" } }, + "node_modules/fast-safe-stringify": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz", + "integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==" + }, "node_modules/fast-uri": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.0.1.tgz", @@ -4287,6 +4319,11 @@ "node": ">= 0.4" } }, + "node_modules/help-me": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/help-me/-/help-me-5.0.0.tgz", + "integrity": "sha512-7xgomUX6ADmcYzFik0HzAxh/73YlKR9bmFzf51CZwR+b6YtzU2m0u49hQCqV6SvlqIqsaxovfwdvbnsw3b/zpg==" + }, "node_modules/html-escaper": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz", @@ -5152,6 +5189,14 @@ "url": "https://github.com/chalk/supports-color?sponsor=1" } }, + "node_modules/joycon": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/joycon/-/joycon-3.1.1.tgz", + "integrity": "sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw==", + "engines": { + "node": ">=10" + } + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -5392,6 +5437,14 @@ "node": "*" } }, + "node_modules/minimist": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, "node_modules/moo": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/moo/-/moo-0.5.2.tgz", @@ -5461,7 +5514,6 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", - "dev": true, "dependencies": { "wrappy": "1" } @@ -5657,6 +5709,38 @@ "split2": "^4.0.0" } }, + "node_modules/pino-pretty": { + "version": "11.2.2", + "resolved": "https://registry.npmjs.org/pino-pretty/-/pino-pretty-11.2.2.tgz", + "integrity": "sha512-2FnyGir8nAJAqD3srROdrF1J5BIcMT4nwj7hHSc60El6Uxlym00UbCCd8pYIterstVBFlMyF1yFV8XdGIPbj4A==", + "dependencies": { + "colorette": "^2.0.7", + "dateformat": "^4.6.3", + "fast-copy": "^3.0.2", + "fast-safe-stringify": "^2.1.1", + "help-me": "^5.0.0", + "joycon": "^3.1.1", + "minimist": "^1.2.6", + "on-exit-leak-free": "^2.1.0", + "pino-abstract-transport": "^1.0.0", + "pump": "^3.0.0", + "readable-stream": "^4.0.0", + "secure-json-parse": "^2.4.0", + "sonic-boom": "^4.0.1", + "strip-json-comments": "^3.1.1" + }, + "bin": { + "pino-pretty": "bin.js" + } + }, + "node_modules/pino-pretty/node_modules/sonic-boom": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/sonic-boom/-/sonic-boom-4.1.0.tgz", + "integrity": "sha512-NGipjjRicyJJ03rPiZCJYjwlsuP2d1/5QUviozRXC7S3WdVWNK5e3Ojieb9CCyfhq2UC+3+SRd9nG3I2lPRvUw==", + "dependencies": { + "atomic-sleep": "^1.0.0" + } + }, "node_modules/pino-std-serializers": { "version": "6.2.2", "resolved": "https://registry.npmjs.org/pino-std-serializers/-/pino-std-serializers-6.2.2.tgz", @@ -5823,6 +5907,15 @@ "node": ">= 6" } }, + "node_modules/pump": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.2.tgz", + "integrity": "sha512-tUPXtzlGM8FE3P0ZL6DVs/3P58k9nk8/jZeQCurTJylQA8qFYzHFfhBJkuqyE0FifOsQ0uKWekiZ5g8wtr28cw==", + "dependencies": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, "node_modules/punycode": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", @@ -6060,6 +6153,11 @@ "resolved": "https://registry.npmjs.org/sax/-/sax-1.4.1.tgz", "integrity": "sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg==" }, + "node_modules/secure-json-parse": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/secure-json-parse/-/secure-json-parse-2.7.0.tgz", + "integrity": "sha512-6aU+Rwsezw7VR8/nyvKTx8QpWH9FrcYiXXlqC4z5d5XQBDRqtbfsRjnwGyqbi3gddNtWHuEk9OANUotL26qKUw==" + }, "node_modules/semver": { "version": "7.5.4", "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", @@ -6253,7 +6351,6 @@ "version": "3.1.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==", - "dev": true, "engines": { "node": ">=8" }, @@ -6613,8 +6710,7 @@ "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", - "dev": true + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, "node_modules/write-file-atomic": { "version": "4.0.2", @@ -9225,6 +9321,11 @@ "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, + "colorette": { + "version": "2.0.20", + "resolved": "https://registry.npmjs.org/colorette/-/colorette-2.0.20.tgz", + "integrity": "sha512-IfEDxwoWIjkeXL1eXcDiow4UbKjhLdq6/EuSVR9GMN7KVH3r9gQ83e73hsz1Nd1T3ijd5xv1wcWRYO+D6kCI2w==" + }, "commander": { "version": "11.1.0", "resolved": "https://registry.npmjs.org/commander/-/commander-11.1.0.tgz", @@ -9279,6 +9380,11 @@ "which": "^2.0.1" } }, + "dateformat": { + "version": "4.6.3", + "resolved": "https://registry.npmjs.org/dateformat/-/dateformat-4.6.3.tgz", + "integrity": "sha512-2P0p0pFGzHS5EMnhdxQi7aJN+iMheud0UhG4dlE1DLAlvL8JHjJJTX/CSm4JXwV0Ka5nGk3zC5mcb5bUQUxxMA==" + }, "debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -9360,6 +9466,14 @@ "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==", "dev": true }, + "end-of-stream": { + "version": "1.4.4", + "resolved": "https://registry.npmjs.org/end-of-stream/-/end-of-stream-1.4.4.tgz", + "integrity": "sha512-+uw1inIHVPQoaVuHzRyXd21icM+cnt4CzD5rW+NC1wjOUSTOs+Te7FOv7AhN7vS9x/oIyhLP5PR1H+phQAHu5Q==", + "requires": { + "once": "^1.4.0" + } + }, "error-ex": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/error-ex/-/error-ex-1.3.2.tgz", @@ -9573,6 +9687,11 @@ "jest-util": "^29.7.0" } }, + "fast-copy": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/fast-copy/-/fast-copy-3.0.2.tgz", + "integrity": "sha512-dl0O9Vhju8IrcLndv2eU4ldt1ftXMqqfgN4H1cpmGV7P6jeB9FwpN9a2c8DPGE1Ys88rNUJVYDHq73CGAGOPfQ==" + }, "fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -9633,6 +9752,11 @@ "resolved": "https://registry.npmjs.org/fast-redact/-/fast-redact-3.3.0.tgz", "integrity": "sha512-6T5V1QK1u4oF+ATxs1lWUmlEk6P2T9HqJG3e2DnHOdVgZy2rFJBoEnrIedcTXlkAHU/zKC+7KETJ+KGGKwxgMQ==" }, + "fast-safe-stringify": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/fast-safe-stringify/-/fast-safe-stringify-2.1.1.tgz", + "integrity": "sha512-W+KJc2dmILlPplD/H4K9l9LcAHAfPtP6BY84uVLXQ6Evcz9Lcg33Y2z1IVblT6xdY54PXYVHEv+0Wpq8Io6zkA==" + }, "fast-uri": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/fast-uri/-/fast-uri-3.0.1.tgz", @@ -9815,6 +9939,11 @@ "function-bind": "^1.1.2" } }, + "help-me": { + "version": "5.0.0", + "resolved": "https://registry.npmjs.org/help-me/-/help-me-5.0.0.tgz", + "integrity": "sha512-7xgomUX6ADmcYzFik0HzAxh/73YlKR9bmFzf51CZwR+b6YtzU2m0u49hQCqV6SvlqIqsaxovfwdvbnsw3b/zpg==" + }, "html-escaper": { "version": "2.0.2", "resolved": "https://registry.npmjs.org/html-escaper/-/html-escaper-2.0.2.tgz", @@ -10456,6 +10585,11 @@ } } }, + "joycon": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/joycon/-/joycon-3.1.1.tgz", + "integrity": "sha512-34wB/Y7MW7bzjKRjUKTa46I2Z7eV62Rkhva+KkopW7Qvv/OSWBqvkSY7vusOPrNuZcUG3tApvdVgNB8POj3SPw==" + }, "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", @@ -10644,6 +10778,11 @@ "brace-expansion": "^1.1.7" } }, + "minimist": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", + "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==" + }, "moo": { "version": "0.5.2", "resolved": "https://registry.npmjs.org/moo/-/moo-0.5.2.tgz", @@ -10701,7 +10840,6 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", - "dev": true, "requires": { "wrappy": "1" } @@ -10843,6 +10981,37 @@ "split2": "^4.0.0" } }, + "pino-pretty": { + "version": "11.2.2", + "resolved": "https://registry.npmjs.org/pino-pretty/-/pino-pretty-11.2.2.tgz", + "integrity": "sha512-2FnyGir8nAJAqD3srROdrF1J5BIcMT4nwj7hHSc60El6Uxlym00UbCCd8pYIterstVBFlMyF1yFV8XdGIPbj4A==", + "requires": { + "colorette": "^2.0.7", + "dateformat": "^4.6.3", + "fast-copy": "^3.0.2", + "fast-safe-stringify": "^2.1.1", + "help-me": "^5.0.0", + "joycon": "^3.1.1", + "minimist": "^1.2.6", + "on-exit-leak-free": "^2.1.0", + "pino-abstract-transport": "^1.0.0", + "pump": "^3.0.0", + "readable-stream": "^4.0.0", + "secure-json-parse": "^2.4.0", + "sonic-boom": "^4.0.1", + "strip-json-comments": "^3.1.1" + }, + "dependencies": { + "sonic-boom": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/sonic-boom/-/sonic-boom-4.1.0.tgz", + "integrity": "sha512-NGipjjRicyJJ03rPiZCJYjwlsuP2d1/5QUviozRXC7S3WdVWNK5e3Ojieb9CCyfhq2UC+3+SRd9nG3I2lPRvUw==", + "requires": { + "atomic-sleep": "^1.0.0" + } + } + } + }, "pino-std-serializers": { "version": "6.2.2", "resolved": "https://registry.npmjs.org/pino-std-serializers/-/pino-std-serializers-6.2.2.tgz", @@ -10962,6 +11131,15 @@ "sisteransi": "^1.0.5" } }, + "pump": { + "version": "3.0.2", + "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.2.tgz", + "integrity": "sha512-tUPXtzlGM8FE3P0ZL6DVs/3P58k9nk8/jZeQCurTJylQA8qFYzHFfhBJkuqyE0FifOsQ0uKWekiZ5g8wtr28cw==", + "requires": { + "end-of-stream": "^1.1.0", + "once": "^1.3.1" + } + }, "punycode": { "version": "2.3.1", "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.3.1.tgz", @@ -11103,6 +11281,11 @@ "resolved": "https://registry.npmjs.org/sax/-/sax-1.4.1.tgz", "integrity": "sha512-+aWOz7yVScEGoKNd4PA10LZ8sk0A/z5+nXQG5giUO5rprX9jgYsTdov9qCchZiPIZezbZH+jRut8nPodFAX4Jg==" }, + "secure-json-parse": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/secure-json-parse/-/secure-json-parse-2.7.0.tgz", + "integrity": "sha512-6aU+Rwsezw7VR8/nyvKTx8QpWH9FrcYiXXlqC4z5d5XQBDRqtbfsRjnwGyqbi3gddNtWHuEk9OANUotL26qKUw==" + }, "semver": { "version": "7.5.4", "resolved": "https://registry.npmjs.org/semver/-/semver-7.5.4.tgz", @@ -11255,8 +11438,7 @@ "strip-json-comments": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", - "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==", - "dev": true + "integrity": "sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==" }, "supports-color": { "version": "7.2.0", @@ -11478,8 +11660,7 @@ "wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", - "dev": true + "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==" }, "write-file-atomic": { "version": "4.0.2", diff --git a/package.json b/package.json index d95199d..db4731e 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "commander": "^11.1.0", "i18n": "^0.15.1", "pino": "^8.17.2", + "pino-pretty": "^11.2.2", "yaml": "^2.5.1" }, "devDependencies": { diff --git a/src/commands/deploy.ts b/src/commands/deploy.ts index e75ec1e..3c3d75b 100644 --- a/src/commands/deploy.ts +++ b/src/commands/deploy.ts @@ -1,17 +1,17 @@ import { deployStack, parseYaml } from '../stack'; -import { printer, constructActionContext } from '../common'; +import { constructActionContext, logger } from '../common'; export const deploy = async ( stackName: string, options: { location: string; parameters: { [key: string]: string } }, ) => { const context = constructActionContext(options); - printer.info('Validating yaml...'); + logger.info('Validating yaml...'); const iac = parseYaml(context.iacLocation); - printer.success('Yaml is valid! πŸŽ‰'); + logger.info('Yaml is valid! πŸŽ‰'); - printer.info('Deploying stack...'); + logger.info('Deploying stack...'); await deployStack(stackName, iac, context); - printer.success('Stack deployed! πŸŽ‰'); + logger.info('Stack deployed! πŸŽ‰'); }; diff --git a/src/commands/validate.ts b/src/commands/validate.ts index dce5918..0c3fffd 100644 --- a/src/commands/validate.ts +++ b/src/commands/validate.ts @@ -1,8 +1,9 @@ -import { printer, constructActionContext } from '../common'; +import { constructActionContext, logger } from '../common'; import { parseYaml } from '../stack'; export const validate = (location?: string) => { const context = constructActionContext({ location }); parseYaml(context.iacLocation); - printer.success('Yaml is valid! πŸŽ‰'); + logger.info('Yaml is valid! πŸŽ‰'); + logger.debug('Yaml is valid! debugπŸŽ‰'); }; diff --git a/src/common/index.ts b/src/common/index.ts index d127481..233aa34 100644 --- a/src/common/index.ts +++ b/src/common/index.ts @@ -1,4 +1,3 @@ -export * from './printer'; export * from './provider'; export * from './logger'; export * from './getVersion'; diff --git a/src/common/logger.ts b/src/common/logger.ts index 52e4ed1..6a9dca6 100644 --- a/src/common/logger.ts +++ b/src/common/logger.ts @@ -2,6 +2,10 @@ import pino from 'pino'; const logger = pino({ name: 'ServerlessInsight', + level: ['ServerlessInsight', '*'].includes(process.env.DEBUG || '') ? 'debug' : 'info', + transport: { + target: 'pino-pretty', + }, }); export { logger }; diff --git a/src/common/printer.ts b/src/common/printer.ts deleted file mode 100644 index ee0413d..0000000 --- a/src/common/printer.ts +++ /dev/null @@ -1,9 +0,0 @@ -import chalk from 'chalk'; - -export const printer = { - success: (message: string) => - console.log(`${chalk.blue('ServerlessInsight')}: ${chalk.green(message)}`), - info: (message: string) => console.log(`${chalk.blue('ServerlessInsight')}: ${message}`), - error: (message: string) => - console.log(`${chalk.bgRed(chalk.black('ServerlessInsight'))}: ${chalk.red(message)}`), -}; diff --git a/src/common/rosClient.ts b/src/common/rosClient.ts index 4d50462..c996343 100644 --- a/src/common/rosClient.ts +++ b/src/common/rosClient.ts @@ -9,7 +9,7 @@ import ROS20190910, { } from '@alicloud/ros20190910'; import { Config } from '@alicloud/openapi-client'; import { ActionContext } from '../types'; -import { printer } from './printer'; +import { logger } from './logger'; const client = new ROS20190910( new Config({ @@ -98,15 +98,15 @@ export const rosStackDeploy = async ( if (stackInfo) { const { Status: stackStatus } = stackInfo; if (stackStatus?.indexOf('IN_PROGRESS') >= 0) { - printer.error(`fail to update stack, because stack status is ${stackStatus}`); + logger.error(`fail to update stack, because stack status is ${stackStatus}`); throw new Error(`fail to update stack, because stack status is ${stackStatus}`); } - printer.info(`Update stack: ${stackName} deploying... `); + logger.info(`Update stack: ${stackName} deploying... `); return await updateStack(stackInfo.stackId as string, templateBody, context); } else { // create stack - printer.info(`Create stack: ${stackName} deploying... `); + logger.info(`Create stack: ${stackName} deploying... `); return await createStack(stackName, templateBody, context); } }; diff --git a/src/stack/deploy.ts b/src/stack/deploy.ts index 5d9d04f..62eb09c 100644 --- a/src/stack/deploy.ts +++ b/src/stack/deploy.ts @@ -1,6 +1,6 @@ import * as ros from '@alicloud/ros-cdk-core'; import { ActionContext, ServerlessIac } from '../types'; -import { printer, rosStackDeploy } from '../common'; +import { logger, rosStackDeploy } from '../common'; import { IacStack } from './iacStack'; const generateStackTemplate = (stackName: string, iac: ServerlessIac, context: ActionContext) => { @@ -19,7 +19,7 @@ export const deployStack = async ( context: ActionContext, ) => { const { template } = generateStackTemplate(stackName, iac, context); - console.log('Generated ROS YAML:', JSON.stringify({ template })); + await rosStackDeploy(stackName, template, context); - printer.info(`Stack deployed! πŸŽ‰`); + logger.info(`Stack deployed! πŸŽ‰`); }; diff --git a/src/stack/iacSchema.ts b/src/stack/iacSchema.ts index a2a6267..cdbac42 100644 --- a/src/stack/iacSchema.ts +++ b/src/stack/iacSchema.ts @@ -1,5 +1,6 @@ import { RawServerlessIac } from '../types'; import Ajv, { ErrorObject } from 'ajv'; +import { logger } from '../common'; const ajv = new Ajv({ allowUnionTypes: true, strict: false, allErrors: true }); @@ -130,7 +131,10 @@ class IacSchemaErrors extends Error { export const validateYaml = (iacJson: RawServerlessIac) => { const validate = ajv.compile(schema); const valid = validate(iacJson); - if (!valid) throw new IacSchemaErrors(validate.errors as Array); + if (!valid) { + logger.debug(`Invalid yaml: ${JSON.stringify(validate.errors)}`); + throw new IacSchemaErrors(validate.errors as Array); + } return true; }; diff --git a/tests/fixtures/serverless-insignt.yml b/tests/fixtures/serverless-insignt.yml index ab0faa4..2e1e43b 100644 --- a/tests/fixtures/serverless-insignt.yml +++ b/tests/fixtures/serverless-insignt.yml @@ -9,6 +9,8 @@ vars: stages: dev: region: ${vars.region} + prod: + region: cn-shanghai service: insight-poc diff --git a/tests/validate.test.ts b/tests/stack/validate.test.ts similarity index 96% rename from tests/validate.test.ts rename to tests/stack/validate.test.ts index 4390723..aea0490 100644 --- a/tests/validate.test.ts +++ b/tests/stack/validate.test.ts @@ -1,8 +1,8 @@ import { readFileSync } from 'node:fs'; import * as path from 'node:path'; import { parse } from 'yaml'; -import { RawServerlessIac } from '../src/types'; -import { validateYaml } from '../src/stack'; +import { validateYaml } from '../../src/stack'; +import { RawServerlessIac } from '../../src/types'; const jsonIac = parse( readFileSync(path.resolve(__dirname, './fixtures/serverless-insignt.yml'), 'utf8'),