Skip to content

Commit

Permalink
(fix) Enforce Node version for tsdx create, update documentation to e…
Browse files Browse the repository at this point in the history
…xpress requirement. (#433)

* Check for Node version when running tsdx create to ensure compatible Node versions

* Add required Node version to docs
  • Loading branch information
lpolito authored and swyxio committed Jan 24, 2020
1 parent 1d8f123 commit 9f183e7
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 2 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ cd mylib
yarn start
```

_Requires Node `>= 10`._


That's it. You don't need to worry about setting up Typescript or Rollup or Jest or other plumbing. Just start editing `src/index.ts` and go!

Below is a list of commands you will probably find useful:
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,13 @@
"@types/react": "^16.9.11",
"@types/rollup-plugin-json": "^3.0.2",
"@types/rollup-plugin-sourcemaps": "^0.4.2",
"@types/semver": "^6.2.0",
"doctoc": "^1.4.0",
"husky": "^3.0.9",
"pretty-quick": "^2.0.0",
"ps-tree": "^1.2.0",
"react": "^16.8.6"
"react": "^16.8.6",
"semver": "^7.1.1"
},
"husky": {
"hooks": {
Expand Down
18 changes: 17 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ import rimraf from 'rimraf';
import execa from 'execa';
import shell from 'shelljs';
import ora from 'ora';
import semver from 'semver';
import { paths } from './constants';
import * as Messages from './messages';
import { createBuildConfigs } from './createBuildConfigs';
import { createJestConfig } from './createJestConfig';
import { createEslintConfig } from './createEslintConfig';
import { resolveApp, safePackageName, clearConsole } from './utils';
import {
resolveApp,
safePackageName,
clearConsole,
getNodeEngineRequirement,
} from './utils';
import { concatAllArray } from 'jpjs';
import getInstallCmd from './getInstallCmd';
import getInstallArgs from './getInstallArgs';
Expand Down Expand Up @@ -226,6 +232,16 @@ prog
process.chdir(projectPath);
const safeName = safePackageName(pkg);
const pkgJson = generatePackageJson({ name: safeName, author });

const nodeVersionReq = getNodeEngineRequirement(pkgJson);
if (
nodeVersionReq &&
!semver.satisfies(process.version, nodeVersionReq)
) {
bootSpinner.fail(Messages.incorrectNodeVersion(nodeVersionReq));
process.exit(1);
}

await fs.outputJSON(path.resolve(projectPath, 'package.json'), pkgJson);
bootSpinner.succeed(`Created ${chalk.bold.green(pkg)}`);
await Messages.start(pkg);
Expand Down
6 changes: 6 additions & 0 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,9 @@ export const start = async function(projectName: string) {
${chalk.green('https://github.com/jaredpalmer/tsdx/issues')}
`;
};

export const incorrectNodeVersion = function(requiredVersion: string) {
return `Unsupported Node version! Your current Node version (${chalk.red(
process.version
)}) does not satisfy the requirement of Node ${chalk.cyan(requiredVersion)}.`;
};
3 changes: 3 additions & 0 deletions src/templates/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ const basicTemplate: Template = {
// module: `dist/${safeName}.esm.js`,
typings: `dist/index.d.ts`,
files: ['dist'],
engines: {
node: '>=10',
},
scripts: {
start: 'tsdx watch',
build: 'tsdx build',
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ export interface PackageJson {
eslint?: any;
dependencies?: { [packageName: string]: string };
devDependencies?: { [packageName: string]: string };
engines?: {
node?: string;
};
}
4 changes: 4 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ export function getReactVersion({
(devDependencies && devDependencies.react)
);
}

export function getNodeEngineRequirement({ engines }: PackageJson) {
return engines && engines.node;
}
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,11 @@
"@types/node" "*"
rollup "^0.63.4"

"@types/semver@^6.2.0":
version "6.2.0"
resolved "https://registry.yarnpkg.com/@types/semver/-/semver-6.2.0.tgz#d688d574400d96c5b0114968705366f431831e1a"
integrity sha512-1OzrNb4RuAzIT7wHSsgZRlMBlNsJl+do6UblR7JMW4oB7bbR+uBEYtUh7gEc/jM84GGilh68lSOokyM/zNUlBA==

"@types/shelljs@^0.8.5":
version "0.8.6"
resolved "https://registry.yarnpkg.com/@types/shelljs/-/shelljs-0.8.6.tgz#45193a51df99e0f00513c39a2152832399783221"
Expand Down Expand Up @@ -5802,6 +5807,11 @@ semver@^6.0.0, semver@^6.1.2, semver@^6.2.0, semver@^6.3.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
integrity sha512-b39TBaTSfV6yBrapU89p5fKekE2m/NwnDocOVruQFS1/veMgdzuPcnOM34M6CwxW8jH/lxEa5rBoDeUwu5HHTw==

semver@^7.1.1:
version "7.1.1"
resolved "https://registry.yarnpkg.com/semver/-/semver-7.1.1.tgz#29104598a197d6cbe4733eeecbe968f7b43a9667"
integrity sha512-WfuG+fl6eh3eZ2qAf6goB7nhiCd7NPXhmyFxigB/TOkQyeLP8w8GsVehvtGNtnNmyboz4TgeK40B1Kbql/8c5A==

serialize-javascript@^1.7.0:
version "1.9.1"
resolved "https://registry.yarnpkg.com/serialize-javascript/-/serialize-javascript-1.9.1.tgz#cfc200aef77b600c47da9bb8149c943e798c2fdb"
Expand Down

0 comments on commit 9f183e7

Please sign in to comment.