Skip to content

Commit

Permalink
fix(core): should generate stackblize bundle.json for examples when c…
Browse files Browse the repository at this point in the history
…ustom site project (#290)

* fix: siteProject stackblize examples

* style: lint

* test(core): siteProject
  • Loading branch information
wszgrcy committed Nov 23, 2021
1 parent 3c8fd72 commit bcf79b8
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
39 changes: 34 additions & 5 deletions packages/core/src/angular/site-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export class SiteBuilder {
await this.detect();
if (this.siteProject) {
this.docgeni.paths.setSitePaths(this.siteProject.root, this.siteProject.sourceRoot);
await this.syncSiteProject();
this.watchSiteProject();
} else {
await this.createSiteProject();
await this.syncPublic();
Expand Down Expand Up @@ -173,7 +175,7 @@ export class SiteBuilder {
await this.docgeni.host.copy(fromPath, resolve(this.siteProject.root, copyFile.to));
}
}
this.updateShareExampleBundleJson();
this.updateShareExampleBundleJson(this.publicDirPath);
}
}

Expand Down Expand Up @@ -205,7 +207,24 @@ export class SiteBuilder {
}
const isStackBlitzDir = events.some(event => !relative(resolve(assetsPath, 'stack-blitz'), event.path).startsWith('..'));
if (isStackBlitzDir) {
this.updateShareExampleBundleJson();
this.updateShareExampleBundleJson(this.publicDirPath);
}
});
}
}

private async syncSiteProject() {
this.updateShareExampleBundleJson(resolve(this.docgeni.paths.absSitePath, 'src'));
}

private async watchSiteProject() {
if (this.docgeni.watch) {
const sourceRoot = resolve(this.docgeni.paths.absSitePath, 'src');
const assetsPath = resolve(sourceRoot, 'assets');
this.docgeni.host.watchAggregated([`${assetsPath}/stack-blitz`]).subscribe(async events => {
const isStackBlitzDir = events.some(event => !event.path.endsWith('stack-blitz/bundle.json'));
if (isStackBlitzDir) {
this.updateShareExampleBundleJson(sourceRoot);
}
});
}
Expand Down Expand Up @@ -239,14 +258,24 @@ export class SiteBuilder {
}, []);
}

private async updateShareExampleBundleJson() {
const sharedExampleDir = resolve(resolve(this.publicDirPath, 'assets'), 'stack-blitz');
private async updateShareExampleBundleJson(sitePath: string) {
const sharedExampleDir = resolve(resolve(sitePath, 'assets'), 'stack-blitz');
if (!(await this.docgeni.host.exists(sharedExampleDir))) {
await this.docgeni.host.writeFile(resolve(this.siteProject.root, `${SITE_ASSETS_RELATIVE_PATH}/stack-blitz/bundle.json`), `[]`);
return;
}
const files = await this.docgeni.host.getFiles(sharedExampleDir, { recursively: true });
const list = [];
for (const file of files) {
if (file === 'bundle.json') {
continue;
}
list.push({ path: file, content: await this.docgeni.host.readFile(resolve(sharedExampleDir, file)) });
}
const content = JSON.stringify(list);
await this.docgeni.host.writeFile(resolve(this.siteProject.root, `${SITE_ASSETS_RELATIVE_PATH}/stack-blitz/bundle.json`), content);
await this.docgeni.host.writeFile(
resolve(this.docgeni.paths.absSitePath, `${SITE_ASSETS_RELATIVE_PATH}/stack-blitz/bundle.json`),
content
);
}
}
31 changes: 31 additions & 0 deletions packages/core/src/angular/site-plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,37 @@ describe('#site-plugin', () => {
expect(context.paths.absSiteContentPath).toEqual(`${DEFAULT_TEST_ROOT_PATH}/custom/site/src/app/content`);
});

it('should watch custom site success', async () => {
const sitePath = `${DEFAULT_TEST_ROOT_PATH}/custom/site`;
context.config.siteProjectName = 'customSite';
const watchAggregated$ = new Subject<HostWatchEvent[]>();

updateContext(context, { watch: true });
const watchAggregatedSpy = spyOn(context.host, 'watchAggregated');
watchAggregatedSpy.and.callFake(files => {
expect(files).toEqual([`${sitePath}/src/assets/stack-blitz`]);
return watchAggregated$.asObservable();
});
await context.hooks.run.promise();
const text1 = toolkit.strings.generateRandomId();
await writeFilesToHost(context.host, {
[`${sitePath}/src/assets/stack-blitz/a.txt`]: text1
});
expect(await context.host.exists(`${sitePath}/src/assets/stack-blitz/a.txt`)).toBeTruthy();
watchAggregated$.next([
{
type: HostWatchEventType.Created,
path: normalize(`${sitePath}/src/assets/stack-blitz/a.txt`),
time: new Date()
}
]);

await toolkit.utils.wait(2000);

expect(await context.host.exists(`${sitePath}/src/assets/stack-blitz/a.txt`)).toBeTruthy();
expect(await context.host.exists(`${sitePath}/src/assets/stack-blitz/bundle.json`)).toBeTruthy();
});

it('should copy public dir success', async () => {
const text1 = toolkit.strings.generateRandomId();
const text2 = toolkit.strings.generateRandomId();
Expand Down

0 comments on commit bcf79b8

Please sign in to comment.