Skip to content

Commit

Permalink
fix(themes): sync before starting the preview server (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
azuradara authored Aug 13, 2024
1 parent ccd6a75 commit f98f9a8
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions packages/theme/lib/cli/services/dev/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,39 @@ export default class ThemeWorker extends Worker.Abstract {

this.theme.metadata = res;

this.logger.write(`pushing changes to ${this.theme.metadata!.theme_name}...`);

for (const type of this.FILE_TYPES) {
const descriptors = this.theme.metadata![type] as FileDescriptor[] ?? [];
const directory = Path.resolve(this.theme.root, type);

const present = await Filesystem.exists(directory)
? await Filesystem.readdir(directory)
: [];

if (type === 'config') {
const order = ['settings_schema.json', 'settings_data.json'];
descriptors.sort((a, b) => order.indexOf(a.file_name) - order.indexOf(b.file_name));
}

present.filter(f => !descriptors.find(d => d.file_name === f))
.forEach(async file => this.enqueue('save', type, file));

descriptors.forEach(async (descriptor) => {
const path = Path.resolve(directory, descriptor.file_name);
if (!(await Filesystem.exists(path))) {
return this.enqueue('delete', type, descriptor.file_name);
}

const buffer = await Filesystem.readFile(path);
const hash = Crypto.sha1(buffer);

if (hash !== descriptor.hash) {
this.enqueue('save', type, descriptor.file_name);
}
});
}

this.io = new Server(7565, {
cors: {
origin: `${Http.scheme()}://${this.store.domain}`,
Expand All @@ -58,39 +91,6 @@ export default class ThemeWorker extends Worker.Abstract {
}

async run(): Promise<void> {
this.logger.write(`pushing changes to ${this.theme.metadata!.theme_name}...`);

for (const type of this.FILE_TYPES) {
const descriptors = this.theme.metadata![type] as FileDescriptor[] ?? [];
const directory = Path.resolve(this.theme.root, type);

const present = await Filesystem.exists(directory)
? await Filesystem.readdir(directory)
: [];

if (type === 'config') {
const order = ['settings_schema.json', 'settings_data.json'];
descriptors.sort((a, b) => order.indexOf(a.file_name) - order.indexOf(b.file_name));
}

present.filter(f => !descriptors.find(d => d.file_name === f))
.forEach(async file => this.enqueue('save', type, file));

descriptors.forEach(async (descriptor) => {
const path = Path.resolve(directory, descriptor.file_name);
if (!(await Filesystem.exists(path))) {
return this.enqueue('delete', type, descriptor.file_name);
}

const buffer = await Filesystem.readFile(path);
const hash = Crypto.sha1(buffer);

if (hash !== descriptor.hash) {
this.enqueue('save', type, descriptor.file_name);
}
});
}

const directories = this.FILE_TYPES.map(t => Path.resolve(this.theme.root, t));

const watcher = Filesystem.watch(directories, {
Expand Down

0 comments on commit f98f9a8

Please sign in to comment.