Skip to content

Commit

Permalink
chore(contented): use non-promise fs for blocking file IO (#546)
Browse files Browse the repository at this point in the history
#### What this PR does / why we need it:

As per title.
  • Loading branch information
fuxingloh authored Aug 7, 2023
1 parent addd965 commit d68f357
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions packages/contented-pipeline/src/Pipeline.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createHash } from 'node:crypto';
import fs from 'node:fs/promises';
import { statSync } from 'node:fs';
import { join, parse, ParsedPath } from 'node:path';

import slugify from '@sindresorhus/slugify';
Expand Down Expand Up @@ -189,7 +189,7 @@ export abstract class ContentedPipeline {
}

protected async computeModifiedDate(filePath: string): Promise<number> {
const stats = await fs.stat(filePath);
const stats = statSync(filePath);
return stats.mtime.getTime();
}
}
18 changes: 9 additions & 9 deletions packages/contented-processor/src/ContentedCodegen.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'node:fs/promises';
import fs from 'node:fs';
import { join } from 'node:path';

import { CodeGenerator } from '@babel/generator';
Expand All @@ -15,27 +15,27 @@ export class ContentedCodegen {
async generateIndex() {
const types = this.config.pipelines.map((pipeline) => pipeline.type);
const ast = generateIndexAST([...new Set(types)]);
await fs.mkdir(this.outPath, { recursive: true });
await fs.writeFile(join(this.outPath, `index.js`), generate(ast).code);
await fs.writeFile(join(this.outPath, `index.d.ts`), generateTypes(types));
fs.mkdirSync(this.outPath, { recursive: true });
fs.writeFileSync(join(this.outPath, `index.js`), generate(ast).code);
fs.writeFileSync(join(this.outPath, `index.d.ts`), generateTypes(types));
}

async generatePipeline(type: string, contents: FileIndex[]) {
await fs.mkdir(join(this.outPath, type), { recursive: true });
fs.mkdirSync(join(this.outPath, type), { recursive: true });

const ast = generatePipelineAST(type, contents);
const indexJs = join(this.outPath, type, `index.js`);
await fs.writeFile(indexJs, generate(ast).code);
fs.writeFileSync(indexJs, generate(ast).code);

const indexJson = join(this.outPath, type, `index.json`);
await fs.writeFile(indexJson, JSON.stringify(contents));
fs.writeFileSync(indexJson, JSON.stringify(contents));
}

async generateFile(content: FileContent) {
const outPath = join(this.outPath, content.type, `${content.fileId}.json`);

await fs.mkdir(join(this.outPath, content.type), { recursive: true });
await fs.writeFile(outPath, JSON.stringify(content));
fs.mkdirSync(join(this.outPath, content.type), { recursive: true });
fs.writeFileSync(outPath, JSON.stringify(content));
}
}

Expand Down
6 changes: 3 additions & 3 deletions packages/contented/src/commands/contented/ContentedPreview.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { spawn, spawnSync } from 'node:child_process';
import { cp, writeFile } from 'node:fs/promises';
import { cpSync, writeFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

Expand All @@ -19,8 +19,8 @@ export class ContentedPreview {

async init() {
const source = join(this.getDirname(), '/../../.preview');
await cp(source, this.previewDir, { recursive: true });
await writeFile(join(this.previewDir, '.env'), generateEnvData(this.config));
cpSync(source, this.previewDir, { recursive: true });
writeFileSync(join(this.previewDir, '.env'), generateEnvData(this.config));
}

async install() {
Expand Down

0 comments on commit d68f357

Please sign in to comment.