Skip to content

Commit d6889c7

Browse files
committed
chore: add deploy utility
1 parent ca6f850 commit d6889c7

3 files changed

Lines changed: 49 additions & 30 deletions

File tree

packages/mpx/src/build.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { onExit } from "signal-exit";
1111
import { PACKAGE_FILES } from "./constants.js";
1212
import { loadConfig } from "./rolldown.js";
1313
import { blue, bold, dim, green, greenBright, inverse } from "./utils/colors.js";
14-
import { isTypeScriptProject, readPackageJson } from "./utils/fs.js";
14+
import { deployToMxProject, isTypeScriptProject, readPackageJson } from "./utils/fs.js";
1515
import { createLogger } from "./utils/logger.js";
1616
import { createMPK } from "./utils/mpk.js";
1717
import { ProjectConfig } from "./utils/project-config.js";
@@ -32,27 +32,28 @@ export async function build(root: string | undefined, options: BuildCommandOptio
3232

3333
const [pkg, isTsProject] = await Promise.all([readPackageJson(root), isTypeScriptProject(root)]);
3434

35-
const project = new ProjectConfig({
35+
const config = new ProjectConfig({
3636
pkg,
3737
isTsProject
3838
});
3939

40-
const projectPath = await project.getProjectPath();
40+
const projectPath = await config.getProjectPath();
4141
if (projectPath) {
4242
logger.info(formatMsg.mxpath(projectPath));
4343
}
4444

45-
const bundles = await loadConfig(project);
45+
const bundles = await loadConfig(config);
4646

47-
await fs.rm(project.outputDirs.dist, { recursive: true, force: true });
48-
console.dir(project.inputFiles);
49-
console.dir(project.outputDirs);
50-
console.dir(project.outputFiles);
51-
console.dir(project.assetsPublicPath);
47+
await fs.rm(config.outputDirs.dist, { recursive: true, force: true });
48+
console.dir(config.inputFiles);
49+
console.dir(config.outputDirs);
50+
console.dir(config.outputFiles);
51+
console.dir(config.assetsPublicPath);
52+
console.dir(config.relativeWidgetPath);
5253
if (options.watch) {
53-
await tasks.watch({ project, bundles, logger, root });
54+
await tasks.watch({ config, bundles, logger, root });
5455
} else {
55-
await tasks.build({ project, bundles, logger, root });
56+
await tasks.build({ config, bundles, logger, root });
5657
}
5758
} catch (error) {
5859
logger.error(error);
@@ -63,13 +64,13 @@ export async function build(root: string | undefined, options: BuildCommandOptio
6364
interface TaskParams {
6465
root: string;
6566
bundles: BuildOptions[];
66-
project: ProjectConfig;
67+
config: ProjectConfig;
6768
logger: ConsolaInstance;
6869
}
6970

7071
const tasks = {
7172
async build(params: TaskParams): Promise<void> {
72-
const { project, bundles, logger } = params;
73+
const { config, bundles, logger } = params;
7374
buildMeasure.start();
7475

7576
for (const bundle of bundles) {
@@ -82,6 +83,11 @@ const tasks = {
8283

8384
const buildInfo = buildMeasure.end();
8485
logger.success("Done in", green(ms(buildInfo.duration)));
86+
87+
const projectPath = await config.getProjectPath();
88+
if (projectPath) {
89+
await deployToMxProject(config, projectPath);
90+
}
8591
},
8692
async watch(params: TaskParams): Promise<void> {
8793
const { root, bundles, logger } = params;
@@ -127,11 +133,11 @@ const tasks = {
127133
logger.log("Build watcher stopped");
128134
});
129135
},
130-
async copyPackageFiles({ project }: TaskParams): Promise<void> {
136+
async copyPackageFiles({ config }: TaskParams): Promise<void> {
131137
const stream = fg.stream(PACKAGE_FILES);
132138
for await (const src of stream) {
133139
const f = path.parse(src as string);
134-
const dst = path.join(project.outputDirs.contentRoot, f.base);
140+
const dst = path.join(config.outputDirs.contentRoot, f.base);
135141

136142
await fs.cp(src as string, dst, {
137143
recursive: true
@@ -140,15 +146,15 @@ const tasks = {
140146
},
141147
/** Watch & copy static package files */
142148
async watchPackageFiles(params: TaskParams): Promise<void> {
143-
const { project, logger } = params;
149+
const { config, logger } = params;
144150

145151
await tasks.copyPackageFiles(params);
146152

147153
const watcher = chokidar.watch(await fg(PACKAGE_FILES));
148154
watcher.on("change", async file => {
149155
logger.info(formatMsg.copy(file));
150156
const f = path.parse(file);
151-
const dst = path.join(project.outputDirs.contentRoot, f.base);
157+
const dst = path.join(config.outputDirs.contentRoot, f.base);
152158
await fs.cp(file, dst);
153159
});
154160

@@ -158,9 +164,9 @@ const tasks = {
158164
},
159165
/** Setup package content watcher to build mpk whenever package files change */
160166
async watchPackageContent(params: TaskParams): Promise<void> {
161-
const { project } = params;
167+
const { config } = params;
162168
await tasks.buildMpk({ ...params, quiet: true });
163-
const watcher = chokidar.watch(project.outputDirs.contentRoot);
169+
const watcher = chokidar.watch(config.outputDirs.contentRoot);
164170

165171
let debounceTimer: NodeJS.Timeout | null = null;
166172

@@ -179,7 +185,7 @@ const tasks = {
179185
watcher.close();
180186
});
181187
},
182-
async buildMpk({ project, logger, quiet = false }: TaskParams & { quiet?: boolean }): Promise<void> {
188+
async buildMpk({ config: project, logger, quiet = false }: TaskParams & { quiet?: boolean }): Promise<void> {
183189
await createMPK(project.outputDirs.contentRoot, project.outputFiles.mpk);
184190
const mpkStat = await fs.stat(project.outputFiles.mpk);
185191
if (!quiet) {

packages/mpx/src/utils/fs.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ export async function isTypeScriptProject(root: string): Promise<boolean> {
1515
return access(path.resolve(root, "tsconfig.json"));
1616
}
1717

18-
export async function hasEditorConfig(project: ProjectConfig): Promise<boolean> {
19-
return access(path.resolve(project.inputFiles.editorConfig));
18+
export async function hasEditorConfig(config: ProjectConfig): Promise<boolean> {
19+
return access(path.resolve(config.inputFiles.editorConfig));
2020
}
2121

22-
export async function hasEditorPreview(project: ProjectConfig): Promise<boolean> {
23-
return access(path.resolve(project.inputFiles.editorPreview));
22+
export async function hasEditorPreview(config: ProjectConfig): Promise<boolean> {
23+
return access(path.resolve(config.inputFiles.editorPreview));
2424
}
2525

2626
export async function readPackageJson(root: string): Promise<PackageJson> {
@@ -31,3 +31,16 @@ export async function readPackageJson(root: string): Promise<PackageJson> {
3131
throw parsePackageError(error);
3232
}
3333
}
34+
35+
export async function deployToMxProject(config: ProjectConfig, projectPath: string): Promise<void> {
36+
const mpkDst = path.join(projectPath, "widgets");
37+
const widgetDst = path.join(projectPath, "deployment", "web", "widgets", config.relativeWidgetPath);
38+
39+
await fs.mkdir(widgetDst, { recursive: true });
40+
await fs.mkdir(mpkDst, { recursive: true });
41+
// Copy widget assets to deployment
42+
// Note: in pwt we copy all files (including xml) which probably not needed
43+
await fs.cp(config.outputDirs.widgetDir, widgetDst, { recursive: true, force: true });
44+
// Copy mpk to "widgets" directory
45+
await fs.cp(config.outputDirs.mpkDir, mpkDst, { recursive: true, force: true });
46+
}

packages/mpx/src/utils/project-config.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,19 @@ export class ProjectConfig {
9797
return { editorConfig, editorPreview, packageXml, widgetFile, widgetXml };
9898
}
9999

100-
/** Directory where widget bundles will be output */
101-
get widgetDir(): string {
102-
const { pkg, contentRoot } = this;
103-
return path.join(contentRoot, ...pkg.packagePath.split("."), pkg.widgetName.toLowerCase());
100+
/** Relative path to the widget directory from the "widgets" */
101+
get relativeWidgetPath(): string {
102+
return path.join(...this.pkg.packagePath.split("."), this.pkg.widgetName.toLowerCase());
104103
}
105104

106105
get outputDirs(): BundleOutputDirs {
106+
const widgetDir = path.join(this.contentRoot, this.relativeWidgetPath);
107107
return {
108108
dist: this.dist,
109109
mpkDir: path.join(this.dist, this.pkg.version),
110110
contentRoot: this.contentRoot,
111-
widgetDir: this.widgetDir,
112-
widgetAssetsDir: path.join(this.widgetDir, "assets")
111+
widgetDir,
112+
widgetAssetsDir: path.join(widgetDir, "assets")
113113
};
114114
}
115115

0 commit comments

Comments
 (0)