Skip to content

Commit

Permalink
feat: support wildcard exports when parsing dependency tree [sc-22632] (
Browse files Browse the repository at this point in the history
checkly#1003)

* feat: add a hidden flag to output the project bundle for debug purposes

The new flag, `--debug-bundle`, outputs the project bundle payload as-is
after the project has been parsed. The bundle is output to a file, by default
`./debug-bundle.json`. A different file can be chosen with the new
`--debug-bundle-output-file` flag, which is also hidden.

* feat: support wildcard exports when parsing code for dependencies

Previously only named exports were supported.

* feat: use wildcard export in tests

Additionally, to make sure that not all files in the tree are getting
included automatically (which would have invalidated the wildcard export
tests), an unreachable file that should not appear in the list of
dependencies was added to the relevant examples.
  • Loading branch information
sorccu authored Dec 13, 2024
1 parent 75797f3 commit e99187c
Show file tree
Hide file tree
Showing 16 changed files with 63 additions and 1 deletion.
20 changes: 20 additions & 0 deletions packages/cli/src/commands/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as fs from 'fs/promises'
import * as api from '../rest/api'
import config from '../services/config'
import prompts from 'prompts'
Expand Down Expand Up @@ -61,6 +62,16 @@ export default class Deploy extends AuthCommand {
allowNo: true,
env: 'CHECKLY_VERIFY_RUNTIME_DEPENDENCIES',
}),
'debug-bundle': Flags.boolean({
description: 'Output the project bundle to a file without deploying any resources.',
default: false,
hidden: true,
}),
'debug-bundle-output-file': Flags.string({
description: 'The file to output the debug debug bundle to.',
default: './debug-bundle.json',
hidden: true,
}),
}

async run (): Promise<void> {
Expand All @@ -73,6 +84,8 @@ export default class Deploy extends AuthCommand {
output,
config: configFilename,
'verify-runtime-dependencies': verifyRuntimeDependencies,
'debug-bundle': debugBundle,
'debug-bundle-output-file': debugBundleOutputFile,
} = flags
const { configDirectory, configFilenames } = splitConfigFilePath(configFilename)
const {
Expand Down Expand Up @@ -120,6 +133,13 @@ export default class Deploy extends AuthCommand {
}
}

if (debugBundle) {
const output = JSON.stringify(projectPayload, null, 2)
await fs.writeFile(debugBundleOutputFile, output, 'utf8')
this.log(`Successfully wrote debug bundle to "${debugBundleOutputFile}".`)
return
}

const { data: account } = await api.accounts.get(config.getAccountId())

if (!force && !preview) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dep6' // wildcard export
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 'Hello World'
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import dep2 from './dep2'
import { dep3 } from './dep3'
export { value } from './dep1'
export { dep4 } from './dep4.mjs'
export { value as otherValue } from './dep5.mjs'

/* eslint-disable no-console */
console.log('Received ', dep2, dep3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file is intentionally not reachable from the entrypoint and should
// not appear in the list of dependencies.

export const unreachable = true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dep6' // wildcard export
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 'Hello World'
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dep2 from './dep2'
import { dep3 } from './dep3'
export { value } from './dep1'
export { value as otherValue } from './dep5'

/* eslint-disable no-console */
console.log('Received ', dep2, dep3)
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file is intentionally not reachable from the entrypoint and should
// not appear in the list of dependencies.

export const unreachable = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file is intentionally not reachable from the entrypoint and should
// not appear in the list of dependencies.

export const unreachable = true
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './dep6' // wildcard export
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const value = 'Hello World'
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import * as moduleImport from './module'
import * as modulePackage from './module-package'
import { ExternalFirstPage } from './pages/external.first.page.js'
import { ExternalSecondPage } from './pages/external.second.page'
export { value } from './dep5' // named export

export function doMath (num: number): number {
return add(num, subtract(10, 7))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// This file is intentionally not reachable from the entrypoint and should
// not appear in the list of dependencies.

export const value = 'Hello World'
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ describe('dependency-parser - parser()', () => {
toAbsolutePath('dep2.ts'),
toAbsolutePath('dep3.ts'),
toAbsolutePath('dep4.js'),
toAbsolutePath('dep5.ts'),
toAbsolutePath('dep6.ts'),
toAbsolutePath('module-package', 'main.js'),
toAbsolutePath('module-package', 'package.json'),
toAbsolutePath('module', 'index.ts'),
Expand All @@ -135,6 +137,8 @@ describe('dependency-parser - parser()', () => {
toAbsolutePath('dep1.js'),
toAbsolutePath('dep2.js'),
toAbsolutePath('dep3.js'),
toAbsolutePath('dep5.js'),
toAbsolutePath('dep6.js'),
])
})

Expand All @@ -149,6 +153,8 @@ describe('dependency-parser - parser()', () => {
toAbsolutePath('dep2.mjs'),
toAbsolutePath('dep3.mjs'),
toAbsolutePath('dep4.mjs'),
toAbsolutePath('dep5.mjs'),
toAbsolutePath('dep6.mjs'),
])
})

Expand Down
13 changes: 12 additions & 1 deletion packages/cli/src/services/check-parser/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,11 @@ export class Parser {
if (node.source.type !== 'Literal') return
Parser.registerDependency(node.source.value, localDependencies, npmDependencies)
},
ExportAllDeclaration (node: any) {
if (node.source === null) return
if (node.source.type !== 'Literal') return
Parser.registerDependency(node.source.value, localDependencies, npmDependencies)
},
}
}

Expand All @@ -264,7 +269,13 @@ export class Parser {
ExportNamedDeclaration (node: TSESTree.ExportNamedDeclaration) {
// The statement isn't importing another dependency
if (node.source === null) return
// For now, we only support literal strings in the import statement
// For now, we only support literal strings in the export statement
if (node.source.type !== tsParser.TSESTree.AST_NODE_TYPES.Literal) return
Parser.registerDependency(node.source.value, localDependencies, npmDependencies)
},
ExportAllDeclaration (node: TSESTree.ExportAllDeclaration) {
if (node.source === null) return
// For now, we only support literal strings in the export statement
if (node.source.type !== tsParser.TSESTree.AST_NODE_TYPES.Literal) return
Parser.registerDependency(node.source.value, localDependencies, npmDependencies)
},
Expand Down

0 comments on commit e99187c

Please sign in to comment.