Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I get different results using mac terminals and using cross-spawn #159

Open
qiaolin-li opened this issue Jul 21, 2024 · 0 comments
Open

Comments

@qiaolin-li
Copy link

qiaolin-li commented Jul 21, 2024

image
qiaolin@qiaolindeMacBook-Pro-2 ~ % npm root -g
/Users/qiaolin/.nvm/versions/node/v16.20.2/lib/node_modules
image
 /**
     * 
     * @param {*} cmd 需要执行的命令,例如install、uninstall、update
     * @param {*} modules 需要操作的模块,例如[plugin1, plugin2]
     * @param {*} where 
     * @returns 
     */
      async execCommand(cmd, modules, where) {
        return await new Promise((resolve, reject) => {
            try {
                // spawn的命令行参数是以数组形式传入
                // 此处将命令和要安装的插件以数组的形式拼接起来
                // 此处的cmd指的是执行的命令,比如install\uninstall\update
                let args = [cmd].concat(modules).concat('--color=always').concat('--save')
                
                // 执行npm,并通过 cwd指定执行的路径——配置文件所在文件夹
                const npm = spawn(`npm`, args, { cwd: where }) 

                let output = ''
                // 获取输出、报错日志
                npm.stdout.on('data', (data) => output += data).pipe(process.stdout)
                npm.stderr.on('data', (data) => output += data).pipe(process.stderr)

                npm.on('close', (code) => {
                    // 如果没有报错就输出正常日志
                    if (!code) {
                        resolve(output) ;
                    } else {
                        reject(output);
                    }
                })

                npm.on('error', (err) => reject(err));
            } catch (error) {
                reject(error)
            }
        })
    }

   const aa  =lodash.trim(await this.execCommand('config', [ 'ls' ], constant.APPLICATION_PLUGINS_DIR));
     const       rootPath =lodash.trim(await this.execCommand('root', [ '-g' ], constant.APPLICATION_PLUGINS_DIR));

I get different results using mac terminals and using cross-spawn, why is that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant