-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: replace libnpm with npm cli (#139)
* refactor: add npm version resolver * refactor: replace libnpm with npm cli execution * refactor: remove libnpm from project * chore: fix linting issues * chore: rebuild project
- Loading branch information
Showing
11 changed files
with
142 additions
and
1,424 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { exec } from '@actions/exec'; | ||
|
||
/** | ||
* Resolve a package with version range to an exact version. | ||
* This is useful to invalidate the cache _and_ using dist-tags or version ranges. | ||
* It executes `npm info` and parses the latest manifest. | ||
*/ | ||
export async function resolveVersion(name: string, range: string): Promise<string> { | ||
let stdout = ''; | ||
|
||
try { | ||
await exec('npm', ['info', `${name}@${range}`, 'version', '--json'], { | ||
silent: true, | ||
listeners: { | ||
stdout(data) { | ||
stdout += data.toString(); | ||
}, | ||
}, | ||
}); | ||
} catch (error) { | ||
throw new Error(`Could not resolve version "${range}" of "${name}", reason:\n${error.message || error}`); | ||
} | ||
|
||
// thanks npm, for returning a "x.x.x" json value... | ||
if (stdout.startsWith('"')) { | ||
stdout = `[${stdout}]`; | ||
} | ||
|
||
return JSON.parse(stdout).at(-1); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { valid as validVersion } from 'semver'; | ||
import { resolveVersion } from '../src/packager'; | ||
|
||
describe(resolveVersion, () => { | ||
it('resolves expo-cli@^2.0.0 to 2.21.2', async () => { | ||
await expect(resolveVersion('expo-cli', '^2.0.0')).resolves.toBe('2.21.2'); | ||
}); | ||
|
||
it('resolves expo-cli@~3.15.0 to 3.15.5', async () => { | ||
await expect(resolveVersion('expo-cli', '~3.15.0')).resolves.toBe('3.15.5'); | ||
}); | ||
|
||
it('resolves eas-cli@~0.33.0 to 0.33.1', async () => { | ||
await expect(resolveVersion('eas-cli', '~0.33.0')).resolves.toBe('0.33.1'); | ||
}); | ||
|
||
it('resolves expo-cli@latest to a valid version', async () => { | ||
const version = await resolveVersion('expo-cli', 'latest'); | ||
expect(validVersion(version)).not.toBeNull(); | ||
}); | ||
|
||
it('rejects donotpublishthispackageoryouwillbefired with proper error', async () => { | ||
await expect(resolveVersion('donotpublishthispackageoryouwillbefired', 'latest')).rejects.toThrow( | ||
'Could not resolve version "latest" of "donotpublishthispackageoryouwillbefired"' | ||
); | ||
}); | ||
}); |
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
Oops, something went wrong.