generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse_yaml.js
99 lines (90 loc) · 3.95 KB
/
parse_yaml.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const yaml = require('js-yaml');
const fs = require('fs');
const path = require('path')
const { native_commands, font, buildpacks, builders } = require('./constants')
const keyProcessorMap = {
'dockerfile': processDockerfile,
'commands': processCommands,
'buildpack': processBuildpack
};
function appendSubcommand(param, subcommand) {
if (!param) return [];
if (subcommand === native_commands.env) {
return Object.entries(param).flatMap(([key, value]) =>
String(value).split(',').flatMap(env => [subcommand, `${key}=${env.trim() || '""'}`])
);
} else {
return param.split(',').flatMap(element => [subcommand, element.trim()]);
}
}
function processDockerfile(build_parameters, name, tag) {
const dockerfile = build_parameters.dockerfile;
if (!dockerfile) return [];
const path = appendSubcommand(dockerfile, '-f');
const imageTag = appendSubcommand(`${name}:${tag ?? 'latest'}`, '-t');
const buildCommand = [native_commands.docker, native_commands.build, imageTag, path, '.'].flat();
return [buildCommand]
}
function processCommands(build_parameters) {
const commands = build_parameters.commands;
return commands ? commands.map(command => command.trim().split(" ")) : [];
}
function processBuildpack(build_parameters, name, tag) {
let { type, path, env, builder } = build_parameters.buildpack;
type = type ? type.split(',').map(type => {
type = type.trim()
if (buildpacks.has(type)) {
return `paketo-buildpacks/${type}`
} else {
return type
}
}) : []
if (builders.has(builder)){
builder = `paketobuildpacks/${builder}`
}
const imageNameWtihTag = `${name}:${tag}`;
const imageCmd = appendSubcommand(imageNameWtihTag, native_commands.build);
const pathCmd = appendSubcommand(path, native_commands.path);
const buildpackCmd = []
type.forEach(type => {
buildpackCmd.push(appendSubcommand(type, native_commands.buildpack))
})
const builderCmd = appendSubcommand(builder, native_commands.builder);
const envCmd = appendSubcommand(env, native_commands.env);
// for pack command
const buildCommand = [native_commands.pack, imageCmd, pathCmd, buildpackCmd, builderCmd, envCmd].flat()
return [buildCommand]
}
function parse_yaml(filename, repositoryOption = '') {
try {
const moduleFile = yaml.load(fs.readFileSync(path.join(process.cwd(), filename), 'utf8'))
const { repository: repositoryModule, modules, tag: globalTag = 'latest' } = moduleFile
const repository = repositoryOption == '' ? repositoryModule : repositoryOption
const before_all = moduleFile['before-all'] ? moduleFile['before-all'].map(command => command.trim().split(" ")) : []
if (!modules) throw new Error("Modules not defined")
const commands = modules.map((module) => {
const name = module.name, tag = module.tag || globalTag
const build_parameters = module['build-parameters']
const source_image = `${name}:${tag}`;
const repo_image = `${repository}/${source_image}`;
//tag command
const tagCmd = [native_commands.docker, native_commands.tag, source_image, repo_image];
//push command
const pushCmd = [native_commands.docker, native_commands.push, repo_image];
const buildType = keyProcessorMap[Object.keys(build_parameters)]
if (!buildType) throw new Error(`build-parameters ${Object.keys(build_parameters)} is invalid.`)
return {
name,
tag,
buildCmd: buildType(build_parameters, name, tag),
tagCmd,
pushCmd
}
})
return { commands, before_all, repository };
} catch (e) {
console.error(`${font.red}${e.message}${font.reset}`)
return { commands: [], before_all: [], repsository: '' }
}
}
module.exports = { parse_yaml };