Skip to content

Deprecate checkAndInstallPackage and fetchPackage #1247

@ringods

Description

@ringods

I'm hearing from the core maintainers that everything in Patternlab should be(come) more explicit. In the uikit handling (#1225, #1246), I'm making the package name and explicit requirement and I'm loading resources via de NodeJS resolver.

NPM, Yarn v1, Yarn v2 and pnpm don't all have the same "package" lookup strategy. Yarn v2 with PnP even wants to get rid of the complete node_modules folder(s). The reason to work via the NodeJS resolver is that it doesn't matter anymore which package manager our users are using.

As part of the above work, I also bumped into an issue with starterkits. The CLI has the option to install a starterkit based on the package name. But if it isn't found, there is a check to detect which package manager is used and a subprocess will install the required package. But installing the package in a subprocess will, given the different package managers, not always make the package available in the parent process.

So, a starterkit should be a dependency of the project at hand if the lookup happens via the resolver. That is why I want to get rid of the functions checkAndInstallPackage and fetchPackage:

/**
* @func fetchPackage
* @desc Fetches and saves packages from npm into node_modules and adds a reference in the package.json under dependencies
* @param {string} packageName - The package name
*/
const fetchPackage = packageName =>
wrapAsync(function*() {
const useYarn = hasYarn();
const pm = useYarn ? 'yarn' : 'npm';
const installCmd = useYarn ? 'add' : 'install';
try {
if (packageName) {
const cmd = yield spawn(pm, [installCmd, packageName]);
error(cmd.stderr);
}
} catch (err) {
error(
`fetchPackage: Fetching required dependencies from ${pm} failed for ${packageName} with ${err}`
);
throw err; // Rethrow error
}
});
/**
* @func checkAndInstallPackage
* Checks whether a package for a given packageName is installed locally. If package cannot be found, fetch and install it
* @param {string} packageName - The package name
* @return {boolean}
*/
const checkAndInstallPackage = packageName =>
wrapAsync(function*() {
try {
require.resolve(packageName);
return true;
} catch (err) {
debug(
`checkAndInstallPackage: ${packageName} not installed. Fetching it now …`
);
yield fetchPackage(packageName);
return false;
}
});

@sghoweri @JosefBredereck

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions