From db98e8dda0644f9d8735b6c577912b895999a0cc Mon Sep 17 00:00:00 2001 From: sthornock Date: Wed, 15 Mar 2023 15:35:04 -0700 Subject: [PATCH 1/2] feature(Registry): add function/job config to service to allow creation of function/job registry --- src/main.ts | 22 ++++++++++++++++++++-- templates/index.eta | 14 +++++++++++++- templates/service.eta | 15 ++++++++++++++- 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/main.ts b/src/main.ts index f3bb8c7..79c795e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -119,6 +119,7 @@ interface ParseFunctionService { triggers: string[]; jobs: string[]; config?: string; + json?: string; } interface ParseServiceMap { @@ -184,6 +185,9 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance { fs.mkdirSync(this.buildPath!); + const functionsMap = {}; + const jobsMap = {}; + const services = this.schemaPaths!.reduce((memo, schemaPath) => { const p = schemaPath.split(path.sep); const serviceDirName = p[p.length - 2]; @@ -205,7 +209,7 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance { config: hookConfig, }; return hMemo; - }, {} as Hooks); + }, {} as Hooks); memo[serviceDirName] = { name: serviceDirName, @@ -219,8 +223,9 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance { jobs, config, }; + return memo; - }, {} as ParseServiceMap); + }, {} as ParseServiceMap); const helpersFile = await this.makeHelpersFile(services); fs.writeFileSync(path.resolve(`${this.buildPath}`, 'helpers.ts'), helpersFile); @@ -268,6 +273,15 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance { return f; } + // private async makeConfigFile(services: ParseServiceMap): Promise { + // const indexFileString = await eta.renderFile( + // 'config.eta', + // { services, helpers: this.templateHelpers } + // ) as string; + // const f = prettier.format(indexFileString, { parser: 'typescript', printWidth: 112 }); + // return f; + // } + private async makeServiceFile(service: ParseFunctionService): Promise { const serviceFileString = await eta.renderFile( 'service.eta', @@ -280,6 +294,10 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance { /* === TEMPLATE HELPERS === */ +function replaceAllInString(str: string, match: string, replace: string) { + return str.split(match).join(replace); +} + function removeExtension(filePath: string) { return filePath.replace(/\.(t|j)s$/, ''); } diff --git a/templates/index.eta b/templates/index.eta index c076f69..b3ee81e 100644 --- a/templates/index.eta +++ b/templates/index.eta @@ -1,10 +1,22 @@ import type P from 'parse'; import { registerParseInstance } from './helpers'; import { ParseFunctionConfig } from './types'; + <% Object.entries(it.services).forEach(([serviceName, service]) => { %> - import <%~ serviceName %> from './<%~ serviceName %>'; + import <%~ serviceName %>, { functions as <%~ serviceName %>functions, jobs as <%~ serviceName %>jobs } from './<%~ serviceName %>'; <% }) %> +export const functions = { + <% Object.entries(it.services).forEach(([serviceName, service]) => { %> + ...<%~ serviceName %>functions, + <% }) %> +} + +export const jobs = { + <% Object.entries(it.services).forEach(([serviceName, service]) => { %> + ...<%~ serviceName %>jobs, + <% }) %> +} type ParseType = typeof P; diff --git a/templates/service.eta b/templates/service.eta index 8353ea2..002cd4b 100644 --- a/templates/service.eta +++ b/templates/service.eta @@ -60,6 +60,7 @@ const defaultTriggerConfig = { requireMaster: true, }; +export const config = <%~ it.service.json %> /* === SERVICE === */ const service: <% if (it.service.config) { %> ServiceInitializer <% } else { %> ServiceInitializer <% } %> = @@ -163,7 +164,7 @@ const service: <% if (it.service.config) { %> ServiceInitializer Object.assign({}, defaultFunctionConfig, <%~ it.helpers.getSanitizedFunctionName(functionPath, 'config_') %>) ); <% }) %> - + // JOBS <% it.service.jobs.forEach((jobPath) => { %> Parse.Cloud.job( @@ -180,4 +181,16 @@ const service: <% if (it.service.config) { %> ServiceInitializer }; }; +export const functions = { + <% it.service.functions.forEach((functionPath) => { %> + '<%~ it.helpers.getSanitizedFunctionName(functionPath) %>': Object.assign({}, defaultFunctionConfig, <%~ it.helpers.getSanitizedFunctionName(functionPath, 'config_') %>), + <% }) %> +} + +export const jobs = { + <% it.service.jobs.forEach((jobPath) => { %> + '<%~ it.helpers.getSanitizedFunctionName(jobPath) %>': {}, + <% }) %> +} + export default service; From d5506784f472cd552a56c9592bdcfda9d67563cd Mon Sep 17 00:00:00 2001 From: sthornock Date: Wed, 15 Mar 2023 15:52:03 -0700 Subject: [PATCH 2/2] chore(Deps): upgrade deps --- package.json | 6 +++--- src/main.ts | 18 +----------------- 2 files changed, 4 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index e0da6c9..cd65b20 100644 --- a/package.json +++ b/package.json @@ -20,14 +20,14 @@ "@types/parse": "^2.18.18", "@types/prettier": "^2.7.0", "@types/webpack": "^5.28.0", + "husky": "^8.0.0", "typescript": "^4.6.4", - "webpack": "^5.74.0", - "husky": "^8.0.0" + "webpack": "^5.74.0" }, "dependencies": { "eta": "^2.0.0", "glob": "^8.1.0", - "parse": "^3.4.4", + "parse": "^4.0.1", "prettier": "^2.8.3", "webpack-inject-plugin": "^1.5.5" } diff --git a/src/main.ts b/src/main.ts index 79c795e..c8d41b1 100644 --- a/src/main.ts +++ b/src/main.ts @@ -183,10 +183,7 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance { console.warn('[PARSE FUNCTIONS PLUGIN]', 'No build folder found; creating one now'); } - fs.mkdirSync(this.buildPath!); - - const functionsMap = {}; - const jobsMap = {}; + fs.mkdirSync(this.buildPath!); const services = this.schemaPaths!.reduce((memo, schemaPath) => { const p = schemaPath.split(path.sep); @@ -273,15 +270,6 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance { return f; } - // private async makeConfigFile(services: ParseServiceMap): Promise { - // const indexFileString = await eta.renderFile( - // 'config.eta', - // { services, helpers: this.templateHelpers } - // ) as string; - // const f = prettier.format(indexFileString, { parser: 'typescript', printWidth: 112 }); - // return f; - // } - private async makeServiceFile(service: ParseFunctionService): Promise { const serviceFileString = await eta.renderFile( 'service.eta', @@ -294,10 +282,6 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance { /* === TEMPLATE HELPERS === */ -function replaceAllInString(str: string, match: string, replace: string) { - return str.split(match).join(replace); -} - function removeExtension(filePath: string) { return filePath.replace(/\.(t|j)s$/, ''); }