From 9c8ce486ce03ebff344b53b566afa3e164c60647 Mon Sep 17 00:00:00 2001 From: Karl-Philipp Wulfert Date: Thu, 7 Feb 2019 17:50:29 +0100 Subject: [PATCH] fix: correctly check tslint.json Fixes #3, #4 --- package-lock.json | 14 +++++++--- package.json | 2 ++ src/cli.ts | 66 +++++++++++++++++++++++++++++++++++++---------- 3 files changed, 66 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index a8dbcc65..121c1d11 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4,6 +4,14 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@types/chalk": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/@types/chalk/-/chalk-2.2.0.tgz", + "integrity": "sha512-1zzPV9FDe1I/WHhRkf9SNgqtRJWZqrBWgu7JGveuHmmyR9CnAPCie2N/x+iHrgnpYBIcCJWHBoMRv2TRWktsvw==", + "requires": { + "chalk": "*" + } + }, "@types/events": { "version": "1.2.0", "resolved": "http://registry.npmjs.org/@types/events/-/events-1.2.0.tgz", @@ -203,9 +211,9 @@ } }, "chalk": { - "version": "2.4.1", - "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.1.tgz", - "integrity": "sha512-ObN6h1v2fTJSmUXoS3nMQ92LbDK9be4TV+6G+omQlGJFdcUX5heKi1LZ1YnRMIgwTLEj3E24bT6tYni50rlCfQ==", + "version": "2.4.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-2.4.2.tgz", + "integrity": "sha512-Mti+f9lpJNcwF4tWV8/OrTTtF1gZi+f8FqlyAdouralcFWFQWF2+NgCHShjkCb+IFBLq9buZwE1xckQU4peSuQ==", "requires": { "ansi-styles": "^3.2.1", "escape-string-regexp": "^1.0.5", diff --git a/package.json b/package.json index 51f5210c..0456b06b 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,9 @@ ], "license": "GPL-3.0-only", "dependencies": { + "@types/chalk": "2.2.0", "@types/node": "10.12.15", + "chalk": "2.4.2", "commander": "2.19.0", "tslint": "5.12.0", "tslint-eslint-rules": "5.4.0" diff --git a/src/cli.ts b/src/cli.ts index ca5971d4..1732f2d3 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -1,5 +1,5 @@ /* - * Copyright (C) 2018 StApps + * Copyright (C) 2018, 2019 StApps * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation, version 3. @@ -12,6 +12,7 @@ * You should have received a copy of the GNU General Public License along with * this program. If not, see . */ +import chalk from 'chalk'; import * as commander from 'commander'; import {copyFileSync, existsSync, readFileSync, writeFileSync} from 'fs'; import {resolve, sep} from 'path'; @@ -20,6 +21,36 @@ import {EXPECTED_LICENSES, NEEDED_FILES, NYC_CONFIGURATION} from './configuratio /* tslint:disable:no-console */ +/** + * Wrapper for console.info that outputs every argument in cyan + * @param args + */ +function consoleInfo(...args: string[]): void { + args.forEach((arg) => { + console.info(chalk.cyan(arg)); + }); +} + +/** + * Wrapper for console.warn that outputs every argument in red + * @param args + */ +function consoleWarn(...args: string[]): void { + args.forEach((arg) => { + console.warn(chalk.red.bold(arg)); + }); +} + +/** + * Wrapper for console.log that outputs every argument in green + * @param args + */ +function consoleLog(...args: string[]): void { + args.forEach((arg) => { + console.log(chalk.green.bold(arg)); + }); +} + const currentWorkingDirectory = cwd(); // configure commander @@ -48,13 +79,13 @@ const packageJson = JSON.parse(readFileSync(packageJsonPath).toString()); // check if provided path is this package if (packageJson.name === '@openstapps/configuration') { - console.info('Not doing anything, because installing dependencies in myself.'); + consoleInfo('I\'m not going to check myself!'); process.exit(0); } // check if license is one of the expected ones if (EXPECTED_LICENSES.indexOf(packageJson.license) === -1) { - console.warn(`License should be one of "${EXPECTED_LICENSES.join(', ')}"!`); + consoleWarn(`License should be one of "${EXPECTED_LICENSES.join(', ')}"!`); } // check if configuration files are extended @@ -63,11 +94,18 @@ if (EXPECTED_LICENSES.indexOf(packageJson.license) === -1) { const expectedPath = `./node_modules/@openstapps/configuration/${file}`; if (existsSync(fileToCheck)) { - const tsconfigJson = JSON.parse(readFileSync(fileToCheck).toString()); + const configFile = JSON.parse(readFileSync(fileToCheck).toString()); - if (typeof tsconfigJson.extends !== 'string' || tsconfigJson.extends !== expectedPath) { - console.warn(`File "${fileToCheck}" should extend "${expectedPath}"!`); - console.warn(readFileSync(resolve(__dirname, '..', 'templates', file)).toString()); + const configFileExtended = + (typeof configFile.extends === 'string' && configFile.extends === expectedPath) + || (file === 'tslint.json' && Array.isArray(configFile.extends) && configFile.extends.indexOf(expectedPath) >= 0); + + if (!configFileExtended) { + consoleWarn(` +File "${fileToCheck}" should extend "${expectedPath}"! + +Example: +${readFileSync(resolve(__dirname, '..', 'templates', 'template-' + file))}`); } } }); @@ -88,9 +126,9 @@ NEEDED_FILES.forEach((file) => { // check if file exists or replace flag is set if (!existsSync(destination) || commander.replace) { copyFileSync(source, destination); - console.info(`Copied file "${source}" to "${destination}".`); + consoleInfo(`Copied file "${source}" to "${destination}".`); } else { - console.info(`Not replacing "${destination}". Use "-r" or "--replace" to do so.`); + consoleInfo(`Not replacing "${destination}". Use "-r" or "--replace" to do so.`); } }); @@ -101,7 +139,7 @@ if (typeof packageJson.devDependencies === 'object' && Object.keys(packageJson.d packageJsonChanged = true; - console.info(`Added/updated NYC configuration in "${packageJsonPath}".`); + consoleInfo(`Added/updated NYC configuration in "${packageJsonPath}".`); } // check if scripts is a map @@ -120,12 +158,14 @@ if (typeof scriptToCheck === 'undefined') { packageJsonChanged = true; - console.info(`Added "check-configuration" script to "${packageJsonPath}".`); + consoleInfo(`Added "check-configuration" script to "${packageJsonPath}".`); } else if (typeof scriptToCheck === 'string' && scriptToCheck !== 'openstapps-configuration') { - console.warn('NPM script "check-configuration" should be "openstapps-configuration".'); + consoleWarn('NPM script "check-configuration" should be "openstapps-configuration".'); } if (packageJsonChanged) { writeFileSync(resolve(path, 'package.json'), JSON.stringify(packageJson, null, 2)); - console.log(`Changes were written to "${packageJsonPath}".`); + consoleLog(`Changes were written to "${packageJsonPath}".`); } + +consoleLog('Done.');