get_simsimPath; require('readline-sync')
This commit is contained in:
		
							parent
							
								
									94fa5915b3
								
							
						
					
					
						commit
						f5d0f0e3ef
					
				@ -2,6 +2,8 @@ const fs = require('fs')
 | 
			
		||||
const path = require('path')
 | 
			
		||||
const chokidar = require('chokidar')
 | 
			
		||||
const assign_deep = require('assign-deep')
 | 
			
		||||
const readlineSync = require('readline-sync')
 | 
			
		||||
 | 
			
		||||
//const deepmerge = require('deepmerge')
 | 
			
		||||
// https://github.com/jonschlinkert/assign-deep // 类似 Object.assign 直接赋值到第一个对象里。只下载2个包。assign 优选。
 | 
			
		||||
// https://github.com/jonschlinkert/merge-deep // 生成一个新对象。会下载7~8个包。
 | 
			
		||||
@ -30,6 +32,45 @@ module.exports = {
 | 
			
		||||
    })
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
  get_simsimPath ({ envarFiles = [], readable = false, readPrompt = '' }) {
 | 
			
		||||
    let simsimPath
 | 
			
		||||
    // 用户自己定义的优先
 | 
			
		||||
    for (let configFile of envarFiles) {
 | 
			
		||||
      try {
 | 
			
		||||
        const envar = require(path.resolve(configFile))
 | 
			
		||||
        if (envar?.simsimPath) {
 | 
			
		||||
          try {
 | 
			
		||||
            require(path.resolve(envar.simsimPath))
 | 
			
		||||
            simsimPath = envar?.simsimPath
 | 
			
		||||
          } catch (expt) {
 | 
			
		||||
            console.log('!!! ' + path.resolve(envar.simsimPath) + ' is invalid!')
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
      } catch (expt) {
 | 
			
		||||
        console.log('bad format!', path.resolve(configFile))
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    // 其次检测是否已安装
 | 
			
		||||
    if (!simsimPath) {
 | 
			
		||||
      module.paths.forEach((modulesPath) => {
 | 
			
		||||
        if (fs.existsSync(`${modulesPath}/sesame-basic`)) {
 | 
			
		||||
          simsimPath = `${modulesPath}/sesame-basic`
 | 
			
		||||
        }
 | 
			
		||||
      })
 | 
			
		||||
    }
 | 
			
		||||
    // 最后等待用户输入
 | 
			
		||||
    while (!simsimPath && readable) {
 | 
			
		||||
      simsimPath = readlineSync.question(readPrompt)
 | 
			
		||||
      try {
 | 
			
		||||
        require(path.resolve(simsimPath))
 | 
			
		||||
      } catch (expt) {
 | 
			
		||||
        console.log('!!! ' + path.resolve(simsimPath) + ' is invalid!')
 | 
			
		||||
        simsimPath = ''
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    return simsimPath
 | 
			
		||||
  },
 | 
			
		||||
 | 
			
		||||
  /** 合并 envar files 和 commander parameters 中的环境变量。
 | 
			
		||||
   * @param envarFiles:
 | 
			
		||||
   *  - 字符串: 导入文件,内容应当是字符串数组,或者对象。
 | 
			
		||||
 | 
			
		||||
@ -7,7 +7,8 @@
 | 
			
		||||
  "dependencies": {
 | 
			
		||||
    "assign-deep": "^1.0.1",
 | 
			
		||||
    "chokidar": "^4.0.0",
 | 
			
		||||
    "commander": "^4.1.1"
 | 
			
		||||
    "commander": "^4.1.1",
 | 
			
		||||
    "readline-sync": "^1.4.10"
 | 
			
		||||
  },
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "setup": "npm install"
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user