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

feat!: remove requiresBuild from the lockfile #7710

Merged
merged 7 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: create pkg-requires-build
  • Loading branch information
zkochan committed Feb 26, 2024
commit db539f7069f6b395f8fbcc5832fa1486b8846d2d
7 changes: 0 additions & 7 deletions exec/files-include-install-scripts/CHANGELOG.md

This file was deleted.

15 changes: 0 additions & 15 deletions exec/files-include-install-scripts/README.md

This file was deleted.

4 changes: 0 additions & 4 deletions exec/files-include-install-scripts/src/index.ts

This file was deleted.

15 changes: 15 additions & 0 deletions exec/pkg-requires-build/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# @pnpm/exec.pkg-requires-build

> Checks if a package requires to be built

[![npm version](https://img.shields.io/npm/v/@pnpm/exec.pkg-requires-build.svg)](https://www.npmjs.com/package/@pnpm/exec.pkg-requires-build)

## Installation

```sh
pnpm add @pnpm/exec.pkg-requires-build
```

## License

MIT
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@pnpm/exec.files-include-install-scripts",
"version": "1.0.0",
"description": "Checks if a package has files indicating that it needs to be built during installation",
"name": "@pnpm/exec.pkg-requires-build",
"version": "0.0.0",
"description": "Checks if a package requires to be built",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"files": [
Expand All @@ -18,7 +18,7 @@
"fix": "tslint -c tslint.json src/**/*.ts test/**/*.ts --fix",
"compile": "tsc --build && pnpm run lint --fix"
},
"repository": "https://github.com/pnpm/pnpm/blob/main/exec/files-include-install-scripts",
"repository": "https://github.com/pnpm/pnpm/blob/main/exec/pkg-requires-build",
"keywords": [
"pnpm9",
"pnpm"
Expand All @@ -27,9 +27,12 @@
"bugs": {
"url": "https://github.com/pnpm/pnpm/issues"
},
"homepage": "https://github.com/pnpm/pnpm/blob/main/exec/files-include-install-scripts#readme",
"homepage": "https://github.com/pnpm/pnpm/blob/main/exec/pkg-requires-build#readme",
"dependencies": {
"@pnpm/types": "workspace:*"
},
"devDependencies": {
"@pnpm/exec.files-include-install-scripts": "workspace:*"
"@pnpm/exec.pkg-requires-build": "workspace:*"
},
"funding": "https://opencollective.com/pnpm",
"exports": {
Expand Down
17 changes: 17 additions & 0 deletions exec/pkg-requires-build/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { type DependencyManifest } from '@pnpm/types'

export function pkgRequiresBuild (manifest: Partial<DependencyManifest> | undefined, filesIndex: Record<string, unknown>) {
return Boolean(
manifest?.scripts != null && (
Boolean(manifest.scripts.preinstall) ||
Boolean(manifest.scripts.install) ||
Boolean(manifest.scripts.postinstall)
) ||
filesIncludeInstallScripts(filesIndex)
)
}

function filesIncludeInstallScripts (filesIndex: Record<string, unknown>): boolean {
return filesIndex['binding.gyp'] != null ||
Object.keys(filesIndex).some((filename) => !(filename.match(/^[.]hooks[\\/]/) == null)) // TODO: optimize this
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
"src/**/*.ts",
"../../__typings__/**/*.d.ts"
],
"references": [],
"references": [
{
"path": "../../packages/types"
}
],
"composite": true
}
2 changes: 1 addition & 1 deletion fetching/directory-fetcher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@pnpm/logger": "^5.0.0"
},
"dependencies": {
"@pnpm/exec.files-include-install-scripts": "1.0.0",
"@pnpm/exec.pkg-requires-build": "workspace:*",
"@pnpm/fetcher-base": "workspace:*",
"@pnpm/fs.packlist": "workspace:*",
"@pnpm/read-project-manifest": "workspace:*",
Expand Down
56 changes: 16 additions & 40 deletions fetching/directory-fetcher/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { promises as fs, type Stats } from 'fs'
import path from 'path'
import { filesIncludeInstallScripts } from '@pnpm/exec.files-include-install-scripts'
import { pkgRequiresBuild } from '@pnpm/exec.pkg-requires-build'
import type { DirectoryFetcher, DirectoryFetcherOptions } from '@pnpm/fetcher-base'
import { logger } from '@pnpm/logger'
import { packlist } from '@pnpm/fs.packlist'
Expand All @@ -22,7 +22,7 @@ export function createDirectoryFetcher (

const directoryFetcher: DirectoryFetcher = (cafs, resolution, opts) => {
const dir = path.join(opts.lockfileDir, resolution.directory)
return fetchFromDir(dir, opts)
return fetchFromDir(dir)
}

return {
Expand All @@ -37,33 +37,22 @@ export async function fetchFromDir (
opts: FetchFromDirOpts & CreateDirectoryFetcherOptions
) {
if (opts.includeOnlyPackageFiles) {
return fetchPackageFilesFromDir(dir, opts)
return fetchPackageFilesFromDir(dir)
}
const readFileStat: ReadFileStat = opts?.resolveSymlinks === true ? realFileStat : fileStat
return fetchAllFilesFromDir(readFileStat, dir, opts)
return fetchAllFilesFromDir(readFileStat, dir)
}

async function fetchAllFilesFromDir (
readFileStat: ReadFileStat,
dir: string,
opts: FetchFromDirOpts
dir: string
) {
const filesIndex = await _fetchAllFilesFromDir(readFileStat, dir)
let manifest: DependencyManifest | undefined
if (opts.readManifest) {
// In a regular pnpm workspace it will probably never happen that a dependency has no package.json file.
// Safe read was added to support the Bit workspace in which the components have no package.json files.
// Related PR in Bit: https://github.com/teambit/bit/pull/5251
manifest = await safeReadProjectManifestOnly(dir) as DependencyManifest ?? undefined
}
const requiresBuild = Boolean(
manifest?.scripts != null && (
Boolean(manifest.scripts.preinstall) ||
Boolean(manifest.scripts.install) ||
Boolean(manifest.scripts.postinstall)
) ||
filesIncludeInstallScripts(filesIndex)
)
// In a regular pnpm workspace it will probably never happen that a dependency has no package.json file.
// Safe read was added to support the Bit workspace in which the components have no package.json files.
// Related PR in Bit: https://github.com/teambit/bit/pull/5251
const manifest = await safeReadProjectManifestOnly(dir) as DependencyManifest ?? undefined
const requiresBuild = pkgRequiresBuild(manifest, filesIndex)
return {
local: true as const,
filesIndex,
Expand Down Expand Up @@ -134,27 +123,14 @@ async function fileStat (filePath: string): Promise<{ filePath: string, stat: St
}
}

async function fetchPackageFilesFromDir (
dir: string,
opts: FetchFromDirOpts
) {
async function fetchPackageFilesFromDir (dir: string) {
const files = await packlist(dir)
const filesIndex: Record<string, string> = Object.fromEntries(files.map((file) => [file, path.join(dir, file)]))
let manifest: DependencyManifest | undefined
if (opts.readManifest) {
// In a regular pnpm workspace it will probably never happen that a dependency has no package.json file.
// Safe read was added to support the Bit workspace in which the components have no package.json files.
// Related PR in Bit: https://github.com/teambit/bit/pull/5251
manifest = await safeReadProjectManifestOnly(dir) as DependencyManifest ?? undefined
}
const requiresBuild = Boolean(
manifest?.scripts != null && (
Boolean(manifest.scripts.preinstall) ||
Boolean(manifest.scripts.install) ||
Boolean(manifest.scripts.postinstall)
) ||
filesIncludeInstallScripts(filesIndex)
)
// In a regular pnpm workspace it will probably never happen that a dependency has no package.json file.
// Safe read was added to support the Bit workspace in which the components have no package.json files.
// Related PR in Bit: https://github.com/teambit/bit/pull/5251
const manifest = await safeReadProjectManifestOnly(dir) as DependencyManifest ?? undefined
const requiresBuild = pkgRequiresBuild(manifest, filesIndex)
return {
local: true as const,
filesIndex,
Expand Down
3 changes: 3 additions & 0 deletions fetching/directory-fetcher/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
{
"path": "../../__utils__/test-fixtures"
},
{
"path": "../../exec/pkg-requires-build"
},
{
"path": "../../fs/packlist"
},
Expand Down
38 changes: 18 additions & 20 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion store/create-cafs-store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"@pnpm/logger": "^5.0.0"
},
"dependencies": {
"@pnpm/exec.files-include-install-scripts": "workspace:*",
"@pnpm/exec.pkg-requires-build": "workspace:*",
"@pnpm/fetcher-base": "workspace:*",
"@pnpm/fs.indexed-pkg-importer": "workspace:*",
"@pnpm/store-controller-types": "workspace:*",
Expand Down
20 changes: 3 additions & 17 deletions store/create-cafs-store/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { promises as fs, readFileSync } from 'fs'
import { promises as fs } from 'fs'
import path from 'path'
import { filesIncludeInstallScripts } from '@pnpm/exec.files-include-install-scripts'
import {
type CafsLocker,
createCafs,
Expand Down Expand Up @@ -35,7 +34,7 @@ export function createPackageImporterAsync (
const gfm = getFlatMap.bind(null, opts.cafsDir)
return async (to, opts) => {
const { filesMap, isBuilt } = gfm(opts.filesResponse, opts.sideEffectsCacheKey)
const willBeBuilt = !isBuilt && (opts.requiresBuild ?? pkgRequiresBuild(filesMap))
const willBeBuilt = !isBuilt && opts.requiresBuild
const pkgImportMethod = willBeBuilt
? 'clone-or-copy'
: (opts.filesResponse.packageImportMethod ?? packageImportMethod)
Expand Down Expand Up @@ -65,7 +64,7 @@ function createPackageImporter (
const gfm = getFlatMap.bind(null, opts.cafsDir)
return (to, opts) => {
const { filesMap, isBuilt } = gfm(opts.filesResponse, opts.sideEffectsCacheKey)
const willBeBuilt = !isBuilt && (opts.requiresBuild ?? pkgRequiresBuild(filesMap))
const willBeBuilt = !isBuilt && opts.requiresBuild
const pkgImportMethod = willBeBuilt
? 'clone-or-copy'
: (opts.filesResponse.packageImportMethod ?? packageImportMethod)
Expand Down Expand Up @@ -131,16 +130,3 @@ export function createCafsStore (
},
}
}

function pkgRequiresBuild (filesMap: Record<string, string>) {
return filesIncludeInstallScripts(filesMap) ||
filesMap['package.json'] && pkgJsonHasInstallScripts(filesMap['package.json'])
}

function pkgJsonHasInstallScripts (file: string): boolean {
const pkgJson = JSON.parse(readFileSync(file, 'utf8'))
if (!pkgJson.scripts) return false
return Boolean(pkgJson.scripts.preinstall) ||
Boolean(pkgJson.scripts.install) ||
Boolean(pkgJson.scripts.postinstall)
}
2 changes: 1 addition & 1 deletion store/create-cafs-store/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"path": "../../__utils__/prepare"
},
{
"path": "../../exec/files-include-install-scripts"
"path": "../../exec/pkg-requires-build"
},
{
"path": "../../fetching/fetcher-base"
Expand Down
2 changes: 1 addition & 1 deletion worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"@pnpm/cafs-types": "workspace:*",
"@pnpm/create-cafs-store": "workspace:*",
"@pnpm/error": "workspace:*",
"@pnpm/exec.files-include-install-scripts": "1.0.0",
"@pnpm/exec.pkg-requires-build": "workspace:*",
"@pnpm/fs.hard-link-dir": "workspace:*",
"@pnpm/graceful-fs": "workspace:*",
"@pnpm/store.cafs": "workspace:*",
Expand Down
Loading
Loading