Skip to content

Commit

Permalink
feat(rename): update scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
aswanson-nr committed Sep 23, 2021
1 parent f0af784 commit 337c1dc
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 62 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/validate_packs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Validate Packs
name: Validate quickstarts

on: [push, workflow_dispatch, pull_request]

Expand All @@ -19,10 +19,10 @@ jobs:
run: cd utils && yarn install

- name: Validate files
run: cd utils && yarn validate-packs
run: cd utils && yarn validate-quickstarts

ensure-pack-names-are-unique:
name: Ensure pack names are unique
ensure-quickstart-names-are-unique:
name: Ensure quickstart names are unique
runs-on: ubuntu-latest
steps:
- name: Checkout repository
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';
const {
readPackFile,
readQuickstartFile,
removeRepoPathPrefix,
findMainPackConfigFiles,
findMainQuickstartConfigFiles,
} = require('./helpers');

/**
* Removes whitespace and punctuation from a string
* @returns {String} The string with `-` replacing whitespace and punctuation removed
*/
const cleanPackName = (str) =>
const cleanQuickstartName = (str) =>
str
.trim()
.toLowerCase()
Expand All @@ -18,44 +18,44 @@ const cleanPackName = (str) =>
.replace(/[^a-z0-9-]/g, '');

/**
* Returns any packs with matching names
* @param {Object[]} namesAndPaths an array of objects containing the path and name of a pack
* Returns any quickstarts with matching names
* @param {Object[]} namesAndPaths an array of objects containing the path and name of a quickstart
* @returns {Object[]} an array of matching values
*/
const getMatchingNames = (namesAndPaths) => {
return namesAndPaths.reduce((acc, { name, path }) => {
const duplicates = namesAndPaths.filter(
(pack) => pack.name === name && pack.path !== path
(quickstart) => quickstart.name === name && quickstart.path !== path
);

return [...new Set([...acc, ...duplicates])];
}, []);
};

const main = () => {
const configPaths = findMainPackConfigFiles();
const configs = configPaths.map(readPackFile);
const configPaths = findMainQuickstartConfigFiles();
const configs = configPaths.map(readQuickstartFile);
const nameAndPaths = configs.map((c) => ({
name: cleanPackName(c.contents[0].name),
name: cleanQuickstartName(c.contents[0].name),
path: c.path,
}));
const matches = getMatchingNames(nameAndPaths);

if (matches.length > 0) {
console.error(`ERROR: Found matching Observability Pack names`);
console.error(`ERROR: Found matching quickstart names`);
console.error(`Punctuation and white space are removed before comparison`);
matches.forEach((m) =>
console.error(`${m.name} in ${removeRepoPathPrefix(m.path)}`)
);
console.error(`Please update your pack's name to be unique`);
console.error(`Please update your quickstart's name to be unique`);

// `require.main` is equal to `module` when the file is being executed directly
// if it isn't, the file is being import/required
if (require.main === module) {
process.exit(1);
}
} else {
console.log(`All Observability Pack names are unique`);
console.log(`All quickstart names are unique`);
}
};

Expand Down
10 changes: 5 additions & 5 deletions utils/generate-uuids.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

const fs = require('fs');
const { v4: uuidv4 } = require('uuid');
const { findMainPackConfigFiles, readYamlFile } = require('./helpers');
const { findMainQuickstartConfigFiles, readYamlFile } = require('./helpers');

