mirror of
https://github.com/hanzoai/scarf-js.git
synced 2026-09-24 17:02:30 +00:00
get git sha on install (#61)
* get git sha on install * we should only inject sha if the root package allows top level * only get the git sha from root when root = parent * fix up parentIsRoot to be safe against optional values * update runner matrix to supported node.js versions see https://github.com/nodejs/Release/tree/7b8e0ac1b97b4ce4057e64ae024995052408926a#release-schedule * update runner matrix to supported node.js versions see https://github.com/nodejs/Release/tree/7b8e0ac1b97b4ce4057e64ae024995052408926a#release-schedule
This commit is contained in:
@@ -9,7 +9,7 @@ jobs:
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [10.x, 12.x]
|
||||
node-version: [18.x, 20.x]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
@@ -30,7 +30,7 @@ jobs:
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [10.x, 12.x]
|
||||
node-version: [18.x, 20.x]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
@@ -51,7 +51,7 @@ jobs:
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [10.x, 12.x]
|
||||
node-version: [18.x, 20.x]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
@@ -74,8 +74,8 @@ function skipTraversal (rootPackage) {
|
||||
}
|
||||
|
||||
function parentIsRoot (dependencyToReport) {
|
||||
return dependencyToReport.parent.name === dependencyToReport.rootPackage.name &&
|
||||
dependencyToReport.parent.version === dependencyToReport.rootPackage.version
|
||||
return dependencyToReport?.parent?.name === dependencyToReport?.rootPackage?.name &&
|
||||
dependencyToReport?.parent?.version === dependencyToReport?.rootPackage?.version
|
||||
}
|
||||
|
||||
function isTopLevel (dependencyToReport) {
|
||||
@@ -193,6 +193,22 @@ function processDependencyTreeOutput (resolve, reject) {
|
||||
}
|
||||
}
|
||||
|
||||
function processGitRevParseOutput (resolve, reject) {
|
||||
return function (error, stdout, stderr) {
|
||||
if (error && !stdout) {
|
||||
return reject(new Error(`Scarf received an error from git rev-parse: ${error} | ${stderr}`))
|
||||
}
|
||||
|
||||
const output = String(stdout).trim()
|
||||
|
||||
if (output.length > 0) {
|
||||
return resolve(output)
|
||||
} else {
|
||||
return reject(new Error('Scarf did not receive usable output from git rev-parse'))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// packageJSONOverride: a test convenience to set a packageJSON explicitly.
|
||||
// Leave empty to use the actual root package.json.
|
||||
async function getDependencyInfo (packageJSONOverride) {
|
||||
@@ -226,6 +242,18 @@ async function getDependencyInfo (packageJSONOverride) {
|
||||
})
|
||||
}
|
||||
|
||||
async function getGitShaFromRootPath () {
|
||||
const promise = new Promise((resolve, reject) => {
|
||||
exec(`cd ${rootPath} && git rev-parse HEAD`, { timeout: execTimeout, maxBuffer: 1024 * 1024 * 1024 }, processGitRevParseOutput(resolve, reject))
|
||||
})
|
||||
try {
|
||||
return await promise
|
||||
} catch (e) {
|
||||
logIfVerbose(e)
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
async function reportPostInstall () {
|
||||
const scarfApiToken = process.env.SCARF_API_TOKEN
|
||||
|
||||
@@ -235,6 +263,12 @@ async function reportPostInstall () {
|
||||
return Promise.reject(new Error('No parent found, nothing to report'))
|
||||
}
|
||||
|
||||
if (parentIsRoot(dependencyInfo) && allowTopLevel(dependencyInfo.rootPackage)) {
|
||||
const gitSha = await getGitShaFromRootPath()
|
||||
logIfVerbose(`Injecting sha to parent: ${gitSha}`)
|
||||
dependencyInfo.parent.gitSha = gitSha
|
||||
}
|
||||
|
||||
const rootPackage = dependencyInfo.rootPackage
|
||||
|
||||
if (!userHasOptedIn(rootPackage) && isYarn()) {
|
||||
@@ -512,8 +546,10 @@ module.exports = {
|
||||
tmpFileName,
|
||||
dirName,
|
||||
processDependencyTreeOutput,
|
||||
processGitRevParseOutput,
|
||||
npmExecPath,
|
||||
getDependencyInfo,
|
||||
getGitShaFromRootPath,
|
||||
reportPostInstall,
|
||||
hashWithDefault,
|
||||
findScarfInFullDependencyTree
|
||||
|
||||
@@ -147,6 +147,16 @@ describe('Reporting tests', () => {
|
||||
return report.processDependencyTreeOutput(resolve, reject)(null, JSON.stringify(parsedLsOutput), null)
|
||||
})).rejects.toEqual(new Error('The package depending on Scarf is the root package being installed, but Scarf is not configured to run in this case. To enable it, set `scarfSettings.allowTopLevel = true` in your package.json'))
|
||||
})
|
||||
|
||||
test('Can parse example git rev-parse HEAD output', async () => {
|
||||
await expect(new Promise((resolve, reject) => {
|
||||
return report.processGitRevParseOutput(resolve, reject)(null, '9ace16b9e3833ad4e761a49f17fe607723d5bd5e\n', null)
|
||||
})).resolves.toEqual('9ace16b9e3833ad4e761a49f17fe607723d5bd5e')
|
||||
})
|
||||
|
||||
test('getGitShaFromRootPath resolves in test run of cloned repository', async () => {
|
||||
await expect(report.getGitShaFromRootPath()).resolves.toBeTruthy()
|
||||
})
|
||||
})
|
||||
|
||||
function dependencyTreeScarfEnabled () {
|
||||
|
||||
Reference in New Issue
Block a user