Skip to content
Permalink
Browse files

Fix: Point to right entry point

If the user creates a new individual hint, `package.json` will point to
`dist/src/hint.js` instead of `dist/src/index.js` and will no longer
create an `index.js`. This is consistent with the format of the other
hints.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

Fix #1916
Close #2171
  • Loading branch information...
molant committed Apr 3, 2019
1 parent ee6926d commit 2869a75c81b616ebfedbcaf85d0e8affa05e6ade
Showing with 28 additions and 15 deletions.
  1. +25 −12 packages/create-hint/src/create-hint.ts
  2. +3 −3 packages/create-hint/tests/create-hint.ts
@@ -142,18 +142,20 @@ class HintPackage {
this.normalizedName = normalizeStringByDelimiter(data.name, '-');
this.description = escapeSafeString(data.description);
this.official = data.official;
this.packageMain = `dist/src/index.js`; // package.json#main

const prefix = this.official ? '@hint/hint' : 'hint'; // package.json#name

this.packageName = `${prefix}-${this.normalizedName}`;
this.hints = [];

if (this.isMulti) {
this.packageMain = `dist/src/index.js`; // package.json#main

(data.hints as inquirer.Answers[]).forEach((hint) => {
this.hints.push(new NewHint(hint, this.normalizedName));
});
} else {
this.packageMain = `dist/src/hint.js`; // package.json#main
this.hints.push(new NewHint(data));
}

@@ -315,10 +317,6 @@ const copyFiles = async (origin: string, destination: string) => {
/** Copies and processes the required files for a hint package (multi or not). */
const generateHintFiles = async (destination: string, data: HintPackage) => {
const commonFiles = [
{
destination: join(destination, 'src', `index.ts`),
path: join(__dirname, TEMPLATE_PATH, 'index.ts.hbs')
},
{
destination: join(destination, 'README.md'),
path: join(__dirname, TEMPLATE_PATH, 'readme.md.hbs')
@@ -332,6 +330,17 @@ const generateHintFiles = async (destination: string, data: HintPackage) => {
path: join(__dirname, SHARED_TEMPLATE_PATH, 'package.hbs')
}];

/**
* `index.ts` is only necessary if we have multiple files. Otherwise
* `package.json#main` points to `hint.ts`
*/
if (data.isMulti) {
commonFiles.push({
destination: join(destination, 'src', `index.ts`),
path: join(__dirname, TEMPLATE_PATH, 'index.ts.hbs')
});
}

if (!data.official) {
commonFiles.push({
destination: join(destination, '.hintrc'),
@@ -381,13 +390,17 @@ const generateHintFiles = async (destination: string, data: HintPackage) => {

for (const hint of data.hints) {
const [hintContent, testContent, metaContent] = await Promise.all([compileTemplate(hintFile.path, hint), compileTemplate(testFile.path, hint), compileTemplate(metaFile.path, hint)]);

// e.g.: hint-ssllabs/src/ssllabs.ts
const hintPath = join(hintFile.destination, `${hint.normalizedName}.ts`);
// e.g.: hint-ssllabs/tests/ssllabs.ts
const testPath = join(testFile.destination, `${hint.normalizedName}.ts`);
// e.g.: hint-ssllabs/src/meta/is-valid.ts or hint-ssllabs/src/meta.ts
const metaPath = data.isMulti ? join(metaFile.destination, 'meta', `${hint.normalizedName}.ts`) : join(metaFile.destination, 'meta.ts');
const { metaPath, hintPath, testPath } = data.isMulti ?
{
hintPath: join(hintFile.destination, `${hint.normalizedName}.ts`),
metaPath: join(metaFile.destination, 'meta', `${hint.normalizedName}.ts`),
testPath: join(testFile.destination, `${hint.normalizedName}.ts`)
} :
{
hintPath: join(hintFile.destination, 'hint.ts'),
metaPath: join(metaFile.destination, 'meta.ts'),
testPath: join(testFile.destination, `tests.ts`)
};

await Promise.all([mkdirpAsync(dirname(hintPath)), mkdirpAsync(dirname(testPath)), mkdirpAsync(dirname(metaPath))]);

@@ -121,9 +121,9 @@ test('It creates a hint if the option multiple hints is false', async (t) => {
t.true(fsExtraCopyStub.args[0][0].endsWith('files'), 'Unexpected path for official files');
t.is(fsExtraCopyStub.args[0][1], path.join(root, 'hint-awesome-hint'), 'Copy path is not the expected one');

// index.ts, package.json, readme.md, tsconfig.json, hint.ts, meta.ts, tests/hint.ts
t.is(handlebarsCompileTemplateStub.callCount, 7, `Handlebars doesn't complile the right number of files`);
t.is(miscWriteFileAsyncStub.callCount, 7, 'Invalid number of files created');
// package.json, readme.md, tsconfig.json, hint.ts, meta.ts, tests/hint.ts
t.is(handlebarsCompileTemplateStub.callCount, 6, `Handlebars doesn't complile the right number of files`);
t.is(miscWriteFileAsyncStub.callCount, 6, 'Invalid number of files created');

t.true(result);

0 comments on commit 2869a75

Please sign in to comment.
You can’t perform that action at this time.