Skip to content

Commit

Permalink
fix: template resolution order
Browse files Browse the repository at this point in the history
  • Loading branch information
jondot committed Sep 7, 2022
1 parent 93def37 commit 47640e1
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## [6.2.10](https://github.com/jondot/hygen/compare/v6.2.9...v6.2.10) (2022-09-06)
## [6.2.11](https://github.com/jondot/hygen/compare/v6.2.10...v6.2.11) (2022-09-07)



2 changes: 1 addition & 1 deletion dist/config-resolver.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions dist/config-resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ const configResolver = new config_1.ConfigResolver('.hygen.js', {
load: (f) => __awaiter(void 0, void 0, void 0, function* () { return yield Promise.resolve().then(() => __importStar(require(f))); }),
none: (_) => ({}),
});
const resolve = (cwd, templates) => {
const resolve = (cwd, defaultTemplates, templatesOverride) => {
return [
path_1.default.resolve(cwd, templates),
templatesOverride && path_1.default.resolve(cwd, templatesOverride),
process.env.HYGEN_TMPLS,
path_1.default.resolve(cwd, '_templates'),
path_1.default.resolve(cwd, defaultTemplates),
].find((_) => _ && fs_extra_1.default.existsSync(_));
};
exports.default = (config) => __awaiter(void 0, void 0, void 0, function* () {
const { cwd, templates } = config;
const resolvedTemplates = resolve(cwd, templates);
const { cwd, templates: defaultTemplates, templatesOverride } = config;
const resolvedTemplates = resolve(cwd, defaultTemplates, templatesOverride);
return Object.assign(Object.assign(Object.assign({}, config), { templates: resolvedTemplates }), (yield configResolver.resolve(cwd)));
});
1 change: 1 addition & 0 deletions dist/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface RenderedAction {
export interface RunnerConfig {
exec?: (sh: string, body: string) => void;
templates?: string;
templatesOverride?: string;
cwd?: string;
logger?: Logger;
debug?: boolean;
Expand Down
2 changes: 1 addition & 1 deletion dist/types.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hygen",
"version": "6.2.10",
"version": "6.2.11",
"description": "The scalable code generator that saves you time.",
"keywords": [
"template",
Expand Down
20 changes: 18 additions & 2 deletions src/__tests__/config-resolver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,38 @@ const fixture = (dir) => path.join(__dirname, 'fixtures/templates', dir)
const templateParams = ({
cwd,
templates,
templatesOverride,
}: {
cwd: string
templates: string
templatesOverride?: string | undefined
}) => {
return {
cwd,
templates,
// eslint-disable-next-line
templatesOverride,
exec: () => {},
logger: new Logger(console.log),
logger: new Logger(console.log), // eslint-disable-line no-console
debug: false,
helpers: {},
createPrompter: () => require('enquirer'),
}
}
describe('resolve', () => {
it('template overrides takes over all', async () => {
expect(
(
await templateResolver(
templateParams({
cwd: '/1',
templates: fixture('app'),
templatesOverride: fixture('app-custom/other-templates'),
}),
)
).templates,
).toEqual(fixture('app-custom/other-templates'))
})

it('templates explicitly given via config, so take it if it exists', async () => {
expect(
(
Expand Down
13 changes: 9 additions & 4 deletions src/config-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,23 @@ const configResolver = new ConfigResolver('.hygen.js', {
none: (_) => ({}),
})

const resolve = (cwd: string, templates: string | undefined) => {
const resolve = (
cwd: string,
defaultTemplates: string | undefined,
templatesOverride: string | undefined,
) => {
return [
path.resolve(cwd, templates),
templatesOverride && path.resolve(cwd, templatesOverride),
process.env.HYGEN_TMPLS,
path.resolve(cwd, '_templates'),
path.resolve(cwd, defaultTemplates),
].find((_) => _ && fs.existsSync(_))
}

export default async (config: RunnerConfig): Promise<RunnerConfig> => {
const { cwd, templates } = config
const { cwd, templates: defaultTemplates, templatesOverride } = config

const resolvedTemplates = resolve(cwd, templates)
const resolvedTemplates = resolve(cwd, defaultTemplates, templatesOverride)

return {
...config,
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface RenderedAction {
export interface RunnerConfig {
exec?: (sh: string, body: string) => void
templates?: string
templatesOverride?: string
cwd?: string
logger?: Logger
debug?: boolean
Expand Down

0 comments on commit 47640e1

Please sign in to comment.