Skip to content

Commit

Permalink
Intorduce configuration from #575
Browse files Browse the repository at this point in the history
  • Loading branch information
d3xter666 committed Apr 12, 2023
1 parent 0ff1b31 commit 0a6e676
Showing 1 changed file with 251 additions and 0 deletions.
251 changes: 251 additions & 0 deletions lib/config/Configuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
import path from "node:path";
import os from "node:os";

/**
*
*
* @public
* @class
* @alias @ui5/project/config/Configuration
*/
class Configuration {
#cwd;
#source;
#version;
#cacheMode;
#ui5HomeDir;
#snapshotEndpointUrl;
#frameworkDir;
#buildCacheDir;
#artifactsDir;
#packagesDir;
#metadataDir;
#stagingDir;
#lockDir;
#cacheDir;

/**
* @param {object} configuration
* @param {string} [configuration.cwd=""]
* @param {string} [configuration.version=""]
* @param {string} [configuration.sources=""]
* @param {string} [configuration.ui5HomeDir="~/.ui5/"]
* @param {string} [configuration.snapshotEndpointUrl=""]
* @param {string} [configuration.cacheMode="default"]
* @param {string} [configuration.frameworkDir="~/${ui5HomeDir}/framework"]
* Directory to store framework artifacts and metadata in.
* @param {string} [configuration.cacheDir="~/${ui5HomeDir}/cacache"]
* @param {string} [configuration.buildCacheDir="~/${ui5HomeDir}/build-cache"]
* Directory to store build-cache in.
* @param {string} [configuration.artifactsDir="~/${ui5HomeDir}/artifacts"]
* @param {string} [configuration.packagesDir="~/${ui5HomeDir}/packages"]
* @param {string} [configuration.metadataDir="~/${ui5HomeDir}/metadata"]
* @param {string} [configuration.stagingDir="~/${ui5HomeDir}/staging"]
* @param {string} [configuration.lockDir="~/${ui5HomeDir}/locks"]
*/
constructor({
cwd,
version,
sources,
ui5HomeDir,
snapshotEndpointUrl,
cacheMode = "default",
frameworkDir,
cacheDir,
buildCacheDir,
artifactsDir,
packagesDir,
metadataDir,
stagingDir,
lockDir
}) {
this.cwd = cwd;
this.sources = sources;
this.version = version;
this.cacheMode = cacheMode;
this.ui5HomeDir = ui5HomeDir ? path.resolve(ui5HomeDir) : path.join(os.homedir(), ".ui5");

this.snapshotEndpointUrl = process.env.UI5_MAVEN_SNAPSHOT_ENDPOINT || snapshotEndpointUrl;
this.frameworkDir = frameworkDir ? path.resolve(frameworkDir) : path.join(this.ui5HomeDir, "framework");
this.buildCacheDir = buildCacheDir ? path.resolve(buildCacheDir) : path.join(this.ui5HomeDir, "build-cache");
this.artifactsDir = artifactsDir ?
path.resolve(artifactsDir) : path.join(this.ui5HomeDir, "framework", "artifacts");
this.packagesDir = packagesDir ?
path.resolve(packagesDir) : path.join(this.ui5HomeDir, "framework", "packages");
this.metadataDir = metadataDir ?
path.resolve(metadataDir) : path.join(this.ui5HomeDir, "framework", "metadata");
this.stagingDir = stagingDir ? path.resolve(stagingDir) : path.join(this.ui5HomeDir, "framework", "staging");
this.lockDir = lockDir ? path.resolve(lockDir) : path.join(this.ui5HomeDir, "framework", "locks");
this.cacheDir = cacheDir ? path.resolve(cacheDir) : path.join(this.ui5HomeDir, "framework", "cacache");
}

/**
* CWD path
*
* @public
* @returns {string}
*/
getCwd() {
return this.cwd;
}

/**
* CWD path
*
* @public
* @returns {string}
*/
getSources() {
return this.sources;
}

/**
* UI5 Version
*
* @public
* @returns {string}
*/
getVersion() {
return this.version;
}

/**
* Cache mode
*
* @public
* @returns {string}
*/
getCacheMode() {
return this.cacheMode;
}

/**
* .ui5 home direcotry
*
* @public
* @returns {string}
*/
getUi5HomeDir() {
return this.ui5HomeDir;
}

/**
* SNAPSHOTs URL
*
* @public
* @returns {string}
*/
getSnapshotEndpointUrl() {
return this.snapshotEndpointUrl;
}

/**
* Directory to store framework artifacts and metadata in.
*
* @public
* @returns {string}
*/
getFrameworkDir() {
return this.frameworkDir;
}

/**
* Directory to store build-cache in.
*
* @public
* @returns {string}
*/
getBuildCacheDir() {
return this.buildCacheDir;
}

/**
* Directory to store artifacts in.
*
* @public
* @returns {string}
*/
getArtifactsDir() {
return this.artifactsDir;
}

/**
* Directory to packages in
*
* @public
* @returns {string}
*/
getPackagesDir() {
return this.packagesDir;
}

/**
* Directory to store metadata in
*
* @public
* @returns {string}
*/
getMetadataDir() {
return this.metadataDir;
}

/**
* Directory to store staging artifacts in
*
* @public
* @returns {string}
*/
getStagingDir() {
return this.stagingDir;
}

/**
* Lockfiles directory
*
* @public
* @returns {string}
*/
getLockDir() {
return this.lockDir;
}

/**
* Cache directory
*
* @public
* @returns {string}
*/
getCacheDir() {
return this.cacheDir;
}
}

export default Configuration;

/**
* Create Configuration from JSON file
*
* @public
* @static
* @param {string} [filePath="~/.ui5rc"] Path to configuration JSON file
* @returns {@ui5/project/config/Configuration} Configuration instance
*/
export async function fromFile(filePath) {
filePath = filePath || path.join(os.homedir(), ".ui5rc");

const {default: fs} = await import("graceful-fs");
const {promisify} = await import("node:util");
const readFile = promisify(fs.readFile);
let config;
try {
const fileContent = await readFile(filePath);
config = JSON.parse(fileContent);
} catch (err) {
if (err.code === "ENOENT") { // "File or directory does not exist"
config = {};
} else {
throw err;
}
}
return new Configuration(config);
}

0 comments on commit 0a6e676

Please sign in to comment.