Skip to content

Commit 6cb0ff7

Browse files
refactor: move getOssFiles and copyFilesToMpk functions to utils.ts for better organization
1 parent 75413a7 commit 6cb0ff7

3 files changed

Lines changed: 80 additions & 51 deletions

File tree

scripts/release/build-mpk.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import {
88
cloneRepo,
99
createMPK,
1010
exportModuleWithWidgets,
11-
regex,
12-
copyFilesToMpk,
13-
getOssFiles
11+
regex
1412
} from "./module-automation/commons";
13+
import { copyFilesToMpk, getOssFiles } from "./module-automation/utils";
1514

1615
const repoRootPath = join(__dirname, "../../");
1716

scripts/release/module-automation/commons.js

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { basename, extname, join, resolve } = require("path");
22
const { access, readdir, copyFile, readFile, writeFile, rename, rm, mkdir, chmod } = require("fs/promises");
33
const { exec } = require("child_process");
4-
const { globSync } = require("glob");
54

65
const regex = {
76
changelogs: /(?<=## \[unreleased\]\n)((?!## \[\d+\.\d+\.\d+\])\W|\w)*/i,
@@ -261,51 +260,6 @@ function unzip(src, dest) {
261260
return execShellCommand(`unzip "${src}" -d "${dest}"`);
262261
}
263262

264-
async function getOssFiles(folderPath, isOssReadmeRequired) {
265-
if (!folderPath || typeof folderPath !== "string") {
266-
throw new TypeError(`Invalid folderPath: ${folderPath}`);
267-
}
268-
269-
const licenseFile = join(folderPath, `License.txt`);
270-
try {
271-
await access(licenseFile);
272-
} catch {
273-
throw new Error(`License file not found at expected location: ${licenseFile}`);
274-
}
275-
276-
const readmeossPattern = "*__*__READMEOSS_*.html";
277-
const readmeossFiles = globSync(readmeossPattern, { cwd: folderPath, absolute: true, ignore: "**/.*/**" });
278-
279-
if (isOssReadmeRequired && readmeossFiles.length === 0) {
280-
throw new Error(`No OSS README file found in ${folderPath} matching ${readmeossPattern}`);
281-
}
282-
283-
if (readmeossFiles.length === 0) {
284-
return [{ src: licenseFile, dest: basename(licenseFile) }];
285-
}
286-
287-
const ossReadmeFile = readmeossFiles[0];
288-
289-
return [
290-
{ src: licenseFile, dest: basename(licenseFile) },
291-
{ src: ossReadmeFile, dest: basename(ossReadmeFile) }
292-
];
293-
}
294-
295-
async function copyFilesToMpk(files, mpkOutput, moduleName) {
296-
const projectPath = mpkOutput.replace(".mpk", "");
297-
// Unzip the mpk
298-
await unzip(mpkOutput, projectPath);
299-
await rm(mpkOutput, { recursive: true, force: true });
300-
// Add additional files to the MPK
301-
for await (const file of files) {
302-
await copyFile(file.src, join(projectPath, file.dest));
303-
}
304-
// Re-zip and rename
305-
await zip(projectPath, moduleName);
306-
await rename(`${projectPath}.zip`, mpkOutput);
307-
}
308-
309263
// Unzip the module, copy the widget and update package.xml
310264
async function exportModuleWithWidgets(moduleName, mpkOutput, widgetsFolders, additionalFiles) {
311265
console.log(`Adding ${widgetsFolders.length} widgets to module ${moduleName}`);
@@ -374,7 +328,5 @@ module.exports = {
374328
zip,
375329
unzip,
376330
exportModuleWithWidgets,
377-
copyFilesToMpk,
378-
getOssFiles,
379331
regex
380332
};
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { access, copyFile, rename, rm } from "fs/promises";
2+
import { basename, join } from "path";
3+
import { globSync } from "glob";
4+
import { exec } from "child_process";
5+
6+
export async function getOssFiles(
7+
folderPath: string,
8+
isOssReadmeRequired: boolean
9+
): Promise<Array<{ src: string; dest: string }>> {
10+
if (!folderPath || typeof folderPath !== "string") {
11+
throw new TypeError(`Invalid folderPath: ${folderPath}`);
12+
}
13+
14+
const licenseFile = join(folderPath, `License.txt`);
15+
try {
16+
await access(licenseFile);
17+
} catch {
18+
throw new Error(`License file not found at expected location: ${licenseFile}`);
19+
}
20+
21+
const readmeossPattern = "*__*__READMEOSS_*.html";
22+
const readmeossFiles = globSync(readmeossPattern, { cwd: folderPath, absolute: true, ignore: "**/.*/**" });
23+
24+
if (isOssReadmeRequired && readmeossFiles.length === 0) {
25+
throw new Error(`No OSS README file found in ${folderPath} matching ${readmeossPattern}`);
26+
}
27+
28+
if (readmeossFiles.length === 0) {
29+
return [{ src: licenseFile, dest: basename(licenseFile) }];
30+
}
31+
32+
const ossReadmeFile = readmeossFiles[0];
33+
34+
return [
35+
{ src: licenseFile, dest: basename(licenseFile) },
36+
{ src: ossReadmeFile, dest: basename(ossReadmeFile) }
37+
];
38+
}
39+
40+
type FilePathPair = { src: string; dest: string };
41+
42+
export async function copyFilesToMpk(files: FilePathPair[], mpkOutput: string, moduleName: string): Promise<void> {
43+
const projectPath = mpkOutput.replace(".mpk", "");
44+
// Unzip the mpk
45+
await unzip(mpkOutput, projectPath);
46+
await rm(mpkOutput, { recursive: true, force: true });
47+
// Add additional files to the MPK
48+
for await (const file of files) {
49+
await copyFile(file.src, join(projectPath, file.dest));
50+
}
51+
// Re-zip and rename
52+
await zip(projectPath, moduleName);
53+
await rename(`${projectPath}.zip`, mpkOutput);
54+
}
55+
56+
export function zip(src: string, fileName: string): Promise<string> {
57+
return execShellCommand(`cd "${src}" && zip -r ../${fileName} .`);
58+
}
59+
60+
export function unzip(src: string, dest: string): Promise<string> {
61+
return execShellCommand(`unzip "${src}" -d "${dest}"`);
62+
}
63+
64+
function execShellCommand(cmd: string, workingDirectory = process.cwd()): Promise<string> {
65+
return new Promise((resolve, reject) => {
66+
exec(cmd, { cwd: workingDirectory }, (error, stdout, stderr) => {
67+
if (error) {
68+
console.warn(stderr);
69+
console.warn(stdout);
70+
reject(error);
71+
}
72+
if (stderr) {
73+
console.warn(stderr);
74+
}
75+
resolve(stdout);
76+
});
77+
});
78+
}

0 commit comments

Comments
 (0)