Skip to content

Commit

Permalink
refactor: use fs.promises in generate license file, rollup config, Pa…
Browse files Browse the repository at this point in the history
…rt 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 <lukastaegert@users.noreply.github.com>
  • Loading branch information
dnalborczyk and lukastaegert committed Feb 7, 2022
1 parent 89ee52c commit aad5510
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
12 changes: 6 additions & 6 deletions build-plugins/generate-license-file.ts
@@ -1,9 +1,9 @@
import { readFileSync, writeFileSync } from 'fs';
import { promises as fs } from 'fs';
import type { PluginImpl } from 'rollup';
import license, { type Dependency, type Person } from 'rollup-plugin-license';

function generateLicenseFile(dependencies: readonly Dependency[]) {
const coreLicense = readFileSync('LICENSE-CORE.md');
async function generateLicenseFile(dependencies: readonly Dependency[]): Promise<void> {
const coreLicense = await fs.readFile('LICENSE-CORE.md', 'utf8');
const licenses = new Set<string>();
const dependencyLicenseTexts = Array.from(dependencies)
.sort(({ name: nameA }, { name: nameB }) => (nameA! > nameB! ? 1 : -1))
Expand Down Expand Up @@ -52,9 +52,9 @@ function generateLicenseFile(dependencies: readonly Dependency[]) {
`${Array.from(licenses).join(', ')}\n\n` +
`# Bundled dependencies:\n` +
dependencyLicenseTexts;
const existingLicenseText = readFileSync('LICENSE.md', 'utf8');
const existingLicenseText = await fs.readFile('LICENSE.md', 'utf8');
if (existingLicenseText !== licenseText) {
writeFileSync('LICENSE.md', licenseText);
await fs.writeFile('LICENSE.md', licenseText);
console.warn('LICENSE.md updated. You should commit the updated file.');
}
}
Expand All @@ -80,7 +80,7 @@ export default function getLicenseHandler(): LicenseHandler {
return {
name: 'write-license',
writeBundle() {
generateLicenseFile(Array.from(licenses.values()));
return generateLicenseFile(Array.from(licenses.values()));
}
};
}
Expand Down
31 changes: 19 additions & 12 deletions rollup.config.ts
@@ -1,6 +1,6 @@
import { readFileSync } from 'fs';
import { promises as fs } from 'fs';
import { resolve } from 'path';
import process from 'process';
import { env } from 'process';
import alias from '@rollup/plugin-alias';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
Expand All @@ -17,26 +17,29 @@ import getLicenseHandler from './build-plugins/generate-license-file';
import replaceBrowserModules from './build-plugins/replace-browser-modules';
import { version } from './package.json';

const commitHash = (function () {
async function getBanner(): Promise<string> {
let commitHash: string;

try {
return readFileSync('.commithash', 'utf-8');
commitHash = await fs.readFile('.commithash', 'utf8');
} catch {
return 'unknown';
commitHash = 'unknown';
}
})();

const { SOURCE_DATE_EPOCH } = process.env;
const now = new Date(SOURCE_DATE_EPOCH ? 1000 * +SOURCE_DATE_EPOCH : Date.now()).toUTCString();
const date = new Date(
env.SOURCE_DATE_EPOCH ? 1000 * +env.SOURCE_DATE_EPOCH : Date.now()
).toUTCString();

const banner = `/*
return `/*
@license
Rollup.js v${version}
${now} - commit ${commitHash}
${date} - commit ${commitHash}
https://github.com/rollup/rollup
Released under the MIT License.
*/`;
}

const onwarn: WarningHandlerWithDefault = warning => {
// eslint-disable-next-line no-console
Expand Down Expand Up @@ -75,8 +78,12 @@ const nodePlugins = [
typescript()
];

export default (command: Record<string, unknown>): RollupOptions | RollupOptions[] => {
export default async function (
command: Record<string, unknown>
): Promise<RollupOptions | RollupOptions[]> {
const banner = await getBanner();
const { collectLicenses, writeLicense } = getLicenseHandler();

const commonJSBuild: RollupOptions = {
// 'fsevents' is a dependency of 'chokidar' that cannot be bundled as it contains binary code
external: ['fsevents'],
Expand Down Expand Up @@ -150,4 +157,4 @@ export default (command: Record<string, unknown>): RollupOptions | RollupOptions
};

return [commonJSBuild, esmBuild, browserBuilds];
};
}

0 comments on commit aad5510

Please sign in to comment.