-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathspec.js
More file actions
82 lines (69 loc) · 2.38 KB
/
spec.js
File metadata and controls
82 lines (69 loc) · 2.38 KB
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
'use strict';
const handlebars = require('handlebars');
const fs = require('fs');
const path = require('path');
const getServiceProperties = require('./serviceProperties');
const templateFile = fs.readFileSync(path.resolve(__dirname, '../templates/spec.hbs'), 'utf-8');
const template = handlebars.compile(templateFile);
const defaultRelease = 1;
function getReleaseNumber(release) {
if (release) {
return release;
}
return defaultRelease;
}
function getVersionNumber({ spec, version }) {
const replaceHyphens = getValueFromSpec(spec, 'replaceHyphens', false);
const allowedReplacements = ['~', '_', '+'];
let newVersion;
if (replaceHyphens === true) {
// Stay backwards compatible. Previous version was true -> tilde.
newVersion = version.replace(/-/g, '~');
} else if (typeof replaceHyphens === 'string') {
if (allowedReplacements.includes(replaceHyphens)) {
newVersion = version.replace(/-/g, replaceHyphens);
} else {
throw new Error(`replaceHyphens was given a forbidden string, so no replacement was done. Please use one of ${allowedReplacements.join('')}`);
}
}
return newVersion || version;
}
function getValueFromSpec(spec, key, fallback) {
if (spec && key in spec) {
return spec[key];
}
return fallback;
}
function getExecutableFiles(pkg) {
const name = pkg.name;
const executableFiles = getValueFromSpec(pkg.spec, 'executable', []).map((file) => {
return path.join('/usr/lib/', name, file);
});
return {
executableFiles,
hasExecutableFiles: executableFiles.length !== 0
};
}
module.exports = function (pkg, release) {
const specProperties = Object.assign(
{
release: getReleaseNumber(release),
requires: getValueFromSpec(pkg.spec, 'requires', []),
buildRequires: getValueFromSpec(pkg.spec, 'buildRequires', []),
buildSteps: getValueFromSpec(pkg.spec, 'buildSteps'),
postInstallCommands: getValueFromSpec(pkg.spec, 'post', []),
nodeVersion: getValueFromSpec(pkg.spec, 'nodeVersion'),
version: getVersionNumber(pkg),
license: pkg.license,
prune: getValueFromSpec(pkg.spec, 'prune', true),
rebuild: getValueFromSpec(pkg.spec, 'rebuild', true)
},
getExecutableFiles(pkg),
getServiceProperties(pkg)
);
if (specProperties.buildSteps) {
specProperties.prune = false;
specProperties.rebuild = false;
}
return template(specProperties);
};