-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[updates] Change fingerprint diff to use versioned fingerprint CLI (#460
- Loading branch information
Showing
5 changed files
with
254 additions
and
15 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
packages/build-tools/src/utils/__tests__/diffFingerprintsAsync.test.ts
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,80 @@ | ||
import { vol } from 'memfs'; | ||
import fs from 'fs-extra'; | ||
|
||
import { diffFingerprintsAsync } from '../diffFingerprintsAsync'; | ||
import { isModernExpoFingerprintCLISupportedAsync } from '../expoFingerprintCli'; | ||
|
||
jest.mock('../expoFingerprintCli', () => ({ | ||
...jest.requireActual('../expoFingerprintCli'), | ||
expoFingerprintCommandAsync: jest.fn(), | ||
isModernExpoFingerprintCLISupportedAsync: jest.fn(), | ||
})); | ||
|
||
jest.mock('fs'); | ||
|
||
describe(diffFingerprintsAsync, () => { | ||
beforeEach(async () => { | ||
vol.reset(); | ||
}); | ||
|
||
it('falls back to local when CLI command fails', async () => { | ||
await fs.mkdirp('test'); | ||
await fs.writeFile( | ||
'test/fp1', | ||
Buffer.from( | ||
JSON.stringify({ | ||
sources: [ | ||
{ | ||
type: 'file', | ||
filePath: './assets/images/adaptive-icon.png', | ||
reasons: ['expoConfigExternalFile'], | ||
hash: '19b53640a95efdc2ccc7fc20f3ea4d0d381bb5c4', | ||
}, | ||
], | ||
}) | ||
) | ||
); | ||
|
||
await fs.writeFile( | ||
'test/fp2', | ||
Buffer.from( | ||
JSON.stringify({ | ||
sources: [ | ||
{ | ||
type: 'file', | ||
filePath: './assets/images/adaptive-icon.png', | ||
reasons: ['expoConfigExternalFile'], | ||
hash: '19b53640a95efdc2ccc7fc20f3ea4d0d381bb5c5', | ||
}, | ||
], | ||
}) | ||
) | ||
); | ||
|
||
jest.mocked(isModernExpoFingerprintCLISupportedAsync).mockResolvedValue(false); | ||
|
||
const diff = await diffFingerprintsAsync('test', 'test/fp1', 'test/fp2', { | ||
env: {}, | ||
logger: { debug: jest.fn() } as any, | ||
}); | ||
expect(diff).toEqual( | ||
JSON.stringify([ | ||
{ | ||
op: 'changed', | ||
beforeSource: { | ||
type: 'file', | ||
filePath: './assets/images/adaptive-icon.png', | ||
reasons: ['expoConfigExternalFile'], | ||
hash: '19b53640a95efdc2ccc7fc20f3ea4d0d381bb5c4', | ||
}, | ||
afterSource: { | ||
type: 'file', | ||
filePath: './assets/images/adaptive-icon.png', | ||
reasons: ['expoConfigExternalFile'], | ||
hash: '19b53640a95efdc2ccc7fc20f3ea4d0d381bb5c5', | ||
}, | ||
}, | ||
]) | ||
); | ||
}); | ||
}); |
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,66 @@ | ||
import { BuildStepEnv } from '@expo/steps'; | ||
import fs from 'fs-extra'; | ||
import { bunyan } from '@expo/logger'; | ||
|
||
import { | ||
ExpoFingerprintCLICommandFailedError, | ||
ExpoFingerprintCLIInvalidCommandError, | ||
ExpoFingerprintCLIModuleNotFoundError, | ||
expoFingerprintCommandAsync, | ||
isModernExpoFingerprintCLISupportedAsync, | ||
} from './expoFingerprintCli'; | ||
import { diffFingerprints } from './fingerprint'; | ||
|
||
export async function diffFingerprintsAsync( | ||
projectDir: string, | ||
fingerprint1File: string, | ||
fingerprint2File: string, | ||
{ env, logger }: { env: BuildStepEnv; logger: bunyan } | ||
): Promise<string> { | ||
if (!(await isModernExpoFingerprintCLISupportedAsync(projectDir))) { | ||
logger.debug('Falling back to local fingerprint diff'); | ||
return await diffFingerprintsFallbackAsync(fingerprint1File, fingerprint2File); | ||
} | ||
|
||
try { | ||
return await diffFingerprintsCommandAsync(projectDir, fingerprint1File, fingerprint2File, { | ||
env, | ||
}); | ||
} catch (e) { | ||
if ( | ||
e instanceof ExpoFingerprintCLIModuleNotFoundError || | ||
e instanceof ExpoFingerprintCLICommandFailedError || | ||
e instanceof ExpoFingerprintCLIInvalidCommandError | ||
) { | ||
logger.debug('Falling back to local fingerprint diff'); | ||
return await diffFingerprintsFallbackAsync(fingerprint1File, fingerprint2File); | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
async function diffFingerprintsCommandAsync( | ||
projectDir: string, | ||
fingerprint1File: string, | ||
fingerprint2File: string, | ||
{ env }: { env: BuildStepEnv } | ||
): Promise<string> { | ||
return await expoFingerprintCommandAsync( | ||
projectDir, | ||
['fingerprint:diff', fingerprint1File, fingerprint2File], | ||
{ | ||
env, | ||
} | ||
); | ||
} | ||
|
||
async function diffFingerprintsFallbackAsync( | ||
fingerprint1File: string, | ||
fingerprint2File: string | ||
): Promise<string> { | ||
const [fingeprint1, fingerprint2] = await Promise.all([ | ||
fs.readJSON(fingerprint1File), | ||
fs.readJSON(fingerprint2File), | ||
]); | ||
return JSON.stringify(diffFingerprints(fingeprint1, fingerprint2)); | ||
} |
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,84 @@ | ||
import resolveFrom from 'resolve-from'; | ||
import spawnAsync from '@expo/turtle-spawn'; | ||
import { BuildStepEnv } from '@expo/steps'; | ||
import fs from 'fs-extra'; | ||
import semver from 'semver'; | ||
|
||
export class ExpoFingerprintCLIModuleNotFoundError extends Error {} | ||
export class ExpoFingerprintCLIInvalidCommandError extends Error {} | ||
export class ExpoFingerprintCLICommandFailedError extends Error {} | ||
|
||
function resolveExpoFingerprintCLI(projectRoot: string): string { | ||
const expoPackageRoot = resolveFrom.silent(projectRoot, 'expo/package.json'); | ||
try { | ||
return ( | ||
resolveFrom.silent(expoPackageRoot ?? projectRoot, '@expo/fingerprint/bin/cli') ?? | ||
resolveFrom(expoPackageRoot ?? projectRoot, '@expo/fingerprint/bin/cli.js') | ||
); | ||
} catch (e: any) { | ||
if (e.code === 'MODULE_NOT_FOUND') { | ||
throw new ExpoFingerprintCLIModuleNotFoundError( | ||
`The \`@expo/fingerprint\` package was not found.` | ||
); | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
export async function expoFingerprintCommandAsync( | ||
projectDir: string, | ||
args: string[], | ||
{ env }: { env: BuildStepEnv } | ||
): Promise<string> { | ||
const expoFingerprintCli = resolveExpoFingerprintCLI(projectDir); | ||
try { | ||
const spawnResult = await spawnAsync(expoFingerprintCli, args, { | ||
stdio: 'pipe', | ||
cwd: projectDir, | ||
env, | ||
}); | ||
return spawnResult.stdout; | ||
} catch (e: any) { | ||
if (e.stderr && typeof e.stderr === 'string') { | ||
if (e.stderr.includes('Invalid command')) { | ||
throw new ExpoFingerprintCLIInvalidCommandError( | ||
`The command specified by ${args} was not valid in the \`@expo/fingerprint\` CLI.` | ||
); | ||
} else { | ||
throw new ExpoFingerprintCLICommandFailedError(e.stderr); | ||
} | ||
} | ||
throw e; | ||
} | ||
} | ||
|
||
async function getExpoFingerprintPackageVersionIfInstalledAsync( | ||
projectDir: string | ||
): Promise<string | null> { | ||
const expoPackageRoot = resolveFrom.silent(projectDir, 'expo/package.json'); | ||
const maybePackageJson = resolveFrom.silent( | ||
expoPackageRoot ?? projectDir, | ||
'@expo/fingerprint/package.json' | ||
); | ||
if (!maybePackageJson) { | ||
return null; | ||
} | ||
const { version } = await fs.readJson(maybePackageJson); | ||
return version ?? null; | ||
} | ||
|
||
export async function isModernExpoFingerprintCLISupportedAsync( | ||
projectDir: string | ||
): Promise<boolean> { | ||
const expoFingerprintPackageVersion = | ||
await getExpoFingerprintPackageVersionIfInstalledAsync(projectDir); | ||
if (!expoFingerprintPackageVersion) { | ||
return false; | ||
} | ||
|
||
if (expoFingerprintPackageVersion.includes('canary')) { | ||
return true; | ||
} | ||
|
||
return semver.gte(expoFingerprintPackageVersion, '0.11.2'); | ||
} |
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