如何升级节点文件中的特定包?
How to upgrade a specific package inside a Node file?
我有一个 Node
项目,其结构如下:
.
├── index.js
├── package.json
├── updater.js
└── yarn.lock
我在这个项目中使用 Yarn
。
内部文件:package.json
有对包的引用:@emotion/core@^10.0.22
如下所示:
{
"name": "my-project",
...
"dependencies": {
...
"@emotion/core": "^10.0.22",
...
}
...
}
我需要的是:
从内部文件:updater.js
文件,升级包:@emotion/core
到版本:^10.0.27
(以防万一,记住我使用的是Yarn
),所以我能做到:
$ node updater.js
从命令行我可以很容易地实现这一点:
$ yarn upgrade @emotion/core@^10.0.27
但我需要从文件内部执行此操作:updater.js
(这是一项要求)。这是一个最大问题的简化(所以我需要满足这个虚拟用例的要求,即使它没有意义)。
此代码将放入一个自定义包中,该包将负责安装其他一些包。
提前致谢!
最简单的方法是使用 child_process
执行任何脚本:
const { exec } = require('child_process');
const childProcess = exec('yarn upgrade @emotion/core@^10.0.27', (error, stdout, stderr) => {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
查看更多内容
您可以通过以下方式将 stdio 管道化以获得接近实时的输出:
childProcess.stdout.pipe(process.stdout)
但要小心使用 *Sync
(如 execSync
)库,您应该 NOT
尽可能阻止代码。最好使用 callback
或 Promise
也有像shelljs
这样的精彩包来封装这个。
我有一个 Node
项目,其结构如下:
.
├── index.js
├── package.json
├── updater.js
└── yarn.lock
我在这个项目中使用 Yarn
。
内部文件:package.json
有对包的引用:@emotion/core@^10.0.22
如下所示:
{
"name": "my-project",
...
"dependencies": {
...
"@emotion/core": "^10.0.22",
...
}
...
}
我需要的是:
从内部文件:updater.js
文件,升级包:@emotion/core
到版本:^10.0.27
(以防万一,记住我使用的是Yarn
),所以我能做到:
$ node updater.js
从命令行我可以很容易地实现这一点:
$ yarn upgrade @emotion/core@^10.0.27
但我需要从文件内部执行此操作:updater.js
(这是一项要求)。这是一个最大问题的简化(所以我需要满足这个虚拟用例的要求,即使它没有意义)。
此代码将放入一个自定义包中,该包将负责安装其他一些包。
提前致谢!
最简单的方法是使用 child_process
执行任何脚本:
const { exec } = require('child_process');
const childProcess = exec('yarn upgrade @emotion/core@^10.0.27', (error, stdout, stderr) => {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
查看更多内容
您可以通过以下方式将 stdio 管道化以获得接近实时的输出:
childProcess.stdout.pipe(process.stdout)
但要小心使用 *Sync
(如 execSync
)库,您应该 NOT
尽可能阻止代码。最好使用 callback
或 Promise
也有像shelljs
这样的精彩包来封装这个。