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

feat: generate package lock file #63

Merged
merged 7 commits into from
Feb 1, 2024
Merged

Conversation

akitaSummer
Copy link
Contributor

No description provided.

@@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

独立一个方法来做 ensure 逻辑?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

try {
await fs.stat(lockPath);
} catch {
const arb = new Arborist({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可能还需要额外传一下 workspaces 相关参数。
直接用子进程调 npm 客户端来生成?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修改了默认值逻辑,会有一个不兼容变更 🤔,需要发 0.4 了

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,这是个大版本改动

@akitaSummer akitaSummer force-pushed the feat/generta_package_lock branch 8 times, most recently from b81b97a to c7df817 Compare December 12, 2023 14:43
@@ -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', {
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@akitaSummer akitaSummer force-pushed the feat/generta_package_lock branch 4 times, most recently from 045ed49 to cd0c7fd Compare December 13, 2023 05:56
@@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里是不是也该成 options.packageLock 比较好,尽量不使用双重否定。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

packageLock字段在下面第26行被占用了

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那换个字段名称吧,避开 no 这种情况。

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在 AsyncFunction 中使用 then/catch 不是一个好主意。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个仅仅是用于判断文件是否存在,如果再套层try catch,会显得有点回掉地狱的感觉

Copy link
Contributor

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 仅捕获一个错误。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

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 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么需要使用 --force? 另外 npm install 时还有大量额外参数是不是应该一起透传才符合直觉?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用force是为了尽量能生成,我们需要传递这么多参数吗,我们只是一个安装器,并不是一个npm cli吧

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}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样 stdio 不如直接 inherit?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用了execSync的inherit

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 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用子进程并不是一个好主意,在开发 cli 时需要很注意子进程的生命周期,如果父进程退出了,子进程未退出会造成 shell 的异常。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成了execSync是不是就不需要注意这个了呢

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

需要去封装一下 cp.exec 方法,注意退出。

Copy link
Contributor Author

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 () => {
Copy link
Contributor

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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

怎么会有这个环境变量?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已删

@elrrrrrrr elrrrrrrr merged commit 29e0f82 into master Feb 1, 2024
14 checks passed
@elrrrrrrr elrrrrrrr deleted the feat/generta_package_lock branch February 1, 2024 09:41
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

Successfully merging this pull request may close these issues.

3 participants