Skip to content

Commit aad5510

Browse files
refactor: use fs.promises in generate license file, rollup config, Part 5 (#4389)
* refactor: use fs.promises in generate license file * fix: add return type * refactor: use fs.promises in rollup config Co-authored-by: Lukas Taegert-Atkinson <[email protected]>
1 parent 89ee52c commit aad5510

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

build-plugins/generate-license-file.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { readFileSync, writeFileSync } from 'fs';
1+
import { promises as fs } from 'fs';
22
import type { PluginImpl } from 'rollup';
33
import license, { type Dependency, type Person } from 'rollup-plugin-license';
44

5-
function generateLicenseFile(dependencies: readonly Dependency[]) {
6-
const coreLicense = readFileSync('LICENSE-CORE.md');
5+
async function generateLicenseFile(dependencies: readonly Dependency[]): Promise<void> {
6+
const coreLicense = await fs.readFile('LICENSE-CORE.md', 'utf8');
77
const licenses = new Set<string>();
88
const dependencyLicenseTexts = Array.from(dependencies)
99
.sort(({ name: nameA }, { name: nameB }) => (nameA! > nameB! ? 1 : -1))
@@ -52,9 +52,9 @@ function generateLicenseFile(dependencies: readonly Dependency[]) {
5252
`${Array.from(licenses).join(', ')}\n\n` +
5353
`# Bundled dependencies:\n` +
5454
dependencyLicenseTexts;
55-
const existingLicenseText = readFileSync('LICENSE.md', 'utf8');
55+
const existingLicenseText = await fs.readFile('LICENSE.md', 'utf8');
5656
if (existingLicenseText !== licenseText) {
57-
writeFileSync('LICENSE.md', licenseText);
57+
await fs.writeFile('LICENSE.md', licenseText);
5858
console.warn('LICENSE.md updated. You should commit the updated file.');
5959
}
6060
}
@@ -80,7 +80,7 @@ export default function getLicenseHandler(): LicenseHandler {
8080
return {
8181
name: 'write-license',
8282
writeBundle() {
83-
generateLicenseFile(Array.from(licenses.values()));
83+
return generateLicenseFile(Array.from(licenses.values()));
8484
}
8585
};
8686
}

rollup.config.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { readFileSync } from 'fs';
1+
import { promises as fs } from 'fs';
22
import { resolve } from 'path';
3-
import process from 'process';
3+
import { env } from 'process';
44
import alias from '@rollup/plugin-alias';
55
import commonjs from '@rollup/plugin-commonjs';
66
import json from '@rollup/plugin-json';
@@ -17,26 +17,29 @@ import getLicenseHandler from './build-plugins/generate-license-file';
1717
import replaceBrowserModules from './build-plugins/replace-browser-modules';
1818
import { version } from './package.json';
1919

20-
const commitHash = (function () {
20+
async function getBanner(): Promise<string> {
21+
let commitHash: string;
22+
2123
try {
22-
return readFileSync('.commithash', 'utf-8');
24+
commitHash = await fs.readFile('.commithash', 'utf8');
2325
} catch {
24-
return 'unknown';
26+
commitHash = 'unknown';
2527
}
26-
})();
2728

28-
const { SOURCE_DATE_EPOCH } = process.env;
29-
const now = new Date(SOURCE_DATE_EPOCH ? 1000 * +SOURCE_DATE_EPOCH : Date.now()).toUTCString();
29+
const date = new Date(
30+
env.SOURCE_DATE_EPOCH ? 1000 * +env.SOURCE_DATE_EPOCH : Date.now()
31+
).toUTCString();
3032

31-
const banner = `/*
33+
return `/*
3234
@license
3335
Rollup.js v${version}
34-
${now} - commit ${commitHash}
36+
${date} - commit ${commitHash}
3537
3638
https://github.com/rollup/rollup
3739
3840
Released under the MIT License.
3941
*/`;
42+
}
4043

4144
const onwarn: WarningHandlerWithDefault = warning => {
4245
// eslint-disable-next-line no-console
@@ -75,8 +78,12 @@ const nodePlugins = [
7578
typescript()
7679
];
7780

78-
export default (command: Record<string, unknown>): RollupOptions | RollupOptions[] => {
81+
export default async function (
82+
command: Record<string, unknown>
83+
): Promise<RollupOptions | RollupOptions[]> {
84+
const banner = await getBanner();
7985
const { collectLicenses, writeLicense } = getLicenseHandler();
86+
8087
const commonJSBuild: RollupOptions = {
8188
// 'fsevents' is a dependency of 'chokidar' that cannot be bundled as it contains binary code
8289
external: ['fsevents'],
@@ -150,4 +157,4 @@ export default (command: Record<string, unknown>): RollupOptions | RollupOptions
150157
};
151158

152159
return [commonJSBuild, esmBuild, browserBuilds];
153-
};
160+
}

0 commit comments

Comments
 (0)