diff --git a/src/cli.ts b/src/cli.ts index 639ce6e9..4fe3803c 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -23,8 +23,12 @@ if (!existsSync(resolve(path, 'package.json'))) { throw new Error(`No package.json in "${path}".`); } +// path to examined package.json const packageJsonPath = resolve(path, 'package.json'); +// wheter or not the contents of the package.json were changed +let packageJsonChanged = false; + // read package.json in provided path const packageJson = JSON.parse(readFileSync(packageJsonPath).toString()); @@ -78,6 +82,34 @@ NEEDED_FILES.forEach((file) => { if (typeof packageJson.devDependencies === 'object' && Object.keys(packageJson.devDependencies).indexOf('nyc') >= 0) { // add/update NYC configuration packageJson.nyc = NYC_CONFIGURATION; - writeFileSync(resolve(path, 'package.json'), JSON.stringify(packageJson)); + + packageJsonChanged = true; + console.info(`Added/updated NYC configuration in "${packageJsonPath}".`); } + +// check if scripts is a map +if (typeof packageJson.scripts !== 'object') { + packageJson.scripts = {}; + + packageJsonChanged = true; +} + +const scriptToCheck = packageJson.scripts['check-configuration']; + +// check if "check-configuration" script exists +if (typeof scriptToCheck === 'undefined') { + // add/update NYC configuration + packageJson.scripts['check-configuration'] = 'openstapps-configuration'; + + packageJsonChanged = true; + + console.info(`Added "check-configuration" script to "${packageJsonPath}".`); +} else if (typeof scriptToCheck === 'string' && scriptToCheck !== 'openstapps-configuration') { + console.warn('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}".`); +}