-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[updates] Change fingeprint diff to use versioned fingerprint CLI
- Loading branch information
Showing
5 changed files
with
215 additions
and
15 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
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,84 @@ | ||
import { vol } from 'memfs'; | ||
import fs from 'fs-extra'; | ||
|
||
import { diffFingerprintsAsync } from '../diffFingerprintsAsync'; | ||
import { | ||
ExpoFingerprintCLIModuleNotFoundError, | ||
expoFingerprintCommandAsync, | ||
} from '../expoFingerprintCli'; | ||
|
||
jest.mock('../expoFingerprintCli', () => ({ | ||
...jest.requireActual('../expoFingerprintCli'), | ||
expoFingerprintCommandAsync: jest.fn(), | ||
})); | ||
|
||
jest.mock('fs'); | ||
|
||
describe(diffFingerprintsAsync, () => { | ||
beforeEach(async () => { | ||
vol.reset(); | ||
}); | ||
|
||
it('falls back to local when CLI is not available', 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(expoFingerprintCommandAsync) | ||
.mockRejectedValue(new ExpoFingerprintCLIModuleNotFoundError()); | ||
|
||
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,56 @@ | ||
import { BuildStepEnv } from '@expo/steps'; | ||
import fs from 'fs-extra'; | ||
import { bunyan } from '@expo/logger'; | ||
|
||
import { | ||
ExpoFingerprintCLICommandFailedError, | ||
ExpoFingerprintCLIInvalidCommandError, | ||
ExpoFingerprintCLIModuleNotFoundError, | ||
expoFingerprintCommandAsync, | ||
} from './expoFingerprintCli'; | ||
import { diffFingerprints } from './fingerprint'; | ||
|
||
export async function diffFingerprintsAsync( | ||
projectDir: string, | ||
fingerprint1File: string, | ||
fingerprint2File: string, | ||
{ env, logger }: { env: BuildStepEnv; logger: bunyan } | ||
): Promise<string> { | ||
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, [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,51 @@ | ||
import resolveFrom from 'resolve-from'; | ||
import spawnAsync from '@expo/turtle-spawn'; | ||
import { BuildStepEnv } from '@expo/steps'; | ||
|
||
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; | ||
} | ||
} |
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