Skip to content

Feature/root relative imports #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ interface ParseFunctionServiceSchema {
interface ParseFunctionService {
name: string;
className: string;
basePath: string;
schemaPath: string;
schema: ParseFunctionServiceSchema;
functions: string[];
Expand Down Expand Up @@ -164,7 +165,7 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance {
this.schemaPaths = glob.sync(`${this.basePath}/**/schema.json`);
this.buildPath = path.resolve(this.basePath, '.build');
this.indexFilePath = path.resolve(this.buildPath, `index.ts`);

compiler.hooks.compilation.tap(this.constructor.name, this.startBuild);

// Look for import of modules that begins with `@@function` and replace with build folder
Expand All @@ -189,7 +190,7 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance {
const servicePath = path.join(this.basePath!, serviceDirName);
const configPath = path.join(servicePath, 'config.ts');
const schema: ParseFunctionServiceSchema = JSON.parse(fs.readFileSync(schemaPath, { encoding: 'utf-8' }));
const config = fs.existsSync(configPath) ? configPath : undefined;
const config = fs.existsSync(configPath) ? configPath.replace(this.basePath!, '..') : undefined;
const triggers = glob.sync(`${servicePath}/triggers/*`);
const functions = glob.sync(`${servicePath}/functions/**/*`);
const jobs = glob.sync(`${servicePath}/jobs/**/*`);
Expand All @@ -205,9 +206,10 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance {
};
return hMemo;
}, {} as Hooks);

memo[serviceDirName] = {
name: serviceDirName,
basePath: this.basePath!,
className: schema.className,
schema,
schemaPath,
Expand All @@ -219,10 +221,10 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance {
};
return memo;
}, {} as ParseServiceMap);

const helpersFile = await this.makeHelpersFile(services);
fs.writeFileSync(path.resolve(`${this.buildPath}`, 'helpers.ts'), helpersFile);

const typesFile = await this.makeTypesFile(services);
fs.writeFileSync(path.resolve(`${this.buildPath}`, 'types.ts'), typesFile);

Expand All @@ -247,7 +249,7 @@ export class ParseFunctionsPlugin implements WebpackPluginInstance {
const f = prettier.format(helpersFileString, { parser: 'typescript', printWidth: 112 });
return f;
}

private async makeTypesFile(services: ParseServiceMap): Promise<string> {
const typesFileString = await eta.renderFile(
'types.eta',
Expand Down Expand Up @@ -282,8 +284,8 @@ function removeExtension(filePath: string) {
return filePath.replace(/\.(t|j)s$/, '');
}

function createImportFromFilename(filePath: string, prefix: string = '') {
return `import ${getSanitizedFunctionName(filePath, prefix)} from '${filePath.replace(/\.ts$/, '')}';`
function createImportFromFilename(filePath: string, prefix: string = '', basePath: string = '') {
return `import ${getSanitizedFunctionName(filePath, prefix)} from '${filePath.replace(/\.ts$/, '').replace(basePath, '..')}';`
}

function getSanitizedFunctionName(filePath: string, prefix: string = ''): string {
Expand All @@ -295,10 +297,10 @@ function getSanitizedFunctionName(filePath: string, prefix: string = ''): string
}

function mapSchemaTypeToTSType(schemaField: ParseFunctionServiceSchemaField): string {
switch(schemaField.type) {
switch (schemaField.type) {
case ParseServiceSchemaFieldType.Relation:
return `Parse.Relation`
// return `Parse.Relation<${schemaField.targetClass}>`
// return `Parse.Relation<${schemaField.targetClass}>`
case ParseServiceSchemaFieldType.Pointer:
return `Parse.Pointer`
case ParseServiceSchemaFieldType.String:
Expand Down
10 changes: 5 additions & 5 deletions templates/service.eta
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import { ActionTrigger, ServiceInitializer, TriggerCloudRequestHandler, TriggerH

<% Object.entries(it.service.hooks).forEach(([hookName, hooks]) => { %>
<% if (hooks.config) { %>
<%~ it.helpers.createImportFromFilename(hooks.config, `hook_${hookName}_`) %>
<%~ it.helpers.createImportFromFilename(hooks.config, `hook_${hookName}_`, it.service.basePath) %>
<% } %>

<% hooks.hooks.forEach((hookPath) => { %>
<%~ it.helpers.createImportFromFilename(hookPath, `hook_${hookName}_`) %>
<%~ it.helpers.createImportFromFilename(hookPath, `hook_${hookName}_`, it.service.basePath) %>
<% }) %>
<% }) %>

<% it.service.triggers.forEach((triggerPath) => { %>
<%~ it.helpers.createImportFromFilename(triggerPath, 'trigger_') %>
<%~ it.helpers.createImportFromFilename(triggerPath, 'trigger_', it.service.basePath) %>
<% }) %>

<% it.service.functions.forEach((functionPath) => { %>
import <%~ it.helpers.getSanitizedFunctionName(functionPath) %>, { config as <%~ it.helpers.getSanitizedFunctionName(functionPath, 'config_') %> } from '<%~ it.helpers.removeExtension(functionPath) %>';
import <%~ it.helpers.getSanitizedFunctionName(functionPath) %>, { config as <%~ it.helpers.getSanitizedFunctionName(functionPath, 'config_') %> } from '<%~ it.helpers.removeExtension(functionPath.replace(it.service.basePath, '..')) %>';
<% }) %>

<% it.service.jobs.forEach((jobPath) => { %>
<%~ it.helpers.createImportFromFilename(jobPath, 'job_') %>
<%~ it.helpers.createImportFromFilename(jobPath, 'job_', it.service.basePath) %>
<% }) %>


Expand Down
4 changes: 2 additions & 2 deletions templates/types.eta
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as P from 'parse';
<% Object.entries(it.services).forEach(([serviceName, service]) => { %>
<% service.triggers.forEach((trigger) => { %>
import { payloadSchema as <%~ it.helpers.getSanitizedFunctionName(trigger, 'trigger_payload_schema_') %> } from '<%~ it.helpers.removeExtension(trigger) %>';
import { payloadSchema as <%~ it.helpers.getSanitizedFunctionName(trigger, 'trigger_payload_schema_') %> } from '<%~ it.helpers.removeExtension(trigger).replace(service.basePath, '..') %>';
<% }) %>

<% if (!service.config) { return; } %>
<%~ it.helpers.createImportFromFilename(service.config, `${serviceName}_`) %>
<%~ it.helpers.createImportFromFilename(service.config, `${serviceName}_`).replace('..', '') %>
<% }) %>


Expand Down