Skip to content
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

fix(generators): fix and refactor entry util, add tests #1392

Merged
merged 3 commits into from
Apr 1, 2020
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
88 changes: 88 additions & 0 deletions packages/generators/__tests__/utils/entry.test.ts
Original file line number Diff line number Diff line change
@@ -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\'',
});
});
});
2 changes: 1 addition & 1 deletion packages/generators/src/init-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
91 changes: 30 additions & 61 deletions packages/generators/src/utils/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,21 @@ export default async function entry(
multiEntries: boolean,
autoGenerateDefaults = false
): Promise<void | {}> {
const webpackEntryPoint: object = {};
// TODO: refactoring to async/await
async function forEachPromise(
entries: string[],
fn: (entryProp: string) => Promise<void | {}>
): Promise<object | {}> {
return entries.reduce((promise: Promise<{}>, prop: string): object => {
const trimmedProp: string = prop.trim();

return promise.then(
(n: object): Promise<void | {}> => {
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",
Expand All @@ -65,35 +46,21 @@ export default async function entry(
);

const entryIdentifiers: string[] = multipleEntriesAnswer.multipleEntries.split(",");

const entryPropAnswer = await forEachPromise(
entryIdentifiers,
(entryProp: string): Promise<void | {}> => {
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(
Expand All @@ -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;
}