fix: correctly check tslint.json

Fixes #3, #4
This commit is contained in:
Karl-Philipp Wulfert
2019-02-07 17:50:29 +01:00
parent 557880d846
commit 9c8ce486ce
3 changed files with 66 additions and 16 deletions

View File

@@ -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 <https://www.gnu.org/licenses/>.
*/
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.');