-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: host user specific configuration externally
- Loading branch information
Showing
14 changed files
with
124 additions
and
96 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
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 |
---|---|---|
@@ -1,6 +1,8 @@ | ||
"use strict"; | ||
|
||
const { resolve } = require("path") | ||
, setupPackage = require("./lib/setup-package"); | ||
const ensureString = require("es5-ext/object/validate-stringifiable-value") | ||
, ensureConfiguration = require("./lib/ensure-configuration") | ||
, installPackage = require("./lib/install-package"); | ||
|
||
module.exports = (packagesPath, packageName) => setupPackage(resolve(packagesPath), packageName); | ||
module.exports = (packageName, configuration) => | ||
installPackage(ensureString(packageName), ensureConfiguration(configuration)); |
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,3 @@ | ||
"use strict"; | ||
|
||
module.exports = class DevPackageInstallError extends Error {}; |
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,37 @@ | ||
"use strict"; | ||
|
||
const ensureObject = require("es5-ext/object/valid-object") | ||
, ensurePlainFunction = require("es5-ext/object/ensure-plain-function") | ||
, ensureString = require("es5-ext/object/validate-stringifiable-value") | ||
, { resolve } = require("path") | ||
, { homedir } = require("os") | ||
, log = require("log4").get("dev-package-install:configuration").info; | ||
|
||
module.exports = userConfiguration => { | ||
log.debug("user configuration: %O", userConfiguration); | ||
|
||
const configuration = {}; | ||
|
||
configuration.packagesPath = userConfiguration.packagesPath | ||
? resolve(ensureString(userConfiguration.packagesPath)) | ||
: resolve(homedir(), "npm-packages"); | ||
log("packages path: %s", configuration.packagesPath); | ||
|
||
ensureObject(userConfiguration.packagesMeta); | ||
const packagesMeta = (configuration.packagesMeta = Object.create(null)); | ||
for (const [packageName, userPackageMeta] of Object.entries(userConfiguration.packagesMeta)) { | ||
ensureObject(userPackageMeta); | ||
const packageMeta = (packagesMeta[packageName] = {}); | ||
packageMeta.repoUrl = ensureString(userPackageMeta.repoUrl); | ||
log.get("package-meta")("recognize %s at %s", packageName, packageMeta.repoUrl); | ||
} | ||
|
||
const hooks = (configuration.hooks = {}); | ||
if (!userConfiguration.hooks) return configuration; | ||
const userHooks = ensureObject(userConfiguration.hooks); | ||
if (userHooks.afterPackageInstall) { | ||
hooks.afterPackageInstall = ensurePlainFunction(userHooks.afterPackageInstall); | ||
log.get("hooks")("recognize %s", "afterPackageInstall"); | ||
} | ||
return configuration; | ||
}; |
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 |
---|---|---|
|
@@ -2,25 +2,21 @@ | |
|
||
const { resolve } = require("path") | ||
, rmdir = require("fs2/rmdir") | ||
, logger = require("log4").get("setup-own-package") | ||
, isOwnPackage = require("./is-own-package") | ||
, logger = require("log4").get("dev-package-install") | ||
, isPackageLinked = require("./is-package-linked") | ||
, runProgram = require("./run-program") | ||
, setupPrettier = require("./setup-prettier") | ||
, setupRepository = require("./setup-repository") | ||
, setupNpmLink = require("./setup-npm-link"); | ||
|
||
const exceptionsMap = new Map([["webmake", "modules-webmake"], ["next", "node-ext"]]); | ||
|
||
const ongoingMap = new Map(); | ||
const done = new Set(); | ||
|
||
const setupDependencies = async (packagesPath, packageName) => { | ||
const setupDependencies = async (packageName, { packagesPath, packagesMeta, hooks }) => { | ||
const packagePath = resolve(packagesPath, packageName); | ||
const packageMeta = require(resolve(packagePath, "package.json")); | ||
const packagePkgJson = require(resolve(packagePath, "package.json")); | ||
const dependencies = new Set( | ||
Object.keys(packageMeta.dependencies || {}).concat( | ||
Object.keys(packageMeta.devDependencies || {}) | ||
Object.keys(packagePkgJson.dependencies || {}).concat( | ||
Object.keys(packagePkgJson.devDependencies || {}) | ||
) | ||
); | ||
dependencies.delete(packageName); | ||
|
@@ -33,15 +29,15 @@ const setupDependencies = async (packagesPath, packageName) => { | |
.get(dependencyName) | ||
.push(() => setupNpmLink(packagePath, dependencyName)); | ||
continue; | ||
} else if (exceptionsMap.has(dependencyName) || await isOwnPackage(dependencyName)) { | ||
await module.exports(packagesPath, dependencyName); | ||
} else if (packagesMeta[dependencyName]) { | ||
await module.exports(dependencyName, { packagesPath, packagesMeta, hooks }); | ||
} | ||
} | ||
await setupNpmLink(packagePath, dependencyName); | ||
} | ||
|
||
// Eventual optional dependencies | ||
for (const dependencyName of Object.keys(packageMeta.optionalDependencies || {})) { | ||
for (const dependencyName of Object.keys(packagePkgJson.optionalDependencies || {})) { | ||
if (dependencyName === packageName) continue; | ||
if (dependencies.has(dependencyName)) continue; | ||
try { await setupNpmLink(packagePath, dependencyName); } | ||
|
@@ -54,17 +50,15 @@ const setupDependencies = async (packagesPath, packageName) => { | |
} | ||
}; | ||
|
||
module.exports = async (packagesPath, packageName) => { | ||
module.exports = async (packageName, { packagesPath, packagesMeta, hooks }) => { | ||
const packagePath = resolve(packagesPath, packageName); | ||
|
||
logger.notice("setup package %s", packageName); | ||
const pendingJobs = []; | ||
ongoingMap.set(packageName, pendingJobs); | ||
|
||
// Setup repository | ||
await setupRepository( | ||
packagePath, `[email protected]:medikoo/${ exceptionsMap.get(packageName) || packageName }.git` | ||
); | ||
await setupRepository(packagePath, packagesMeta[packageName].repoUrl); | ||
|
||
// Cleanup eventual npm crashes | ||
await rmdir(resolve(packagePath, "node_modules/.staging"), { | ||
|
@@ -74,19 +68,24 @@ module.exports = async (packagesPath, packageName) => { | |
}); | ||
|
||
// Setup dependencies | ||
await setupDependencies(packagesPath, packageName); | ||
await setupDependencies(packageName, { packagesPath, packagesMeta, hooks }); | ||
|
||
// Link package | ||
if (!await isPackageLinked(packageName)) { | ||
if (!(await isPackageLinked(packageName))) { | ||
logger.notice("link %s", packageName); | ||
await runProgram("npm", ["link"], { | ||
cwd: packagePath, | ||
logger: logger.levelRoot.get("npm:link") | ||
}); | ||
} | ||
|
||
// Setup prettier link | ||
await setupPrettier(packagesPath, packagePath); | ||
// Run eventual afterPackageInstall hooks | ||
if (hooks.afterPackageInstall) { | ||
await hooks.afterPackageInstall(packageName, { | ||
packagesPath, | ||
packageMeta: packagesMeta[packageName] | ||
}); | ||
} | ||
|
||
// Done | ||
logger.notice("done %s", packageName); | ||
|
This file was deleted.
Oops, something went wrong.
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,24 @@ | ||
"use strict"; | ||
|
||
const { resolve } = require("path") | ||
, { homedir } = require("os") | ||
, isModuleNotFoundError = require("cjs-module/is-module-not-found-error") | ||
, DevPackageInstallError = require("./dev-package-install-error") | ||
, ensureConfiguration = require("./ensure-configuration"); | ||
|
||
const configurationPath = resolve(homedir(), ".dev-package-install"); | ||
|
||
const resolveConfigurationModule = () => { | ||
try { | ||
return require(configurationPath); | ||
} catch (error) { | ||
if (isModuleNotFoundError(error, configurationPath)) { | ||
throw new DevPackageInstallError( | ||
"Configuration not provided at ~/.dev-package-install" | ||
); | ||
} | ||
throw error; | ||
} | ||
}; | ||
|
||
module.exports = async () => ensureConfiguration(await resolveConfigurationModule()); |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
"use strict"; | ||
|
||
const logger = require("log4").get("setup-own-package") | ||
const logger = require("log4").get("dev-package-install") | ||
, rm = require("fs2/rm") | ||
, symlink = require("fs2/symlink"); | ||
|
||
module.exports = async (target, path) => { | ||
await rm(path); | ||
await rm(path, { loose: true }); | ||
logger.info("create symlink of %s at %s", target, path); | ||
await symlink(target, path, { type: "dir", intermediate: true }); | ||
}; |
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