Skip to content

Commit

Permalink
fix(Runner): use appropriate preset template directory
Browse files Browse the repository at this point in the history
  • Loading branch information
RWOverdijk committed Feb 1, 2018
1 parent bcfbc4d commit ded4867
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions lib/Runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Runner {
if (this.config.tasks[task]) {
parameters._tasks = this.config.tasks;

return this.config.tasks[task];
return { instructions: this.config.tasks[task] };
}

// Preset. Check if exists.
Expand All @@ -29,20 +29,19 @@ class Runner {
throw new Error(`Preset "${presetName}" not found.`);
}

parameters.fallbackSourceDirectory = preset.templateRoot;
parameters._tasks = preset.tasks;

parameters._tasks = preset.tasks;

if (!preset.tasks[task] && task === 'tasks') {
return presets.default.tasks.tasks;
return { instructions: presets.default.tasks.tasks, fallbackSourceDirectory: presets.default.templateRoot };
}

return preset.tasks[task];
return { instructions: preset.tasks[task], fallbackSourceDirectory: preset.templateRoot };
}

getPresets() {
if (!this.presets) {
this.presets = this.boards.getPlugins();
this.presets.default = require('boards-preset-default');
this.presets = this.boards.getPlugins();
}

return this.presets;
Expand All @@ -56,9 +55,12 @@ class Runner {

apply(task, parameters) {
let instructions = task;
let fallbackSourceDirectory;

if (typeof task === 'string') {
instructions = this.composeTask(task, parameters);
const composed = this.composeTask(task, parameters);
instructions = composed.instructions;
fallbackSourceDirectory = composed.fallbackSourceDirectory;

if (!instructions) {
return Promise.reject(new Error(`Instructions for task "${task}" not found.`));
Expand All @@ -79,10 +81,10 @@ class Runner {
let runner;

if (previousTask) {
runner = previousTask.then(() => this.runTask(instruction, parameters));
runner = previousTask.then(() => this.runTask(instruction, parameters, fallbackSourceDirectory));
previousTask = null;
} else {
runner = this.runTask(instruction, parameters);
runner = this.runTask(instruction, parameters, fallbackSourceDirectory);
}

if (instruction.sync) {
Expand All @@ -94,6 +96,10 @@ class Runner {
}

prepareParams(method, params) {
if (typeof method !== 'function' && typeof method === 'object') {
return Homefront.merge(params, method);
}

const preparedParams = method(params);

if (typeof preparedParams === 'object') {
Expand All @@ -103,11 +109,11 @@ class Runner {
return params;
}

runTask(instruction, parameters) {
runTask(instruction, parameters, fallbackSourceDirectory) {
instruction.from = instruction.from || '';

// Prepare for step. Copy parameters to not affect other tasks.
if (typeof instruction.prepare === 'function') {
if (instruction.prepare) {
parameters = this.prepareParams(instruction.prepare, Homefront.merge({}, parameters));
}

Expand Down Expand Up @@ -138,7 +144,7 @@ class Runner {
throw new Error(`Invalid task "${instruction.task}" supplied`);
}

return this[instruction.task](instruction, parameters);
return this[instruction.task](instruction, parameters, fallbackSourceDirectory);
}

modify(instruction, parameters) {
Expand All @@ -161,11 +167,12 @@ class Runner {
}));
}

generate(instruction, parameters) {
generate(instruction, parameters, fallbackSourceDirectory = null) {
if (!instruction.glob) {
const template = this.getTarget(instruction.template, parameters);
const template = this.getTarget(instruction.template, parameters);
const templateDirectory = this.getTemplateDirectory(instruction, parameters, fallbackSourceDirectory);

return this.applyGenerate(template, instruction.target, this.getTemplateDirectory(instruction, parameters), instruction, parameters);
return this.applyGenerate(template, instruction.target, templateDirectory, instruction, parameters);
}

const glob = this.getTarget(instruction.glob, parameters);
Expand All @@ -178,8 +185,8 @@ class Runner {
globPromises.push(Promise.resolve([]));
}

if (parameters.fallbackSourceDirectory) {
globPromises.push(this.glob(glob, path.join(parameters.fallbackSourceDirectory, instruction.from)));
if (fallbackSourceDirectory) {
globPromises.push(this.glob(glob, path.join(fallbackSourceDirectory, instruction.from)));
}

return Promise.all(globPromises)
Expand All @@ -197,19 +204,19 @@ class Runner {
if (globbed[1]) {
globbed[1]
.filter(match => !projectMatches.includes(match))
.forEach(match => makeApply(match, parameters.fallbackSourceDirectory));
.forEach(match => makeApply(match, fallbackSourceDirectory));
}

return Promise.all(generates);
});
}

getTemplateDirectory(instruction, parameters) {
getTemplateDirectory(instruction, parameters, fallbackSourceDirectory = null) {
if (fileExists.sync(path.resolve(this.config.templateRoot, instruction.template))) {
return this.config.templateRoot;
}

const fallback = parameters.fallbackSourceDirectory;
const fallback = fallbackSourceDirectory;

if (!fallback) {
throw new Error(`Template "${instruction.template}" not found.`);
Expand Down

0 comments on commit ded4867

Please sign in to comment.