mirror of
https://github.com/pnpm/pnpm.git
synced 2025-12-23 23:29:17 -05:00
feat: add own implementation of the init command (#4422)
This commit is contained in:
5
.changeset/cyan-beers-tease.md
Normal file
5
.changeset/cyan-beers-tease.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@pnpm/plugin-commands-init": major
|
||||
---
|
||||
|
||||
Initial release.
|
||||
5
.changeset/friendly-coats-buy.md
Normal file
5
.changeset/friendly-coats-buy.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@pnpm/types": minor
|
||||
---
|
||||
|
||||
New fields add to package.json type.
|
||||
5
.changeset/long-fishes-compete.md
Normal file
5
.changeset/long-fishes-compete.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"pnpm": major
|
||||
---
|
||||
|
||||
`pnpm init` is non-interactive [#4422](https://github.com/pnpm/pnpm/pull/4422).
|
||||
15
packages/plugin-commands-init/README.md
Normal file
15
packages/plugin-commands-init/README.md
Normal file
@@ -0,0 +1,15 @@
|
||||
# @pnpm/plugin-commands-init
|
||||
|
||||
> Create a package.json file
|
||||
|
||||
[](https://www.npmjs.com/package/@pnpm/plugin-commands-init)
|
||||
|
||||
## Installation
|
||||
|
||||
```sh
|
||||
pnpm add @pnpm/plugin-commands-init
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
4
packages/plugin-commands-init/jest.config.js
Normal file
4
packages/plugin-commands-init/jest.config.js
Normal file
@@ -0,0 +1,4 @@
|
||||
const config = require('../../jest.config.js')
|
||||
|
||||
module.exports = config
|
||||
|
||||
46
packages/plugin-commands-init/package.json
Normal file
46
packages/plugin-commands-init/package.json
Normal file
@@ -0,0 +1,46 @@
|
||||
{
|
||||
"name": "@pnpm/plugin-commands-init",
|
||||
"version": "0.0.0",
|
||||
"description": "Create a package.json file",
|
||||
"main": "lib/index.js",
|
||||
"types": "lib/index.d.ts",
|
||||
"files": [
|
||||
"lib",
|
||||
"!*.map"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=12.17"
|
||||
},
|
||||
"scripts": {
|
||||
"lint": "eslint src/**/*.ts test/**/*.ts",
|
||||
"_test": "jest",
|
||||
"test": "pnpm run compile && pnpm run _test",
|
||||
"prepublishOnly": "pnpm run compile",
|
||||
"compile": "tsc --build && pnpm run lint --fix",
|
||||
"update-responses": "ts-node test/utils/responses/update.ts"
|
||||
},
|
||||
"repository": "https://github.com/pnpm/pnpm/blob/main/packages/plugin-commands-init",
|
||||
"keywords": [
|
||||
"pnpm7",
|
||||
"pnpm",
|
||||
"init"
|
||||
],
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/pnpm/pnpm/issues"
|
||||
},
|
||||
"homepage": "https://github.com/pnpm/pnpm/blob/main/packages/plugin-commands-init#readme",
|
||||
"devDependencies": {
|
||||
"@pnpm/plugin-commands-init": "workspace:0.0.0",
|
||||
"@pnpm/prepare": "workspace:*",
|
||||
"load-json-file": "^6.2.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pnpm/cli-utils": "workspace:0.6.50",
|
||||
"@pnpm/config": "workspace:13.13.2",
|
||||
"@pnpm/error": "workspace:2.1.0",
|
||||
"@pnpm/write-project-manifest": "workspace:2.0.11",
|
||||
"render-help": "^1.0.1"
|
||||
},
|
||||
"funding": "https://opencollective.com/pnpm"
|
||||
}
|
||||
3
packages/plugin-commands-init/src/index.ts
Normal file
3
packages/plugin-commands-init/src/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import * as init from './init'
|
||||
|
||||
export { init }
|
||||
51
packages/plugin-commands-init/src/init.ts
Normal file
51
packages/plugin-commands-init/src/init.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import fs from 'fs'
|
||||
import path from 'path'
|
||||
import { docsUrl } from '@pnpm/cli-utils'
|
||||
import { UniversalOptions } from '@pnpm/config'
|
||||
import PnpmError from '@pnpm/error'
|
||||
import writeProjectManifest from '@pnpm/write-project-manifest'
|
||||
import renderHelp from 'render-help'
|
||||
|
||||
export const rcOptionsTypes = cliOptionsTypes
|
||||
|
||||
export function cliOptionsTypes () {
|
||||
return {}
|
||||
}
|
||||
|
||||
export const commandNames = ['init']
|
||||
|
||||
export function help () {
|
||||
return renderHelp({
|
||||
description: 'Create a package.json file',
|
||||
descriptionLists: [],
|
||||
url: docsUrl('init'),
|
||||
usages: ['pnpm init'],
|
||||
})
|
||||
}
|
||||
|
||||
export async function handler (
|
||||
opts: Pick<UniversalOptions, 'dir'>
|
||||
) {
|
||||
const manifestPath = path.join(opts.dir, 'package.json')
|
||||
if (fs.existsSync(manifestPath)) {
|
||||
throw new PnpmError('PACKAGE_JSON_EXISTS', 'package.json already exists')
|
||||
}
|
||||
const manifest = {
|
||||
name: path.basename(opts.dir),
|
||||
version: '1.0.0',
|
||||
description: '',
|
||||
main: 'index.js',
|
||||
scripts: {
|
||||
test: 'echo "Error: no test specified" && exit 1',
|
||||
},
|
||||
keywords: [],
|
||||
author: '',
|
||||
license: 'ISC',
|
||||
}
|
||||
await writeProjectManifest(manifestPath, manifest, {
|
||||
indent: 2,
|
||||
})
|
||||
return `Wrote to ${path.join(opts.dir, 'package.json')}
|
||||
|
||||
${JSON.stringify(manifest, null, 2)}`
|
||||
}
|
||||
19
packages/plugin-commands-init/test/init.test.ts
Normal file
19
packages/plugin-commands-init/test/init.test.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import path from 'path'
|
||||
import { init } from '@pnpm/plugin-commands-init'
|
||||
import prepare, { prepareEmpty } from '@pnpm/prepare'
|
||||
import { sync as loadJsonFile } from 'load-json-file'
|
||||
|
||||
test('init a new package.json', async () => {
|
||||
prepareEmpty()
|
||||
await init.handler({ dir: process.cwd() })
|
||||
const manifest = loadJsonFile(path.resolve('package.json'))
|
||||
expect(manifest).toBeTruthy()
|
||||
})
|
||||
|
||||
test('throws an error if a package.json exists in the current directory', async () => {
|
||||
prepare({})
|
||||
|
||||
await expect(
|
||||
init.handler({ dir: process.cwd() })
|
||||
).rejects.toThrow('package.json already exists')
|
||||
})
|
||||
28
packages/plugin-commands-init/tsconfig.json
Normal file
28
packages/plugin-commands-init/tsconfig.json
Normal file
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"extends": "@pnpm/tsconfig",
|
||||
"compilerOptions": {
|
||||
"outDir": "lib",
|
||||
"rootDir": "src"
|
||||
},
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"../../typings/**/*.d.ts"
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"path": "../../privatePackages/prepare"
|
||||
},
|
||||
{
|
||||
"path": "../cli-utils"
|
||||
},
|
||||
{
|
||||
"path": "../config"
|
||||
},
|
||||
{
|
||||
"path": "../error"
|
||||
},
|
||||
{
|
||||
"path": "../write-project-manifest"
|
||||
}
|
||||
]
|
||||
}
|
||||
8
packages/plugin-commands-init/tsconfig.lint.json
Normal file
8
packages/plugin-commands-init/tsconfig.lint.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"include": [
|
||||
"src/**/*.ts",
|
||||
"test/**/*.ts",
|
||||
"../../typings/**/*.d.ts"
|
||||
]
|
||||
}
|
||||
@@ -42,6 +42,7 @@
|
||||
"@pnpm/pick-registry-for-package": "workspace:2.0.11",
|
||||
"@pnpm/plugin-commands-audit": "workspace:5.1.42",
|
||||
"@pnpm/plugin-commands-env": "workspace:1.4.14",
|
||||
"@pnpm/plugin-commands-init": "workspace:0.0.0",
|
||||
"@pnpm/plugin-commands-installation": "workspace:8.4.4",
|
||||
"@pnpm/plugin-commands-listing": "workspace:4.1.11",
|
||||
"@pnpm/plugin-commands-outdated": "workspace:5.1.10",
|
||||
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
import { server } from '@pnpm/plugin-commands-server'
|
||||
import { setup } from '@pnpm/plugin-commands-setup'
|
||||
import { store } from '@pnpm/plugin-commands-store'
|
||||
import { init } from '@pnpm/plugin-commands-init'
|
||||
import pick from 'ramda/src/pick'
|
||||
import { PnpmOptions } from '../types'
|
||||
import * as bin from './bin'
|
||||
@@ -99,6 +100,7 @@ const commands: CommandDefinition[] = [
|
||||
exec,
|
||||
fetch,
|
||||
importCommand,
|
||||
init,
|
||||
install,
|
||||
installTest,
|
||||
link,
|
||||
|
||||
@@ -36,7 +36,6 @@ const argv = process.argv.slice(2)
|
||||
case 'edit':
|
||||
case 'get':
|
||||
case 'info':
|
||||
case 'init':
|
||||
case 'login':
|
||||
case 'logout':
|
||||
case 'owner':
|
||||
|
||||
@@ -61,7 +61,7 @@ test('pass through to npm with all the args', async () => {
|
||||
prepare()
|
||||
await rimraf('package.json')
|
||||
|
||||
const result = execPnpmSync(['init', '-y'])
|
||||
const result = execPnpmSync(['dist-tag', 'ls', 'pnpm'])
|
||||
|
||||
expect(result.status).toBe(0)
|
||||
})
|
||||
|
||||
@@ -78,6 +78,9 @@
|
||||
{
|
||||
"path": "../plugin-commands-env"
|
||||
},
|
||||
{
|
||||
"path": "../plugin-commands-init"
|
||||
},
|
||||
{
|
||||
"path": "../plugin-commands-installation"
|
||||
},
|
||||
|
||||
@@ -91,6 +91,9 @@ export interface BaseManifest {
|
||||
types?: string
|
||||
publishConfig?: PublishConfig
|
||||
readme?: string
|
||||
keywords?: string[]
|
||||
author?: string
|
||||
license?: string
|
||||
}
|
||||
|
||||
export type DependencyManifest = BaseManifest & Required<Pick<BaseManifest, 'name' | 'version'>>
|
||||
|
||||
23
pnpm-lock.yaml
generated
23
pnpm-lock.yaml
generated
@@ -2092,6 +2092,27 @@ importers:
|
||||
node-fetch: 3.0.0-beta.9
|
||||
path-name: 1.0.0
|
||||
|
||||
packages/plugin-commands-init:
|
||||
specifiers:
|
||||
'@pnpm/cli-utils': workspace:0.6.50
|
||||
'@pnpm/config': workspace:13.13.2
|
||||
'@pnpm/error': workspace:2.1.0
|
||||
'@pnpm/plugin-commands-init': workspace:0.0.0
|
||||
'@pnpm/prepare': workspace:*
|
||||
'@pnpm/write-project-manifest': workspace:2.0.11
|
||||
load-json-file: ^6.2.0
|
||||
render-help: ^1.0.1
|
||||
dependencies:
|
||||
'@pnpm/cli-utils': link:../cli-utils
|
||||
'@pnpm/config': link:../config
|
||||
'@pnpm/error': link:../error
|
||||
'@pnpm/write-project-manifest': link:../write-project-manifest
|
||||
render-help: 1.0.2
|
||||
devDependencies:
|
||||
'@pnpm/plugin-commands-init': 'link:'
|
||||
'@pnpm/prepare': link:../../privatePackages/prepare
|
||||
load-json-file: 6.2.0
|
||||
|
||||
packages/plugin-commands-installation:
|
||||
specifiers:
|
||||
'@pnpm/assert-project': workspace:*
|
||||
@@ -2744,6 +2765,7 @@ importers:
|
||||
'@pnpm/pick-registry-for-package': workspace:2.0.11
|
||||
'@pnpm/plugin-commands-audit': workspace:5.1.42
|
||||
'@pnpm/plugin-commands-env': workspace:1.4.14
|
||||
'@pnpm/plugin-commands-init': workspace:0.0.0
|
||||
'@pnpm/plugin-commands-installation': workspace:8.4.4
|
||||
'@pnpm/plugin-commands-listing': workspace:4.1.11
|
||||
'@pnpm/plugin-commands-outdated': workspace:5.1.10
|
||||
@@ -2832,6 +2854,7 @@ importers:
|
||||
'@pnpm/pick-registry-for-package': link:../pick-registry-for-package
|
||||
'@pnpm/plugin-commands-audit': link:../plugin-commands-audit
|
||||
'@pnpm/plugin-commands-env': link:../plugin-commands-env
|
||||
'@pnpm/plugin-commands-init': link:../plugin-commands-init
|
||||
'@pnpm/plugin-commands-installation': link:../plugin-commands-installation
|
||||
'@pnpm/plugin-commands-listing': link:../plugin-commands-listing
|
||||
'@pnpm/plugin-commands-outdated': link:../plugin-commands-outdated
|
||||
|
||||
Reference in New Issue
Block a user