Skip to content

Commit

Permalink
feat(taro-cli): 配置文件分开存放,优化 taro config 输出
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfield550 authored and luckyadam committed Jan 7, 2020
1 parent 69151e3 commit a641649
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
9 changes: 6 additions & 3 deletions packages/taro-cli/bin/taro-config
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ const program = require('commander')
const helper = require('../dist/taro-config')

program
.name('taro config')
.usage('<command> [options]')
.option('--json', '以 JSON 形式输出')
.on('--help', function () {
console.log('')
Expand All @@ -22,21 +24,22 @@ const [cmd, key, value] = args

switch (cmd) {
case 'get':
if (!key) return
if (!key) return console.log('Usage: taro config get foo')
helper.get(key)
break
case 'set':
if (!key || !value) return
if (!key || !value) return console.log('Usage: taro config set foo bar')
helper.set(key, value)
break
case 'delete':
if (!key) return
if (!key) return console.log('Usage: taro config delete foo')
helper.deleteKey(key)
break
case 'list':
case 'ls':
helper.list(json)
break
default:
program.help()
break
}
19 changes: 17 additions & 2 deletions packages/taro-cli/src/taro-config/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import * as fs from 'fs-extra'
import * as path from 'path'
import { getUserHomeDir } from '../util'
import { TARO_CONFIG_FLODER, TARO_BASE_CONFIG } from '../util/constants'

const homedir = getUserHomeDir()
const configPath = path.join(homedir, '.taro/index.json')
const configPath = path.join(homedir, `${TARO_CONFIG_FLODER}/${TARO_BASE_CONFIG}`)

function displayConfigPath (configPath) {
console.log('Config path:', configPath)
console.log()
}

export function get (key: string) {
if (!homedir) return console.log('找不到用户根目录')

if (fs.existsSync(configPath)) {
displayConfigPath(configPath)
const config = fs.readJSONSync(configPath)
console.log(config[key])
console.log('Key:', key, ', value:', config[key])
}
}

export function set (key: string, value: string) {
if (!homedir) return console.log('找不到用户根目录')

if (fs.existsSync(configPath)) {
displayConfigPath(configPath)
const config = fs.readJSONSync(configPath)
config[key] = value
fs.writeJSONSync(configPath, config)
Expand All @@ -26,21 +35,27 @@ export function set (key: string, value: string) {
[key]: value
})
}
console.log('Set key:', key, ', value:', value)
}

export function deleteKey (key: string) {
if (!homedir) return console.log('找不到用户根目录')

if (fs.existsSync(configPath)) {
displayConfigPath(configPath)
const config = fs.readJSONSync(configPath)
delete config[key]
fs.writeJSONSync(configPath, config)
}
console.log('Deleted:', key)
}

export function list (isJSONFormat: boolean = false) {
if (!homedir) return console.log('找不到用户根目录')

if (fs.existsSync(configPath)) {
displayConfigPath(configPath)
console.log('Config info:')
const config = fs.readJSONSync(configPath)
if (isJSONFormat) {
console.log(JSON.stringify(config, null, 2))
Expand Down
2 changes: 1 addition & 1 deletion packages/taro-cli/src/util/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,5 +365,5 @@ export const DEVICE_RATIO_NAME = 'deviceRatio'
export const isWindows = os.platform() === 'win32'

export const DEFAULT_TEMPLATE_SRC = 'github:NervJS/taro-project-templates#next'
export const TARO_CONFIG_FLODER = '.taro'
export const TARO_CONFIG_FLODER = '.taro2'
export const TARO_BASE_CONFIG = 'index.json'

0 comments on commit a641649

Please sign in to comment.