Skip to content

Commit

Permalink
use types for client generator needle
Browse files Browse the repository at this point in the history
  • Loading branch information
Tcharl committed Mar 30, 2023
1 parent d8cf6ea commit 5253f19
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 44 deletions.
13 changes: 9 additions & 4 deletions generators/client/generator.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import statistics from '../statistics.mjs';
import { GENERATOR_BOOTSTRAP_APPLICATION, GENERATOR_CYPRESS, GENERATOR_COMMON, GENERATOR_CLIENT } from '../generator-list.mjs';

import { testFrameworkTypes, clientFrameworkTypes } from '../../jdl/jhipster/index.mjs';

import { createNeedleCallback } from '../base/support/index.mjs';
const { ANGULAR, VUE, REACT } = clientFrameworkTypes;
const { CYPRESS } = testFrameworkTypes;

Expand Down Expand Up @@ -148,9 +148,14 @@ export default class JHipsterClientGenerator extends BaseApplicationGenerator {
},

addExternalResource({ application, source }) {
source.addExternalResourceToRoot = (resources, comment) => {
this.needleApi.client.addExternalResourcesToApplicationRoot(application, resources, comment);
};
source.addExternalResourceToRoot = ({ resource, comment }) =>
this.editFile(
`${application.clientSrcDir}index.html`,
createNeedleCallback({
needle: 'add-resources-to-root',
contentToAdd: [comment ? `<!-- ${comment} -->` : undefined, resource].filter(i => i).join('\n'),
})
);
},
});
}
Expand Down
36 changes: 0 additions & 36 deletions generators/client/needle-api/needle-client.mts
Original file line number Diff line number Diff line change
Expand Up @@ -40,40 +40,4 @@ export default class extends needleBase {

return styleBlock;
}

addExternalResourcesToRoot(resources: string, comment: string) {
const errorMessage = 'Resources are not added to JHipster app.';
const file = `${this.clientSrcDir}index.html`;
let resourcesBlock = '';
if (comment) {
resourcesBlock += `<!-- ${comment} -->\n`;
}
resourcesBlock += `${resources}\n`;
this.addBlockContentToFile(
{
file,
needle: 'jhipster-needle-add-resources-to-root',
splicable: resourcesBlock,
},
errorMessage
);
}

addExternalResourcesToApplicationRoot(application: any, resources: string, comment: string) {
const errorMessage = 'Resources are not added to JHipster app.';
const file = `${application.clientSrcDir}index.html`;
let resourcesBlock = '';
if (comment) {
resourcesBlock += `<!-- ${comment} -->\n`;
}
resourcesBlock += `${resources}\n`;
this.addBlockContentToFile(
{
file,
needle: 'jhipster-needle-add-resources-to-root',
splicable: resourcesBlock,
},
errorMessage
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { dryRunHelpers as helpers, result as runResult } from '../support/index.mjs';
import ClientGenerator from '../../generators/client/index.mjs';
import { CLIENT_MAIN_SRC_DIR } from '../../generators/generator-constants.mjs';
import { getGenerator } from '../support/index.mjs';
import { CLIENT_MAIN_SRC_DIR } from '../generator-constants.mjs';
import { dryRunHelpers as helpers, result as runResult, getGenerator } from '../../test/support/index.mjs';

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const mockBlueprintSubGen: any = class extends ClientGenerator {
Expand All @@ -21,7 +20,10 @@ const mockBlueprintSubGen: any = class extends ClientGenerator {
return this.asPostWritingTaskGroup({
// @ts-ignore
async additionalResource({ source }) {
source.addExternalResourceToRoot('<link rel="stylesheet" href="content/css/my.css">', 'Comment added by JHipster API');
source.addExternalResourceToRoot({
resource: '<link rel="stylesheet" href="content/css/my.css">',
comment: 'Comment added by JHipster API',
});
},
});
}
Expand Down
18 changes: 18 additions & 0 deletions generators/client/types.d.mts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,21 @@ export type ClientApplication = CommonClientServerApplication &
webappLoginRegExp: string;
webappEnumerationsDir?: string;
};

/**
* @private
* Add external resources to root file(index.html).
*/
export type ClientResources = {
/**
* @param {string} resource - Resources added to root file.
*/
resource: string;
/**
* @param {string} comment - comment to add before resources content.
*/
comment?: string | null;
};
export type ClientSourceType = {
addExternalResourceToRoot?(resources: ClientResources): void;
};

0 comments on commit 5253f19

Please sign in to comment.