-
Notifications
You must be signed in to change notification settings - Fork 687
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
[feature]: Debugging Reporter #2910
Merged
Merged
Changes from 3 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
ba9e55a
Add script command. Remove sample backend script
sirugh c0a6e56
Initial
sirugh abd3484
More
sirugh eb1a6cf
cleanup
sirugh 3663c4b
remove unused const
sirugh ed501b7
Update bug report template
sirugh 0a4bfa4
Update bug report template
sirugh 4072227
Remove magento version check since not all backends will expose it
sirugh 750d7b4
process.version() -> process.version
revanth0212 7c73482
Parsing package-lock.json directly to get version details.
revanth0212 cc77b05
Using @yarnpkg/lockfile to generate dep-version list.
revanth0212 e9a314d
Added try catch around os version check.
revanth0212 c858cf5
Merge branch 'develop' into rugh/hackathon-2020-build-report
revanth0212 65d300e
Removed unnecessary code.
revanth0212 a44a00c
Merge branch 'rugh/hackathon-2020-build-report' of https://github.com…
revanth0212 045811b
Merge branch 'develop' into rugh/hackathon-2020-build-report
sirugh 6a97577
Version updates.
revanth0212 f8559fb
Added supprort for multiple versions.
revanth0212 789c124
Minor logger change.
revanth0212 7c7ae86
Added mock fixtures.
revanth0212 abec157
Added initial test work.
revanth0212 ae7a30a
Added yarn.lock tests.
revanth0212 535cfd6
Added npm related tests.
revanth0212 4ddb8f1
Added other tests.
revanth0212 4ea451c
Added support for @adobe packages scanning.
revanth0212 a061195
Run tests if env is valid.
revanth0212 d527baa
Merge branch 'develop' into rugh/hackathon-2020-build-report
dpatil-magento File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
210 changes: 210 additions & 0 deletions
210
packages/pwa-buildpack/lib/cli/generate-build-report.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
const { existsSync } = require('fs'); | ||
const os = require('os'); | ||
const path = require('path'); | ||
const { spawnSync } = require('child_process'); | ||
const https = require('https'); | ||
const fetch = require('node-fetch'); | ||
const agent = new https.Agent({ | ||
rejectUnauthorized: false | ||
}); | ||
|
||
const fetchWithAgent = async url => { | ||
return await fetch(url, { agent }); | ||
}; | ||
|
||
const prettyLogger = require('../util/pretty-logger'); | ||
const { loadEnvironment } = require('../Utilities'); | ||
|
||
const { uniqBy } = require('lodash'); | ||
const { sampleBackends: defaultSampleBackends } = require('./create-project'); | ||
|
||
const removeDuplicateBackends = backendEnvironments => | ||
uniqBy(backendEnvironments, 'url'); | ||
|
||
const fetchSampleBackendUrls = async () => { | ||
try { | ||
const res = await fetch( | ||
'https://fvp0esmt8f.execute-api.us-east-1.amazonaws.com/default/getSampleBackends' | ||
); | ||
const { sampleBackends } = await res.json(); | ||
|
||
return removeDuplicateBackends([ | ||
...sampleBackends.environments, | ||
...defaultSampleBackends.environments | ||
]).map(({ url }) => url); | ||
} catch { | ||
return defaultSampleBackends.environments.map(({ url }) => url); | ||
} | ||
}; | ||
|
||
const cwd = process.cwd(); | ||
|
||
function inspectDependencies(package) { | ||
prettyLogger.info('Inspecting Dependencies'); | ||
|
||
// Inspect all '@magento/...' dependencies. | ||
const magentoDependencies = [ | ||
...Object.keys(package.dependencies), | ||
...Object.keys(package.devDependencies) | ||
].filter(dependency => dependency.startsWith('@magento/')); | ||
|
||
let dependencies; | ||
if (existsSync(path.resolve(cwd, 'yarn.lock'))) { | ||
// using yarn | ||
dependencies = getYarnDependencies(magentoDependencies); | ||
prettyLogger.log( | ||
`Found ${dependencies.size} @magento dependencies in yarn.lock` | ||
); | ||
} else if (existsSync(path.resolve(cwd, 'package-lock.json'))) { | ||
// using npm | ||
dependencies = getNpmDependencies(magentoDependencies); | ||
prettyLogger.log( | ||
`Found ${ | ||
dependencies.size | ||
} @magento dependencies in package-lock.json` | ||
); | ||
} else { | ||
throw new Error( | ||
'Dependencies have not been installed, please run `yarn install` or npm install` then generate the build report again.' | ||
); | ||
} | ||
|
||
function logMapElements(value, key) { | ||
prettyLogger.log(`${key} @ ${value}`); | ||
} | ||
|
||
dependencies.forEach(logMapElements); | ||
} | ||
|
||
function getYarnDependencies(dependencies) { | ||
const dependencyMap = new Map(); | ||
|
||
dependencies.map(packageName => { | ||
const whyBuffer = spawnSync('yarn', ['why', packageName]); | ||
const grepBuffer = spawnSync('grep', ['Found'], { | ||
input: whyBuffer.stdout | ||
}); | ||
|
||
// [ 'info \r=> Found "@magento/pwa-buildpack@7.0.0"', '' ] | ||
const outputArray = grepBuffer.stdout.toString().split('\n'); | ||
|
||
// [ '7.0.0' ] | ||
const parsedOutputArray = outputArray | ||
.filter(output => output.length > 0) | ||
.map(output => output.split('@')[2].replace('"', '')); | ||
|
||
dependencyMap.set(packageName, parsedOutputArray.toString()); | ||
}); | ||
|
||
return dependencyMap; | ||
} | ||
|
||
function getNpmDependencies(dependencies) { | ||
const dependencyMap = new Map(); | ||
|
||
dependencies.map(packageName => { | ||
const listbuffer = spawnSync('npm', ['list', packageName]); | ||
const grepBuffer = spawnSync('grep', ['└──'], { | ||
input: listbuffer.stdout | ||
}); | ||
|
||
// TODO: handle multiple versions installed when not dupe. ie "npm ls core-js" | ||
const [, , version] = grepBuffer.stdout | ||
.toString() | ||
.split('\n') // ["│ └── @magento/upward-js@5.0.0 deduped", "└── @magento/upward-js@5.0.0"] | ||
.filter(text => !text.includes('deduped'))[0] // ["└── @magento/upward-js@5.0.0"] | ||
.split('@'); // [ '└── ', 'magento/upward-js', '5.0.0' ] | ||
|
||
dependencyMap.set(packageName, version); | ||
}); | ||
|
||
return dependencyMap; | ||
} | ||
|
||
async function inspectBackend() { | ||
prettyLogger.info('Inspecting Magento Backend'); | ||
const [projectConfig, sampleBackends] = await Promise.all([ | ||
loadEnvironment( | ||
// Load .env from root | ||
path.resolve(cwd) | ||
), | ||
fetchSampleBackendUrls() | ||
]); | ||
|
||
if (projectConfig.error) { | ||
throw projectConfig.error; | ||
} | ||
|
||
const { | ||
env: { MAGENTO_BACKEND_URL } | ||
} = projectConfig; | ||
|
||
if (sampleBackends.includes(MAGENTO_BACKEND_URL)) { | ||
prettyLogger.log('Using sample backend: ', MAGENTO_BACKEND_URL); | ||
} else { | ||
prettyLogger.log('Not using sample backend.'); | ||
} | ||
|
||
// Upcheck | ||
try { | ||
const res = await fetchWithAgent(MAGENTO_BACKEND_URL); | ||
prettyLogger.log('Backend is UP!'); | ||
} catch (e) { | ||
prettyLogger.log('Backend is DOWN!'); | ||
prettyLogger.error('Reason:', e); | ||
} | ||
|
||
// Backend Version | ||
try { | ||
const res = await fetchWithAgent( | ||
`${MAGENTO_BACKEND_URL}/magento_version` | ||
); | ||
prettyLogger.log('Magento Version:', await res.text()); | ||
} catch (e) { | ||
prettyLogger.warn( | ||
'Unable to determine Magento version - ensure that the Magento_Version route is enabled when generating this report.' | ||
); | ||
prettyLogger.error('Reason:', e); | ||
} | ||
// https://venia-cicd-lrov2hi-mfwmkrjfqvbjk.us-4.magentosite.cloud/magento_version | ||
} | ||
|
||
function inspectBuildEnv() { | ||
prettyLogger.info('Inspecting System'); | ||
prettyLogger.log('OS:', os.version()); | ||
|
||
prettyLogger.log('Node Version:', process.version); | ||
const versionBuffer = spawnSync('npm', ['-v']); | ||
prettyLogger.log('NPM Version:', versionBuffer.stdout.toString()); | ||
} | ||
/** | ||
* main | ||
*/ | ||
async function buildpackCli() { | ||
const package = require(path.resolve(cwd, 'package.json')); | ||
prettyLogger.info( | ||
`Generating build report for ${package.name}@${ | ||
package.version | ||
}. This may take a moment.` | ||
); | ||
prettyLogger.log('\n'); | ||
|
||
inspectDependencies(package); | ||
|
||
prettyLogger.log('\n'); | ||
|
||
await inspectBackend(); | ||
|
||
prettyLogger.log('\n'); | ||
|
||
inspectBuildEnv(); | ||
|
||
prettyLogger.log('\n'); | ||
} | ||
|
||
module.exports.command = 'generate-build-report'; | ||
|
||
module.exports.describe = | ||
'Generates a report of general build information that can be used when debugging an issue.'; | ||
|
||
module.exports.handler = buildpackCli; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sirugh while writing tests for this file, it hit me, we should be capturing all
adobe
andmagento
packages right, not justmagento
. What do you think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depends on what you really want to glean from this information. I think we have at least one adobe dependency so might as well throw that in :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We actually use two:
@adobe/adobe-client-data-layer
@adobe/apollo-link-mutation-queue