feat: embed-readme option (#4265)

This commit is contained in:
Dan Onoshko
2022-01-21 00:20:27 +07:00
committed by GitHub
parent 1d013d7e04
commit b7566b9791
8 changed files with 84 additions and 6 deletions

View File

@@ -0,0 +1,6 @@
---
"@pnpm/config": minor
"@pnpm/plugin-commands-publishing": minor
---
embed-readme option was added

View File

@@ -138,6 +138,7 @@ export interface Config {
enablePnp?: boolean
enableModulesDir: boolean
modulesCacheMaxAge: number
embedReadme?: boolean
registries: Registries
ignoreWorkspaceRootCheck: boolean

View File

@@ -120,6 +120,7 @@ export const types = Object.assign({
'workspace-root': Boolean,
'test-pattern': [String, Array],
'changed-files-ignore-pattern': [String, Array],
'embed-readme': Boolean,
}, npmTypes.types)
export type CliOptions = Record<string, unknown> & { dir?: string }
@@ -217,6 +218,10 @@ export default async (
'virtual-store-dir': 'node_modules/.pnpm',
'workspace-concurrency': 4,
'workspace-prefix': opts.workspaceDir,
/**
* @todo Make `false` by default in v7.
*/
'embed-readme': true,
})
npmConfig.addFile(path.resolve(path.join(__dirname, 'pnpmrc')), 'pnpm-builtin')

View File

@@ -0,0 +1 @@
# README

View File

@@ -0,0 +1,4 @@
{
"name": "readme",
"version": "0.0.0"
}

View File

@@ -55,7 +55,7 @@ export function help () {
}
export async function handler (
opts: Pick<UniversalOptions, 'dir'> & Pick<Config, 'ignoreScripts' | 'rawConfig'> & Partial<Pick<Config, 'extraBinPaths'>> & {
opts: Pick<UniversalOptions, 'dir'> & Pick<Config, 'ignoreScripts' | 'rawConfig' | 'embedReadme'> & Partial<Pick<Config, 'extraBinPaths'>> & {
argv: {
original: string[]
}
@@ -98,7 +98,12 @@ export async function handler (
const destDir = opts.packDestination
? (path.isAbsolute(opts.packDestination) ? opts.packDestination : path.join(dir, opts.packDestination ?? '.'))
: dir
await packPkg(path.join(destDir, tarballName), filesMap, dir)
await packPkg({
destFile: path.join(destDir, tarballName),
filesMap,
projectDir: dir,
embedReadme: opts.embedReadme,
})
if (!opts.ignoreScripts) {
await _runScriptsIfPresent(['postpack'], entryManifest)
}
@@ -107,7 +112,25 @@ export async function handler (
const modeIsExecutable = (mode: number) => (mode & 0o111) === 0o111
async function packPkg (destFile: string, filesMap: Record<string, string>, projectDir: string): Promise<void> {
async function readReadmeFile (filesMap: Record<string, string>) {
const readmePath = Object.keys(filesMap).find(name => /^package\/readme\.md$/i.test(name))
const readmeFile = readmePath ? await fs.promises.readFile(filesMap[readmePath], 'utf8') : undefined
return readmeFile
}
async function packPkg (opts: {
destFile: string
filesMap: Record<string, string>
projectDir: string
embedReadme?: boolean
}): Promise<void> {
const {
destFile,
filesMap,
projectDir,
embedReadme,
} = opts
const { manifest } = await readProjectManifest(projectDir, {})
const bins = [
...(await binify(manifest as DependencyManifest, projectDir)).map(({ path }) => path),
@@ -124,8 +147,7 @@ async function packPkg (destFile: string, filesMap: Record<string, string>, proj
}
const mode = isExecutable ? 0o755 : 0o644
if (/^package\/package\.(json|json5|yaml)/.test(name)) {
const readmePath = Object.keys(filesMap).find(name => /^package\/readme.md$/i.test(name))
const readmeFile = readmePath ? await fs.promises.readFile(filesMap[readmePath], 'utf8') : undefined
const readmeFile = embedReadme ? await readReadmeFile(filesMap) : undefined
const publishManifest = await exportableManifest(projectDir, manifest, { readmeFile })
pack.entry({ mode, mtime, name: 'package/package.json' }, JSON.stringify(publishManifest, null, 2))
continue

View File

@@ -26,6 +26,7 @@ export function rcOptionsTypes () {
'registry',
'tag',
'unsafe-perm',
'embed-readme',
], allTypes)
}
@@ -104,7 +105,7 @@ export async function handler (
engineStrict?: boolean
recursive?: boolean
workspaceDir?: string
} & Pick<Config, 'allProjects' | 'gitChecks' | 'ignoreScripts' | 'publishBranch'>,
} & Pick<Config, 'allProjects' | 'gitChecks' | 'ignoreScripts' | 'publishBranch' | 'embedReadme'>,
params: string[]
) {
if (opts.gitChecks !== false && await isGitRepo()) {

View File

@@ -127,3 +127,41 @@ const modeIsExecutable = (mode: number) => (mode & 0o111) === 0o111
expect(modeIsExecutable(stat.mode)).toBeFalsy()
}
})
test('pack: should embed readme', async () => {
tempDir()
await pack.handler({
...DEFAULT_OPTS,
argv: { original: [] },
dir: path.join(__dirname, '../fixtures/readme'),
extraBinPaths: [],
packDestination: process.cwd(),
embedReadme: true,
})
await tar.x({ file: 'readme-0.0.0.tgz' })
const pkg = await import(path.resolve('package/package.json'))
expect(pkg.readme).toBeTruthy()
})
test('pack: should not embed readme', async () => {
tempDir()
await pack.handler({
...DEFAULT_OPTS,
argv: { original: [] },
dir: path.join(__dirname, '../fixtures/readme'),
extraBinPaths: [],
packDestination: process.cwd(),
embedReadme: false,
})
await tar.x({ file: 'readme-0.0.0.tgz' })
const pkg = await import(path.resolve('package/package.json'))
expect(pkg.readme).toBeFalsy()
})