Skip to content

Commit

Permalink
feat: embed-readme option (#4265)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangreen committed Jan 20, 2022
1 parent 1d013d7 commit b7566b9
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/tough-terms-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@pnpm/config": minor
"@pnpm/plugin-commands-publishing": minor
---

embed-readme option was added
1 change: 1 addition & 0 deletions packages/config/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export interface Config {
enablePnp?: boolean
enableModulesDir: boolean
modulesCacheMaxAge: number
embedReadme?: boolean

registries: Registries
ignoreWorkspaceRootCheck: boolean
Expand Down
5 changes: 5 additions & 0 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "readme",
"version": "0.0.0"
}
32 changes: 27 additions & 5 deletions packages/plugin-commands-publishing/src/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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),
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin-commands-publishing/src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export function rcOptionsTypes () {
'registry',
'tag',
'unsafe-perm',
'embed-readme',
], allTypes)
}

Expand Down Expand Up @@ -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()) {
Expand Down
38 changes: 38 additions & 0 deletions packages/plugin-commands-publishing/test/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

0 comments on commit b7566b9

Please sign in to comment.