diff --git a/packages/generators/__tests__/utils/entry.test.ts b/packages/generators/__tests__/utils/entry.test.ts new file mode 100644 index 00000000000..52e0d465377 --- /dev/null +++ b/packages/generators/__tests__/utils/entry.test.ts @@ -0,0 +1,88 @@ +jest.setMock('@webpack-cli/webpack-scaffold', { + Input: jest.fn(), + InputValidate: jest.fn(), +}); + +import { Input, InputValidate } from '@webpack-cli/webpack-scaffold'; +import entry from '../../lib/utils/entry'; + +describe('entry', () => { + const InputMock = Input as jest.Mock; + const InputValidateMock = InputValidate as jest.Mock; + + it('handles single entry empty string', async () => { + InputMock.mockReturnValue({ + singularEntry: '', + }); + expect(await entry(null, false, null)).toEqual(''); + }); + + it('handles single entry path', async () => { + InputMock.mockReturnValue({ + singularEntry: 'path/to/index', + }); + expect(await entry(null, false, null)).toEqual('\'./path/to/index.js\''); + }); + + it('handles single entry path with js extension', async () => { + InputMock.mockReturnValue({ + singularEntry: 'path/to/index.js', + }); + expect(await entry(null, false, null)).toEqual('\'./path/to/index.js\''); + }); + + it('handles single entry relative path', async () => { + InputMock.mockReturnValue({ + singularEntry: './path/to/index', + }); + expect(await entry(null, false, null)).toEqual('\'./path/to/index.js\''); + }); + + it('handles multiple entry paths', async () => { + let calls = 0; + InputValidateMock.mockImplementation(() => { + calls++; + if (calls === 1) { + return { + multipleEntries: 'test1, test2', + }; + } else if (calls === 2) { + return { + test1: 'src/test1', + }; + } else { + return { + test2: 'src/test2', + }; + } + }); + expect(await entry(null, true, null)).toEqual({ + test1: '\'./src/test1.js\'', + test2: '\'./src/test2.js\'', + }); + }); + + it('handles multiple entry paths with varying formats', async () => { + let calls = 0; + InputValidateMock.mockImplementation(() => { + calls++; + if (calls === 1) { + return { + multipleEntries: ' test1 , test2 ', + }; + } else if (calls === 2) { + return { + test1: './path/to/test1', + }; + } else { + return { + test2: 'src/test2.js', + }; + } + }); + expect(await entry(null, true, null)).toEqual({ + test1: '\'./path/to/test1.js\'', + test2: '\'./src/test2.js\'', + }); + }); +}); diff --git a/packages/generators/src/init-generator.ts b/packages/generators/src/init-generator.ts index 7a07f7a122a..352c1aa76a0 100644 --- a/packages/generators/src/init-generator.ts +++ b/packages/generators/src/init-generator.ts @@ -289,7 +289,7 @@ export default class InitGenerator extends Generator { this.destinationPath("index.html"), {} ); - this.fs.copyTpl(path.resolve(__dirname, './templates/sw.js'), this.destinationPath('sw.js'), {}); + this.fs.copyTpl(path.resolve(__dirname, '../templates/sw.js'), this.destinationPath('sw.js'), {}); } // Generate tsconfig diff --git a/packages/generators/src/utils/entry.ts b/packages/generators/src/utils/entry.ts index 10321a1ad60..91c7eb1a080 100644 --- a/packages/generators/src/utils/entry.ts +++ b/packages/generators/src/utils/entry.ts @@ -21,40 +21,21 @@ export default async function entry( multiEntries: boolean, autoGenerateDefaults = false ): Promise { - const webpackEntryPoint: object = {}; - // TODO: refactoring to async/await - async function forEachPromise( - entries: string[], - fn: (entryProp: string) => Promise - ): Promise { - return entries.reduce((promise: Promise<{}>, prop: string): object => { - const trimmedProp: string = prop.trim(); - - return promise.then( - (n: object): Promise => { - if (n) { - Object.keys(n).forEach((val: string): void => { - if ( - n[val].charAt(0) !== "(" && - n[val].charAt(0) !== "[" && - !n[val].includes("function") && - !n[val].includes("path") && - !n[val].includes("process") - ) { - n[val] = `\'./${n[val].replace(/"|'/g, "").concat(".js")}\'`; - } - webpackEntryPoint[val] = n[val]; - }); - } else { - n = {}; - } - return fn(trimmedProp); - } - ); - }, Promise.resolve()); - } + const fixEntry = (entry: string): string => { + entry = entry.trim().replace(/"|'/g, ""); + if (!entry.startsWith("./")) { + entry = `./${entry}`; + } + if (!entry.endsWith(".js")) { + entry = entry.concat(".js"); + } + entry = `'${entry}'`; + return entry; + }; if (multiEntries) { + const webpackEntryPoint: object = {}; + const multipleEntriesAnswer = await InputValidate( self, "multipleEntries", @@ -65,35 +46,21 @@ export default async function entry( ); const entryIdentifiers: string[] = multipleEntriesAnswer.multipleEntries.split(","); - - const entryPropAnswer = await forEachPromise( - entryIdentifiers, - (entryProp: string): Promise => { - return InputValidate( - self, - `${entryProp}`, - `What is the location of "${entryProp}"?`, - validate, - `src/${entryProp}`, - autoGenerateDefaults - ); - } - ); - const remainingEntryPropKeys = Object.keys(entryPropAnswer); - if (remainingEntryPropKeys.length > 0) { - remainingEntryPropKeys.forEach((val: string): void => { - if ( - entryPropAnswer[val].charAt(0) !== "(" && - entryPropAnswer[val].charAt(0) !== "[" && - !entryPropAnswer[val].includes("function") && - !entryPropAnswer[val].includes("path") && - !entryPropAnswer[val].includes("process") - ) { - entryPropAnswer[val] = `\'./${entryPropAnswer[val].replace(/"|'/g, "").concat(".js")}\'`; - } - webpackEntryPoint[val] = entryPropAnswer[val]; - }); + + for (let i = 0; i < entryIdentifiers.length; i++) { + const entryProp = entryIdentifiers[i].trim(); + const entryResult = await InputValidate( + self, + `${entryProp}`, + `What is the location of "${entryProp}"?`, + validate, + `src/${entryProp}`, + autoGenerateDefaults + ); + const entry = fixEntry(entryResult[entryProp]); + webpackEntryPoint[entryProp] = entry; } + return webpackEntryPoint; } const singleEntryResult = await Input( @@ -104,6 +71,8 @@ export default async function entry( autoGenerateDefaults ); let { singularEntry } = singleEntryResult; - singularEntry = `\'./${singularEntry.replace(/"|'/g, "").concat(".js")}\'`; + if (singularEntry.length > 0) { + singularEntry = fixEntry(singularEntry); + } return singularEntry; }