Skip to content

Commit ebce8ab

Browse files
committed
feat: install next and plugin
1 parent d5084f9 commit ebce8ab

File tree

5 files changed

+96
-10
lines changed

5 files changed

+96
-10
lines changed

packages/create-nextjs/cli.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
'use strict'
3+
const create = require('.')
4+
5+
create()

packages/create-nextjs/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict'
2+
const path = require('path')
3+
const execa = require('execa')
4+
const hasYarn = require('has-yarn')
5+
const readPkgUp = require('read-pkg-up')
6+
const writePkg = require('write-pkg')
7+
8+
module.exports = async (options = {}) => {
9+
const packageResult = readPkgUp.sync({
10+
cwd: options.cwd,
11+
normalize: false
12+
}) || {}
13+
const packageJson = packageResult.package || {}
14+
const packagePath = packageResult.path || path.resolve(options.cwd || process.cwd(), 'package.json')
15+
const packageCwd = path.dirname(packagePath)
16+
17+
packageJson.scripts = packageJson.scripts || {}
18+
19+
const s = packageJson.scripts
20+
s['build:nextjs'] = 'taro build --type nextjs'
21+
s['dev:nextjs'] = 'npm run build:nextjs -- --watch'
22+
23+
writePkg.sync(packagePath, packageJson, {normalize: false})
24+
25+
if (options.skipInstall) {
26+
return
27+
}
28+
29+
const nextTag = 'next'
30+
const pluginTag = 'tarojs-plugin-platform-nextjs'
31+
32+
if (hasYarn(packageCwd)) {
33+
const yarnArguments = ['add', nextTag, pluginTag]
34+
35+
try {
36+
await execa('yarn', yarnArguments, {
37+
cwd: packageCwd,
38+
stdio: 'inherit'
39+
})
40+
} catch (error) {
41+
if (error.code === 'ENOENT') {
42+
console.error('This project uses Yarn but you don\'t seem to have Yarn installed.\nRun `npm install --global yarn` to install it.')
43+
return
44+
}
45+
46+
throw error
47+
}
48+
49+
return
50+
}
51+
52+
const npmArguments = ['install']
53+
54+
npmArguments.push(nextTag)
55+
npmArguments.push(pluginTag)
56+
57+
await execa('npm', npmArguments, {
58+
cwd: packageCwd,
59+
stdio: 'inherit'
60+
})
61+
}

packages/create-nextjs/package.json

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
"name": "create-tarojs-plugin-platform-nextjs",
33
"version": "2.0.11",
44
"description": "添加 Taro Next.js 插件到你的项目中",
5-
"main": "lib/index.js",
5+
"bin": "cli.js",
66
"scripts": {
7+
"test": "ava"
78
},
89
"keywords": [
910
"init",
@@ -31,5 +32,11 @@
3132
"bugs": {
3233
"url": "https://github.com/NervJS/tarojs-plugin-platform-nextjs/issues"
3334
},
34-
"homepage": "https://github.com/NervJS/tarojs-plugin-platform-nextjs#readme"
35+
"homepage": "https://github.com/NervJS/tarojs-plugin-platform-nextjs#readme",
36+
"dependencies": {
37+
"execa": "^0.7.0",
38+
"has-yarn": "^2.1.0",
39+
"read-pkg-up": "^6.0.0",
40+
"write-pkg": "^4.0.0"
41+
}
3542
}

