-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmason.js
236 lines (186 loc) · 5.95 KB
/
mason.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
228
229
230
231
232
233
234
235
236
const mason = require("@joomlatools/mason-tools-v1");
const path = require("path");
const fs = require("fs").promises;
const exec = require('child_process').exec;
const pagesRoot = process.cwd();
async function build({ config = {} }) {
config = mason.config.merge(
{
location: pagesRoot,
destination: `${pagesRoot}/artifacts/com_pages.zip`,
compress: true,
},
config
);
const { path: tmp, cleanup } = await mason.fs.getTemporaryDirectory();
const pkg = `${tmp}/package`;
await fs.mkdir(pkg);
mason.log.debug(`Using ${tmp} folder for extension build`);
mason.log.debug(`Using ${config.location} folder for extension source`);
await mason.fs.copyWithoutHiddenFiles(`${config.location}/code`, pkg);
await mason.fs.ensureDir(`${config.location}/artifacts`);
if (config.compress) {
mason.log.debug(`Creating ZIP file in ${config.destination}`);
await mason.fs.archiveDirectory(pkg, config.destination);
} else {
mason.log.debug(`Copying final package to ${config.destination}`);
await mason.fs.copy(pkg, config.destination);
}
await cleanup();
}
function execShellCommand(cmd) {
return new Promise((resolve, reject) => {
exec(cmd, (error, stdout, stderr) => {
if (error) {
reject(error);
}
resolve(stdout ? stdout : stderr);
});
});
}
async function buildExtensions({ config = {} }) {
config = mason.config.merge(
{
location: pagesRoot,
folder: "contrib",
},
{ ...config, ...(config.extension || {}) }
);
const contribFolder = `${config.location}/${config.folder}`;
if (!require("fs").existsSync(contribFolder)) {
mason.log.error(`${contribFolder} does not exist.`);
}
await mason.fs.ensureDir(`${config.location}/artifacts`);
const extensionFolder = `${contribFolder}/extensions`;
if (require("fs").existsSync(extensionFolder)) {
const extensionFolders = await fs.readdir(extensionFolder, {
withFileTypes: true,
});
await Promise.all(
extensionFolders
.filter((dirent) => dirent.isDirectory())
.map(async (dirent) => {
const folder = dirent.name;
mason.log.debug(`Building extension: ${folder}…`);
const cmd = `<?php
$phar = new PharData('${config.location}/artifacts/extension-${folder}.zip');
$phar->buildFromDirectory('${config.location}/contrib/extensions/${folder}');
$phar->compressFiles(Phar::GZ);
$phar->setSignatureAlgorithm(Phar::SHA256);`
.replace(/\s+/g, ' ')
.replace(/[\$]+/g, '\\$');
return execShellCommand(`echo "${cmd}" | php`);
})
);
}
const siteFolder = `${contribFolder}/sites`;
if (require("fs").existsSync(siteFolder)) {
const siteFolders = await fs.readdir(siteFolder, {
withFileTypes: true,
});
await Promise.all(
siteFolders
.filter((dirent) => dirent.isDirectory())
.map((dirent) => {
const folder = dirent.name;
mason.log.debug(`Building site: ${folder}…`);
return mason.fs.archiveDirectory(
path.join(siteFolder, folder),
`${config.location}/artifacts/site-${folder}.zip`
);
})
);
}
}
async function buildFramework({ config = {} }) {
const projectsFolder = path.resolve(pagesRoot, "..");
config = mason.config.merge(
{
source: "remote", // or 'local'
location: `${projectsFolder}/joomlatools-framework`,
destination: `${projectsFolder}/joomlatools-framework/joomlatools-framework.zip`,
branch: "master",
includeComponents: false,
},
{ ...config, ...(config.framework || {}) }
);
let frameworkMasonfile;
let tmp, cleanup;
if (config.source !== "remote" && config.location) {
frameworkMasonfile = `${config.location}/mason.js`;
} else {
({ path: tmp, cleanup } = await mason.fs.getTemporaryDirectory());
mason.log.debug(`Using ${tmp} folder for framework build`);
config.location = `${tmp}/framework`;
frameworkMasonfile = `${config.location}/mason.js`;
await mason.github.download({
repo: "joomlatools/joomlatools-framework",
branch: config.branch,
destination: config.location,
});
}
const frameworkMason = await mason.core.getMasonFile(frameworkMasonfile);
await frameworkMason.tasks.build({
config: { ...frameworkMason.config, ...config, source: "local" },
});
cleanup && (await cleanup());
}
async function bundle({ config = {} }) {
config = mason.config.merge(
{
githubToken: null,
framework: {},
bundle: {
manifest: {},
},
},
config
);
if (!config.githubToken) {
throw new Error(
"Github token is required for bundle task to download joomlatools-extension-installer"
);
}
const { path: tmp, cleanup } = await mason.fs.getTemporaryDirectory();
mason.log.debug(`Using ${tmp} folder for bundle`);
await buildFramework({
config: {
...config.framework,
compress: false,
destination: `${tmp}`,
},
});
await build({
config: {
...config,
compress: false,
destination: `${tmp}/libraries/joomlatools-components/pages`,
},
});
const xmlManifest = (
await fs.readFile(`${tmp}/libraries/joomlatools-components/pages/version/version.php`)
).toString();
const version = xmlManifest.match(/VERSION\s*=\s*'(.*?)'/);
await mason.fs.ensureDir(`${pagesRoot}/artifacts`);
await mason.fs.archiveDirectory(
tmp,
`${pagesRoot}/artifacts/com_pages${version ? "_v" + version[1] : "_bundle"}.zip`
);
await cleanup();
}
module.exports = {
version: "1.0",
options: {
githubToken: {
type: "string",
description: "Github token for packaging. Required for bundle task",
},
},
tasks: {
build,
buildExtensions,
buildFramework,
bundle,
default: ["bundle" , 'buildExtensions'],
},
};