Skip to content

Commit

Permalink
feat: verify package structure
Browse files Browse the repository at this point in the history
  • Loading branch information
antongolub committed Nov 5, 2021
1 parent d087c41 commit 93b254a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/ts/flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
patchLockfile,
printRuntimeDigest,
syncLockfile,
verify,
yarnImport,
yarnInstall,
yarnLockToPkgLock,
Expand All @@ -17,6 +18,7 @@ import {
export const convert: TFlow = {
main: [
['Runtime digest', printRuntimeDigest],
['Verifying package structure...', verify],
['Preparing temp assets...', clear, createTempAssets, createSymlinks],
['Generating package-lock.json from yarn.lock...', yarnLockToPkgLock],
['Applying npm audit fix...', npmAuditFix],
Expand All @@ -36,6 +38,7 @@ export const convert: TFlow = {
export const patch: TFlow = {
main: [
['Runtime digest', printRuntimeDigest],
['Verifying package structure...', verify],
['Preparing temp assets...', clear, createTempAssets, createSymlinks],
[
'Patching yarn.lock with audit data...',
Expand Down
15 changes: 15 additions & 0 deletions src/main/ts/stages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,18 @@ export const patchLockfile: TCallback = ({ temp, ctx }) => {
lf.patch(lockfile, report, ctx)
lf.write(lockfilePath, lockfile)
}

/**
* Check that everything is fine with pkg dir.
* @param {TContext} cxt
* @return {void}
*/
export const verify: TCallback = ({ cwd }) => {
const required = ['yarn.lock', 'package.json', 'node_modules']

required.forEach(resource => {
if (!fs.existsSync(join(cwd, resource))) {
throw new Error(`not found: ${resource}`)
}
})
}
12 changes: 11 additions & 1 deletion src/test/ts/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,10 +213,20 @@ describe('yarn-audit-fix', () => {
expect(handler).toHaveBeenCalledTimes(1)
})

it('throws error on broken package structure', async () => {
// @ts-ignore
fs.existsSync.mockReturnValueOnce(false)

expect(run({cwd: 'unknown'})).rejects.toEqual(
new Error('not found: yarn.lock')
)
})

it('throws error on unsupported flow', async () =>
expect(run({ flow: 'unknown' })).rejects.toEqual(
new Error('Unsupported flow: unknown'),
))
)
)

describe('`patch` flow', () => {
it('invokes cmd queue with proper args', async () => {
Expand Down

0 comments on commit 93b254a

Please sign in to comment.