|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const Yaml = require('js-yaml'); |
| 4 | + |
| 5 | +const Loader = require('../loader'); |
| 6 | +const Utils = require('../utils'); |
| 7 | + |
| 8 | +const TravisMerge = require('./merge'); |
| 9 | + |
| 10 | + |
| 11 | +const internals = { |
| 12 | + validMergeModes: new Set(['deep_merge_append', 'deep_merge_prepend', 'deep_merge', 'merge']) |
| 13 | +}; |
| 14 | + |
| 15 | + |
| 16 | +internals.normalizeImports = (travisYaml, { relativeTo, breadcrumb }) => { |
| 17 | + |
| 18 | + const context = relativeTo ? relativeTo.source : '.travis.yml'; |
| 19 | + |
| 20 | + return Utils.toArray(travisYaml.import) |
| 21 | + .map((entry) => { |
| 22 | + |
| 23 | + if (typeof entry === 'string') { |
| 24 | + entry = { source: entry }; |
| 25 | + } |
| 26 | + |
| 27 | + const original = entry.source; |
| 28 | + |
| 29 | + if (entry.source.startsWith('./')) { |
| 30 | + entry.source = entry.source.substring(2); |
| 31 | + |
| 32 | + if (relativeTo) { |
| 33 | + const relativeParts = relativeTo.source.split('/'); |
| 34 | + relativeParts.pop(); |
| 35 | + relativeParts.push(entry.source); |
| 36 | + entry.source = relativeParts.join('/'); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + if (!entry.mode) { |
| 41 | + entry.mode = 'deep_merge_append'; |
| 42 | + } |
| 43 | + |
| 44 | + if (!internals.validMergeModes.has(entry.mode)) { |
| 45 | + throw new Error(`Invalid merge mode for ${original} in ${context}: ${entry.mode}`); |
| 46 | + } |
| 47 | + |
| 48 | + if (original.includes('@')) { |
| 49 | + throw new Error(`Importing at commitish unsupported in ${context}: ${original}`); |
| 50 | + } |
| 51 | + |
| 52 | + const alreadyImported = breadcrumb.indexOf(entry.source); |
| 53 | + if (alreadyImported >= 0) { |
| 54 | + throw new Error(`Circular dependency ${entry.source} requested by ${context} (already imported at ${breadcrumb[alreadyImported - 1]})`); |
| 55 | + } |
| 56 | + |
| 57 | + return entry; |
| 58 | + }) |
| 59 | + .filter((entry) => !entry.if); // @todo: log a warning |
| 60 | +}; |
| 61 | + |
| 62 | + |
| 63 | +internals.loadSource = async (source, { loadFile }) => { |
| 64 | + |
| 65 | + let path = source; |
| 66 | + |
| 67 | + if (source.includes(':')) { |
| 68 | + const [repository, fileName] = source.split(':'); |
| 69 | + const loader = await Loader.create({ repository: `https://github.com/${repository}` }); |
| 70 | + |
| 71 | + path = fileName; |
| 72 | + loadFile = loader.loadFile; |
| 73 | + } |
| 74 | + |
| 75 | + return loadFile(path); |
| 76 | +}; |
| 77 | + |
| 78 | + |
| 79 | +exports.apply = async (yaml, { loadFile, relativeTo, breadcrumb = ['.travis.yml'] }) => { |
| 80 | + |
| 81 | + if (!yaml.import) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const imports = internals.normalizeImports(yaml, { relativeTo, breadcrumb }); |
| 86 | + |
| 87 | + for (const entry of imports) { |
| 88 | + |
| 89 | + const buffer = await internals.loadSource(entry.source, { loadFile }); |
| 90 | + |
| 91 | + const imported = Yaml.safeLoad(buffer, { |
| 92 | + schema: Yaml.FAILSAFE_SCHEMA, |
| 93 | + json: true |
| 94 | + }); |
| 95 | + |
| 96 | + await exports.apply(imported, { loadFile, relativeTo: entry, breadcrumb: [...breadcrumb, entry.source] }); |
| 97 | + |
| 98 | + delete imported.import; |
| 99 | + |
| 100 | + TravisMerge[entry.mode](yaml, imported); |
| 101 | + } |
| 102 | +}; |
0 commit comments