feat: add own implementation of the init command (#4422)

This commit is contained in:
Zoltan Kochan
2022-03-08 14:31:05 +02:00
committed by GitHub
parent 52e9b8b912
commit d504dc380c
18 changed files with 222 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/plugin-commands-init": major
---
Initial release.

View File

@@ -0,0 +1,5 @@
---
"@pnpm/types": minor
---
New fields add to package.json type.

View File

@@ -0,0 +1,5 @@
---
"pnpm": major
---
`pnpm init` is non-interactive [#4422](https://github.com/pnpm/pnpm/pull/4422).

View File

@@ -0,0 +1,15 @@
# @pnpm/plugin-commands-init
> Create a package.json file
[![npm version](https://img.shields.io/npm/v/@pnpm/plugin-commands-init.svg)](https://www.npmjs.com/package/@pnpm/plugin-commands-init)
## Installation
```sh
pnpm add @pnpm/plugin-commands-init
```
## License
MIT

View File

@@ -0,0 +1,4 @@
const config = require('../../jest.config.js')
module.exports = config

View 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"
}

View File

@@ -0,0 +1,3 @@
import * as init from './init'
export { init }

View 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)}`
}

View 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')
})

View 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"
}
]
}

View File

@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"include": [
"src/**/*.ts",
"test/**/*.ts",
"../../typings/**/*.d.ts"
]
}

View File

@@ -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",

View File

@@ -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,

View File

@@ -36,7 +36,6 @@ const argv = process.argv.slice(2)
case 'edit':
case 'get':
case 'info':
case 'init':
case 'login':
case 'logout':
case 'owner':

View File

@@ -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)
})

View File

@@ -78,6 +78,9 @@
{
"path": "../plugin-commands-env"
},
{
"path": "../plugin-commands-init"
},
{
"path": "../plugin-commands-installation"
},

View File

@@ -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
View File

@@ -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