Skip to content

Commit

Permalink
CLI: write into a log file if automigrations fail
Browse files Browse the repository at this point in the history
- the errors will get supressed in normal output but shown in the log file
  • Loading branch information
yannbf committed Dec 16, 2022
1 parent 5ec24bd commit 54c12bd
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 3 deletions.
1 change: 1 addition & 0 deletions code/lib/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"semver": "^7.3.7",
"shelljs": "^0.8.5",
"strip-json-comments": "^3.0.1",
"tempy": "^1.0.1",
"ts-dedent": "^2.0.0",
"update-notifier": "^6.0.2",
"util-deprecate": "^1.0.2"
Expand Down
12 changes: 12 additions & 0 deletions code/lib/cli/src/automigrate/helpers/cleanLog.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { EOL } from 'os';

export const cleanLog = (str: string) =>
str
// remove chalk ANSI colors
// eslint-disable-next-line no-control-regex
.replace(/\u001b\[.*?m/g, '')
// fix boxen output
.replace(/╮│/g, '╮\n│')
.replace(/││/g, '│\n│')
.replace(/│╰/g, '│\n╰')
.replace(/⚠️ {2}failed to check/g, `${EOL}⚠️ failed to check`);
58 changes: 55 additions & 3 deletions code/lib/cli/src/automigrate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,42 @@
import prompts from 'prompts';
import chalk from 'chalk';
import boxen from 'boxen';
import { createWriteStream, move, remove } from 'fs-extra';
import tempy from 'tempy';
import dedent from 'ts-dedent';

import { join } from 'path';
import { JsPackageManagerFactory, type PackageManagerName } from '../js-package-manager';

import type { Fix } from './fixes';
import { fixes } from './fixes';
import { cleanLog } from './helpers/cleanLog';

const logger = console;
const LOG_FILE_NAME = 'migration-storybook.log';
const LOG_FILE_PATH = join(process.cwd(), LOG_FILE_NAME);
let TEMP_LOG_FILE_PATH = '';

const originalStdOutWrite = process.stdout.write.bind(process.stdout);
const originalStdErrWrite = process.stderr.write.bind(process.stdout);

const augmentLogsToFile = () => {
TEMP_LOG_FILE_PATH = tempy.file({ name: LOG_FILE_NAME });
const logStream = createWriteStream(TEMP_LOG_FILE_PATH);

process.stdout.write = (d: string) => {
originalStdOutWrite(d);
return logStream.write(cleanLog(d));
};
process.stderr.write = (d: string) => {
return logStream.write(cleanLog(d));
};
};

const cleanup = () => {
process.stdout.write = originalStdOutWrite;
process.stderr.write = originalStdErrWrite;
};

type FixId = string;

Expand Down Expand Up @@ -38,6 +67,7 @@ type FixSummary = {
};

export const automigrate = async ({ fixId, dryRun, yes, useNpm, force }: FixOptions = {}) => {
augmentLogsToFile();
const packageManager = JsPackageManagerFactory.getPackageManager({ useNpm, force });
const filtered = fixId ? fixes.filter((f) => f.id === fixId) : fixes;

Expand All @@ -53,6 +83,7 @@ export const automigrate = async ({ fixId, dryRun, yes, useNpm, force }: FixOpti
result = await f.check({ packageManager });
} catch (error) {
logger.info(`⚠️ failed to check fix ${chalk.bold(f.id)}`);
logger.error(`\n${error.stack}`);
fixSummary.failed[f.id] = error.message;
fixResults[f.id] = FixStatus.CHECK_FAILED;
}
Expand All @@ -77,6 +108,10 @@ export const automigrate = async ({ fixId, dryRun, yes, useNpm, force }: FixOpti
runAnswer = { fix: false };
} else if (yes) {
runAnswer = { fix: true };
if (f.promptOnly) {
fixResults[f.id] = FixStatus.MANUAL_SUCCEEDED;
fixSummary.manual.push(f.id);
}
} else if (f.promptOnly) {
fixResults[f.id] = FixStatus.MANUAL_SUCCEEDED;
fixSummary.manual.push(f.id);
Expand Down Expand Up @@ -148,14 +183,31 @@ export const automigrate = async ({ fixId, dryRun, yes, useNpm, force }: FixOpti
}
}

const hasFailures = Object.values(fixResults).some(
(r) => r === FixStatus.FAILED || r === FixStatus.CHECK_FAILED
);

// if migration failed, display a log file in the users cwd
if (hasFailures) {
await move(TEMP_LOG_FILE_PATH, join(process.cwd(), LOG_FILE_NAME), { overwrite: true });
} else {
await remove(TEMP_LOG_FILE_PATH);
}

logger.info();
logger.info(getMigrationSummary(fixResults, fixSummary));
logger.info(getMigrationSummary(fixResults, fixSummary, LOG_FILE_PATH));
logger.info();

cleanup();

return fixResults;
};

function getMigrationSummary(fixResults: Record<string, FixStatus>, fixSummary: FixSummary) {
function getMigrationSummary(
fixResults: Record<string, FixStatus>,
fixSummary: FixSummary,
logFile?: string
) {
const hasNoFixes = Object.values(fixResults).every((r) => r === FixStatus.UNNECESSARY);
const hasFailures = Object.values(fixResults).some(
(r) => r === FixStatus.FAILED || r === FixStatus.CHECK_FAILED
Expand Down Expand Up @@ -185,7 +237,7 @@ function getMigrationSummary(fixResults: Record<string, FixStatus>, fixSummary:
},
''
)}
`
\nYou can find the full logs in ${chalk.cyan(logFile)}\n`
: '';

const manualFixesMessage =
Expand Down
44 changes: 44 additions & 0 deletions code/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6190,6 +6190,7 @@ __metadata:
semver: ^7.3.7
shelljs: ^0.8.5
strip-json-comments: ^3.1.1
tempy: ^1.0.1
ts-dedent: ^2.0.0
typescript: ~4.9.3
update-notifier: ^6.0.2
Expand Down Expand Up @@ -13662,6 +13663,13 @@ __metadata:
languageName: node
linkType: hard

"crypto-random-string@npm:^2.0.0":
version: 2.0.0
resolution: "crypto-random-string@npm:2.0.0"
checksum: 288589b2484fe787f9e146f56c4be90b940018f17af1b152e4dde12309042ff5a2bf69e949aab8b8ac253948381529cc6f3e5a2427b73643a71ff177fa122b37
languageName: node
linkType: hard

"crypto-random-string@npm:^4.0.0":
version: 4.0.0
resolution: "crypto-random-string@npm:4.0.0"
Expand Down Expand Up @@ -31493,6 +31501,13 @@ __metadata:
languageName: node
linkType: hard

"temp-dir@npm:^2.0.0":
version: 2.0.0
resolution: "temp-dir@npm:2.0.0"
checksum: b1df969e3f3f7903f3426861887ed76ba3b495f63f6d0c8e1ce22588679d9384d336df6064210fda14e640ed422e2a17d5c40d901f60e161c99482d723f4d309
languageName: node
linkType: hard

"temp-write@npm:^3.4.0":
version: 3.4.0
resolution: "temp-write@npm:3.4.0"
Expand All @@ -31516,6 +31531,19 @@ __metadata:
languageName: node
linkType: hard

"tempy@npm:^1.0.1":
version: 1.0.1
resolution: "tempy@npm:1.0.1"
dependencies:
del: ^6.0.0
is-stream: ^2.0.0
temp-dir: ^2.0.0
type-fest: ^0.16.0
unique-string: ^2.0.0
checksum: 864a1cf1b5536dc21e84ae45dbbc3ba4dd2c7ec1674d895f99c349cf209df959a53d797ca38d0b2cf69c7684d565fde5cfc67faaa63b7208ffb21d454b957472
languageName: node
linkType: hard

"terminal-link@npm:^2.0.0":
version: 2.1.1
resolution: "terminal-link@npm:2.1.1"
Expand Down Expand Up @@ -32314,6 +32342,13 @@ __metadata:
languageName: node
linkType: hard

"type-fest@npm:^0.16.0":
version: 0.16.0
resolution: "type-fest@npm:0.16.0"
checksum: 6b4d846534e7bcb49a6160b068ffaed2b62570d989d909ac3f29df5ef1e993859f890a4242eebe023c9e923f96adbcb3b3e88a198c35a1ee9a731e147a6839c3
languageName: node
linkType: hard

"type-fest@npm:^0.18.0":
version: 0.18.1
resolution: "type-fest@npm:0.18.1"
Expand Down Expand Up @@ -32711,6 +32746,15 @@ __metadata:
languageName: node
linkType: hard

"unique-string@npm:^2.0.0":
version: 2.0.0
resolution: "unique-string@npm:2.0.0"
dependencies:
crypto-random-string: ^2.0.0
checksum: 11820db0a4ba069d174bedfa96c588fc2c96b083066fafa186851e563951d0de78181ac79c744c1ed28b51f9d82ac5b8196ff3e4560d0178046ef455d8c2244b
languageName: node
linkType: hard

"unique-string@npm:^3.0.0":
version: 3.0.0
resolution: "unique-string@npm:3.0.0"
Expand Down

0 comments on commit 54c12bd

Please sign in to comment.