Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use fs.promises in generate license file, rollup config, Part 5 #4389

Merged
merged 4 commits into from Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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];
};
}