-
Notifications
You must be signed in to change notification settings - Fork 4
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
feat: generate package lock file #63
Conversation
8a66d8f
to
9c0eb08
Compare
f651c7d
to
74c2750
Compare
packages/cli/lib/util.js
Outdated
@@ -600,9 +601,25 @@ exports.readPkgJSON = async function readPkgJSON(cwd) { | |||
return { pkg, pkgPath }; | |||
}; | |||
|
|||
exports.readPackageLock = async function readPackageLock(cwd) { | |||
exports.readPackageLock = async function readPackageLock(cwd, noPackageLock) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
独立一个方法来做 ensure 逻辑?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
packages/cli/lib/util.js
Outdated
try { | ||
await fs.stat(lockPath); | ||
} catch { | ||
const arb = new Arborist({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可能还需要额外传一下 workspaces 相关参数。
直接用子进程调 npm 客户端来生成?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
packages/cli/lib/index.js
Outdated
@@ -18,7 +18,7 @@ const { MirrorConfig } = require('binary-mirror-config'); | |||
// 有依赖树(package-lock.json)走 npm / npminstall 极速安装 | |||
exports.install = async options => { | |||
options.env = util.getEnv(options.env, options.args); | |||
const { packageLock } = options.packageLock || (await util.readPackageLock(options.cwd)); | |||
const { packageLock } = options.packageLock || (await util.readPackageLock(options.cwd, options.noPackageLock)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修改了默认值逻辑,会有一个不兼容变更 🤔,需要发 0.4 了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是的,这是个大版本改动
b81b97a
to
c7df817
Compare
c7df817
to
86f621f
Compare
packages/cli/bin/rapid.js
Outdated
@@ -32,12 +32,17 @@ const argv = yargs | |||
describe: 'Dependency types to omit from the installation tree on disk', | |||
type: 'array', | |||
default: [], | |||
}) | |||
.option('no-package-lock', { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这是 npm 的标准参数吗?
另外一般行为不会加 no 前缀,这是命令行解析工具自己会做的。 --no-package-lock 等价于 --package-lock=false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
045ed49
to
cd0c7fd
Compare
cd0c7fd
to
b1b1e72
Compare
@@ -18,6 +18,11 @@ const { MirrorConfig } = require('binary-mirror-config'); | |||
// 有依赖树(package-lock.json)走 npm / npminstall 极速安装 | |||
exports.install = async options => { | |||
options.env = util.getEnv(options.env, options.args); | |||
|
|||
if (!options.noPackageLock) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里是不是也该成 options.packageLock 比较好,尽量不使用双重否定。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
packageLock字段在下面第26行被占用了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
那换个字段名称吧,避开 no 这种情况。
packages/cli/lib/util.js
Outdated
exports.generatePackageLock = async cwd => { | ||
try { | ||
const lockPath = path.join(cwd || exports.findLocalPrefix(), './package-lock.json'); | ||
const isExist = await fs.stat(lockPath).then(() => true).catch(() => false); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
在 AsyncFunction 中使用 then/catch 不是一个好主意。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这个仅仅是用于判断文件是否存在,如果再套层try catch,会显得有点回掉地狱的感觉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
try/catch 不嵌套是正确的。可以用过把 try/catch 拆小来解决这个问题。
let isExist = true;
try {
await fs.stat(lockPath);
} catch {
isExist = false;
}
... generate package lock
最好的实践是 try/catch 仅捕获一个错误。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
packages/cli/lib/util.js
Outdated
const isExist = await fs.stat(lockPath).then(() => true).catch(() => false); | ||
if (!isExist) { | ||
console.log('npm install --force --package-lock-only --ignore-scripts is running'); | ||
const { stdout, stderr } = await exec('npm install --force --package-lock-only --ignore-scripts', { cwd }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么需要使用 --force
? 另外 npm install 时还有大量额外参数是不是应该一起透传才符合直觉?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用force是为了尽量能生成,我们需要传递这么多参数吗,我们只是一个安装器,并不是一个npm cli吧
packages/cli/lib/util.js
Outdated
if (!isExist) { | ||
console.log('npm install --force --package-lock-only --ignore-scripts is running'); | ||
const { stdout, stderr } = await exec('npm install --force --package-lock-only --ignore-scripts', { cwd }); | ||
console.log(`${stdout}`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这样 stdio 不如直接 inherit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用了execSync的inherit
packages/cli/lib/util.js
Outdated
const isExist = await fs.stat(lockPath).then(() => true).catch(() => false); | ||
if (!isExist) { | ||
console.log('npm install --force --package-lock-only --ignore-scripts is running'); | ||
const { stdout, stderr } = await exec('npm install --force --package-lock-only --ignore-scripts', { cwd }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用子进程并不是一个好主意,在开发 cli 时需要很注意子进程的生命周期,如果父进程退出了,子进程未退出会造成 shell 的异常。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
改成了execSync是不是就不需要注意这个了呢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
需要去封装一下 cp.exec 方法,注意退出。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
mm.restore(); | ||
}); | ||
|
||
it('should run all project installation scripts', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不要只写集成测试。这样各种边界 case 是未测试的。
describe('not exist lock file', async () => { | ||
let fixture; | ||
beforeEach(async () => { | ||
mm(process.env, NYDUS_CSI_ROOT_ENV, 'true'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
怎么会有这个环境变量?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
已删
No description provided.