-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #237 from hypermod-io/fetcher-pkgmanager
refactors the experimental module-loader
- Loading branch information
Showing
7 changed files
with
134 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
'@hypermod/fetcher': minor | ||
'@hypermod/cli': minor | ||
--- | ||
|
||
Refactors the module loader in order to support custom npm registries + auth keys. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import path from 'path'; | ||
import fs from 'fs-extra'; | ||
import { installPackage } from '@antfu/install-pkg'; | ||
|
||
import { ModuleLoader } from '@hypermod/fetcher'; | ||
|
||
const ModuleLoader = (config: { | ||
npmRegistryUrl?: string; | ||
authToken?: string; | ||
}): ModuleLoader => { | ||
const getInfo = (packageName: string) => { | ||
const entryPath = require.resolve(packageName); | ||
const location = entryPath.split(packageName)[0] + packageName; | ||
const pkgJsonRaw = fs.readFileSync( | ||
path.join(location, 'package.json'), | ||
'utf8', | ||
); | ||
const pkgJson = JSON.parse(pkgJsonRaw); | ||
|
||
return { | ||
location, | ||
entryPath, | ||
pkgJson, | ||
}; | ||
}; | ||
|
||
const install = async (packageName: string) => { | ||
await installPackage(packageName, { | ||
cwd: __dirname, | ||
packageManager: 'npm', | ||
additionalArgs: [ | ||
'--force', | ||
// --registry=https://your-custom-registry-url/ --//your-custom-registry-url/:_authToken=YOUR_AUTH_TOKEN | ||
config.npmRegistryUrl ? `--registry=${config.npmRegistryUrl}` : '', | ||
config.authToken | ||
? `--${config.npmRegistryUrl}/:_authToken=${config.authToken}` | ||
: '', | ||
], | ||
}); | ||
|
||
const { pkgJson } = getInfo(packageName); | ||
|
||
// Install whitelisted devDependencies | ||
if (pkgJson?.hypermod?.dependencies) { | ||
await Promise.all( | ||
pkgJson.hypermod.dependencies.map((dep: string) => { | ||
const version = pkgJson.devDependencies[dep]; | ||
if (!version) return; | ||
return installPackage(`${dep}@${version}`, { | ||
cwd: __dirname, | ||
packageManager: 'npm', | ||
additionalArgs: ['--force'], | ||
}); | ||
}), | ||
); | ||
} | ||
}; | ||
|
||
return { | ||
install, | ||
getInfo, | ||
require: (packageName: string) => require(packageName), | ||
}; | ||
}; | ||
|
||
export default ModuleLoader; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.