From 5270e3410931e058a33fe538562149ec8e9c3591 Mon Sep 17 00:00:00 2001 From: Alex Rock Date: Wed, 3 Jul 2024 11:56:36 -0600 Subject: [PATCH 1/4] feat: first pass at sending the raw file along to Flatfile --- apps/getting-started/package.json | 6 ++- package-lock.json | 13 ++++--- packages/cli/src/x/actions/deploy.action.ts | 42 +++++++++++++++------ 3 files changed, 41 insertions(+), 20 deletions(-) diff --git a/apps/getting-started/package.json b/apps/getting-started/package.json index eaf81a52..b75a2df6 100644 --- a/apps/getting-started/package.json +++ b/apps/getting-started/package.json @@ -6,15 +6,17 @@ "main": "index.js", "scripts": { "develop": "flatfile develop index.ts", + "deploy": "flatfile deploy index.ts", "paginate": "flatfile develop paginate-records.ts" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { + "@flatfile/api": "^1.8.5", "@flatfile/cross-env-config": "*", "@flatfile/hooks": "^1.3.0", - "@flatfile/api": "^1.8.5", + "@flatfile/listener": "^1.0.5", "@flatfile/plugin-record-hook": "^1.5.2", "actions": "^1.3.0", "ansi-colors": "^4.1.3", @@ -28,4 +30,4 @@ "devDependencies": { "@types/node": "^18.16.0" } -} \ No newline at end of file +} diff --git a/package-lock.json b/package-lock.json index 614a6c0f..2eb08164 100644 --- a/package-lock.json +++ b/package-lock.json @@ -40,6 +40,7 @@ "@flatfile/api": "^1.8.5", "@flatfile/cross-env-config": "*", "@flatfile/hooks": "^1.3.0", + "@flatfile/listener": "^1.0.5", "@flatfile/plugin-record-hook": "^1.5.2", "actions": "^1.3.0", "ansi-colors": "^4.1.3", @@ -33306,7 +33307,7 @@ }, "packages/embedded-utils": { "name": "@flatfile/embedded-utils", - "version": "1.2.4", + "version": "1.3.0", "license": "ISC", "dependencies": { "@flatfile/api": "^1.8.9", @@ -33430,10 +33431,10 @@ }, "packages/javascript": { "name": "@flatfile/javascript", - "version": "1.3.7", + "version": "1.3.9", "license": "ISC", "dependencies": { - "@flatfile/embedded-utils": "^1.2.4", + "@flatfile/embedded-utils": "^1.3.0", "@flatfile/listener": "^1.0.4", "@flatfile/plugin-record-hook": "^1.5.2" }, @@ -36769,7 +36770,7 @@ }, "packages/react": { "name": "@flatfile/react", - "version": "7.9.9", + "version": "7.9.10", "license": "ISC", "dependencies": { "@flatfile/api": "^1.8.5", @@ -38734,9 +38735,9 @@ }, "packages/vue": { "name": "@flatfile/vue", - "version": "1.0.17", + "version": "1.0.18", "dependencies": { - "@flatfile/embedded-utils": "1.2.4", + "@flatfile/embedded-utils": "1.3.0", "@flatfile/listener": "^1.0.4", "@flatfile/plugin-record-hook": "^1.5.2", "@vue/runtime-dom": "^3.3.4", diff --git a/packages/cli/src/x/actions/deploy.action.ts b/packages/cli/src/x/actions/deploy.action.ts index 9be81b6a..5fab054b 100644 --- a/packages/cli/src/x/actions/deploy.action.ts +++ b/packages/cli/src/x/actions/deploy.action.ts @@ -73,36 +73,50 @@ async function handleAgentSelection( } } -function findActiveTopics(allTopics: ListenerTopics[], client: any, topicsWithListeners = new Set()) { +function findActiveTopics( + allTopics: ListenerTopics[], + client: any, + topicsWithListeners = new Set() +) { client.listeners?.forEach((listener: ListenerTopics | ListenerTopics[]) => { - const listenerTopics = Array.isArray(listener[0]) ? listener[0] : [listener[0]] - listenerTopics.forEach(listenerTopic => { + const listenerTopics = Array.isArray(listener[0]) + ? listener[0] + : [listener[0]] + listenerTopics.forEach((listenerTopic) => { if (listenerTopic === '**') { // Filter cron events out of '**' list - they must be added explicitly - const filteredTopics = allTopics.filter(event => !event.startsWith('cron:')) - filteredTopics.forEach(topic => topicsWithListeners.add(topic)) + const filteredTopics = allTopics.filter( + (event) => !event.startsWith('cron:') + ) + filteredTopics.forEach((topic) => topicsWithListeners.add(topic)) } else if (listenerTopic.includes('**')) { const [prefix] = listenerTopic.split(':') - allTopics.forEach(topic => { if (topic.split(':')[0] === prefix) topicsWithListeners.add(topic) }) + allTopics.forEach((topic) => { + if (topic.split(':')[0] === prefix) topicsWithListeners.add(topic) + }) } else if (allTopics.includes(listenerTopic)) { topicsWithListeners.add(listenerTopic) } }) }) - client.nodes?.forEach((nestedClient: any) => findActiveTopics(allTopics, nestedClient, topicsWithListeners)) + client.nodes?.forEach((nestedClient: any) => + findActiveTopics(allTopics, nestedClient, topicsWithListeners) + ) return topicsWithListeners } -async function getActiveTopics(file: string): Promise{ +async function getActiveTopics(file: string): Promise { const allTopics = Object.values(Flatfile.events.EventTopic) let mount try { mount = await import(url.pathToFileURL(file).href) - } catch(e) { + } catch (e) { return program.error(messages.error(e)) } - return Array.from(findActiveTopics(allTopics, mount.default)) as Flatfile.EventTopic[]; + return Array.from( + findActiveTopics(allTopics, mount.default) + ) as Flatfile.EventTopic[] } export async function deployAction( @@ -164,8 +178,10 @@ export async function deployAction( path.basename(file!) ) ) - + const fileContent = fs.readFileSync(file, 'utf8') + console.log({ file, fileContent }) const entry = result.split(path.sep).join(path.posix.sep) + // console.log({ entry }) fs.writeFileSync(path.join(outDir, '_entry.js'), entry, 'utf8') const buildingSpinner = ora({ text: `Building deployable code package`, @@ -215,7 +231,9 @@ export async function deployAction( const deployFile = path.join(outDir, 'deploy.js') fs.writeFileSync(deployFile, code, 'utf8') - const activeTopics: Flatfile.EventTopic[] = await getActiveTopics(deployFile) + const activeTopics: Flatfile.EventTopic[] = await getActiveTopics( + deployFile + ) if (err) { return program.error(messages.error(err)) From b180cf4e7249797918e6dcef8ad3b15debd38d04 Mon Sep 17 00:00:00 2001 From: Alex Rock Date: Thu, 4 Jul 2024 15:15:25 -0500 Subject: [PATCH 2/4] feat: export source maps from vercel/ncc --- packages/cli/src/x/actions/deploy.action.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/cli/src/x/actions/deploy.action.ts b/packages/cli/src/x/actions/deploy.action.ts index 5fab054b..8d39b7d1 100644 --- a/packages/cli/src/x/actions/deploy.action.ts +++ b/packages/cli/src/x/actions/deploy.action.ts @@ -220,9 +220,10 @@ export async function deployAction( }).start() try { - const { err, code } = await ncc(path.join(outDir, '_entry.js'), { + const { err, code, map } = await ncc(path.join(outDir, '_entry.js'), { minify: liteMode, target: 'es2020', + sourceMap: true, cache: false, // TODO: add debug flag to add this and other debug options quiet: true, @@ -231,6 +232,8 @@ export async function deployAction( const deployFile = path.join(outDir, 'deploy.js') fs.writeFileSync(deployFile, code, 'utf8') + const mapFile = path.join(outDir, 'deploy.js.map') + fs.writeFileSync(mapFile, map, 'utf8') const activeTopics: Flatfile.EventTopic[] = await getActiveTopics( deployFile ) @@ -245,6 +248,9 @@ export async function deployAction( topics: activeTopics, compiler: 'js', source: code, + // TODO: Add this to the Agent Table + // @ts-ignore + map, slug: slug ?? selectedAgent?.slug, }, }) From ed7a5a4bb17f2b70a18bb09f0e163fd644e0e01f Mon Sep 17 00:00:00 2001 From: "komment-ai[bot]" <122626893+komment-ai[bot]@users.noreply.github.com> Date: Thu, 4 Jul 2024 20:16:01 +0000 Subject: [PATCH 3/4] Added comments to 4 functions across 1 file --- packages/cli/src/x/actions/deploy.action.ts | 92 +++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/packages/cli/src/x/actions/deploy.action.ts b/packages/cli/src/x/actions/deploy.action.ts index 8d39b7d1..b231bfac 100644 --- a/packages/cli/src/x/actions/deploy.action.ts +++ b/packages/cli/src/x/actions/deploy.action.ts @@ -20,6 +20,36 @@ const readPackageJson = util.promisify(require('read-package-json')) type ListenerTopics = Flatfile.EventTopic | '**' +/** + * @description 1) returns an selected agent from a list of agents if multiple agents + * are present in the environment and a slug is not provided, 2) provides information + * about having multiple agents in the environment, 3) confirms user selection of an + * agent to deploy to, 4) allows user to select an agent from a list, and 5) returns + * the selected agent. + * + * @param {Flatfile.Agent[] | undefined} data - array of agents to deploy from, and + * it allows the function to check if there are multiple agents present in the + * environment before prompting the user to select one. + * + * @param {string | undefined} slug - input of the selected agent to deploy if only + * one is available. + * + * @param {ora.Ora} validatingSpinner - ora Ora object that provides a spinner for + * validating user inputs, showing messages to the user in real-time based on the + * success or failure of their inputs. + * + * @returns {Flatfile.Agent element} an array of agents selected by the user. + * + * * If `data` is defined and has multiple elements, and a slug is already provided, + * the function returns the agent with the matching slug. + * * If there are multiple agents and no slug is provided, the function prompts the + * user to select an agent and returns the selected agent. + * * If there is only one agent, the function returns the first element of the `data` + * array. + * + * In summary, the output returned by the `handleAgentSelection` function depends + * on the input provided and whether a slug is already provided or not. + */ async function handleAgentSelection( data: Flatfile.Agent[] | undefined, slug: string | undefined, @@ -73,6 +103,34 @@ async function handleAgentSelection( } } +/** + * @description Iterates through client.listeners and client.nodes, filtering and + * adding topics to a Set of active topics based on their prefixes. It returns the + * filtered topics with listeners attached. + * + * @param {ListenerTopics[]} allTopics - list of all available topics to be processed + * by the function, which is then filtered and transformed based on the client's + * listeners to generate the active topics with listeners. + * + * @param {any} client - client that will have its listeners searched for active + * topics, and it is used to recursively call the function on nested clients. + * + * @param {new_expression} topicsWithListeners - Set of active topics after filtering + * out duplicates and removing cron events explicitly added, and it is updated at + * each iteration with new topics found in listeners. + * + * @returns {Set} a new `Set` containing the active topics and their listeners. + * + * * `topicsWithListeners`: A `Set` containing the active topics that have listeners + * registered on them. + * + * Explanation: + * This set contains the active topics that have listeners registered on them after + * filtering out cron events and adding them to the set using the `includes()` method. + * + * Note: The function does not destructure the output directly, but rather explains + * its properties for understanding its behavior. + */ function findActiveTopics( allTopics: ListenerTopics[], client: any, @@ -105,6 +163,16 @@ function findActiveTopics( return topicsWithListeners } +/** + * @description Imports a file, then uses `findActiveTopics` to identify topics that + * are active based on mount points. Finally, it returns the identified active topics + * as a promise. + * + * @param {string} file - file to be scanned for active topics. + * + * @returns {Promise} an array of `Flatfile.EventTopic` objects + * representing the active topics in the provided file. + */ async function getActiveTopics(file: string): Promise { const allTopics = Object.values(Flatfile.events.EventTopic) @@ -119,6 +187,30 @@ async function getActiveTopics(file: string): Promise { ) as Flatfile.EventTopic[] } +/** + * @description Generates high-quality documentation for code given to it, by: + * + * * Checking if a directory exists and creating it if needed. + * * Reading a package.json file and installing any missing dependencies. + * * Creating an authenticated API client using an API key. + * * Validating the code package. + * * Selecting a Flatfile agent based on slug or environment name. + * * Deploying an event listener to Flatfile using the selected agent. + * + * @param {string | null | undefined} file - entry file containing the Flatfile event + * listener code, which is read and compiled into a deployable package for Flatfile + * environments. + * + * @param {Partial<{ + * slug: string + * topics: string + * apiUrl: string + * token: string + * }>} options - Optional partial state of the agent to deploy, which includes the + * slug, topics, API URL, and token. + * + * @returns {Promise} a successful deployment of an event listener to Flatfile. + */ export async function deployAction( file?: string | null | undefined, options?: Partial<{ From 6950fb74ab5734341fe9393c1e7e9904ab3d4331 Mon Sep 17 00:00:00 2001 From: "komment-ai[bot]" <122626893+komment-ai[bot]@users.noreply.github.com> Date: Thu, 4 Jul 2024 20:53:42 +0000 Subject: [PATCH 4/4] Added comments to 4 functions across 1 file --- packages/cli/src/x/actions/deploy.action.ts | 106 ++++++++------------ 1 file changed, 42 insertions(+), 64 deletions(-) diff --git a/packages/cli/src/x/actions/deploy.action.ts b/packages/cli/src/x/actions/deploy.action.ts index b231bfac..f889684f 100644 --- a/packages/cli/src/x/actions/deploy.action.ts +++ b/packages/cli/src/x/actions/deploy.action.ts @@ -21,34 +21,27 @@ const readPackageJson = util.promisify(require('read-package-json')) type ListenerTopics = Flatfile.EventTopic | '**' /** - * @description 1) returns an selected agent from a list of agents if multiple agents - * are present in the environment and a slug is not provided, 2) provides information - * about having multiple agents in the environment, 3) confirms user selection of an - * agent to deploy to, 4) allows user to select an agent from a list, and 5) returns - * the selected agent. + * @description Handles selecting an agent from a list of agents based on user input. + * If multiple agents are provided, it prompts the user to select one. Otherwise, it + * returns the first or only agent depending on the input. * - * @param {Flatfile.Agent[] | undefined} data - array of agents to deploy from, and - * it allows the function to check if there are multiple agents present in the - * environment before prompting the user to select one. + * @param {Flatfile.Agent[] | undefined} data - list of Flatfile.Agent objects that + * can be filtered or selected by the user based on their slugs or other properties. * - * @param {string | undefined} slug - input of the selected agent to deploy if only - * one is available. + * @param {string | undefined} slug - identifier assigned to each agent in the + * environment, which the function uses to find and return the selected agent. * - * @param {ora.Ora} validatingSpinner - ora Ora object that provides a spinner for - * validating user inputs, showing messages to the user in real-time based on the - * success or failure of their inputs. + * @param {ora.Ora} validatingSpinner - ora spinner used to display informative failure + * messages when multiple agents are found in the environment. * - * @returns {Flatfile.Agent element} an array of agents selected by the user. + * @returns {Flatfile.Agent} a selected agent from a list of multiple agents, based + * on user input. * - * * If `data` is defined and has multiple elements, and a slug is already provided, - * the function returns the agent with the matching slug. - * * If there are multiple agents and no slug is provided, the function prompts the - * user to select an agent and returns the selected agent. - * * If there is only one agent, the function returns the first element of the `data` - * array. + * * `data`: The found agent or `undefined` if there's no data. + * * `slug`: The selected slug or `` if no slug was provided. + * * `validatingSpinner`: The Ora spinner used for validating the selection process. * - * In summary, the output returned by the `handleAgentSelection` function depends - * on the input provided and whether a slug is already provided or not. + * The output is directly returned without any additional summaries or information. */ async function handleAgentSelection( data: Flatfile.Agent[] | undefined, @@ -104,32 +97,23 @@ async function handleAgentSelection( } /** - * @description Iterates through client.listeners and client.nodes, filtering and - * adding topics to a Set of active topics based on their prefixes. It returns the - * filtered topics with listeners attached. + * @description Processes a list of listener topics and sub-clients to filter and + * identify active topics based on inclusion patterns. It iterates over the listeners, + * nodes, and topics to find matching and non-matching patterns and add them to a Set + * data structure for further processing. * - * @param {ListenerTopics[]} allTopics - list of all available topics to be processed - * by the function, which is then filtered and transformed based on the client's - * listeners to generate the active topics with listeners. + * @param {ListenerTopics[]} allTopics - list of all topics that the listener is + * interested in, which are filtered and added to a new Set data structure to form + * the active topics list returned by the function. * - * @param {any} client - client that will have its listeners searched for active - * topics, and it is used to recursively call the function on nested clients. + * @param {any} client - client to search for listeners in, and its `listeners` + * property is traversed to find active topics. * - * @param {new_expression} topicsWithListeners - Set of active topics after filtering - * out duplicates and removing cron events explicitly added, and it is updated at - * each iteration with new topics found in listeners. + * @param {new_expression} topicsWithListeners - Set of all the active topics that + * have listeners attached to them after filtering out any cron events and iterating + * over nested clients. * - * @returns {Set} a new `Set` containing the active topics and their listeners. - * - * * `topicsWithListeners`: A `Set` containing the active topics that have listeners - * registered on them. - * - * Explanation: - * This set contains the active topics that have listeners registered on them after - * filtering out cron events and adding them to the set using the `includes()` method. - * - * Note: The function does not destructure the output directly, but rather explains - * its properties for understanding its behavior. + * @returns {object} a set of top-level and nested topics with listeners. */ function findActiveTopics( allTopics: ListenerTopics[], @@ -164,14 +148,13 @@ function findActiveTopics( } /** - * @description Imports a file, then uses `findActiveTopics` to identify topics that - * are active based on mount points. Finally, it returns the identified active topics - * as a promise. + * @description Retrieves a list of active topics from a given file and returns them + * as an array of `Flatfile.EventTopic`. * - * @param {string} file - file to be scanned for active topics. + * @param {string} file - Flatfile file to be analyzed for active topics, and the + * function retrieves and returns an array of event topics from that file. * - * @returns {Promise} an array of `Flatfile.EventTopic` objects - * representing the active topics in the provided file. + * @returns {Promise} an array of Flatfile event topics. */ async function getActiveTopics(file: string): Promise { const allTopics = Object.values(Flatfile.events.EventTopic) @@ -188,28 +171,23 @@ async function getActiveTopics(file: string): Promise { } /** - * @description Generates high-quality documentation for code given to it, by: - * - * * Checking if a directory exists and creating it if needed. - * * Reading a package.json file and installing any missing dependencies. - * * Creating an authenticated API client using an API key. - * * Validating the code package. - * * Selecting a Flatfile agent based on slug or environment name. - * * Deploying an event listener to Flatfile using the selected agent. + * @description * Deploys an event listener to a Flatfile environment using an + * authenticated API client + * * Creates a build package from a source file, validates it, and deploys it to the + * environment. * - * @param {string | null | undefined} file - entry file containing the Flatfile event - * listener code, which is read and compiled into a deployable package for Flatfile - * environments. + * @param {string | null | undefined} file - entry file for Flatfile, which is used + * to compile and deploy an event listener code package. * * @param {Partial<{ * slug: string * topics: string * apiUrl: string * token: string - * }>} options - Optional partial state of the agent to deploy, which includes the - * slug, topics, API URL, and token. + * }>} options - 2-level nested object that allows passing various deployment + * configuration settings, including the `slug`, `topics`, `apiUrl`, and `token` properties. * - * @returns {Promise} a successful deployment of an event listener to Flatfile. + * @returns {Promise} a list of events generated by the deployed event listener. */ export async function deployAction( file?: string | null | undefined,