feat: check entries in CI config

Fixes #10
This commit is contained in:
Karl-Philipp Wulfert
2019-04-04 12:27:53 +02:00
parent 081e6008cd
commit 61d2285a1f
4 changed files with 133 additions and 16 deletions

View File

@@ -18,7 +18,8 @@ import {copyFileSync, existsSync, readFileSync, writeFileSync} from 'fs';
import {resolve, sep} from 'path';
import {cwd} from 'process';
import {isDeepStrictEqual} from 'util';
import {EXPECTED_LICENSES, NEEDED_FILES, NYC_CONFIGURATION, SCRIPTS} from './configuration';
import {parse, stringify} from 'yaml';
import {EXPECTED_CI_CONFIG, EXPECTED_LICENSES, NEEDED_FILES, NYC_CONFIGURATION, SCRIPTS} from './configuration';
/* tslint:disable:no-console */
@@ -28,7 +29,7 @@ import {EXPECTED_LICENSES, NEEDED_FILES, NYC_CONFIGURATION, SCRIPTS} from './con
*/
function consoleInfo(...args: string[]): void {
args.forEach((arg) => {
console.info(chalk.cyan(arg));
console.info('\n' + chalk.cyan(arg));
});
}
@@ -38,7 +39,7 @@ function consoleInfo(...args: string[]): void {
*/
function consoleWarn(...args: string[]): void {
args.forEach((arg) => {
console.warn(chalk.red.bold(arg));
console.warn('\n' + chalk.red.bold(arg));
});
}
@@ -48,7 +49,7 @@ function consoleWarn(...args: string[]): void {
*/
function consoleLog(...args: string[]): void {
args.forEach((arg) => {
console.log(chalk.green.bold(arg));
console.log('\n' + chalk.green.bold(arg));
});
}
@@ -157,16 +158,46 @@ Object.keys(SCRIPTS).forEach((scriptName) => {
// check if script exists
if (typeof scriptToCheck === 'undefined' || commander.replace) {
packageJson.scripts[scriptName] = SCRIPTS[scriptName];
packageJson.scripts[scriptName] = SCRIPTS[scriptName].replace('\n', '\\n');
packageJsonChanged = true;
consoleInfo(`Added "${scriptName}" script to "${packageJsonPath}".`);
} else if (typeof scriptToCheck === 'string' && scriptToCheck !== SCRIPTS[scriptName]) {
consoleWarn(`NPM script "${scriptName}" should be "${SCRIPTS[scriptName]}".`);
consoleWarn(`NPM script "${scriptName}" should be "${SCRIPTS[scriptName].replace('\n', '\\n')}".`);
}
});
// check CI config if it exists
const pathToCiConfig = resolve(path, '.gitlab-ci.yml');
if (existsSync(pathToCiConfig)) {
// read CI config
const buffer = readFileSync(pathToCiConfig);
try {
const ciConfig = parse(buffer.toString());
// check entries
for (const entry in EXPECTED_CI_CONFIG) {
if (!EXPECTED_CI_CONFIG.hasOwnProperty(entry)) {
continue;
}
if (!isDeepStrictEqual(EXPECTED_CI_CONFIG[entry], ciConfig[entry])) {
consoleWarn(`Entry '${entry}' in ${pathToCiConfig} is incorrect. Expected value is:`);
consoleInfo(stringify((() => {
const completeEntry: any = {};
completeEntry[entry] = EXPECTED_CI_CONFIG[entry];
return completeEntry;
})()));
}
}
} catch (error) {
consoleWarn(`Could not parse ${pathToCiConfig} because of '${error.message}'.
Please ensure consistency of CI config manually.`);
consoleInfo(stringify(EXPECTED_CI_CONFIG));
}
}
if (packageJsonChanged) {
writeFileSync(resolve(path, 'package.json'), JSON.stringify(packageJson, null, 2));
consoleLog(`Changes were written to "${packageJsonPath}".`);

View File

@@ -72,3 +72,52 @@ export const EXPECTED_LICENSES = [
'AGPL-3.0-only',
'GPL-3.0-only',
];
/* tslint:disable:object-literal-sort-keys */
/**
* Expected values in CI config
*/
export const EXPECTED_CI_CONFIG: any = {
image: 'registry.gitlab.com/openstapps/projectmanagement/node',
cache: {
key: '${CI_COMMIT_REF_SLUG}',
paths: [
'node_modules',
],
},
audit: {
allow_failure: true,
except: [
'schedules',
],
script: [
'npm audit',
],
stage: 'test',
},
'scheduled-audit': {
only: [
'schedules',
],
script: [
'npm audit',
],
stage: 'test',
},
pages: {
artifacts: {
'paths': [
'public',
],
},
only: [
'/^v[0-9]+\\.[0-9]+\\.[0-9]+$/',
],
script: [
'npm run documentation',
'mv docs public',
],
stage: 'deploy',
},
};
/* tslint:enable */