Skip to content

Commit

Permalink
Code cleanup including better error throw and ensure functions define…
Browse files Browse the repository at this point in the history
…d before use

Signed-off-by: Zach Hill <zach@anchore.com>
  • Loading branch information
zhill committed Nov 4, 2019
1 parent a54b269 commit 2209bd4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 70 deletions.
71 changes: 36 additions & 35 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,40 @@ const core = __webpack_require__(470);
const execSync = __webpack_require__(129).execSync;
const fs = __webpack_require__(747);

// Find all 'content-*.json' files in the directory. dirname should include the full path
function findContent(searchDir) {
let contentFiles = [];
let match = /content-.*\.json/;
var dirItems = fs.readdirSync(searchDir);
if (dirItems) {
for (let i = 0; i < dirItems.length; i++) {
if (match.test(dirItems[i])) {
contentFiles.push(`${searchDir}/${dirItems[i]}`);
}
}
} else {
console.log("no dir content found");
}

console.log(contentFiles);
return contentFiles;
}

// Load the json content of each file in a list and return them as a list
function loadContent(files) {
let contents = [];
if (files) {
files.forEach(item => contents.push(JSON.parse(fs.readFileSync(item))));
}
return contents
}

// Merge the multiple content output types into a single array
function mergeResults(contentArray) {
return contentArray.reduce((merged, n) => merged.concat(n.content), []);
}


async function run() {
try {
core.debug((new Date()).toTimeString());
Expand Down Expand Up @@ -91,14 +125,14 @@ async function run() {
custom_policy = JSON.parse(custom_policy);
bundle_name = custom_policy.id;
if (!bundle_name) {
throw "Could not extract id from custom policy bundle. May be malformed json or not contain id property";
throw new Error("Could not extract id from custom policy bundle. May be malformed json or not contain id property");
} else {
core.info(`Detected custom policy id: ${bundle_name}`);
}
policy_bundle_name = bundle_name;
policy_bundle_path = bundle_path;
} else {
throw `Custom policy specified at ${policy_bundle_path} but not found`;
throw new Error(`Custom policy specified at ${policy_bundle_path} but not found`);
}
}

Expand Down Expand Up @@ -167,39 +201,6 @@ async function run() {
}
}

// Find all 'content-*.json' files in the directory. dirname should include the full path
function findContent(searchDir) {
let contentFiles = [];
let match = /content-.*\.json/;
var dirItems = fs.readdirSync(searchDir);
if (dirItems) {
for (let i = 0; i < dirItems.length; i++) {
if (match.test(dirItems[i])) {
contentFiles.push(`${searchDir}/${dirItems[i]}`);
}
}
} else {
console.log("no dir content found");
}

console.log(contentFiles);
return contentFiles;
}

// Load the json content of each file in a list and return them as a list
function loadContent(files) {
let contents = [];
if (files) {
files.forEach(item => contents.push(JSON.parse(fs.readFileSync(item))));
}
return contents
}

// Merge the multiple content output types into a single array
function mergeResults(contentArray) {
return contentArray.reduce((merged, n) => merged.concat(n.content), []);
}

module.exports = {run, mergeResults, findContent, loadContent};

if (require.main === require.cache[eval('__filename')]) {
Expand Down
71 changes: 36 additions & 35 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,40 @@ const core = require('@actions/core');
const execSync = require('child_process').execSync;
const fs = require('fs');

// Find all 'content-*.json' files in the directory. dirname should include the full path
function findContent(searchDir) {
let contentFiles = [];
let match = /content-.*\.json/;
var dirItems = fs.readdirSync(searchDir);
if (dirItems) {
for (let i = 0; i < dirItems.length; i++) {
if (match.test(dirItems[i])) {
contentFiles.push(`${searchDir}/${dirItems[i]}`);
}
}
} else {
console.log("no dir content found");
}

console.log(contentFiles);
return contentFiles;
}

// Load the json content of each file in a list and return them as a list
function loadContent(files) {
let contents = [];
if (files) {
files.forEach(item => contents.push(JSON.parse(fs.readFileSync(item))));
}
return contents
}

// Merge the multiple content output types into a single array
function mergeResults(contentArray) {
return contentArray.reduce((merged, n) => merged.concat(n.content), []);
}


async function run() {
try {
core.debug((new Date()).toTimeString());
Expand Down Expand Up @@ -36,14 +70,14 @@ async function run() {
custom_policy = JSON.parse(custom_policy);
bundle_name = custom_policy.id;
if (!bundle_name) {
throw "Could not extract id from custom policy bundle. May be malformed json or not contain id property";
throw new Error("Could not extract id from custom policy bundle. May be malformed json or not contain id property");
} else {
core.info(`Detected custom policy id: ${bundle_name}`);
}
policy_bundle_name = bundle_name;
policy_bundle_path = bundle_path;
} else {
throw `Custom policy specified at ${policy_bundle_path} but not found`;
throw new Error(`Custom policy specified at ${policy_bundle_path} but not found`);
}
}

Expand Down Expand Up @@ -112,39 +146,6 @@ async function run() {
}
}

// Find all 'content-*.json' files in the directory. dirname should include the full path
function findContent(searchDir) {
let contentFiles = [];
let match = /content-.*\.json/;
var dirItems = fs.readdirSync(searchDir);
if (dirItems) {
for (let i = 0; i < dirItems.length; i++) {
if (match.test(dirItems[i])) {
contentFiles.push(`${searchDir}/${dirItems[i]}`);
}
}
} else {
console.log("no dir content found");
}

console.log(contentFiles);
return contentFiles;
}

// Load the json content of each file in a list and return them as a list
function loadContent(files) {
let contents = [];
if (files) {
files.forEach(item => contents.push(JSON.parse(fs.readFileSync(item))));
}
return contents
}

// Merge the multiple content output types into a single array
function mergeResults(contentArray) {
return contentArray.reduce((merged, n) => merged.concat(n.content), []);
}

module.exports = {run, mergeResults, findContent, loadContent};

if (require.main === module) {
Expand Down

0 comments on commit 2209bd4

Please sign in to comment.