typescript-strictly-typed
Advanced tools
Comparing version 3.1.0 to 3.2.0
@@ -1,2 +0,2 @@ | ||
import { findConfig, getConfig, modifyJSON, saveConfig } from './config-utils.js'; | ||
import { findConfig, getConfig, modifyJSON, saveConfig } from "./config-utils.js"; | ||
/** | ||
@@ -14,3 +14,3 @@ * Enable the following Angular compiler options: | ||
export async function enableAngularStrict(cwd) { | ||
const file = findConfig(cwd, ['tsconfig.base.json', 'tsconfig.json']); | ||
const file = findConfig(cwd, ["tsconfig.base.json", "tsconfig.json"]); | ||
if (!file) { | ||
@@ -23,6 +23,6 @@ return false; | ||
} | ||
config.raw = modifyJSON(config.raw, ['angularCompilerOptions', 'strictInjectionParameters'], true); | ||
config.raw = modifyJSON(config.raw, ['angularCompilerOptions', 'strictTemplates'], true); | ||
config.raw = modifyJSON(config.raw, ['angularCompilerOptions', 'strictInputAccessModifiers'], true); | ||
config.raw = modifyJSON(config.raw, ["angularCompilerOptions", "strictInjectionParameters"], true); | ||
config.raw = modifyJSON(config.raw, ["angularCompilerOptions", "strictTemplates"], true); | ||
config.raw = modifyJSON(config.raw, ["angularCompilerOptions", "strictInputAccessModifiers"], true); | ||
return saveConfig(cwd, file, config); | ||
} |
#!/usr/bin/env node | ||
import '../cli.js'; | ||
import "../cli.js"; |
@@ -1,4 +0,4 @@ | ||
import { cwd } from 'node:process'; | ||
import { typescriptStrictlyTyped } from './index.js'; | ||
import { logError } from './log-utils.js'; | ||
import { cwd } from "node:process"; | ||
import { typescriptStrictlyTyped } from "./index.js"; | ||
import { logError } from "./log-utils.js"; | ||
/* Get the path where the command is invoked */ | ||
@@ -5,0 +5,0 @@ typescriptStrictlyTyped(cwd()).catch(() => { |
@@ -1,8 +0,8 @@ | ||
import { basename, extname, join } from 'node:path'; | ||
import { existsSync, readFileSync, writeFileSync } from 'node:fs'; | ||
import { applyEdits, modify, parse } from 'jsonc-parser'; | ||
import { dump, load } from 'js-yaml'; | ||
import { pkgUpSync } from 'pkg-up'; | ||
import { coerce, satisfies } from 'semver'; | ||
import { logError, logInfo } from './log-utils.js'; | ||
import { dump, load } from "js-yaml"; | ||
import { applyEdits, modify, parse } from "jsonc-parser"; | ||
import { existsSync, readFileSync, writeFileSync } from "node:fs"; | ||
import { basename, extname, join } from "node:path"; | ||
import { pkgUpSync } from "pkg-up"; | ||
import { coerce, satisfies } from "semver"; | ||
import { logError, logInfo } from "./log-utils.js"; | ||
/** | ||
@@ -24,3 +24,3 @@ * Search the config file of a tool | ||
if (files[0]) { | ||
logInfo(`Can't find ${basename(files[0], '.json')} config file. Skipping this configuration.`); | ||
logInfo(`Can't find ${basename(files[0], ".json")} config file. Skipping this configuration.`); | ||
} | ||
@@ -39,3 +39,3 @@ return null; | ||
const filePath = join(cwd, file); | ||
const raw = readFileSync(filePath, { encoding: 'utf8' }); | ||
const raw = readFileSync(filePath, { encoding: "utf8" }); | ||
const fileType = extname(file); | ||
@@ -45,3 +45,3 @@ let config = null; | ||
switch (fileType) { | ||
case '.json': { | ||
case ".json": { | ||
config = { | ||
@@ -53,4 +53,4 @@ raw, | ||
} | ||
case '.yaml': | ||
case '.yml': { | ||
case ".yaml": | ||
case ".yml": { | ||
const json = load(raw); | ||
@@ -63,4 +63,4 @@ config = { | ||
} | ||
case '.js': | ||
case '.cjs': { | ||
case ".js": | ||
case ".cjs": { | ||
const moduleImport = await import(filePath); | ||
@@ -96,7 +96,7 @@ const json = moduleImport.default; | ||
switch (fileType) { | ||
case '.json': | ||
case ".json": | ||
configStringified = config.raw; | ||
break; | ||
case '.yaml': | ||
case '.yml': | ||
case ".yaml": | ||
case ".yml": | ||
configStringified = dump(config.json, { indent: 2 }); | ||
@@ -131,3 +131,3 @@ break; | ||
}; | ||
return applyEdits(json, modify(json.toString(), path, value, options ?? {})); | ||
return applyEdits(json, modify(json.toString(), path, value, options)); | ||
} | ||
@@ -144,3 +144,3 @@ /** | ||
if (filePath) { | ||
const packageJsonFile = readFileSync(filePath, { encoding: 'utf8' }); | ||
const packageJsonFile = readFileSync(filePath, { encoding: "utf8" }); | ||
const packageJsonConfig = parse(packageJsonFile); | ||
@@ -165,3 +165,3 @@ const prodDependencyVersion = packageJsonConfig?.dependencies?.[name]; | ||
if (filePath) { | ||
const packageJsonFile = readFileSync(filePath, { encoding: 'utf8' }); | ||
const packageJsonFile = readFileSync(filePath, { encoding: "utf8" }); | ||
const packageJsonConfig = parse(packageJsonFile); | ||
@@ -168,0 +168,0 @@ if (packageJsonConfig?.dependencies && (name in packageJsonConfig.dependencies)) { |
@@ -1,3 +0,3 @@ | ||
import { dependencyExists, findConfig, getConfig, modifyJSON, saveConfig } from './config-utils.js'; | ||
import { logWarning } from './log-utils.js'; | ||
import { dependencyExists, findConfig, getConfig, modifyJSON, saveConfig } from "./config-utils.js"; | ||
import { logWarning } from "./log-utils.js"; | ||
/** | ||
@@ -14,6 +14,6 @@ * Enable the following ESLint rules: | ||
export async function enableESLintStrict(cwd) { | ||
const possibleConfigFiles = ['.eslintrc.js', '.eslintrc.cjs', '.eslintrc.yaml', '.eslintrc.yml', '.eslintrc.json', 'package.json']; | ||
const tsFilesConfig = '*.ts'; | ||
const htmlFilesConfig = '*.html'; | ||
const eslintAngularTemplatePlugin = '@angular-eslint/template'; | ||
const possibleConfigFiles = [".eslintrc.js", ".eslintrc.cjs", ".eslintrc.yaml", ".eslintrc.yml", ".eslintrc.json", "package.json"]; | ||
const tsFilesConfig = "*.ts"; | ||
const htmlFilesConfig = "*.html"; | ||
const eslintAngularTemplatePlugin = "@angular-eslint/template"; | ||
let config = null; | ||
@@ -25,5 +25,5 @@ let packageJSONConfig = null; | ||
} | ||
if (file === 'package.json') { | ||
if (file === "package.json") { | ||
packageJSONConfig = await getConfig(cwd, file); | ||
if (!packageJSONConfig || !packageJSONConfig.json.eslintConfig) { | ||
if (!packageJSONConfig?.json.eslintConfig) { | ||
return false; | ||
@@ -42,3 +42,3 @@ } | ||
} | ||
if (!dependencyExists(cwd, '@typescript-eslint/eslint-plugin')) { | ||
if (!dependencyExists(cwd, "@typescript-eslint/eslint-plugin")) { | ||
logWarning(`'@typescript-eslint/eslint-plugin' dependency must be installed, otherwise rules won't be checked.`); | ||
@@ -52,3 +52,3 @@ } | ||
if (files.some((file) => file.includes(tsFilesConfig))) { | ||
addTSConfig(config, ['overrides', indexNumber], config.json.overrides?.[indexNumber]?.rules); | ||
addTSConfig(config, ["overrides", indexNumber], config.json.overrides?.[indexNumber]?.rules); | ||
tsConfigAdded = true; | ||
@@ -60,3 +60,3 @@ } | ||
|| extendsConfig.some((extendConfig) => extendConfig.includes(eslintAngularTemplatePlugin))) | ||
addAngularHTMLConfig(config, ['overrides', indexNumber], config.json.overrides?.[indexNumber]?.rules); | ||
addAngularHTMLConfig(config, ["overrides", indexNumber], config.json.overrides?.[indexNumber]?.rules); | ||
} | ||
@@ -66,11 +66,11 @@ } | ||
if (!tsConfigAdded) { | ||
addTSConfig(config, [], config.json?.rules); | ||
addTSConfig(config, [], config.json.rules); | ||
} | ||
if (packageJSONConfig) { | ||
config.raw = modifyJSON(packageJSONConfig.raw, ['eslintConfig'], config.json); | ||
config.raw = modifyJSON(packageJSONConfig.raw, ["eslintConfig"], config.json); | ||
return saveConfig(cwd, file, packageJSONConfig); | ||
} | ||
else if (file.endsWith('.js') || file.endsWith('.cjs')) { | ||
else if (file.endsWith(".js") || file.endsWith(".cjs")) { | ||
logWarning(`Your project is using the advanced .eslintrc.(c)js format for ESLint config, and it can't be overwrited directly, as it could mess up with advanced configuration. So the new strict configuration was saved in .eslintrc.json. As .eslintrc.(c)js has precedence over .eslintrc.json, you need to manually copy the new options from the new .eslintrc.json to your preexisting .eslintrc.(c)js.`); | ||
return saveConfig(cwd, '.eslintrc.json', config); | ||
return saveConfig(cwd, ".eslintrc.json", config); | ||
} | ||
@@ -82,21 +82,21 @@ else { | ||
function addTSConfig(config, path, rules) { | ||
if (Array.isArray(rules?.['@typescript-eslint/no-explicit-any'])) { | ||
config.raw = modifyJSON(config.raw, [...path, 'rules', '@typescript-eslint/no-explicit-any', 0], 'error'); | ||
if (Array.isArray(rules?.["@typescript-eslint/no-explicit-any"])) { | ||
config.raw = modifyJSON(config.raw, [...path, "rules", "@typescript-eslint/no-explicit-any", 0], "error"); | ||
} | ||
else { | ||
config.raw = modifyJSON(config.raw, [...path, 'rules', '@typescript-eslint/no-explicit-any'], 'error'); | ||
config.raw = modifyJSON(config.raw, [...path, "rules", "@typescript-eslint/no-explicit-any"], "error"); | ||
} | ||
if (Array.isArray(rules?.['@typescript-eslint/explicit-module-boundary-types'])) { | ||
config.raw = modifyJSON(config.raw, [...path, 'rules', '@typescript-eslint/explicit-module-boundary-types', 0], 'error'); | ||
if (Array.isArray(rules?.["@typescript-eslint/explicit-module-boundary-types"])) { | ||
config.raw = modifyJSON(config.raw, [...path, "rules", "@typescript-eslint/explicit-module-boundary-types", 0], "error"); | ||
} | ||
else { | ||
config.raw = modifyJSON(config.raw, [...path, 'rules', '@typescript-eslint/explicit-module-boundary-types'], 'error'); | ||
config.raw = modifyJSON(config.raw, [...path, "rules", "@typescript-eslint/explicit-module-boundary-types"], "error"); | ||
} | ||
} | ||
function addAngularHTMLConfig(config, path, rules) { | ||
if (Array.isArray(rules?.['@angular-eslint/template/no-any'])) { | ||
config.raw = modifyJSON(config.raw, [...path, 'rules', '@angular-eslint/template/no-any', 0], 'error'); | ||
if (Array.isArray(rules?.["@angular-eslint/template/no-any"])) { | ||
config.raw = modifyJSON(config.raw, [...path, "rules", "@angular-eslint/template/no-any", 0], "error"); | ||
} | ||
else { | ||
config.raw = modifyJSON(config.raw, [...path, 'rules', '@angular-eslint/template/no-any'], 'error'); | ||
config.raw = modifyJSON(config.raw, [...path, "rules", "@angular-eslint/template/no-any"], "error"); | ||
} | ||
@@ -103,0 +103,0 @@ } |
24
index.js
@@ -1,6 +0,7 @@ | ||
import { findConfig } from './config-utils.js'; | ||
import { logError, logSuccess } from './log-utils.js'; | ||
import { enableTypescriptStrict } from './typescript-strict.js'; | ||
import { enableESLintStrict } from './eslint-strict.js'; | ||
import { enableAngularStrict } from './angular-strict.js'; | ||
import { enableAngularStrict } from "./angular-strict.js"; | ||
import { isGitStatusDirty } from "./check-git-status.js"; | ||
import { findConfig } from "./config-utils.js"; | ||
import { enableESLintStrict } from "./eslint-strict.js"; | ||
import { logError, logSuccess } from "./log-utils.js"; | ||
import { enableTypescriptStrict } from "./typescript-strict.js"; | ||
/** | ||
@@ -15,12 +16,15 @@ * Enable strictly typed configurations for: | ||
export async function typescriptStrictlyTyped(cwd) { | ||
if (isGitStatusDirty(cwd)) { | ||
return; | ||
} | ||
const success = []; | ||
if (await enableTypescriptStrict(cwd)) { | ||
success.push('TypeScript'); | ||
success.push("TypeScript"); | ||
} | ||
if (await enableESLintStrict(cwd)) { | ||
success.push('ESLint'); | ||
success.push("ESLint"); | ||
} | ||
if (findConfig(cwd, ['angular.json', '.angular.json', 'angular-cli.json', '.angular-cli.json']) | ||
if (findConfig(cwd, ["angular.json", ".angular.json", "angular-cli.json", ".angular-cli.json"]) | ||
&& await enableAngularStrict(cwd)) { | ||
success.push('Angular'); | ||
success.push("Angular"); | ||
} | ||
@@ -31,4 +35,4 @@ if (success.length === 0) { | ||
else { | ||
logSuccess(`Configuration finished. It succeeded for: ${success.join(', ')}.\n`); | ||
logSuccess(`Configuration finished. It succeeded for: ${success.join(", ")}.\n`); | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import chalk from 'chalk'; | ||
import chalk from "chalk"; | ||
export function logSuccess(message) { | ||
@@ -3,0 +3,0 @@ console.log(`\n${chalk.green(message)}`); |
{ | ||
"name": "typescript-strictly-typed", | ||
"version": "3.1.0", | ||
"version": "3.2.0", | ||
"description": "Enable configurations for strictly typed TypeScript, ESLint, and optionally Angular.", | ||
@@ -52,5 +52,5 @@ "funding": { | ||
"eslint": "^8.13.0", | ||
"rimraf": "^4.4.0", | ||
"rimraf": "^5.0.0", | ||
"typescript": "^5.0.2" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
import { checkDependencyVersion, findConfig, getConfig, modifyJSON, saveConfig } from './config-utils.js'; | ||
import { checkDependencyVersion, findConfig, getConfig, modifyJSON, saveConfig } from "./config-utils.js"; | ||
/** | ||
@@ -17,3 +17,3 @@ * Enable the following TypeScript compiler options: | ||
export async function enableTypescriptStrict(cwd) { | ||
const file = findConfig(cwd, ['tsconfig.base.json', 'tsconfig.json']); | ||
const file = findConfig(cwd, ["tsconfig.base.json", "tsconfig.json"]); | ||
if (!file) { | ||
@@ -26,36 +26,36 @@ return false; | ||
} | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'strict'], true); | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'noFallthroughCasesInSwitch'], true); | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'noImplicitReturns'], true); | ||
if (checkDependencyVersion(cwd, 'typescript', '>=4.2.0')) { | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'noPropertyAccessFromIndexSignature'], true); | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "strict"], true); | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "noFallthroughCasesInSwitch"], true); | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "noImplicitReturns"], true); | ||
if (checkDependencyVersion(cwd, "typescript", ">=4.2.0")) { | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "noPropertyAccessFromIndexSignature"], true); | ||
} | ||
if (checkDependencyVersion(cwd, 'typescript', '>=4.3.0')) { | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'noImplicitOverride'], true); | ||
if (checkDependencyVersion(cwd, "typescript", ">=4.3.0")) { | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "noImplicitOverride"], true); | ||
} | ||
if (checkDependencyVersion(cwd, 'typescript', '>=4.4.0')) { | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'exactOptionalPropertyTypes'], true); | ||
if (checkDependencyVersion(cwd, "typescript", ">=4.4.0")) { | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "exactOptionalPropertyTypes"], true); | ||
} | ||
/* Already true by defaut since TypeScript 5.0 */ | ||
if (checkDependencyVersion(cwd, 'typescript', '<5.0.0')) { | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'forceConsistentCasingInFileNames'], true); | ||
if (checkDependencyVersion(cwd, "typescript", "<5.0.0")) { | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "forceConsistentCasingInFileNames"], true); | ||
} | ||
else { | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'forceConsistentCasingInFileNames'], undefined); | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "forceConsistentCasingInFileNames"], undefined); | ||
} | ||
/* Available since 4.1.0 but before 5.0.0 compiler was not smart enough and thus too annoying with Records | ||
* Still annoying with arrays, but it should not happen if using modern syntaxes */ | ||
if (checkDependencyVersion(cwd, 'typescript', '>=5.0.0')) { | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'noUncheckedIndexedAccess'], true); | ||
if (checkDependencyVersion(cwd, "typescript", ">=5.0.0")) { | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "noUncheckedIndexedAccess"], true); | ||
} | ||
/* Clean up options included in strict mode */ | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'alwaysStrict'], undefined); | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'noImplicitAny'], undefined); | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'noImplicitThis'], undefined); | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'strictBindCallApply'], undefined); | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'strictFunctionTypes'], undefined); | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'strictNullChecks'], undefined); | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'strictPropertyInitialization'], undefined); | ||
config.raw = modifyJSON(config.raw, ['compilerOptions', 'useUnknownInCatchVariables'], undefined); | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "alwaysStrict"], undefined); | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "noImplicitAny"], undefined); | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "noImplicitThis"], undefined); | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "strictBindCallApply"], undefined); | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "strictFunctionTypes"], undefined); | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "strictNullChecks"], undefined); | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "strictPropertyInitialization"], undefined); | ||
config.raw = modifyJSON(config.raw, ["compilerOptions", "useUnknownInCatchVariables"], undefined); | ||
return saveConfig(cwd, file, config); | ||
} |
19664
12
440