Skip to content

Commit

Permalink
fix: use promise based APIs instead of promisify
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguttandin committed Aug 20, 2024
1 parent 8ca1830 commit 5c95566
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
19 changes: 7 additions & 12 deletions src/functions/prerender.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { mkdir, readFile, writeFile } from 'fs';
import { mkdir, readFile, writeFile } from 'fs/promises';
import { dirname, join, sep } from 'path';
import { promisify } from 'util';
import { cwd } from 'process';
import { WorkspaceSchema } from '@schematics/angular/utility/workspace-models'; // eslint-disable-line import/no-internal-modules, max-len, node/file-extension-in-import
import chalk from 'chalk';
Expand All @@ -13,10 +12,6 @@ import { resolveRoutes } from './resolve-routes.js';
import { retrieveRoutes } from './retrieve-routes.js';
import { scanRoutes } from './scan-routes.js';

const mkdirAsync = promisify(mkdir);
const readFileAsync = promisify(readFile);
const writeFileAsync = promisify(writeFile);

export const prerender = async (
config: string,
excludeRoutes: string[],
Expand All @@ -36,7 +31,7 @@ export const prerender = async (
console.log(chalk.gray(`The path of the angular.json config file is "${config}".`)); // eslint-disable-line no-console
}

const { defaultProject, projects } = <WorkspaceSchema & { defaultProject?: string }>JSON.parse(await readFileAsync(config, 'utf8'));
const { defaultProject, projects } = <WorkspaceSchema & { defaultProject?: string }>JSON.parse(await readFile(config, 'utf8'));
const outputPath = join(dirname(config), readProperty(projects, defaultProject, target, 'outputPath'), sep);
const browserOutputPath = join(outputPath, 'browser');
const serverOutputPath = join(outputPath, 'server');
Expand Down Expand Up @@ -64,7 +59,7 @@ export const prerender = async (
console.log(chalk.gray(`The path of the index.html file is "${index}".`)); // eslint-disable-line no-console
}

const document = await readFileAsync(index, 'utf8');
const document = await readFile(index, 'utf8');
const tsConfig = join(cwd(), readProperty(projects, defaultProject, target, 'tsConfig'));

if (isVerbose) {
Expand Down Expand Up @@ -119,7 +114,7 @@ export const prerender = async (
);
});
const resolvedRoutes = resolveRoutes(renderableRoutesWithParameters);
const routeProcessPlugins = scullyPlugins === null ? [] : scullyPlugins.get('routeProcess') ?? [];
const routeProcessPlugins = scullyPlugins === null ? [] : (scullyPlugins.get('routeProcess') ?? []);
const routeProcessPluginEntries = Array.from(routeProcessPlugins.entries());

routeProcessPluginEntries.sort(([a], [b]) => a - b);
Expand All @@ -139,7 +134,7 @@ export const prerender = async (
for (const route of processedRoutes) {
const path = join(browserOutputPath, route, sep);

await mkdirAsync(path, { recursive: true });
await mkdir(path, { recursive: true });

const html = await render({
document,
Expand Down Expand Up @@ -180,7 +175,7 @@ export const prerender = async (
// eslint-disable-next-line no-console
console.log(chalk.green(`The index.html file will be preserved as start.html because it would otherwise be overwritten.`));

const didUpdateNgServiceWorker = await preserveIndexHtml(browserOutputPath, document, readFileAsync, writeFileAsync);
const didUpdateNgServiceWorker = await preserveIndexHtml(browserOutputPath, document, readFile, writeFile);

if (didUpdateNgServiceWorker) {
console.log(chalk.green(`The ngsw.json file was updated to replace index.html with start.html.`)); // eslint-disable-line max-len, no-console
Expand All @@ -196,7 +191,7 @@ export const prerender = async (
}
}

await writeFileAsync(join(path, 'index.html'), transformedHtml);
await writeFile(join(path, 'index.html'), transformedHtml);

console.log(chalk.green(`The route at "${route}" was rendered successfully.`)); // eslint-disable-line no-console

Expand Down
14 changes: 6 additions & 8 deletions test/integration/app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { cwd, env } from 'process';
import { join, relative, sep } from 'path';
import { mkdir, mkdtemp, readFile, readFileSync } from 'fs';
import { mkdir, mkdtemp, readFile } from 'fs/promises';
import { exec } from 'child_process';
import { promisify } from 'util';
import { readFileSync } from 'fs';
import { rimraf } from 'rimraf';
import { tmpdir } from 'os';

const execAsync = promisify(exec);
const mkdirAsync = promisify(mkdir);
const mkdtempAsync = promisify(mkdtemp);
const readFileAsync = promisify(readFile);
const makeFakedTemporaryDirectory = async () => {
const fakedTemporaryDirectory = join(cwd(), '..', 'temp');

await mkdirAsync(fakedTemporaryDirectory);
await mkdir(fakedTemporaryDirectory);

return fakedTemporaryDirectory;
};
Expand Down Expand Up @@ -52,7 +50,7 @@ describe('angular-prerender', () => {
before(async function () {
this.timeout(600000);

directory = env.CI ? await makeFakedTemporaryDirectory() : await mkdtempAsync(`${tmpdir()}${sep}`);
directory = env.CI ? await makeFakedTemporaryDirectory() : await mkdtemp(`${tmpdir()}${sep}`);

await execAsync(
`npx --package @angular/cli@${angularMajorVersion} --call "ng new universe --no-interactive --routing --standalone ${standalone}"`,
Expand Down Expand Up @@ -97,7 +95,7 @@ describe('angular-prerender', () => {
cwd: projectDirectory
});

const content = await readFileAsync(join(projectDirectory, 'dist/universe/browser/index.html'), 'utf8');
const content = await readFile(join(projectDirectory, 'dist/universe/browser/index.html'), 'utf8');

expect(content).to.match(/<h1.*>Hello, universe<\/h1>/);
});
Expand All @@ -114,7 +112,7 @@ describe('angular-prerender', () => {
}
);

const content = await readFileAsync(join(projectDirectory, 'dist/universe/browser/index.html'), 'utf8');
const content = await readFile(join(projectDirectory, 'dist/universe/browser/index.html'), 'utf8');

expect(content).to.match(/<h1.*>Hello, universe<\/h1>/);
});
Expand Down

0 comments on commit 5c95566

Please sign in to comment.