diff --git a/src/Elder.ts b/src/Elder.ts index be997328..fb496b38 100644 --- a/src/Elder.ts +++ b/src/Elder.ts @@ -136,13 +136,13 @@ class Elder { /** * Finalize hooks - * Import User Hooks.js + * Import User defined Hooks * Validate Hooks * Filter out hooks that are disabled. */ let hooksJs: Array = []; - const hookSrcPath = path.resolve(this.settings.srcDir, './hooks.js'); + const hookSrcPath = path.resolve(this.settings.srcDir, './hooks'); try { const hooksReq = require(hookSrcPath); @@ -182,12 +182,12 @@ class Elder { /** * Finalize Shortcodes - * Import User Shortcodes.js + * Import User defined Shortcodes * Validate Shortcodes */ let shortcodesJs: ShortcodeDefs = []; - const shortcodeSrcPath = path.resolve(this.settings.srcDir, './shortcodes.js'); + const shortcodeSrcPath = path.resolve(this.settings.srcDir, './shortcodes'); try { const shortcodeReq = require(shortcodeSrcPath); diff --git a/src/__tests__/Elder.spec.ts b/src/__tests__/Elder.spec.ts index 5a2fe8de..f21e8f72 100644 --- a/src/__tests__/Elder.spec.ts +++ b/src/__tests__/Elder.spec.ts @@ -1,13 +1,3 @@ -process.cwd = () => 'test'; - -jest.mock('path', () => { - return { - resolve: (...strings) => strings.join('/').replace('./', '').replace('//', '/').replace('/./', '/'), - join: (...strings) => strings.join('/').replace('./', '').replace('//', '/').replace('/./', '/'), - posix: () => ({ dirname: () => '' }), - }; -}); - jest.mock('../routes/routes', () => () => ({ 'route-a': { hooks: [ @@ -85,9 +75,9 @@ describe('#Elder', () => { jest.mock('fs-extra', () => ({ existsSync: () => true, })); - jest.mock('test/___ELDER___/compiled/fakepath/Test.js', () => () => ({}), { virtual: true }); + jest.mock(`${process.cwd()}/test/___ELDER___/compiled/fakepath/Test.js`, () => () => ({}), { virtual: true }); jest.mock( - 'test/__ELDER__/hooks.js', + `${process.cwd()}/test/__ELDER__/hooks.js`, () => ({ default: [ { @@ -101,7 +91,7 @@ describe('#Elder', () => { { virtual: true }, ); jest.mock( - 'test/src/plugins/elder-plugin-upload-s3/index.js', + `${process.cwd()}/test/src/plugins/elder-plugin-upload-s3/index.js`, () => ({ hooks: [], routes: { 'test-a': { hooks: [], template: 'fakepath/Test.svelte', all: [] }, 'test-b': { data: () => {} } }, @@ -132,9 +122,9 @@ describe('#Elder', () => { jest.mock('fs-extra', () => ({ existsSync: () => true, })); - jest.mock('test/___ELDER___/compiled/fakepath/Test.js', () => () => ({}), { virtual: true }); + jest.mock(`${process.cwd()}/test/___ELDER___/compiled/fakepath/Test.js`, () => () => ({}), { virtual: true }); jest.mock( - 'test/src/hooks.js', + `${process.cwd()}/test/src/hooks`, () => ({ default: [ { @@ -148,7 +138,7 @@ describe('#Elder', () => { { virtual: true }, ); jest.mock( - 'test/src/plugins/elder-plugin-upload-s3/index.js', + `${process.cwd()}/test/src/plugins/elder-plugin-upload-s3/index.js`, () => ({ hooks: [ { diff --git a/src/__tests__/hooks.spec.ts b/src/__tests__/hooks.spec.ts index a390d307..d4c4b6a6 100644 --- a/src/__tests__/hooks.spec.ts +++ b/src/__tests__/hooks.spec.ts @@ -17,13 +17,6 @@ jest.mock('../utils/Page', () => { })); }); -process.cwd = () => 'test'; - -jest.mock('path', () => ({ - resolve: (...strings) => strings.join('/').replace('./', '').replace('//', '/'), - posix: () => ({ dirname: () => '' }), -})); - jest.mock('fs-extra', () => ({ writeJSONSync: jest.fn(), outputFileSync: jest @@ -148,7 +141,7 @@ describe('#hooks', () => { const hook = hooks.find((h) => h.name === 'elderConsoleLogErrors'); expect( await hook.run({ settings: { worker: false }, request: { permalink: '/foo' }, errors: ['foo', 'bar'] }), - ).toBe(undefined); + ).toBeUndefined(); }); it('elderWriteHtmlFileToPublic', async () => { const hook = hooks.find((h) => h.name === 'elderWriteHtmlFileToPublic'); @@ -159,21 +152,21 @@ describe('#hooks', () => { errors: [], settings: {}, }), - ).toBe(null); + ).toBeNull(); expect( await hook.run({ request: { permalink: '/foo' }, htmlString: 'string', errors: [], - settings: { build: './build', locations: { public: './public' } }, + settings: { build: './build', locations: { public: './public' }, distDir: process.cwd() }, }), - ).toBe(null); + ).toBeNull(); expect( await hook.run({ request: { permalink: '/foo' }, htmlString: 'string', errors: [], - settings: { build: './build', locations: { public: './public' } }, + settings: { build: './build', locations: { public: './public' }, distDir: process.cwd() }, }), ).toEqual({ errors: [new Error('Failed to write')] }); }); @@ -188,7 +181,7 @@ describe('#hooks', () => { ], settings: { debug: { performance: true } }, }), - ).toBe(undefined); + ).toBeUndefined(); }); it('elderShowParsedBuildTimes', async () => { const hook = hooks.find((h) => h.name === 'elderShowParsedBuildTimes'); @@ -206,15 +199,15 @@ describe('#hooks', () => { ], settings: { debug: { performance: true } }, }), - ).toBe(undefined); + ).toBeUndefined(); }); it('elderWriteBuildErrors', async () => { const hook = hooks.find((h) => h.name === 'elderWriteBuildErrors'); expect( await hook.run({ errors: ['error1', 'error2'], - settings: { debug: { performance: true } }, + settings: { debug: { performance: true }, rootDir: process.cwd() }, }), - ).toBe(undefined); + ).toBeUndefined(); }); }); diff --git a/src/routes/routes.ts b/src/routes/routes.ts index e7d73f6a..b10a7b10 100644 --- a/src/routes/routes.ts +++ b/src/routes/routes.ts @@ -1,5 +1,6 @@ /* eslint-disable global-require */ /* eslint-disable import/no-dynamic-require */ +import * as path from 'path'; import glob from 'glob'; import kebabcase from 'lodash.kebabcase'; import type { RouteOptions } from './types'; @@ -16,23 +17,24 @@ function routes(settings: SettingsOptions) { `, ); - const files = glob.sync(`${settings.srcDir}/routes/*/+(*.js|*.svelte)`); + const files = glob.sync(`${settings.srcDir}/routes/*/+(*.js|*.ts|*.svelte)`); const ssrFolder = settings.$$internal.ssrComponents; const ssrComponents = glob.sync(`${ssrFolder}/*.js`); - const routejsFiles = files.filter((f) => f.endsWith('/route.js')); + const routeJsRegex = /route.t|js$/; + const routejsFiles = files.filter((f) => routeJsRegex.test(f)); const output = routejsFiles.reduce((out, cv) => { - const routeName = cv.replace('/route.js', '').split('/').pop(); + const routeName = path.dirname(cv).split('/').pop(); const capitalizedRoute = capitalizeFirstLetter(routeName); const routeReq = require(cv); const route: RouteOptions = routeReq.default || routeReq; const filesForThisRoute = files .filter((r) => r.includes(`/routes/${routeName}`)) - .filter((r) => !r.includes('route.js')); + .filter((r) => !routeJsRegex.test(r)); if (!route.permalink) { if (settings.debug.automagic) {