|
| 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 | +} |
0 commit comments