-
Notifications
You must be signed in to change notification settings - Fork 4
/
umbraco_package.js
227 lines (190 loc) · 6.39 KB
/
umbraco_package.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
222
223
224
225
226
227
module.exports = function (grunt) {
grunt.registerMultiTask('umbracoPackage', 'Create Umbraco Package', function () {
var done = this.async();
var Guid = require('guid');
var path = require('path');
var rimraf = require('rimraf');
var archiver = require('archiver');
var fs = require('fs');
var fse = require('fs-extra');
var chalk = require('chalk');
var xml = require('js2xmlparser');
var options = this.options({
minimumUmbracoVersion: '',
files: [],
cwd: '/',
readme: ''
});
requireOptions(['name', 'version', 'license', 'licenseUrl', 'url', 'author', 'authorUrl'], options);
validateDirectories(this.files, options);
// Declare the name of the generated ZIP file
var packageFileName = options.outputName ? options.outputName : options.name + '_' + options.version + '.zip';
// Ensure output directory exists
if (!fs.existsSync(options.outputDir)) {
fs.mkdirSync(options.outputDir);
}
// Declare the path of the generated ZIP file
var tmpOutput = path.join(options.outputDir, packageFileName);
// Delete the ZIP file if it already exists (eg. already been generated for same version)
if (fs.existsSync(tmpOutput)) fs.unlinkSync(tmpOutput);
// Gather files
var filesToPackage = [];
this.files.forEach(function (file) {
file.src.forEach(function (sourceDir) {
var sourceDirFiles = getFilesRecursive(sourceDir);
filesToPackage.push.apply(filesToPackage, sourceDirFiles.map(function (f) {
return {
guid: f.name,
dir: f.dir.replace(sourceDir, ''),
sourceDir: sourceDir,
name: f.name,
ext: f.name.split('.').pop()
};
}));
});
});
// Avoid duplicate GUIDs
var guids = {};
filesToPackage.forEach(function(file) {
if (guids[file.guid]) {
file.guid = Guid.raw() + '_' + file.guid;
}
guids[file.guid] = true;
});
// Initialize the archive and it's output stream
var output = fs.createWriteStream(tmpOutput);
var archive = archiver('zip');
// Listen for when the ZIP is closed
output.on('close', function () {
console.log('Package created at ' + chalk.cyan(tmpOutput) + " (" + chalk.cyan(archive.pointer()).toString() + " bytes)");
done(true);
});
// Listen for ZIP errors
archive.on('error', function (err) {
throw err;
});
// Set the output stream of the ZIP
archive.pipe(output);
if (options.manifest) {
// Load / transform XML Manifest
options.files = filesToPackage;
var manifest = grunt.file.read(options.manifest);
manifest = grunt.template.process(manifest, { data: options });
// Append the manifest to the ZIP file
archive.append(manifest, { name:'package.xml' });
// Append files from the source directory
filesToPackage.forEach(function(file) {
// Get the original path of the file
var src = path.join(file.sourceDir, file.dir, file.name);
// Append the file to the ZIP
archive.append(fs.createReadStream(src), { name: file.guid });
});
} else {
// Build the data for the package manifest
var data = {
info: {
package: {
name: options.name,
version: options.version,
license: {
'@': {
url: ''
},
'#': options.license
},
url: options.url,
requirements: {
major: 0,
minor: 0,
patch: 0
}
},
author: {
name: options.author
},
readme: options.readme
},
DocumentTypes: {},
Templates: {},
Stylesheets: {},
Macros: {},
DictionaryItems: {},
Languages: {},
DataTypes: {},
control: {},
Actions: {},
files: {},
};
// Set optional URLs in the manifest
if (options.licenseUrl) data.info.package.license['@'].url = options.licenseUrl;
if (options.authorUrl) data.info.author['website'] = options.authorUrl;
// Add files to the ZIP and manifest
if (filesToPackage.length > 0) {
data.files = [];
filesToPackage.forEach(function(file) {
// Get the original path of the file
var src = path.join(file.sourceDir, file.dir, file.name);
// Append the file to the ZIP
archive.append(fs.createReadStream(src), { name: file.guid });
// Append the file to the manifest
data.files.push({
guid: file.guid,
orgPath: file.dir,
orgName: file.name
});
});
}
// Append the manifest to the ZIP file
archive.append(xml('umbPackage', data, {
arrayMap: {
files: 'file'
},
prettyPrinting: {
enabled: true,
returnString: "\r\n"
}
}), { name:'package.xml' });
}
archive.finalize();
function getFilesRecursive(dir) {
var ret = [];
var files = fs.readdirSync(dir);
for (var i in files) {
if (!files.hasOwnProperty(i)) continue;
var name = dir + '/' + files[i];
if (fs.statSync(name).isDirectory()) {
ret.push.apply(ret, getFilesRecursive(name));
} else {
ret.push({ dir: dir, name: files[i] });
}
}
return ret;
}
function requireOptions (opts, options) {
opts.forEach(function (opt) {
if (!options.hasOwnProperty(opt) || options[opt] == null || options[opt].toString().length == 0) {
grunt.fail.warn('Error creating Umbraco Package - required property missing: ' + opt);
return;
}
})
};
function validateDirectories (files, options) {
if (files.length < 1) {
grunt.fail.warn('Error creating Umbraco Package - no source specified');
return;
}
var src = files[0].src[0];
var dest = files[0].dest;
if (src == null || src.length == 0) {
grunt.fail.warn('Error creating Umbraco Package - no source specified');
return;
}
if (dest == null || dest.length == 0) {
grunt.fail.warn('Error creating Umbraco Package - no source specified');
return;
}
options.sourceDir = src;
options.outputDir = dest;
}
});
};