Skip to content

Conversation

@kinland
Copy link

@kinland kinland commented Oct 19, 2023

This PR allows you to write a custom hash. This is primarily a workaround for #16

With these changes, I can now do something like

/** @type {import('package-changed/types')} */
const { isPackageChanged } = require('package-changed');

/** @type {import('child_process')} */
const { execSync } = require('node:child_process');

(async () => {
    const { oldHash, hash: rootHash, writeHash } = await isPackageChanged({ noHashFile: true });
    const { hash: nextjsHash } = await isPackageChanged({ noHashFile: true, cwd: './nextjs' });

    const newHash = `${rootHash}${nextjsHash}`;

    if (newHash !== oldHash) {
        console.log("Package changed. Running 'rm -f .eslintcache && pnpm install'.");
        // dependencies in a package.json have changed since last run
        execSync('rm -f .eslintcache');
        execSync('pnpm install');

        writeHash(newHash);
    }
})();

@thdk
Copy link
Owner

thdk commented Oct 22, 2023

Hi, thanks for your effort. I like your approach as it keeps the library simple without requiring too much knowledge of package managers logic.

However, I think we can add a helper function to the library writeHash(hash: string): void which could also be used internally when calling the writeHash(): void function returned from isPackageChanged.

So your example usage would become:

/** @type {import('package-changed/types')} */
const { isPackageChanged, writeHash } = require('package-changed');

/** @type {import('child_process')} */
const { execSync } = require('node:child_process');

(async () => {
    const { oldHash, hash: rootHash } = await isPackageChanged({ noHashFile: true });
    const { hash: nextjsHash } = await isPackageChanged({ noHashFile: true, cwd: './nextjs' });

    const newHash = `${rootHash}${nextjsHash}`;

    if (newHash !== oldHash) {
        console.log("Package changed. Running 'rm -f .eslintcache && pnpm install'.");
        // dependencies in a package.json have changed since last run
        execSync('rm -f .eslintcache');
        execSync('pnpm install');

        writeHash(newHash);
    }
})();

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.

2 participants