-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rn): One line RN SDK install command (#243)
- Loading branch information
1 parent
6224447
commit 71f4f6a
Showing
9 changed files
with
342 additions
and
165 deletions.
There are no files selected for viewing
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,61 @@ | ||
import * as _ from 'lodash'; | ||
import { satisfies, subset, valid, validRange } from 'semver'; | ||
|
||
import { green, red } from './Logging'; | ||
|
||
export function checkPackageVersion( | ||
appPackage: unknown, | ||
packageName: string, | ||
acceptableVersions: string, | ||
canBeLatest: boolean, | ||
): boolean { | ||
const depsVersion = _.get(appPackage, ['dependencies', packageName]); | ||
const devDepsVersion = _.get(appPackage, ['devDependencies', packageName]); | ||
|
||
if (!depsVersion && !devDepsVersion) { | ||
red(`✗ ${packageName} isn't in your dependencies.`); | ||
red(' Please install it with yarn/npm.'); | ||
return false; | ||
} else if ( | ||
!fulfillsVersionRange(depsVersion, acceptableVersions, canBeLatest) && | ||
!fulfillsVersionRange(devDepsVersion, acceptableVersions, canBeLatest) | ||
) { | ||
red( | ||
`✗ Your \`package.json\` specifies a version of \`${packageName}\` outside of the compatible version range ${acceptableVersions}.\n`, | ||
); | ||
return false; | ||
} else { | ||
green( | ||
`✓ A compatible version of \`${packageName}\` is specified in \`package.json\`.`, | ||
); | ||
return true; | ||
} | ||
} | ||
|
||
function fulfillsVersionRange( | ||
version: string, | ||
acceptableVersions: string, | ||
canBeLatest: boolean, | ||
): boolean { | ||
if (version === 'latest') { | ||
return canBeLatest; | ||
} | ||
|
||
let cleanedUserVersion, isRange; | ||
|
||
if (valid(version)) { | ||
cleanedUserVersion = valid(version); | ||
isRange = false; | ||
} else if (validRange(version)) { | ||
cleanedUserVersion = validRange(version); | ||
isRange = true; | ||
} | ||
|
||
return ( | ||
// If the given version is a bogus format, this will still be undefined and we'll automatically reject it | ||
!!cleanedUserVersion && | ||
(isRange | ||
? subset(cleanedUserVersion, acceptableVersions) | ||
: satisfies(cleanedUserVersion, acceptableVersions)) | ||
); | ||
} |
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,64 @@ | ||
/* eslint-disable @typescript-eslint/typedef */ | ||
import { exec } from 'child_process'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import { promisify } from 'util'; | ||
|
||
import { green } from './Logging'; | ||
|
||
export function getPackageMangerChoice(): PackageManager | null { | ||
if (fs.existsSync(path.join(process.cwd(), Yarn.LOCK_FILE))) { | ||
return new Yarn(); | ||
} | ||
if (fs.existsSync(path.join(process.cwd(), Pnpm.LOCK_FILE))) { | ||
return new Pnpm(); | ||
} | ||
if (fs.existsSync(path.join(process.cwd(), Npm.LOCK_FILE))) { | ||
return new Npm(); | ||
} | ||
return null; | ||
} | ||
|
||
export interface PackageManager { | ||
installPackage(packageName: string): Promise<void>; | ||
} | ||
|
||
export class Npm implements PackageManager { | ||
public static LOCK_FILE = 'package-lock.json'; | ||
public static LABEL = 'npm'; | ||
public static INSTALL_COMMAND = 'npm install'; | ||
|
||
public async installPackage(packageName: string): Promise<void> { | ||
await installPackage(Npm.INSTALL_COMMAND, packageName, Npm.LABEL); | ||
} | ||
} | ||
|
||
export class Yarn implements PackageManager { | ||
public static LOCK_FILE = 'yarn.lock'; | ||
public static LABEL = 'yarn'; | ||
public static INSTALL_COMMAND = 'yarn add'; | ||
|
||
public async installPackage(packageName: string): Promise<void> { | ||
await installPackage(Yarn.INSTALL_COMMAND, packageName, Yarn.LABEL); | ||
} | ||
} | ||
|
||
export class Pnpm implements PackageManager { | ||
public static LOCK_FILE = 'pnpm-lock.yaml'; | ||
public static LABEL = 'pnpm'; | ||
public static INSTALL_COMMAND = 'pnpm add'; | ||
|
||
public async installPackage(packageName: string): Promise<void> { | ||
await installPackage(Pnpm.INSTALL_COMMAND, packageName, Pnpm.LABEL); | ||
} | ||
} | ||
|
||
async function installPackage( | ||
command: string, | ||
packageName: string, | ||
label: string, | ||
): Promise<void> { | ||
await promisify(exec)(`${command} ${packageName}`); | ||
green(`✓ Added \`${packageName}\` using \`${label}\`.`); | ||
return; | ||
} |
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.