Skip to content

Commit

Permalink
feat: inline type from @types/node (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtgtybhertgeghgtwtg authored Aug 17, 2021
1 parent c5990bc commit cea68fd
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 51 deletions.
10 changes: 6 additions & 4 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,21 @@ module.exports = {
files: ['__mocks__/*.ts', 'scripts/jest/*.js', '.eslintrc.js'],
rules: {
// Mocks and some configuration files cannot be modules.
'unicorn/prefer-module': 'off'
}
}
'unicorn/prefer-module': 'off',
},
},
],
parserOptions: {
project: path.join(__dirname, './tsconfig.eslint.json'),
},
rules: {
'eslint-comments/no-unused-disable': 'error',
// Can't speak to `chalk`, but on the premise of the others, I disagree.
'unicorn/import-style': 'off',
// Since this is covered by 'eslint-comments/no-unlimited-disable'.
'unicorn/no-abusive-eslint-disable': 'off',
'unicorn/no-unsafe-regex': 'error',
// Does not work with TypeScript and is not backported to all supported versions of Node.
'unicorn/prefer-node-protocol': 'off'
'unicorn/prefer-node-protocol': 'off',
},
};
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@
"email": "wtgtybhertgeghgtwtg@gmail.com",
"name": "Matthew Fernando Garcia"
},
"dependencies": {
"@types/node": "^14.0.0"
},
"dependencies": {},
"description": "Load configuration from environmental variables and files.",
"devDependencies": {
"@arkweid/lefthook": "^0.7.6",
Expand All @@ -14,10 +12,9 @@
"@babel/preset-typescript": "^7.15.0",
"@commitlint/cli": "^13.1.0",
"@commitlint/config-conventional": "^13.1.0",
"@rollup/plugin-node-resolve": "^13.0.4",
"@rollup/plugin-typescript": "^8.2.5",
"@rushstack/eslint-config": "^2.4.0",
"@types/jest": "^27.0.0",
"@types/node": "^16.6.1",
"babel-jest": "^27.0.6",
"builtin-modules": "^3.2.0",
"cross-env": "^7.0.3",
Expand All @@ -29,7 +26,8 @@
"prettier": "2.3.2",
"rimraf": "^3.0.2",
"rollup": "^2.56.0",
"tslib": "^2.3.0",
"rollup-plugin-ts": "^1.4.0",
"tslib": "^2.3.1",
"typescript": "~4.3.5"
},
"engines": {
Expand Down Expand Up @@ -67,6 +65,6 @@
"test:dist": "jest --config scripts/jest/config.dist.js"
},
"sideEffects": false,
"types": "dist/source",
"types": "dist/index.d.ts",
"version": "0.0.0-semantic-release"
}
47 changes: 27 additions & 20 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
import nodeResolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import builtinModules from 'builtin-modules';
import {dependencies} from './package.json';
import ts from 'rollup-plugin-ts';
import {fileURLToPath} from 'url';
import packageJson from './package.json';

export default {
external: [...builtinModules, ...Object.keys(dependencies)],
input: 'source/index.ts',
output: [
{
dir: 'dist',
entryFileNames: '[name].[format].js',
format: 'cjs',
sourcemap: true,
},
{
dir: 'dist',
entryFileNames: '[name].[format].js',
format: 'es',
const external = [...builtinModules, ...Object.keys(packageJson.dependencies)];
const typePath = fileURLToPath(new URL(packageJson.types, import.meta.url));

function createConfig(isEs) {
return {
external,
input: 'source/index.ts',
output: {
file: isEs ? packageJson.module : packageJson.main,
format: isEs ? 'es' : 'cjs',
sourcemap: true,
},
],
plugins: [nodeResolve({extensions: ['.ts']}), typescript()],
};
plugins: [
ts({
hook: {
outputPath: (path, kind) =>
kind === 'declaration' ? typePath : path,
},
tsconfig: (resolvedConfig) =>
isEs ? resolvedConfig : {...resolvedConfig, declaration: false},
}),
],
};
}

export default [createConfig(false), createConfig(true)];
2 changes: 1 addition & 1 deletion scripts/jest/config.dist.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ const sourceConfig = require('./config.source');

module.exports = {
...sourceConfig,
moduleNameMapper: {'../src': '<rootDir>/dist/index.cjs'},
moduleNameMapper: {'^../source$': '<rootDir>/dist/index.cjs'},
};
2 changes: 1 addition & 1 deletion scripts/jest/config.source.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
collectCoverageFrom: ['src/**/*.ts', '!src/{index,types}.ts'],
collectCoverageFrom: ['source/**/*.ts', '!source/{index,types}.ts'],
rootDir: process.cwd(),
testEnvironment: 'node',
testMatch: ['**/*.test.ts'],
Expand Down
28 changes: 12 additions & 16 deletions source/is-environment-config.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,22 @@
import assert from 'assert';
import {EnvironmentConfig, FileConfig} from './types';

/**
* Determine whether a PropertyConfig is an EnvironmentConfig.
*
* @param propertyConfig - The PropertyConfig that may be an EnvironmentConfig.
* @param propertyName - The name of the property.
* @returns True if the PropertyConfig is an EnvironmentConfig.
*/
export default function isEnvironmentConfig(
propertyConfig: EnvironmentConfig<unknown> | FileConfig<unknown>,
propertyName: string,
): propertyConfig is EnvironmentConfig<unknown> {
const isEnvironmentConfig = 'variableName' in propertyConfig;
const isFileConfig = 'filePath' in propertyConfig;
assert(
isEnvironmentConfig || isFileConfig,
`"configMap.${propertyName}" must be a string, EnvironmentConfig object, or FileConfig object. Neither "filePath" nor "variableName" are defined.`,
);
assert(
!(isEnvironmentConfig && isFileConfig),
`Cannot determine whether "configMap.${propertyName}" is an EnvironmentConfig object or a FileConfig object. Both "filePath" and "variableName" are defined.`,
);
if (!(isEnvironmentConfig || isFileConfig)) {
throw new Error(
`"configMap.${propertyName}" must be a string, EnvironmentConfig object, or FileConfig object. Neither "filePath" nor "variableName" are defined.`,
);
}

if (isEnvironmentConfig && isFileConfig) {
throw new Error(
`Cannot determine whether "configMap.${propertyName}" is an EnvironmentConfig object or a FileConfig object. Both "filePath" and "variableName" are defined.`,
);
}

return isEnvironmentConfig;
}
13 changes: 12 additions & 1 deletion source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,18 @@ export interface FileConfig<Value> extends BaseConfig<Value> {
* The encoding of the file containing the secret.
* @defaultValue 'utf8'
*/
encoding?: BufferEncoding;
encoding?:
| 'ascii'
| 'utf8'
| 'utf-8'
| 'utf16le'
| 'ucs2'
| 'ucs-2'
| 'base64'
| 'base64url'
| 'latin1'
| 'binary'
| 'hex';

/**
* The path to the file containing the secret.
Expand Down
1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"outDir": "dist",
"resolveJsonModule": true,
"rootDir": ".",
"skipLibCheck": true,
"strict": true,
"target": "ES2019"
},
Expand Down

0 comments on commit cea68fd

Please sign in to comment.