/**
* Checks for and generates UUIDs for packs
* @param {String[]} paths an array of pack config paths
* Checks for and generates UUIDs for quickstarts
* @param {String[]} paths an array of quickstarts config paths
**/
const main = (paths) => {
for (const path of paths) {
Expand All @@ -22,11 +22,11 @@ const main = (paths) => {
};

/**
* This allows us to check if the script was invoked directly from the command line, i.e 'node validate_packs.js', or if it was imported.
* This allows us to check if the script was invoked directly from the command line, i.e 'node validate_quickstarts.js', or if it was imported.
* This would be true if this was used in one of our GitHub workflows, but false when imported for use in a test.
* See here: https://nodejs.org/docs/latest/api/modules.html#modules_accessing_the_main_module
*/
if (require.main === module) {
const configPaths = findMainPackConfigFiles();
const configPaths = findMainQuickstartConfigFiles();
main(configPaths);
}
18 changes: 10 additions & 8 deletions utils/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const readJsonFile = (filePath) => {
* @param {String} filePath - The path to the JSON or YAML file
* @returns {Object} An object containing the path and contents of the file
*/
const readPackFile = (filePath) =>
const readQuickstartFile = (filePath) =>
path.extname(filePath) === '.json'
? readJsonFile(filePath)
: readYamlFile(filePath);
Expand Down Expand Up @@ -65,12 +65,12 @@ const checkArgs = (length) => {
};

/**
* Removes the `newrelic-observability-packs/` path prefix from a string
* Removes the `newrelic-quickstarts/` path prefix from a string
* @param {String} filePath the path to change
* @returns {String} The path with the prefix
*/
const removeRepoPathPrefix = (filePath) =>
filePath.split(`newrelic-observability-packs/`).pop();
filePath.split(`newrelic-quickstarts/`).pop();

/**
* Checks if a path is a direectory
Expand Down Expand Up @@ -118,16 +118,18 @@ const globFiles = (dir) => {
};

/**
* Finds the path to all top level pack configs
* Finds the path to all top level quickstart configs
* @returns {String[]} An array of the file paths
*/
const findMainPackConfigFiles = () =>
glob.sync(path.resolve(process.cwd(), '../packs/**/config.+(yml|yaml)'));
const findMainQuickstartConfigFiles = () =>
glob.sync(
path.resolve(process.cwd(), '../quickstarts/**/config.+(yml|yaml)')
);

module.exports = {
readYamlFile,
readJsonFile,
readPackFile,
readQuickstartFile,
removeCWDPrefix,
removeRepoPathPrefix,
checkArgs,
Expand All @@ -136,5 +138,5 @@ module.exports = {
getFileExtension,
globFiles,
isDirectory,
findMainPackConfigFiles,
findMainQuickstartConfigFiles,
};
8 changes: 4 additions & 4 deletions utils/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "newrelic-observability-packs",
"name": "newrelic-quickstarts",
"version": "0.0.1",
"repository": "https://github.com/newrelic/newrelic-observability-packs.git",
"repository": "https://github.com/newrelic/newrelic-quickstarts.git",
"author": "NewRelic",
"license": "Apache-2.0",
"scripts": {
"validate-packs": "node validate_packs.js",
"validate-quickstarts": "node validate_quickstarts.js",
"test": "jest -i",
"check-for-matching-names": "node check_pack_uniqueness.js",
"check-for-matching-names": "node check_quickstart_uniqueness.js",
"validate-images": "node validate_images.js",
"format": "prettier --write \"./**/*.js\"",
"validate-icons-and-logos": "node validate_icons_and_logos.js",
Expand Down
11 changes: 7 additions & 4 deletions utils/validate_icons_and_logos.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
const fs = require('fs');
const path = require('path');

const { readPackFile, findMainPackConfigFiles } = require('./helpers');
const {
readQuickstartFile,
findMainQuickstartConfigFiles,
} = require('./helpers');

/**
* Method to validate icons and logos exist if supplied.
Expand All @@ -15,7 +18,7 @@ const validateIconAndLogo = (mainConfigPaths) => {
for (const configPath of mainConfigPaths) {
const {
contents: [config],
} = readPackFile(configPath);
} = readQuickstartFile(configPath);

if ('icon' in config) {
// icon and logo will be supplied as relative paths, so we path.join on the directory of where the main config lives.
Expand Down Expand Up @@ -60,14 +63,14 @@ const handleErrors = (errorMessages) => {

const main = () => {
console.log(''); // add an extra new line for more visual separation in the workflow
var mainConfigPaths = findMainPackConfigFiles();
var mainConfigPaths = findMainQuickstartConfigFiles();
var errorMessages = validateIconAndLogo(mainConfigPaths);
handleErrors(errorMessages);
console.log(''); // add an extra new line for more visual separation in the workflow
};

/**
* This allows us to check if the script was invoked directly from the command line, i.e 'node validate_packs.js', or if it was imported.
* This allows us to check if the script was invoked directly from the command line, i.e 'node validate_quickstarts.js', or if it was imported.
* This would be true if this was used in one of our GitHub workflows, but false when imported for use in a test.
* See here: https://nodejs.org/docs/latest/api/modules.html#modules_accessing_the_main_module
*/
Expand Down
34 changes: 17 additions & 17 deletions utils/validate_images.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ const {
getFileSize,
getFileExtension,
globFiles,
readPackFile,
findMainPackConfigFiles,
readQuickstartFile,
findMainQuickstartConfigFiles,
} = require('./helpers');

const glob = require('glob');
Expand All @@ -21,20 +21,20 @@ const ALLOWED_IMG_EXT = ['.png', '.jpeg', '.jpg', '.svg'];
* @param {Array} files - The array of globbed file names
*/

const validateImageCounts = (packDirs) => {
const directories = packDirs
.map((pack) => {
const packDirName = path.dirname(pack);
// get all images for pack
const validateImageCounts = (quickstartDirs) => {
const directories = quickstartDirs
.map((quickstart) => {
const quickstartDirName = path.dirname(quickstart);
// get all images for a quickstart
const imagePaths = glob.sync(
path.resolve(path.dirname(pack), '**/*.+(png|jpeg|jpg|svg)')
path.resolve(path.dirname(quickstart), '**/*.+(png|jpeg|jpg|svg)')
);
const packConfig = readPackFile(pack).contents[0];
const iconPath = packConfig.icon
? path.resolve(path.dirname(pack), packConfig.icon)
const quickstartConfig = readQuickstartFile(quickstart).contents[0];
const iconPath = quickstartConfig.icon
? path.resolve(path.dirname(quickstart), quickstartConfig.icon)
: null;
const logoPath = packConfig.logo
? path.resolve(path.dirname(pack), packConfig.logo)
const logoPath = quickstartConfig.logo
? path.resolve(path.dirname(quickstart), quickstartConfig.logo)
: null;

const screenshotPaths = imagePaths.filter(
Expand All @@ -43,7 +43,7 @@ const validateImageCounts = (packDirs) => {

if (screenshotPaths.length > MAX_NUM_IMG) {
return {
folder: packDirName,
folder: quickstartDirName,
imageCount: screenshotPaths.length,
};
}
Expand Down Expand Up @@ -98,16 +98,16 @@ const validateImageExtensions = (globbedFiles) => {
};

const main = () => {
const packDirs = findMainPackConfigFiles();
validateImageCounts(packDirs);
const quickstartDirs = findMainQuickstartConfigFiles();
validateImageCounts(quickstartDirs);

const globbedFiles = globFiles(BASE_PATH);
validateFileSizes(globbedFiles);
validateImageExtensions(globbedFiles);
};

/**
* This allows us to check if the script was invoked directly from the command line, i.e 'node validate_packs.js', or if it was imported.
* This allows us to check if the script was invoked directly from the command line, i.e 'node validate_quickstarts.js', or if it was imported.
* This would be true if this was used in one of our GitHub workflows, but false when imported for use in a test.
* See here: https://nodejs.org/docs/latest/api/modules.html#modules_accessing_the_main_module
*/
Expand Down
16 changes: 8 additions & 8 deletions utils/validate_packs.js → utils/validate_quickstarts.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Ajv = require('ajv');
const { ErrorObject } = require('ajv');
const ajv = new Ajv({ allErrors: true });

const { readPackFile, removeRepoPathPrefix } = require('./helpers');
const { readQuickstartFile, removeRepoPathPrefix } = require('./helpers');

// schemas
const mainConfigSchema = require('./schemas/main_config.json');
Expand Down Expand Up @@ -120,19 +120,19 @@ const validateFile = (file) => {
* @param {String} basePath - the base path to search under, usually the current working directory
* @returns {String[]} An array containing the file paths
*/
const getPackFilePaths = (basePath) => {
const getQuickstartFilePaths = (basePath) => {
const options = {
ignore: EXCLUDED_DIRECTORY_PATTERNS.map((d) => path.resolve(basePath, d)),
};

const yamlFilePaths = [
...glob.sync(path.resolve(basePath, '../packs/**/*.yaml'), options),
...glob.sync(path.resolve(basePath, '../packs/**/*.yml'), options),
...glob.sync(path.resolve(basePath, '../quickstarts/**/*.yaml'), options),
...glob.sync(path.resolve(basePath, '../quickstarts/**/*.yml'), options),
...glob.sync(path.resolve(basePath, '../install/**/*.yml'), options),
];

const jsonFilePaths = glob.sync(
path.resolve(basePath, '../packs/**/*.json'),
path.resolve(basePath, '../quickstarts/**/*.json'),
options
);

Expand All @@ -155,8 +155,8 @@ const printErrors = (filesWithErrors) => {
};

const main = () => {
const filePaths = getPackFilePaths(process.cwd()).sort();
const files = filePaths.map(readPackFile);
const filePaths = getQuickstartFilePaths(process.cwd()).sort();
const files = filePaths.map(readQuickstartFile);

const filesWithErrors = files
.map(validateFile)
Expand All @@ -170,7 +170,7 @@ const main = () => {
};

/**
* This allows us to check if the script was invoked directly from the command line, i.e 'node validate_packs.js', or if it was imported.
* This allows us to check if the script was invoked directly from the command line, i.e 'node validate_quickstarts.js', or if it was imported.
* This would be true if this was used in one of our GitHub workflows, but false when imported for use in a test.
* See here: https://nodejs.org/docs/latest/api/modules.html#modules_accessing_the_main_module
*/
Expand Down

0 comments on commit 337c1dc

Please sign in to comment.