feat: add ability to check scripts

Furthermore do not overwrite existing NYC configuration.

Fixes #6
This commit is contained in:
Karl-Philipp Wulfert
2019-02-25 19:27:47 +01:00
parent c3dde94530
commit 662f534550
2 changed files with 34 additions and 17 deletions

View File

@@ -17,7 +17,8 @@ import * as commander from 'commander';
import {copyFileSync, existsSync, readFileSync, writeFileSync} from 'fs';
import {resolve, sep} from 'path';
import {cwd} from 'process';
import {EXPECTED_LICENSES, NEEDED_FILES, NYC_CONFIGURATION} from './configuration';
import {isDeepStrictEqual} from 'util';
import {EXPECTED_LICENSES, NEEDED_FILES, NYC_CONFIGURATION, SCRIPTS} from './configuration';
/* tslint:disable:no-console */
@@ -127,19 +128,21 @@ NEEDED_FILES.forEach((file) => {
if (!existsSync(destination) || commander.replace) {
copyFileSync(source, destination);
consoleInfo(`Copied file "${source}" to "${destination}".`);
} else {
consoleInfo(`Not replacing "${destination}". Use "-r" or "--replace" to do so.`);
}
});
// check if nyc is a dependency
if (typeof packageJson.devDependencies === 'object' && Object.keys(packageJson.devDependencies).indexOf('nyc') >= 0) {
// add/update NYC configuration
packageJson.nyc = NYC_CONFIGURATION;
if (typeof packageJson.nyc === 'undefined' || commander.replace) {
// add NYC configuration
packageJson.nyc = NYC_CONFIGURATION;
packageJsonChanged = true;
packageJsonChanged = true;
consoleInfo(`Added/updated NYC configuration in "${packageJsonPath}".`);
consoleInfo(`Added NYC configuration in "${packageJsonPath}".`);
} else if (!isDeepStrictEqual(packageJson.nyc, NYC_CONFIGURATION)) {
consoleLog(`NYC configuration differs from the proposed one. Please check manually...`);
}
}
// check if scripts is a map
@@ -149,19 +152,20 @@ if (typeof packageJson.scripts !== 'object') {
packageJsonChanged = true;
}
const scriptToCheck = packageJson.scripts['check-configuration'];
Object.keys(SCRIPTS).forEach((scriptName) => {
const scriptToCheck = packageJson.scripts[scriptName];
// check if "check-configuration" script exists
if (typeof scriptToCheck === 'undefined') {
// add/update NYC configuration
packageJson.scripts['check-configuration'] = 'openstapps-configuration';
// check if script exists
if (typeof scriptToCheck === 'undefined' || commander.replace) {
packageJson.scripts[scriptName] = SCRIPTS[scriptName];
packageJsonChanged = true;
packageJsonChanged = true;
consoleInfo(`Added "check-configuration" script to "${packageJsonPath}".`);
} else if (typeof scriptToCheck === 'string' && scriptToCheck !== 'openstapps-configuration') {
consoleWarn('NPM script "check-configuration" should be "openstapps-configuration".');
}
consoleInfo(`Added "${scriptName}" script to "${packageJsonPath}".`);
} else if (typeof scriptToCheck === 'string' && scriptToCheck !== SCRIPTS[scriptName]) {
consoleWarn(`NPM script "${scriptName}" should be "${SCRIPTS[scriptName]}".`);
}
});
if (packageJsonChanged) {
writeFileSync(resolve(path, 'package.json'), JSON.stringify(packageJson, null, 2));

View File

@@ -52,6 +52,19 @@ export const NYC_CONFIGURATION = {
statements: 95,
};
/**
* Map of expected scripts
*/
export const SCRIPTS: { [k: string]: string } = {
/* tslint:disable-next-line:max-line-length */
'changelog': 'conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add CHANGELOG.md && git commit -m \'docs: update changelog\'',
'check-configuration': 'openstapps-configuration',
'compile': 'rimraf lib/* && tsc --outDir lib && prepend lib/cli.js \'#!/usr/bin/env node\n\'',
/* tslint:disable-next-line:max-line-length */
'documentation': 'typedoc --includeDeclarations --mode modules --out docs --readme README.md --listInvalidSymbolLinks src',
'prepublishOnly': 'npm ci && npm run build',
};
/**
* List of expected licenses
*/