Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
joe-yeager committed Sep 11, 2024
1 parent 6bb329c commit 9267f1b
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module.exports = {
es6: true,
},
parserOptions: {
ecmaVersion: 2018,
ecmaVersion: 2021,
},
rules: {
'no-console': 'off',
Expand Down
109 changes: 72 additions & 37 deletions packages/cli/commands/doctor.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,72 @@
const pkg = require('../package.json');
const util = require('util');
const { getProjectConfig } = require('../lib/projects');
const { getAccountId } = require('../lib/commonOpts');
const { getAccessToken } = require('@hubspot/local-dev-lib/personalAccessKey');
const { trackCommandUsage } = require('../lib/usageTracking');
const { getAccountConfig } = require('@hubspot/local-dev-lib/config');
const execSync = require('child_process').execSync;
const { walk } = require('@hubspot/local-dev-lib/fs');
const path = require('path');
const { logger } = require('@hubspot/local-dev-lib/logger');
const SpinniesManager = require('../lib/ui/SpinniesManager');
const fs = require('fs');
const DoctorManager = require('../lib/DoctorManager');

// const i18nKey = 'commands.doctor';
exports.command = 'doctor';
exports.describe = 'The doctor is in';

function getNpmVersion() {
exports.handler = async ({ file }) => {
const doctorManager = new DoctorManager();
try {
return execSync('npm --version')
.toString()
.trim();
trackCommandUsage('doctor', null, doctorManager.accountId);
} catch (e) {
return null;
logger.debug(e);
}
}

function shouldIncludeFile(file) {
SpinniesManager.init();
SpinniesManager.add('loadingProjectDetails', {
text: 'Loading project details',
});

let projectConfig;
let projectDetails;
try {
const ignoredDirs = ['node_modules'];
for (const ignoredDir of ignoredDirs) {
if (path.dirname(file).includes(path.join(path.sep, ignoredDir))) {
return false;
}
}
projectConfig = await getProjectConfig();
projectDetails = await doctorManager.fetchProjectDetails(
doctorManager.accountId,
projectConfig
);
} catch (e) {
logger.debug(e);
}
return true;
}

exports.handler = async () => {
const accountId = getAccountId();
const projectConfig = await getProjectConfig();
SpinniesManager.succeed('loadingProjectDetails', {
text: 'Project details loaded',
});

const { env, authType, personalAccessKey, accountType } = getAccountConfig(
accountId
doctorManager.accountId
);

let accessToken = {};
try {
trackCommandUsage('doctor', null, accountId);
accessToken = await getAccessToken(
personalAccessKey,
env,
doctorManager.accountId
);
} catch (e) {
logger.debug(e);
}

let accessToken = {};
let files = [];
try {
accessToken = await getAccessToken(personalAccessKey, env, accountId);
files = (await walk(projectConfig.projectDir))
.filter(doctorManager.shouldIncludeFile)
.map(filename => path.relative(projectConfig.projectDir, filename));
} catch (e) {
logger.debug(e);
}

const files = (await walk(projectConfig.projectDir))
.filter(shouldIncludeFile)
.map(filename => path.relative(projectConfig.projectDir, filename));

const {
platform,
arch,
Expand All @@ -76,16 +81,22 @@ exports.handler = async () => {
versions: {
'@hubspot/cli': pkg.version,
node,
npm: getNpmVersion(),
npm: doctorManager.getNpmVersion(),
},
projectConfig,
account: {
accountId,
accountId: doctorManager.accountId,
accountType,
authType,
name: accessToken && accessToken.hubName,
scopeGroups: accessToken && accessToken.scopeGroups,
enabledFeatures: accessToken && accessToken.enabledFeatures,
name: accessToken.hubName,
scopeGroups: accessToken?.scopeGroups,
enabledFeatures: accessToken?.enabledFeatures,
},
project: {
config:
projectConfig && projectConfig.projectConfig
? projectConfig
: undefined,
details: projectDetails,
},
packageFiles: files.filter(file => {
return path.parse(file).base === 'package.json';
Expand All @@ -98,7 +109,31 @@ exports.handler = async () => {
files,
};

console.log(util.inspect(output, false, 5, true));
const stringifiedOutput = JSON.stringify(output, null, 4);

if (file) {
try {
SpinniesManager.add('writingToFile', {
text: `Writing output to ${file}`,
});
fs.writeFileSync(file, JSON.stringify(output, null, 4));
SpinniesManager.succeed('writingToFile', {
text: `Output written to ${file}`,
});
} catch (e) {
SpinniesManager.fail('writingToFile', {
text: 'Unable to write to file',
});
}
} else {
console.log(stringifiedOutput);
}
};

exports.builder = yargs => yargs;
exports.builder = yargs =>
yargs.option({
file: {
describe: 'Where to write the output',
type: 'string',
},
});
52 changes: 52 additions & 0 deletions packages/cli/lib/DoctorManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const { execSync } = require('child_process');
const path = require('path');
const { logger } = require('@hubspot/local-dev-lib/logger');
const { fetchProject } = require('@hubspot/local-dev-lib/api/projects');
const { getAccountId } = require('./commonOpts');

class DoctorManager {
constructor() {
this.accountId = getAccountId();
}

getNpmVersion() {
try {
return execSync('npm --version')
.toString()
.trim();
} catch (e) {
return null;
}
}

shouldIncludeFile(file) {
try {
const ignoredDirs = ['node_modules'];
for (const ignoredDir of ignoredDirs) {
if (path.dirname(file).includes(path.join(path.sep, ignoredDir))) {
return false;
}
}
} catch (e) {
logger.debug(e);
}
return true;
}
async fetchProjectDetails(accountId, projectConfig) {
let projectDetails;
try {
projectDetails = await fetchProject(
accountId,
projectConfig.projectConfig.name
);
delete projectDetails.deployedBuild;
delete projectDetails.latestBuild;
delete projectDetails.portalId;
} catch (e) {
logger.debug(e);
}
return projectDetails;
}
}

module.exports = DoctorManager;

0 comments on commit 9267f1b

Please sign in to comment.