Skip to content

Commit

Permalink
Fix: Load latest versions from package in create-hint templates
Browse files Browse the repository at this point in the history
Fix #2657
Close #2783
  • Loading branch information
jaspreet57 authored and antross committed Aug 5, 2019
1 parent 93322e5 commit e84e4c9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 21 deletions.
2 changes: 2 additions & 0 deletions packages/create-hint/package.json
Expand Up @@ -12,6 +12,8 @@
"dependencies": {},
"description": "webhint's hint initializer package",
"devDependencies": {
"@hint/utils-tests-helpers": "^5.0.3",
"@types/debug": "^4.1.4",
"@typescript-eslint/eslint-plugin": "^1.13.0",
"@typescript-eslint/parser": "^1.12.0",
"@types/mkdirp": "^0.5.2",
Expand Down
14 changes: 6 additions & 8 deletions packages/create-hint/src/handlebars-utils.ts
@@ -1,23 +1,21 @@
import * as Handlebars from 'handlebars';

import { fs } from '@hint/utils';
import { utils } from 'hint';
import { loadCreateHintPackage } from './utils';

const { packages: { loadHintPackage } } = utils;
const { readFileAsync } = fs;

const pkg = loadHintPackage();
const pkg = loadCreateHintPackage();

/**
* Searches the current version used for a package in `hint` and uses that version or the `defaultVersion`.
* Searches package version in `create-hint/package.json` for given `packageName` and uses that version or the `defaultVersion`.
*
* It is recommended to add package as `devDependencies` in `create-hint/package.json` whenever new `package` is added to `package.hbs` template.
*
* This is used when creating a new hint via the CLI to make sure the dependencies are up-to-date in the moment
* of creation.
*/
Handlebars.registerHelper('dependencyVersion', (packageName, defaultVersion): string => {
const version = packageName === 'hint' ?
`^${pkg.version}` :
pkg.dependencies[packageName] ||
const version = pkg.dependencies[packageName] ||
pkg.devDependencies[packageName] ||
defaultVersion;

Expand Down
26 changes: 13 additions & 13 deletions packages/create-hint/src/shared-templates/package.hbs
Expand Up @@ -8,20 +8,20 @@
},
"description": "{{description}}",
"devDependencies": {
{{{dependencyVersion "@hint/utils-tests-helpers" "^2.0.3"}}},
{{{dependencyVersion "@types/debug" "0.0.31"}}},
{{{dependencyVersion "@types/node" "11.12.0"}}},
{{{dependencyVersion "@typescript-eslint/eslint-plugin" "^1.3.0"}}},
{{{dependencyVersion "@typescript-eslint/parser" "^1.3.0"}}},
{{{dependencyVersion "ava" "^1.2.0"}}},
{{{dependencyVersion "cpx" "^1.5.0"}}},
{{{dependencyVersion "eslint" "^5.13.0"}}},
{{{dependencyVersion "eslint-plugin-markdown" "^1.0.0"}}},
{{{dependencyVersion "npm-run-all" "^4.1.5"}}},
{{{dependencyVersion "nyc" "^13.1.0"}}},
{{{dependencyVersion "rimraf" "^2.6.3"}}},
{{{dependencyVersion "@hint/utils-tests-helpers"}}},
{{{dependencyVersion "@types/debug"}}},
{{{dependencyVersion "@types/node"}}},
{{{dependencyVersion "@typescript-eslint/eslint-plugin"}}},
{{{dependencyVersion "@typescript-eslint/parser"}}},
{{{dependencyVersion "ava"}}},
{{{dependencyVersion "cpx"}}},
{{{dependencyVersion "eslint"}}},
{{{dependencyVersion "eslint-plugin-markdown"}}},
{{{dependencyVersion "npm-run-all"}}},
{{{dependencyVersion "nyc"}}},
{{{dependencyVersion "rimraf"}}},
{{{dependencyVersion "hint"}}},
{{{dependencyVersion "typescript" "^3.3.1"}}}
{{{dependencyVersion "typescript"}}}
},
"engines": {
"node": ">=8.0.0"
Expand Down
16 changes: 16 additions & 0 deletions packages/create-hint/src/utils.ts
@@ -0,0 +1,16 @@
import { packages } from '@hint/utils';

const { findPackageRoot } = packages;

/** Returns an object that represents the `package.json` version of `create-hint` */
export const loadCreateHintPackage = () => {
// webpack will embed the package.json
/* istanbul ignore if */
if (process.env.webpack) { // eslint-disable-line no-process-env
return require('../package.json');
}

const pkgRoot = findPackageRoot(__dirname, 'package.json');

return require(`${pkgRoot}/package.json`);
};

0 comments on commit e84e4c9

Please sign in to comment.