-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into mvenkov/add-list-angular-template
- Loading branch information
Showing
15 changed files
with
504 additions
and
16 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 |
---|---|---|
@@ -1,26 +1,72 @@ | ||
import * as fs from "fs-extra"; | ||
import * as os from "os"; | ||
import * as path from "path"; | ||
import { Util } from "./Util"; | ||
|
||
export class ProjectConfig { | ||
|
||
public static configFile: string = "ignite-ui-cli.json"; | ||
public static readonly defaults: Config = require("./config/defaults.json"); | ||
|
||
public static getConfig(): Config { | ||
/** Returns true if there's a CLI config file in the current working directory */ | ||
public static hasLocalConfig(): boolean { | ||
const filePath = path.join(process.cwd(), this.configFile); | ||
return fs.existsSync(filePath); | ||
} | ||
|
||
/** | ||
* Get effective CLI configuration (merged defaults, global and local) | ||
* @param global return only global values | ||
*/ | ||
public static getConfig(global: boolean = false): Config { | ||
const filePath = path.join(process.cwd(), this.configFile); | ||
const config = {}; | ||
|
||
Util.merge(config, this.defaults); | ||
Util.merge(config, this.globalConfig()); | ||
|
||
if (!global) { | ||
Util.merge(config, this.localConfig()); | ||
} | ||
return config as Config; | ||
} | ||
|
||
/** | ||
* Write a configuration file (either local or global) with given `Config` object. | ||
* Will create or overwrite. | ||
* @param config Config object to set | ||
* @param global Set global values instead | ||
*/ | ||
public static setConfig(config: Config, global: boolean = false) { | ||
const basePath = global ? os.homedir() : process.cwd(); | ||
const filePath = path.join(basePath, this.configFile); | ||
fs.writeJsonSync(filePath, config, { spaces: 4 }); | ||
} | ||
|
||
/*** Get local configuration only */ | ||
public static localConfig(): Config { | ||
const filePath = path.join(process.cwd(), this.configFile); | ||
let localConfig = {}; | ||
|
||
if (fs.existsSync(filePath)) { | ||
try { | ||
return JSON.parse(fs.readFileSync(filePath, "utf8")) as Config; | ||
localConfig = JSON.parse(fs.readFileSync(filePath, "utf8")); | ||
} catch (error) { | ||
throw new Error(`The ${this.configFile} file is not parsed correctly. ` + | ||
`The following error has occurred: ${error.message}`); | ||
} | ||
} | ||
return null; | ||
return localConfig as Config; | ||
} | ||
public static setConfig(config: Config) { | ||
const filePath = path.join(process.cwd(), this.configFile); | ||
if (fs.existsSync(filePath)) { | ||
fs.writeJsonSync(filePath, config, { spaces: 4 }); | ||
|
||
/*** Get global configuration only */ | ||
public static globalConfig(): Config { | ||
const globalConfigPath = path.join(os.homedir(), this.configFile); | ||
let globalConfig = {}; | ||
|
||
if (fs.existsSync(globalConfigPath)) { | ||
globalConfig = require(globalConfigPath); | ||
} | ||
return globalConfig as Config; | ||
} | ||
} |
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
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,122 @@ | ||
import { Util } from "../Util"; | ||
import { ProjectConfig } from "./../ProjectConfig"; | ||
|
||
const command = { | ||
// tslint:disable:object-literal-sort-keys | ||
command: "config", | ||
desc: "Get or set configuration properties", | ||
builder: yargs => { | ||
yargs.command({ | ||
command: "get <property>", | ||
desc: "Get configuration properties", | ||
builder: { | ||
property: { | ||
describe: "Config property to get", | ||
type: "string" | ||
} | ||
}, | ||
handler: command.getHandler | ||
}).command({ | ||
command: "set <property> <value>", | ||
desc: "Set configuration properties", | ||
builder: { | ||
property: { | ||
describe: "Config property to set", | ||
type: "string" | ||
}, | ||
value: { | ||
describe: "New value for the property", | ||
type: "string" | ||
} | ||
}, | ||
handler: command.setHandler | ||
}).command({ | ||
command: "add <property> <value>", | ||
desc: "Add a value to an existing configuration array", | ||
builder: { | ||
property: { | ||
describe: "Config property to add to", | ||
type: "string" | ||
}, | ||
value: { | ||
describe: "New value to add", | ||
type: "string" | ||
} | ||
}, | ||
handler: command.addHandler | ||
}).option("global", { | ||
alias: "g", | ||
type: "boolean", | ||
global: true, | ||
describe: "Specify if the global configuration should be used" | ||
}) | ||
// at least one command is required | ||
.demand(1, "Please use either get or set command"); | ||
}, | ||
// tslint:enable:object-literal-sort-keys | ||
getHandler(argv) { | ||
if (!argv.global && !ProjectConfig.hasLocalConfig()) { | ||
Util.error("No configuration file found in this folder!", "red"); | ||
return; | ||
} | ||
const config = ProjectConfig.getConfig(argv.global); | ||
if (config[argv.property] !== undefined) { | ||
Util.log(config[argv.property]); | ||
} else { | ||
Util.error(`No value found for "${argv.property}" property`, "red"); | ||
} | ||
}, | ||
setHandler(argv) { | ||
let config; | ||
|
||
if (argv.global) { | ||
config = ProjectConfig.globalConfig(); | ||
} else { | ||
if (!ProjectConfig.hasLocalConfig()) { | ||
Util.error("No configuration file found in this folder!", "red"); | ||
return; | ||
} | ||
config = ProjectConfig.localConfig(); | ||
} | ||
|
||
if (config[argv.property]) { | ||
// TODO: Schema/property validation? | ||
} | ||
|
||
config[argv.property] = argv.value; | ||
ProjectConfig.setConfig(config, argv.global); | ||
Util.log(`Property "${argv.property}" set.`); | ||
}, | ||
addHandler(argv) { | ||
let config; | ||
|
||
if (argv.global) { | ||
config = ProjectConfig.globalConfig(); | ||
} else { | ||
if (!ProjectConfig.hasLocalConfig()) { | ||
Util.error("No configuration file found in this folder!", "red"); | ||
return; | ||
} | ||
config = ProjectConfig.localConfig(); | ||
} | ||
|
||
// TODO: Schema/property validation? | ||
if (!config[argv.property]) { | ||
config[argv.property] = []; | ||
} else if (!Array.isArray(config[argv.property])) { | ||
Util.error(`Configuration property "${argv.property}" is not an array, use config set instead.`, "red"); | ||
return; | ||
} | ||
|
||
if (config[argv.property].indexOf(argv.value) !== -1) { | ||
Util.log(`Value already exists in "${argv.property}".`); | ||
return; | ||
} | ||
|
||
config[argv.property].push(argv.value); | ||
ProjectConfig.setConfig(config, argv.global); | ||
Util.log(`Property "${argv.property}" updated.`); | ||
} | ||
}; | ||
|
||
export default command; |
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,4 @@ | ||
{ | ||
"igPackageRegistry": "https://packages.infragistics.com/npm/js-licensed/", | ||
"customTemplates": [] | ||
} |
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.