-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathsteps.ts
More file actions
339 lines (272 loc) · 10.6 KB
/
Copy pathsteps.ts
File metadata and controls
339 lines (272 loc) · 10.6 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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import { readFile, writeFile } from "node:fs/promises";
import { dirname, join, parse, relative, resolve } from "path";
import chalk from "chalk";
import {
CommonBuildConfig,
getModuleConfigs,
getWidgetConfigs,
ModuleBuildConfig,
WidgetBuildConfig
} from "./build-config";
import { cloneRepo, cloneRepoShallow, setLocalGitUserInfo } from "./git";
import { copyMpkFiles, getMpkPaths } from "./monorepo";
import { createModuleMpkInDocker } from "./mpk";
import { ModuleInfo, PackageInfo, WidgetInfo } from "./package-info";
import { addFilesToPackageXml, PackageType } from "./package-xml";
import { chmod, cp, ensureFileExists, exec, find, mkdir, popd, pushd, rm, unzip, zip } from "./shell";
type Step<Info, Config> = (params: { info: Info; config: Config }) => Promise<void>;
export type CommonStepParams = {
info: PackageInfo;
config: CommonBuildConfig;
};
export type WidgetStepParams = {
info: WidgetInfo;
config: WidgetBuildConfig;
};
export type ModuleStepParams = {
info: ModuleInfo;
config: ModuleBuildConfig;
};
export const logStep = (name: string): void => console.info(`[step]: ${name}`);
// Common steps
export async function removeDist({ config }: CommonStepParams): Promise<void> {
logStep("Remove dist");
rm("-rf", config.paths.dist);
}
export async function cloneTestProject({ info, config }: CommonStepParams): Promise<void> {
logStep("Clone test project");
const { testProject } = info;
const clone = process.env.CI ? cloneRepoShallow : cloneRepo;
rm("-rf", config.paths.targetProject);
await clone({
remoteUrl: testProject!.githubUrl,
branch: testProject!.branchName,
localFolder: config.paths.targetProject
});
}
type CopyFileEntry = {
/** File path relative to project root */
filePath: string;
/** Path relative to package (will be included in package.xml) */
pkgPath: string;
};
/**
* Copy files to mpk
*
* Example:
* copyFilesToMpk([
* { filePath: "LICENSE", pkgPath: "themesource/mymodule/LICENSE" },
* { filePath: "src/index.js", pkgPath: "some/deep/data.js" },
* { filePath: "package.json", pkgPath: "boba/zuza/out.json" }
* ])
*/
export function copyFilesToMpk(
files: CopyFileEntry[],
packageType: PackageType
): (params: CommonStepParams) => Promise<void> {
return async ({ config }) => {
logStep("Copy files to mpk");
const { paths, output } = config;
const mpk = output.files.mpk;
const target = join(paths.tmp, "copyfiles_tmp");
const packageXml = join(target, "package.xml");
console.log(`Input MPK: ${relative(paths.package, mpk)}`);
ensureFileExists(output.files.mpk);
rm("-rf", target);
mkdir("-p", target);
console.info("Unzip module mpk");
await unzip(mpk, target);
for (const file of files) {
const source = resolve(paths.package, file.filePath);
const dest = resolve(target, file.pkgPath);
console.info(`Copy ${join(file.filePath)} to ${join(file.pkgPath)}`);
mkdir("-p", dirname(dest));
cp(source, dest);
}
console.info(`Update file entries in package.xml`);
await addFilesToPackageXml(
packageXml,
files.map(f => f.pkgPath),
packageType
);
console.info("Create module zip archive");
rm(mpk);
await zip(target, mpk);
rm("-rf", target);
};
}
// Module steps
export async function copyThemesourceToProject({ config }: ModuleStepParams): Promise<void> {
logStep("Copy module themesource to project");
const { output, paths } = config;
console.info("Remove module themesource in targetProject");
rm("-rf", output.dirs.themesource);
console.info("Copy module themesource to targetProject");
mkdir("-p", output.dirs.themesource);
cp("-R", `${paths.themesource}/*`, output.dirs.themesource);
}
export function copyActionsFiles(files: string[]): (params: ModuleStepParams) => Promise<void> {
return async ({ config }) => {
logStep("Copy JS Action(s) files");
const { paths, output } = config;
mkdir("-p", join(output.dirs.javascriptsource, "actions"));
for (const file of files) {
const src = join(paths.javascriptsource, "actions", file);
const dest = join(output.dirs.javascriptsource, "actions", file);
const content = await readFile(src, { encoding: "utf-8" });
// Studio Pro require CRLF endings to read action file.
console.log(">", dest);
await writeFile(dest, content.replace(/\r\n|\r|\n/g, "\r\n"));
}
};
}
export async function copyWidgetsToProject({ config }: ModuleStepParams): Promise<void> {
logStep("Copy module widgets to project");
await copyMpkFiles(config.dependencies, config.output.dirs.widgets);
}
export async function createModuleMpk({ info, config }: ModuleStepParams): Promise<void> {
logStep("Create module mpk");
await createModuleMpkInDocker(
config.paths.targetProject,
info.mxpackage.name,
info.marketplace.minimumMXVersion,
"^(resources|userlib)/.*"
);
}
async function insertWidgetsIntoMpk(
mpk: string,
widgetPaths: string[],
tmpDir: string,
additionalFiles?: Array<{ src: string; destRelative: string }>
): Promise<void> {
const mpkEntry = parse(mpk);
const widgetsOut = join(tmpDir, "widgets");
const packageXml = join(tmpDir, "package.xml");
const packageFilePaths = widgetPaths.map(p => `widgets/${parse(p).base}`);
rm("-rf", tmpDir);
console.info("Unzip module mpk");
await unzip(mpk, tmpDir);
mkdir("-p", widgetsOut);
chmod("-R", "a+rw", tmpDir);
console.info(`Add ${widgetPaths.length} widgets to ${mpkEntry.base}`);
cp(widgetPaths, widgetsOut);
if (additionalFiles) {
for (const { src, destRelative } of additionalFiles) {
cp(src, join(tmpDir, destRelative));
}
}
console.info(`Add file entries to package.xml`);
await addFilesToPackageXml(packageXml, packageFilePaths, "modelerProject");
rm(mpk);
console.info("Create module zip archive");
await zip(tmpDir, mpk);
rm("-rf", tmpDir);
}
export async function addWidgetsToMpk({ config }: ModuleStepParams): Promise<void> {
logStep("Add widgets to mpk");
const mpk = config.output.files.modulePackage;
const widgets = await getMpkPaths(config.dependencies);
const tmpDir = join(parse(mpk).dir, "tmp");
await insertWidgetsIntoMpk(mpk, widgets, tmpDir, [
{ src: join(config.paths.package, "LICENSE"), destRelative: "License.txt" }
]);
}
export function addTestProjectWidgetsToMpk(widgetFileNames: string[]): (params: ModuleStepParams) => Promise<void> {
return async ({ config }) => {
logStep("Add test project widgets to mpk");
const mpk = config.output.files.modulePackage;
const widgetPaths = widgetFileNames.map(name => join(config.output.dirs.widgets, name));
const tmpDir = join(parse(mpk).dir, "tmp_local_widgets");
await insertWidgetsIntoMpk(mpk, widgetPaths, tmpDir);
};
}
export async function moveModuleToDist({ info, config }: ModuleStepParams): Promise<void> {
logStep("Move module to dist");
const { output, paths } = config;
console.info(`Move ${info.mpkName} to dist`);
mkdir("-p", join(paths.dist, info.version.format()));
// Can't use mv because of https://github.com/shelljs/shelljs/issues/878
cp(output.files.modulePackage, output.files.mpk);
rm(output.files.modulePackage);
}
export async function pushUpdateToTestProject({ info, config }: ModuleStepParams): Promise<void> {
logStep("Push update to test project");
if (!process.env.CI) {
console.warn(chalk.yellow("You run script in non CI env"));
console.warn(chalk.yellow("Set CI=1 in your env if you want to push changes to remote test project"));
console.warn(chalk.yellow("Skip push step"));
return;
}
const { paths } = config;
pushd(paths.targetProject);
const status = (await exec(`git status --porcelain`, { stdio: "pipe" })).stdout.trim();
if (status === "") {
console.warn(chalk.yellow("Nothing to commit"));
console.warn(chalk.yellow("Skip push step"));
return;
}
await setLocalGitUserInfo();
await exec(`git add .`);
await exec(`git commit -m "Automated update for ${info.mxpackage.name} module [${info.version}]"`);
await exec(`git push origin`);
popd();
}
export async function writeModuleVersion({ config, info }: ModuleStepParams): Promise<void> {
logStep("Write module version");
mkdir("-p", config.output.dirs.themesource);
await writeFile(join(config.output.dirs.themesource, ".version"), info.version.format());
}
export async function copyModuleLicense({ config }: ModuleStepParams): Promise<void> {
logStep("Copy module license");
const { paths, output } = config;
const license = join(paths.package, "LICENSE");
ensureFileExists(license);
mkdir("-p", output.dirs.themesource);
cp(license, output.dirs.themesource);
}
export async function writeVersionAndLicenseToJSActions({ config, info }: ModuleStepParams): Promise<void> {
logStep("Write module version & LICENSE");
const { paths, output } = config;
const license = join(paths.package, "LICENSE");
ensureFileExists(license);
mkdir("-p", output.dirs.javascriptsource);
cp(license, output.dirs.javascriptsource);
await writeFile(join(config.output.dirs.javascriptsource, ".version"), info.version.format());
}
export async function runSteps<Info, Config>(params: {
packageInfo: Info;
config: Config;
steps: Array<Step<Info, Config>>;
}): Promise<void> {
for (const step of params.steps) {
await step({ info: params.packageInfo, config: params.config });
}
}
type RunWidgetStepsParams = {
packagePath: string;
steps: Array<Step<WidgetInfo, WidgetBuildConfig>>;
};
export async function runWidgetSteps(params: RunWidgetStepsParams): Promise<void> {
const [packageInfo, config] = await getWidgetConfigs(params.packagePath);
await runSteps({
packageInfo,
config,
steps: params.steps
});
}
type RunModuleStepsParams = {
packagePath: string;
steps: Array<Step<ModuleInfo, ModuleBuildConfig>>;
};
export async function runModuleSteps(params: RunModuleStepsParams): Promise<void> {
const [packageInfo, config] = await getModuleConfigs(params.packagePath);
if (process.env.DEBUG) {
console.dir(packageInfo, { depth: 10 });
console.dir(config, { depth: 10 });
}
await runSteps({
packageInfo,
config,
steps: params.steps
});
}