Skip to content

Commit

Permalink
Merge branch 'next' of https://github.com/NervJS/taro into next
Browse files Browse the repository at this point in the history
  • Loading branch information
SpringHgui committed Apr 12, 2022
2 parents b6b97e6 + bdceeed commit 93cfff1
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 151 deletions.
53 changes: 33 additions & 20 deletions packages/taro-cli/src/__tests__/cli.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,6 @@ describe('inspect', () => {
})

describe('init', () => {
const baseOpts = {
appPath: APP_PATH,
projectName: undefined,
typescript: undefined,
templateSource: undefined,
clone: false,
template: undefined,
css: undefined,
isHelp: false
}

it('should make configs', () => {
const projectName = 'temp'
const templateSource = 'https://url'
Expand All @@ -113,14 +102,23 @@ describe('inspect', () => {
const ins = MockedKernel.mock.instances[0]
expect(ins.run).toHaveBeenCalledWith({
name: 'init',
opts: Object.assign({}, baseOpts, {
projectName,
typescript: true,
templateSource,
clone: true,
template,
css
})
opts: {
_: [
'init',
'temp'
],
options: {
appPath: APP_PATH,
projectName,
typescript: true,
templateSource,
description: undefined,
clone: true,
template,
css
},
isHelp: false
}
})
})

Expand All @@ -131,7 +129,22 @@ describe('inspect', () => {
const ins = MockedKernel.mock.instances[0]
expect(ins.run).toHaveBeenCalledWith({
name: 'init',
opts: Object.assign({}, baseOpts, { projectName })
opts: {
_: [
'init'
],
options: {
appPath: APP_PATH,
projectName,
typescript: undefined,
templateSource: undefined,
description: undefined,
clone: false,
template: undefined,
css: undefined
},
isHelp: false
}
})
})
})
Expand Down
88 changes: 74 additions & 14 deletions packages/taro-cli/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import * as fs from 'fs-extra'
import * as path from 'path'

import * as minimist from 'minimist'
import { Kernel } from '@tarojs/service'

import init from './commands/init'
import customCommand from './commands/customCommand'
import { getPkgVersion } from './util'

Expand Down Expand Up @@ -36,23 +34,85 @@ export default class CLI {
const _ = args._
const command = _[0]
if (command) {
const appPath = this.appPath
const presetsPath = path.resolve(__dirname, 'presets')
const commandsPath = path.resolve(presetsPath, 'commands')
const platformsPath = path.resolve(presetsPath, 'platforms')
const filesPath = path.resolve(presetsPath, 'files')
const commandPlugins = fs.readdirSync(commandsPath)
const targetPlugin = `${command}.js`

// 设置环境变量
process.env.NODE_ENV ||= args.env || (args.watch ? 'development' : 'production')

const kernel = new Kernel({
appPath: this.appPath,
presets: [
path.resolve(__dirname, '.', 'presets', 'index.js')
]
appPath,
presets: [],
plugins: []
})
kernel.optsPlugins ||= []

// 针对不同的内置命令注册对应的命令插件
if (commandPlugins.includes(targetPlugin)) {
kernel.optsPlugins.push(path.resolve(commandsPath, targetPlugin))
}

switch (command) {
case 'build': {
let plugin
let platform = args.type
const { publicPath, bundleOutput, sourcemapOutput, sourceMapUrl, sourcemapSourcesRoot, assetsDest } = args

// 针对不同的内置平台注册对应的端平台插件
switch (platform) {
case 'weapp':
case 'alipay':
case 'swan':
case 'tt':
case 'qq':
case 'jd':
kernel.optsPlugins = [
...kernel.optsPlugins,
`@tarojs/plugin-platform-${platform}`,
path.resolve(filesPath, 'writeFileToDist.js'),
path.resolve(filesPath, 'generateProjectConfig.js'),
path.resolve(filesPath, 'generateFrameworkInfo.js')
]
break
default: {
// h5, rn
const platformPlugins = fs.readdirSync(platformsPath)
const targetPlugin = `${platform}.js`
if (platformPlugins.includes(targetPlugin)) {
kernel.optsPlugins.push(path.resolve(platformsPath, targetPlugin))
}
break
}
}

// 根据 framework 启用插件
const framework = kernel.config?.initialConfig.framework
switch (framework) {
case 'vue':
kernel.optsPlugins.push('@tarojs/plugin-framework-vue2')
break
case 'vue3':
kernel.optsPlugins.push('@tarojs/plugin-framework-vue3')
break
default:
kernel.optsPlugins.push('@tarojs/plugin-framework-react')
break
}

// 编译小程序插件
if (typeof args.plugin === 'string') {
plugin = args.plugin
platform = 'plugin'
kernel.optsPlugins.push(path.resolve(platformsPath, 'plugin.js'))
}
customCommand('build', kernel, {
_: args._,

customCommand(command, kernel, {
_,
platform,
plugin,
isWatch: Boolean(args.watch),
Expand All @@ -73,17 +133,17 @@ export default class CLI {
break
}
case 'init': {
const projectName = _[1] || args.name
init(kernel, {
appPath: this.appPath,
projectName,
customCommand(command, kernel, {
_,
appPath,
projectName: _[1] || args.name,
description: args.description,
typescript: args.typescript,
templateSource: args['template-source'],
clone: !!args.clone,
template: args.template,
css: args.css,
isHelp: args.h
h: args.h
})
break
}
Expand Down
1 change: 0 additions & 1 deletion packages/taro-cli/src/commands/customCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default function customCommand (
})

// 设置环境变量
process.env.NODE_ENV = process.env.NODE_ENV || options.env || (options.isWatch ? 'development' : 'production')
if (options.platform) {
process.env.TARO_ENV = options.platform
}
Expand Down
38 changes: 0 additions & 38 deletions packages/taro-cli/src/commands/init.ts

This file was deleted.

5 changes: 3 additions & 2 deletions packages/taro-cli/src/presets/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ export default (ctx: IPluginContext) => {
'--css [css]': 'CSS预处理器(sass/less/stylus/none)',
'-h, --help': 'output usage information'
},
async fn () {
async fn (opts) {
// init project
const { appPath } = ctx.paths
const { projectName, templateSource, clone, template, description, typescript, css } = ctx.runOpts
const { options } = opts
const { projectName, templateSource, clone, template, description, typescript, css } = options
const Project = require('../../create/project').default
const project = new Project({
projectName,
Expand Down
40 changes: 0 additions & 40 deletions packages/taro-cli/src/presets/index.ts

This file was deleted.

3 changes: 1 addition & 2 deletions packages/taro-helper/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as fs from 'fs-extra'
import * as chalk from 'chalk'
import * as chokidar from 'chokidar'
import createDebug from 'debug'

import * as constants from './constants'
import * as utils from './utils'
Expand All @@ -17,7 +16,7 @@ export const helper = {
fs,
chalk,
chokidar,
createDebug
createDebug: id => require('debug')(id)
}

export default helper
5 changes: 3 additions & 2 deletions packages/taro-helper/src/npm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import * as resolvePath from 'resolve'
import * as spawn from 'cross-spawn'
import * as chalk from 'chalk'

import * as Util from './utils'
Expand All @@ -23,6 +21,7 @@ const defaultInstallOptions: IInstallOptions = {
export const taroPluginPrefix = '@tarojs/plugin-'

export function resolveNpm (pluginName: string, root): Promise<string> {
const resolvePath = require('resolve')
if (!npmCached[pluginName]) {
return new Promise((resolve, reject) => {
resolvePath(`${pluginName}`, { basedir: root }, (err, res) => {
Expand All @@ -38,6 +37,7 @@ export function resolveNpm (pluginName: string, root): Promise<string> {
}

export function resolveNpmSync (pluginName: string, root): string {
const resolvePath = require('resolve')
try {
if (!npmCached[pluginName]) {
const res = resolvePath.sync(pluginName, { basedir: root })
Expand Down Expand Up @@ -101,6 +101,7 @@ export function installNpmPkg (pkgList: string[] | string, options: IInstallOpti
args.push('--save')
}
}
const spawn = require('cross-spawn')
const output = spawn.sync(installer, args, {
stdio: ['ignore', 'pipe', 'inherit']
})
Expand Down
13 changes: 6 additions & 7 deletions packages/taro-helper/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import * as fs from 'fs-extra'
import * as path from 'path'
import * as os from 'os'
import { Transform } from 'stream'
import * as child_process from 'child_process'
import * as parser from '@babel/parser'
import traverse from '@babel/traverse'

import * as chalk from 'chalk'
import * as findWorkspaceRoot from 'find-yarn-workspace-root'
import { isPlainObject, camelCase, mergeWith, flatMap } from 'lodash'
import * as yauzl from 'yauzl'

import {
processTypeEnum,
processTypeMap,
Expand Down Expand Up @@ -123,6 +116,7 @@ export function printLog (type: processTypeEnum, tag: string, filePath?: string)
}

export function recursiveFindNodeModules (filePath: string, lastFindPath?: string): string {
const findWorkspaceRoot = require('find-yarn-workspace-root')
if (lastFindPath && (normalizePath(filePath) === normalizePath(lastFindPath))) {
return filePath
}
Expand Down Expand Up @@ -393,6 +387,9 @@ export const applyArrayedVisitors = obj => {
}

export function unzip (zipPath) {
const Transform = require('stream').Transform
const yauzl = require('yauzl')

return new Promise<void>((resolve, reject) => {
yauzl.open(zipPath, { lazyEntries: true }, (err, zipfile) => {
if (err || !zipfile) throw err
Expand Down Expand Up @@ -495,6 +492,8 @@ export function removeHeadSlash (str: string) {
}

function analyzeImport (filePath: string): string[] {
const parser = require('@babel/parser')
const traverse = require('@babel/traverse').default
const code = fs.readFileSync(filePath).toString()
let importPaths: string[] = []
filePath = path.dirname(filePath)
Expand Down
Loading

0 comments on commit 93cfff1

Please sign in to comment.