From 9970cceaf5c61db2c9201790ec6364065ca9c4ca Mon Sep 17 00:00:00 2001 From: nexdrew Date: Fri, 1 Sep 2017 13:51:04 -0400 Subject: [PATCH] feat: interpret multiple FROM commands as multi-stage builds --- lib/index.js | 18 +++++++++++++----- test/index.js | 6 ------ 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lib/index.js b/lib/index.js index b40737c..426faf8 100644 --- a/lib/index.js +++ b/lib/index.js @@ -50,8 +50,10 @@ module.exports.run = function(configPath, content) { }); var state = { - instructionsProcessed: 0, - cmdFound: false, + stages: [{ + instructionsProcessed: 0, + cmdFound: false // this doesn't seem to be used anywhere + }], items: [], rules: loadRules(configPath) } @@ -62,11 +64,17 @@ module.exports.run = function(configPath, content) { // We care about only having 1 cmd instruction if (result.command === 'cmd') { - state.cmdFound = true; + state.stages[state.stages.length - 1].cmdFound = true; + } else if (result.command === 'from' && state.stages[state.stages.length - 1].instructionsProcessed !== 0) { + // Each FROM command constitutes a new stage in a multistage build + state.stages.push({ + instructionsProcessed: 0, + cmdFound: false + }); } // And we also care about knowing if this is the first command or not - state.instructionsProcessed = state.instructionsProcessed + 1; + state.stages[state.stages.length - 1].instructionsProcessed++; }; return state.items; @@ -116,7 +124,7 @@ function runLine(state, instructions, idx) { // check that the first command is a FROM, this might get reported twice, if the FROM command does exist, // but is not the time (non blank, non commented) line - if ((state.instructionsProcessed === 0 && cmd !== 'from') || (state.instructionsProcessed !== 0 && cmd === 'from')) { + if (state.stages[state.stages.length - 1].instructionsProcessed === 0 && cmd !== 'from') { items.push(messages.build(state.rules, 'from_first', line)); } diff --git a/test/index.js b/test/index.js index a4d4a35..ef7645d 100644 --- a/test/index.js +++ b/test/index.js @@ -101,15 +101,9 @@ describe("index", function(){ { title: 'First Command Must Be FROM', rule: 'from_first', line: 4 }, - { title: 'First Command Must Be FROM', - rule: 'from_first', - line: 5 }, { title: 'Base Image Missing Tag', rule: 'missing_tag', line: 5 }, - { title: 'First Command Must Be FROM', - rule: 'from_first', - line: 6 }, { title: 'Base Image Latest Tag', rule: 'latest_tag', line: 6 },