packages/nextjs/README.md

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,10 @@
2828
npm init tarojs-plugin-platform-nextjs
2929
```
3030

31-
或者,你更喜欢使用 Yarn:
32-
33-
```bash
34-
yarn add tarojs-plugin-platform-nextjs --dev
35-
```
36-
3731
或者,你可以手动安装在你的 Taro 项目中安装**本插件****Next.js**
3832

3933
```bash
40-
yarn add next tarojs-plugin-platform-nextjs
34+
npm install next tarojs-plugin-platform-nextjs
4135
```
4236

4337
并在 Taro 项目的 `config/index.js` 中添加插件。

yarn.lock

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11267,6 +11267,11 @@ has-values@^1.0.0:
1126711267
is-number "^3.0.0"
1126811268
kind-of "^4.0.0"
1126911269

11270+
has-yarn@^2.1.0:
11271+
version "2.1.0"
11272+
resolved "https://registry.npmjs.org/has-yarn/-/has-yarn-2.1.0.tgz#137e11354a7b5bf11aa5cb649cf0c6f3ff2b2e77"
11273+
integrity sha512-UqBRqi4ju7T+TqGNdqAO0PaSVGsDGJUBQvk9eUWNGRY1CFGDzYhLWoM7JQEemnlvVcv/YEmc2wNW8BC24EnUsw==
11274+
1127011275
has@^1.0.0, has@^1.0.1, has@^1.0.3:
1127111276
version "1.0.3"
1127211277
resolved "https://registry.npmjs.org/has/-/has-1.0.3.tgz#722d7cbfc1f6aa8241f16dd814e011e1f41e8796"
@@ -18587,6 +18592,15 @@ read-pkg-up@^3.0.0:
1858718592
find-up "^2.0.0"
1858818593
read-pkg "^3.0.0"
1858918594

18595+
read-pkg-up@^6.0.0:
18596+
version "6.0.0"
18597+
resolved "https://registry.npmjs.org/read-pkg-up/-/read-pkg-up-6.0.0.tgz#da75ce72762f2fa1f20c5a40d4dd80c77db969e3"
18598+
integrity sha512-odtTvLl+EXo1eTsMnoUHRmg/XmXdTkwXVxy4VFE9Kp6cCq7b3l7QMdBndND3eAFzrbSAXC/WCUOQQ9rLjifKZw==
18599+
dependencies:
18600+
find-up "^4.0.0"
18601+
read-pkg "^5.1.1"
18602+
type-fest "^0.5.0"
18603+
1859018604
read-pkg-up@^7.0.1:
1859118605
version "7.0.1"
1859218606
resolved "https://registry.npmjs.org/read-pkg-up/-/read-pkg-up-7.0.1.tgz#f3a6135758459733ae2b95638056e1854e7ef507"
@@ -18614,7 +18628,7 @@ read-pkg@^3.0.0:
1861418628
normalize-package-data "^2.3.2"
1861518629
path-type "^3.0.0"
1861618630

18617-
read-pkg@^5.2.0:
18631+
read-pkg@^5.1.1, read-pkg@^5.2.0:
1861818632
version "5.2.0"
1861918633
resolved "https://registry.npmjs.org/read-pkg/-/read-pkg-5.2.0.tgz#7bf295438ca5a33e56cd30e053b34ee7250c93cc"
1862018634
integrity sha512-Ug69mNOpfvKDAc2Q8DRpMjjzdtrnv9HcSMX+4VsZxD1aZ6ZzrIE7rlzXBtWTyhULSMKg076AW6WR5iZpD0JiOg==
@@ -21604,6 +21618,11 @@ type-fest@^0.4.1:
2160421618
resolved "https://registry.npmjs.org/type-fest/-/type-fest-0.4.1.tgz#8bdf77743385d8a4f13ba95f610f5ccd68c728f8"
2160521619
integrity sha512-IwzA/LSfD2vC1/YDYMv/zHP4rDF1usCwllsDpbolT3D4fUepIO7f9K70jjmUewU/LmGUKJcwcVtDCpnKk4BPMw==
2160621620

21621+
type-fest@^0.5.0:
21622+
version "0.5.2"
21623+
resolved "https://registry.npmjs.org/type-fest/-/type-fest-0.5.2.tgz#d6ef42a0356c6cd45f49485c3b6281fc148e48a2"
21624+
integrity sha512-DWkS49EQKVX//Tbupb9TFa19c7+MK1XmzkrZUR8TAktmE/DizXoaoJV6TZ/tSIPXipqNiRI6CyAe7x69Jb6RSw==
21625+
2160721626
type-fest@^0.6.0:
2160821627
version "0.6.0"
2160921628
resolved "https://registry.npmjs.org/type-fest/-/type-fest-0.6.0.tgz#8d2a2370d3df886eb5c90ada1c5bf6188acf838b"

0 commit comments

Comments
 (0)