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

feat(doctor): introduce "--archive" to archive the workspace as well #7324

Merged
merged 4 commits into from
Apr 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
"stringify-package": "1.0.1",
"stylus-lookup": "3.0.2",
"table": "5.4.6",
"tar-fs": "2.1.1",
"tar-stream": "2.2.0",
"cli-table": "0.3.6",
"uid-number": "0.0.6",
Expand Down
76 changes: 53 additions & 23 deletions src/api/consumer/lib/doctor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import fs from 'fs-extra';
import os from 'os';
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
import Stream from 'stream';
import path from 'path';
import tar from 'tar-stream';
import tarFS from 'tar-fs';
import { getHarmonyVersion } from '../../../bootstrap';

import { CFG_USER_EMAIL_KEY, CFG_USER_NAME_KEY, DEBUG_LOG } from '../../../constants';
Expand Down Expand Up @@ -42,23 +44,31 @@ export type DoctorRunOneResult = {

let runningTimeStamp;

export default async function runAll({ filePath }: { filePath?: string }): Promise<DoctorRunAllResults> {
export default async function runAll({
filePath,
archiveWorkspace,
}: {
filePath?: string;
archiveWorkspace?: boolean;
}): Promise<DoctorRunAllResults> {
registerCoreAndExtensionsDiagnoses();
runningTimeStamp = _getTimeStamp();
const doctorRegistrar = DoctorRegistrar.getInstance();
const examineP = doctorRegistrar.diagnoses.map((diagnosis) => diagnosis.examine());
const examineResults = await Promise.all(examineP);
const envMeta = await _getEnvMeta();
const savedFilePath = await _saveExamineResultsToFile(examineResults, envMeta, filePath);
const savedFilePath = await _saveExamineResultsToFile(examineResults, envMeta, filePath, archiveWorkspace);
return { examineResults, savedFilePath, metaData: envMeta };
}

export async function runOne({
diagnosisName,
filePath,
archiveWorkspace = false,
}: {
diagnosisName: string;
filePath?: string;
archiveWorkspace?: boolean;
}): Promise<DoctorRunOneResult> {
if (!diagnosisName) {
throw new MissingDiagnosisName();
Expand All @@ -72,7 +82,7 @@ export async function runOne({
}
const examineResult = await diagnosis.examine();
const envMeta = await _getEnvMeta();
const savedFilePath = await _saveExamineResultsToFile([examineResult], envMeta, filePath);
const savedFilePath = await _saveExamineResultsToFile([examineResult], envMeta, filePath, archiveWorkspace);
return { examineResult, savedFilePath, metaData: envMeta };
}

Expand All @@ -85,13 +95,14 @@ export async function listDiagnoses(): Promise<Diagnosis[]> {
async function _saveExamineResultsToFile(
examineResults: ExamineResult[],
envMeta: DoctorMetaData,
filePath: string | null | undefined
filePath: string | null | undefined,
archiveWorkspace = false
): Promise<string | null | undefined> {
if (!filePath) {
return Promise.resolve(undefined);
}
const finalFilePath = _calculateFinalFileName(filePath);
const packStream = await _generateExamineResultsTarFile(examineResults, envMeta);
const packStream = await _generateExamineResultsTarFile(examineResults, envMeta, archiveWorkspace, finalFilePath);

const yourTarball = fs.createWriteStream(finalFilePath);

Expand Down Expand Up @@ -135,35 +146,54 @@ function _getTimeStamp() {

async function _generateExamineResultsTarFile(
examineResults: ExamineResult[],
envMeta: DoctorMetaData
envMeta: DoctorMetaData,
archiveWorkspace = false,
tarFilePath: string
): Promise<Stream.Readable> {
const pack = tar.pack(); // pack is a streams2 stream
const debugLog = await _getDebugLogAsBuffer();
const consumerInfo = await _getConsumerInfo();
let bitmap;
if (consumerInfo && consumerInfo.path) {
bitmap = _getBitMap(consumerInfo.path);
}
pack.entry({ name: 'env-meta.json' }, JSON.stringify(envMeta, null, 2));
pack.entry({ name: 'doc-results.json' }, JSON.stringify(examineResults, null, 2));
if (debugLog) {
pack.entry({ name: 'debug.log' }, debugLog);
}
if (bitmap) {
pack.entry({ name: '.bitmap' }, bitmap);
}
if (consumerInfo && consumerInfo.hasConsumerConfig) {
// TODO: support new config as well
const config = await WorkspaceConfig.loadIfExist(consumerInfo.path);
const legacyPlainConfig = config?._legacyPlainObject();
if (legacyPlainConfig) {
pack.entry({ name: 'config.json' }, JSON.stringify(legacyPlainConfig, null, 4));

const packExamineResults = async (pack) => {
pack.entry({ name: 'env-meta.json' }, JSON.stringify(envMeta, null, 2));
pack.entry({ name: 'doc-results.json' }, JSON.stringify(examineResults, null, 2));
if (debugLog) {
pack.entry({ name: 'debug.log' }, debugLog);
}
if (!archiveWorkspace && bitmap) {
pack.entry({ name: '.bitmap' }, bitmap);
}
if (consumerInfo && consumerInfo.hasConsumerConfig) {
// TODO: support new config as well
const config = await WorkspaceConfig.loadIfExist(consumerInfo.path);
const legacyPlainConfig = config?._legacyPlainObject();
if (legacyPlainConfig) {
pack.entry({ name: 'config.json' }, JSON.stringify(legacyPlainConfig, null, 4));
}
}

pack.finalize();

return pack;
};

if (!archiveWorkspace) {
const pack = tar.pack(); // pack is a streams2 stream
return packExamineResults(pack);
}

pack.finalize();
const myPack = tarFS.pack('.', {
ignore: (name) => {
return name.startsWith(`node_modules${path.sep}`) || name === tarFilePath;
},
finalize: false,
finish: packExamineResults,
});

return pack;
return myPack;
}

async function _getEnvMeta(): Promise<DoctorMetaData> {
Expand Down
12 changes: 9 additions & 3 deletions src/cli/commands/public-cmds/doctor-cmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export default class Doctor implements LegacyCommand {
['j', 'json', 'return diagnoses in json format'],
['', 'list', 'list all available diagnoses'],
['s', 'save [filePath]', 'save diagnoses to a file'],
['a', 'archive [filePath]', 'archive the workspace including diagnosis info'],
] as CommandOptions;
migration = false;

Expand All @@ -27,9 +28,11 @@ export default class Doctor implements LegacyCommand {
{
list = false,
save,
archive,
}: {
list?: boolean;
save?: string;
archive?: string;
}
): Promise<DoctorRunAllResults | Diagnosis[] | DoctorRunOneResult> {
if (list) {
Expand All @@ -38,13 +41,16 @@ export default class Doctor implements LegacyCommand {
let filePath = save;
// Happen when used --save without specify the location
// @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX!
if (save === true) {
if (save === true || archive === true) {
filePath = '.';
}
if (typeof archive === 'string') {
filePath = archive;
}
if (diagnosisName) {
return runOne({ diagnosisName, filePath });
return runOne({ diagnosisName, filePath, archiveWorkspace: Boolean(archive) });
}
return runAll({ filePath });
return runAll({ filePath, archiveWorkspace: Boolean(archive) });
}

report(res: DoctorRunAllResults | Diagnosis[], args: any, flags: Record<string, any>): string {
Expand Down