Skip to content

Commit

Permalink
feat(package): node 12 and esm migration
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Oct 9, 2021
1 parent 6ff2664 commit c86bb54
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 64 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ $ yarn add --dev requirements

## setup

Scaffold a new `requirements.config.js` configuration file
Scaffold a new `requirements.config.mjs` configuration file

```bash
$ npx requirements --init
```

## config

Configure the `requirements.config.js` file in your project root.
Configure the `requirements.config.mjs` file in your project root.

```js
module.exports = {
export default {
software: {
node: '*',
yarn: '~1.17.3',
Expand All @@ -53,7 +53,7 @@ module.exports = {

## check requirements

Run `requirements` command in the project root. By default it will try to find the `requirements.config.js` file.
Run `requirements` command in the project root. By default it will try to find the `requirements.config.mjs` file.

```bash
$ npx requirements
Expand All @@ -75,9 +75,9 @@ $ npx requirements --help
Options:
--help, -h Show help [boolean]
--version, -v Show version number [boolean]
--init, -i Create a requirements.config.js file
--init, -i Create a requirements.config.mjs file
--config, -c Path to the configuration file
[default: "requirements.config.js"]
[default: "requirements.config.mjs"]
--force, -f Succeeds even if not all requirements are satisfied
[boolean] [default: false]
--quiet, -q Only output when errors are present [boolean]
Expand Down Expand Up @@ -114,7 +114,7 @@ checkSoftware() returns an Array with results
```bash
# test functionality
yarn build
node bin/requirements.js --config tests/requirements.config.js
node bin/requirements.js --config tests/requirements.config.mjs

# unit tests
yarn test
Expand All @@ -124,4 +124,4 @@ yarn test

The MIT License (MIT)

Copyright (c) 2017-2020 Steven Chim
Copyright (c) 2017-2021 Steven Chim
4 changes: 2 additions & 2 deletions bin/requirements.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#! /usr/bin/env node

const chalk = require('chalk');
const { exec } = require('../dist/bin');
import chalk from 'chalk';
import { exec } from '../dist/bin.js';

(async () => {
try {
Expand Down
12 changes: 10 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
module.exports = {
preset: 'ts-jest',
export default {
preset: 'ts-jest/presets/default-esm',
testEnvironment: 'node',
testPathIgnorePatterns: ['<rootDir>/dist/'],
collectCoverageFrom: ['src/*.ts'],
globals: {
'ts-jest': {
useESM: true,
},
},
moduleNameMapper: {
'^(\\.{1,2}/.*)\\.js$': '$1',
}
};
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
"checker"
],
"engines": {
"node": ">=12.0.0"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"main": "./dist/index.js",
"type": "module",
"exports": "./dist/index.js",
"types": "dist/index.d.ts",
"files": [
"bin",
Expand All @@ -34,8 +35,8 @@
"build": "tsc",
"lint": "prettier --check \"**/*.{js,ts,md}\" || echo '🔧 Run: \"npm run lint:fix\" to fix issues'",
"lint:fix": "prettier --write \"**/*.{js,ts,md}\"",
"test": "jest",
"coverage": "jest --coverage --coverageReporters=lcov",
"test": "NODE_OPTIONS=--experimental-vm-modules jest",
"coverage": "NODE_OPTIONS=--experimental-vm-modules jest --coverage --coverageReporters=lcov",
"prepare": "yarn clean && yarn test && yarn build",
"gitignore:update": "curl -L https://www.toptal.com/developers/gitignore/api/node --silent > .temp && cat tools/dotgitignore | cat - .temp > .gitignore && rm .temp"
},
Expand Down
13 changes: 7 additions & 6 deletions src/__tests__/bin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import fs from 'fs';
import { jest } from '@jest/globals'
import { exec } from '../bin';

describe('bin', () => {
Expand All @@ -12,31 +13,31 @@ describe('bin', () => {
});

it('should execute ok tests', async () => {
await exec({ config: './tests/ok_every.config.js' });
await exec({ config: './tests/ok_every.config.mjs' });
expect(logSpy).toHaveBeenNthCalledWith(1, '🔍 Checking software requirements...');
expect(logSpy).toHaveBeenNthCalledWith(3, '✅ All is well!');
});

it('should execute nok tests', async () => {
try {
await exec({ config: './tests/ok_some.config.js' });
await exec({ config: './tests/ok_some.config.mjs' });
} catch (err) {
expect(err).toMatchInlineSnapshot(`[Error: ❌ Not all requirements are satisfied]`);
}
});

it('should execute nok --force tests', async () => {
await exec({ config: './tests/ok_some.config.js', force: true });
await exec({ config: './tests/ok_some.config.mjs', force: true });
expect(logSpy).toHaveBeenNthCalledWith(3, '⚠️ Not all requirements are satisfied (--force)');
});

it('should print debug data with --debug', async () => {
await exec({ config: './tests/ok_every.config.js', debug: true });
await exec({ config: './tests/ok_every.config.mjs', debug: true });
expect(debugSpy).toHaveBeenCalled();
});

it('should scaffold with --init', async () => {
const filePath = './requirements.config.js';
const filePath = './requirements.config.mjs';

await exec({ init: true });
expect(fs.existsSync(filePath)).toBe(true);
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/requirements.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SoftwareConfiguration } from '../types';
import { normalizeConfig, checkSoftware } from '../requirements';
import type { SoftwareConfiguration } from '../types';
import { normalizeConfig, checkSoftware } from '../requirements.js';

describe('requirements', () => {
describe('checkSoftware()', () => {
Expand Down
5 changes: 2 additions & 3 deletions src/__tests__/results.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as fs from 'fs';
import { isAllOK, getMessages } from '../results';
import { RawResult } from '../types';
import { isAllOK, getMessages } from '../results.js';
import type { RawResult } from '../types';

describe('results', () => {
describe('all results OK', () => {
Expand Down
32 changes: 18 additions & 14 deletions src/bin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import * as yargs from 'yargs';
import * as path from 'path';
import * as chalk from 'chalk';
import { checkSoftware } from './requirements';
import { renderTable, renderMessages } from './reporter';
import { Configuration } from './types';
import { scaffold } from './scaffold';
import { isAllOK, getMessages } from './results';
import yargs from 'yargs';
import path from 'path';
import chalk from 'chalk';
import { checkSoftware } from './requirements.js';
import { renderTable, renderMessages } from './reporter.js';
import type { Configuration } from './types';
import { scaffold } from './scaffold.js';
import { isAllOK, getMessages } from './results.js';

import { createRequire } from "module";
const require = createRequire(import.meta.url);

export async function exec(_debug_argv_?) {
const argv = _debug_argv_ ?? getArgv();
Expand All @@ -19,7 +22,7 @@ export async function exec(_debug_argv_?) {
console.log(`🔍 Checking software requirements...`);
}

const config = getConfiguration(argv);
const config = await getConfiguration(argv);
let rawResults = await checkSoftware(config.software);

const ALL_OK = isAllOK(rawResults);
Expand Down Expand Up @@ -52,19 +55,19 @@ export async function exec(_debug_argv_?) {
}

function getArgv() {
return yargs
return yargs(process.argv)
.help('help')
.alias('help', 'h')
.version('version', require('../package.json').version)
.alias('version', 'v')
.options({
init: {
description: 'Create a requirements.config.js file',
description: 'Create a requirements.config.mjs file',
alias: 'i',
},
config: {
description: 'Path to the configuration file',
default: 'requirements.config.js',
default: 'requirements.config.mjs',
alias: 'c',
},
force: {
Expand All @@ -85,7 +88,7 @@ function getArgv() {
}).argv;
}

function getConfiguration(argv): Configuration {
async function getConfiguration(argv): Promise<Configuration> {
const cwd = process.cwd();
const configPath = argv.config;
let pathConfiguration;
Expand All @@ -101,7 +104,8 @@ function getConfiguration(argv): Configuration {
}

try {
return require(pathConfiguration);
const config = await import(pathConfiguration);
return config.default;
} catch (err) {
throw new Error(`❌ Unable to find configuration file: '${chalk.bold(pathConfiguration)}'`);
}
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkSoftware } from './requirements';
import { renderTable } from './reporter';
import { checkSoftware } from './requirements.js';
import { renderTable } from './reporter.js';

export { checkSoftware, renderTable };
6 changes: 3 additions & 3 deletions src/reporter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { table, getBorderCharacters } from 'table';
import * as logSymbols from 'log-symbols';
import * as chalk from 'chalk';
import { RawResult, Message } from './types';
import logSymbols from 'log-symbols';
import chalk from 'chalk';
import type { RawResult, Message } from './types';

export function renderMessages(messages: Message[] = []): string {
const result = messages.map(({ bin, message }) => {
Expand Down
6 changes: 3 additions & 3 deletions src/requirements.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as binVersion from 'bin-version';
import * as semver from 'semver';
import { RawResult, SoftwareConfiguration } from './types';
import binVersion from 'bin-version';
import semver from 'semver';
import type { RawResult, SoftwareConfiguration } from './types';

export async function checkSoftware(software: SoftwareConfiguration = {}): Promise<RawResult[]> {
const softwareList = normalizeConfig(software);
Expand Down
2 changes: 1 addition & 1 deletion src/results.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RawResult, Message } from './types';
import type { RawResult, Message } from './types';

export function isAllOK(rawResults: RawResult[]) {
return rawResults
Expand Down
12 changes: 6 additions & 6 deletions src/scaffold.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as fs from 'fs';
import * as semver from 'semver';
import * as chalk from 'chalk';
import fs from 'fs';
import semver from 'semver';
import chalk from 'chalk';

const FILENAME = 'requirements.config.js';
const FILENAME = 'requirements.config.mjs';

const TEMPLATE = `module.exports = {
const TEMPLATE = `export default {
software: {
node: '^${semver.clean(process.version)}',
nginx: {
Expand All @@ -22,7 +22,7 @@ const TEMPLATE = `module.exports = {

export function scaffold() {
if (hasExistingConfig()) {
throw new Error(`init failed. Found existing 'requirements.config.js'`);
throw new Error(`init failed. Found existing 'requirements.config.mjs'`);
}

fs.writeFileSync(FILENAME, TEMPLATE);
Expand Down
2 changes: 1 addition & 1 deletion tests/ok_every.config.js → tests/ok_every.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
software: {
node: '*',
},
Expand Down
2 changes: 1 addition & 1 deletion tests/ok_some.config.js → tests/ok_some.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
export default {
software: {
node: '*',
httpd: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const chalk = require('chalk');
import chalk from 'chalk';

module.exports = {
export default {
software: {
// java: '>= 1.8.x',
git: '~1.9.4 || 2.0.0 - 2.10.0',
Expand Down
9 changes: 5 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"compilerOptions": {
"outDir": "./dist",
"lib": ["es2015"],
"module": "commonjs",
"lib": ["ES2019"],
"module": "ES2020",
"moduleResolution": "node",
"target": "es2015",
"declaration": true
"target": "ES2019",
"declaration": true,
"allowSyntheticDefaultImports": true
},
"include": ["./src"],
"exclude": ["./dist"]
Expand Down

0 comments on commit c86bb54

Please sign in to comment.