feat: add cli to copy configurations

Fixes #1
This commit is contained in:
Karl-Philipp Wulfert
2018-12-04 12:58:57 +01:00
parent 84beb5065e
commit 98aa5a5f70
7 changed files with 450 additions and 12 deletions

83
src/cli.ts Normal file
View File

@@ -0,0 +1,83 @@
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';
/* tslint:disable:no-console */
const currentWorkingDirectory = cwd();
// configure commander
commander
.version(JSON.parse(readFileSync(resolve(__dirname, '..', 'package.json')).toString()).version)
.option('-p, --path [path]', `Path of project to add files to (${currentWorkingDirectory})`, currentWorkingDirectory)
.option('-r, --replace', 'Whether to replace existing files or not', false)
.parse(process.argv);
// make path absolute
const path = resolve(commander.path);
// check for existing package.json in provided path
if (!existsSync(resolve(path, 'package.json'))) {
throw new Error(`No package.json in "${path}".`);
}
const packageJsonPath = resolve(path, 'package.json');
// read package.json in provided path
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.');
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(', ')}"!`);
}
// check if configuration files are extended
['tsconfig.json', 'tslint.json'].forEach((file) => {
const fileToCheck = resolve(path, file);
const expectedPath = `./node_modules/@openstapps/configuration/${file}`;
if (existsSync(fileToCheck)) {
const tsconfigJson = 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());
}
}
});
// copy needed files
NEEDED_FILES.forEach((file) => {
let destinationFile = file;
// remove templates directory for destination files
if (destinationFile.indexOf('templates') === 0) {
destinationFile = destinationFile.split(sep).slice(1).join(sep);
}
const destination = resolve(path, destinationFile);
// check if file exists or replace flag is set
if (!existsSync(destination) || commander.replace) {
copyFileSync(resolve(__dirname, '..', file), destination);
console.info(`Added file "${destination}".`);
} else {
console.info(`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;
writeFileSync(resolve(path, 'package.json'), JSON.stringify(packageJson));
console.info(`Added/updated NYC configuration in "${packageJsonPath}".`);
}

46
src/configuration.ts Normal file
View File

@@ -0,0 +1,46 @@
import {join} from 'path';
/**
* Files that need to be copied
*/
export const NEEDED_FILES = [
'.editorconfig',
'.gitignore',
'.npmignore',
join('templates', 'tsconfig.json'),
join('templates', 'tslint.json'),
];
/**
* Configuration for nyc to add to package.json
*/
export const NYC_CONFIGURATION = {
all: true,
branches: 95,
'check-coverage': true,
exclude: [
'src/test/**/*.spec.ts',
],
extension: [
'.ts',
],
functions: 95,
include: [
'src',
],
lines: 95,
'per-file': true,
reporter: [
'html',
'text-summary',
],
statements: 95,
};
/**
* List of expected licenses
*/
export const EXPECTED_LICENSES = [
'AGPL-3.0-only',
'GPL-3.0-only',
];