forked from rictorres/node-rpm-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
221 lines (181 loc) · 5.8 KB
/
index.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
'use strict';
var chalk = require('chalk');
var exec = require('child_process').exec;
var fsx = require('fs-extra');
var globby = require('globby');
var path = require('path');
var shortid = require('shortid');
var _ = require('lodash');
var writeSpec = require('./lib/spec');
var logger;
/**
* Creates the folder structure needed to create
* the RPM package.
*
* @param {String} tmpDir the path where the folder structure will reside
*/
function setupTempDir(tmpDir) {
var rpmStructure = ['BUILD', 'BUILDROOT', 'RPMS', 'SOURCES', 'SPECS', 'SRPMS'];
// If the tmpDir exists (probably from previous build), delete it first
if (fsx.existsSync(tmpDir)) {
logger(chalk.cyan('Removing old temporary directory.'));
fsx.removeSync(tmpDir);
}
// Create RPM folder structure
logger(chalk.cyan('Creating RPM directory structure at:'), tmpDir);
_.forEach(rpmStructure, function(dirName) {
fsx.mkdirpSync(path.join(tmpDir, dirName));
});
}
/**
* Expand the patterns/files (specified by the user)
* that should be ignored when building the RPM package.
*
* @param {Array} excludeFiles patterns/files to ignore
* @return {Array} expanded list of files to ignore
*/
function retrieveFilesToExclude(excludeFiles) {
return globby.sync(excludeFiles).map(function(file) {
return path.normalize(file);
});
}
function checkDirective(directive) {
if (typeof directive === 'undefined') {
return true;
}
return directive.match(/^(?:doc|config|attr|verify|docdir|dir)/);
}
/**
* 1. Normalize and expand the patterns/files (specified by the user)
* that should be included in the RPM package.
* 2. Copies the files to the respective destination.
*
* @param {Array} files patterns/files to include
* @param {Array} excludeFiles expanded list of files to ignore
* @param {String} buildRoot where all files should be copied into
* @return {Array} list of files to include in the RPM
*/
function prepareFiles(files, excludeFiles, buildRoot) {
var _files = [];
var filesToExclude = retrieveFilesToExclude(excludeFiles);
_.forEach(files, function(file) {
if (!file.hasOwnProperty('src') || !file.hasOwnProperty('dest')) {
throw new Error('All files/folders must have source (src) and destination (dest) set');
}
file.cwd = (file.cwd || '.') + '/';
var actualSrc = globby.sync(path.join(file.cwd, file.src));
fsx.ensureDir(path.join(buildRoot, file.dest));
_.forEach(actualSrc, function(srcFile) {
// Check whether to ignore this file
if (filesToExclude.indexOf(srcFile) > -1) {
return;
}
// files/folders should be copied
// taking into account the cwd
// so the destination should be
// relative to the cwd
var copyTarget = path.normalize(srcFile).replace(path.normalize(file.cwd), '');
var dest = path.join(file.dest, copyTarget);
if (checkDirective(file.directive)) {
_files.push({path: dest, directive: file.directive});
}
else {
throw new Error('Invalid file directive informed: ' + file.directive);
}
fsx.copySync(srcFile, path.join(buildRoot, dest));
});
});
return _files;
}
/**
* Runs the rpmbuild tool in a child process.
*
* @param {String} buildRoot where all included files reside
* @param {String} specFile path to the file from which the RPM package will be created
* @param {String} rpmDest where the .rpm file should be copied to
* @param {Function} cb callback function to be executed when the task is done
*/
function buildRpm(buildRoot, specFile, rpmDest, execOpts, cb) {
// Build the RPM package.
var cmd = [
'rpmbuild',
'-bb',
'-vv',
'--buildroot',
buildRoot,
specFile
].join(' ');
logger(chalk.cyan('Executing:'), cmd);
execOpts = execOpts || {};
exec(cmd, execOpts, function rpmbuild(err, stdout) {
if (err) {
return cb(err);
}
if (stdout) {
var rpm = stdout.match(/(\/.+\..+\.rpm)/);
if (rpm && rpm.length > 0) {
var rpmDestination = rpm[0];
if (rpmDest) {
rpmDestination = path.join(rpmDest, path.basename(rpmDestination));
logger(chalk.cyan('Copying RPM package to:'), rpmDestination);
fsx.copySync(rpm[0], rpmDestination);
}
return cb(null, rpmDestination);
}
}
});
}
function rpm(options, cb) {
if (!options || typeof options !== 'object') {
throw new TypeError('options object is missing');
}
if (!cb || typeof cb !== 'function') {
throw new TypeError('callback is missing');
}
var defaults = {
name: 'no-name',
summary: 'No summary',
description: 'No description',
version: '0.0.0',
release: '1',
license: 'MIT',
vendor: 'Vendor',
group: 'Development/Tools',
buildArch: 'noarch',
tempDir: 'tmp-' + shortid.generate(),
files: [],
excludeFiles: [],
rpmDest: process.cwd(),
keepTemp: false,
verbose: true,
execOpts: {}
};
options = _.defaults(options, defaults);
logger = options.verbose ? require('./lib/logger') : function() {
return;
};
var tmpDir = path.resolve(options.tempDir);
var buildRoot = path.join(tmpDir, '/BUILDROOT/');
var files = [];
setupTempDir(tmpDir);
try {
files = prepareFiles(options.files, options.excludeFiles, buildRoot);
} catch(ex) {
return cb(ex);
}
// Write spec file
var specFile = writeSpec(files, options);
logger(chalk.cyan('SPEC file created:'), specFile);
buildRpm(buildRoot, specFile, options.rpmDest, options.execOpts, function(err, rpm) {
if (err) {
return cb(err);
}
// Remove temp folder
if (!options.keepTemp) {
logger(chalk.cyan('Removing RPM directory structure at:'), tmpDir);
fsx.removeSync(tmpDir);
}
return cb(null, rpm);
});
}
module.exports = rpm;