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

fix(core): do not replace legacy package mentions in binary files #16547

Merged
merged 1 commit into from
Apr 25, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 2 additions & 35 deletions packages/devkit/src/generators/generate-files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,10 @@ import { readFileSync, readdirSync, statSync } from 'fs';
import * as path from 'path';
import type { Tree } from 'nx/src/generators/tree';
import { requireNx } from '../../nx';
import { isBinaryPath } from '../utils/binary-extensions';

const { logger } = requireNx();

const binaryExts = new Set([
// // Image types originally from https://github.com/sindresorhus/image-type/blob/5541b6a/index.js
'.jpg',
'.jpeg',
'.png',
'.gif',
'.webp',
'.flif',
'.cr2',
'.tif',
'.bmp',
'.jxr',
'.psd',
'.ico',
'.bpg',
'.jp2',
'.jpm',
'.jpx',
'.heic',
'.cur',
'.tgz',

// Java files
'.jar',
'.keystore',

// Font files
'.ttf',
'.otf',
'.woff',
'.woff2',
'.eot',
]);

/**
* Generates a folder of files based on provided templates.
*
Expand Down Expand Up @@ -84,7 +51,7 @@ export function generateFiles(
substitutions
);

if (binaryExts.has(path.extname(filePath))) {
if (isBinaryPath(filePath)) {
newContent = readFileSync(filePath);
} else {
const template = readFileSync(filePath, 'utf-8');
Expand Down
48 changes: 48 additions & 0 deletions packages/devkit/src/utils/binary-extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { extname } from 'path';

const binaryExtensions = new Set([
// // Image types originally from https://github.com/sindresorhus/image-type/blob/5541b6a/index.js
'.jpg',
'.jpeg',
'.png',
'.gif',
'.webp',
'.flif',
'.cr2',
'.tif',
'.bmp',
'.jxr',
'.psd',
'.ico',
'.bpg',
'.jp2',
'.jpm',
'.jpx',
'.heic',
'.cur',
'.avif',
'.dcm',

// Compressed files
'.tgz',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.avif and .dcm extensions is missing for images

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pxz and pxd are also missing

'.gz',
'.zip',

// Documents
'.pdf',

// Java files
'.jar',
'.keystore',

// Font files
'.ttf',
'.otf',
'.woff',
'.woff2',
'.eot',
]);

export function isBinaryPath(path: string): boolean {
return binaryExtensions.has(extname(path));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to list out more extensions like .pdf, .gz, .zip, etc.?

There's a long list here: https://github.com/sindresorhus/binary-extensions/blob/main/binary-extensions.json

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added these to the list.. let's see if we want to import this list differently in the future. 🤔

}
5 changes: 5 additions & 0 deletions packages/devkit/src/utils/replace-package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { requireNx } from '../../nx';
import { NX_VERSION } from './package-json';
import { visitNotIgnoredFiles } from '../generators/visit-not-ignored-files';
import { basename } from 'path';
import { isBinaryPath } from './binary-extensions';

const {
getProjects,
Expand Down Expand Up @@ -148,6 +149,10 @@ function replaceMentions(
newPackageName: string
) {
visitNotIgnoredFiles(tree, '.', (path) => {
if (isBinaryPath(path)) {
return;
}

const ignoredFiles = [
'yarn.lock',
'package-lock.json',
Expand Down