Skip to content

Commit

Permalink
feat(@angular/cli): update cli config with workspace support
Browse files Browse the repository at this point in the history
  • Loading branch information
clydin authored and hansl committed Mar 28, 2018
1 parent ec0d910 commit 64ebf9c
Show file tree
Hide file tree
Showing 14 changed files with 289 additions and 653 deletions.
7 changes: 3 additions & 4 deletions packages/@angular/cli/bin/ng
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Due to an obscure Mac bug, do not start this title with any symbol.
process.title = 'ng';

const CliConfig = require('../models/config').CliConfig;
const isWarningEnabled = require('../utilities/config').isWarningEnabled;
const Version = require('../upgrade/version').Version;

const fs = require('fs');
Expand Down Expand Up @@ -68,8 +68,7 @@ if (process.env['NG_CLI_PROFILING']) {

// Show the warnings/errors due to package and version deprecation.
const version = new SemVer(process.version);
if (version.compare(new SemVer('8.9.0')) < 0
&& CliConfig.fromGlobal().get('warnings.nodeDeprecation')) {
if (version.compare(new SemVer('8.9.0')) < 0 && isWarningEnabled('nodeDeprecation')) {
process.stderr.write(yellow(stripIndents`
You are running version ${version.version} of Node, which is not supported by Angular CLI v6.
The official Node version that is supported is 8.9 and greater.
Expand Down Expand Up @@ -104,7 +103,7 @@ resolve('@angular/cli', { basedir: process.cwd() },
shouldWarn = true;
}

if (shouldWarn && CliConfig.fromGlobal().get('warnings.versionMismatch')) {
if (shouldWarn && isWarningEnabled('versionMismatch')) {
let warning = yellow(stripIndents`
Your global Angular CLI version (${globalVersion}) is greater than your local
version (${localVersion}). The local Angular CLI version is used.
Expand Down
4 changes: 2 additions & 2 deletions packages/@angular/cli/commands/add.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import chalk from 'chalk';
import { CommandScope, Option } from '../models/command';
import { parseOptions } from '../models/command-runner';
import { CliConfig } from '../models/config';
import { getPackageManager } from '../utilities/config';
import { SchematicCommand } from '../models/schematic-command';
import { NpmInstall } from '../tasks/npm-install';

Expand Down Expand Up @@ -49,7 +49,7 @@ export default class AddCommand extends SchematicCommand {
);
}

const packageManager = CliConfig.fromGlobal().get('packageManager');
const packageManager = getPackageManager();

const npmInstall: NpmInstall = require('../tasks/npm-install').default;

Expand Down
228 changes: 138 additions & 90 deletions packages/@angular/cli/commands/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { Command } from '../models/command';
import * as fs from 'fs';
import { CliConfig } from '../models/config';
import { oneLine } from 'common-tags';
import { writeFileSync } from 'fs';
import { Command, Option } from '../models/command';
import { getWorkspace, getWorkspaceRaw, validateWorkspace } from '../utilities/config';
import {
JsonValue,
JsonArray,
JsonObject,
JsonParseMode,
experimental,
parseJson,
} from '@angular-devkit/core';
import { WorkspaceJson } from '@angular-devkit/core/src/workspace';

const SilentError = require('silent-error');

Expand All @@ -12,41 +20,133 @@ export interface ConfigOptions {
global?: boolean;
}

/**
* Splits a JSON path string into fragments. Fragments can be used to get the value referenced
* by the path. For example, a path of "a[3].foo.bar[2]" would give you a fragment array of
* ["a", 3, "foo", "bar", 2].
* @param path The JSON string to parse.
* @returns {string[]} The fragments for the string.
* @private
*/
function parseJsonPath(path: string): string[] {
const fragments = (path || '').split(/\./g);
const result: string[] = [];

while (fragments.length > 0) {
const fragment = fragments.shift();

const match = fragment.match(/([^\[]+)((\[.*\])*)/);
if (!match) {
throw new Error('Invalid JSON path.');
}

result.push(match[1]);
if (match[2]) {
const indices = match[2].slice(1, -1).split('][');
result.push(...indices);
}
}

return result.filter(fragment => !!fragment);
}

function getValueFromPath<T extends JsonArray | JsonObject>(
root: T,
path: string,
): JsonValue | undefined {
const fragments = parseJsonPath(path);

try {
return fragments.reduce((value: JsonValue, current: string | number) => {
if (value == undefined || typeof value != 'object') {
return undefined;
} else if (typeof current == 'string' && !Array.isArray(value)) {
return value[current];
} else if (typeof current == 'number' && Array.isArray(value)) {
return value[current];
} else {
return undefined;
}
}, root);
} catch {
return undefined;
}
}

function setValueFromPath<T extends JsonArray | JsonObject>(
root: T,
path: string,
newValue: JsonValue,
): JsonValue | undefined {
const fragments = parseJsonPath(path);

try {
return fragments.reduce((value: JsonValue, current: string | number, index: number) => {
if (value == undefined || typeof value != 'object') {
return undefined;
} else if (typeof current == 'string' && !Array.isArray(value)) {
if (index === fragments.length - 1) {
value[current] = newValue;
} else if (value[current] == undefined) {
if (typeof fragments[index + 1] == 'number') {
value[current] = [];
} else if (typeof fragments[index + 1] == 'string') {
value[current] = {};
}
}
return value[current];
} else if (typeof current == 'number' && Array.isArray(value)) {
if (index === fragments.length - 1) {
value[current] = newValue;
} else if (value[current] == undefined) {
if (typeof fragments[index + 1] == 'number') {
value[current] = [];
} else if (typeof fragments[index + 1] == 'string') {
value[current] = {};
}
}
return value[current];
} else {
return undefined;
}
}, root);
} catch {
return undefined;
}
}

export default class ConfigCommand extends Command {
public readonly name = 'config';
public readonly description = 'Get/set configuration values.';
public readonly arguments = ['jsonPath', 'value'];
public readonly options = [
{
name: 'global',
type: Boolean,
'default': false,
aliases: ['g'],
description: 'Get/set the value in the global configuration (in your home directory).'
}
public readonly options: Option[] = [
// {
// name: 'global',
// type: Boolean,
// 'default': false,
// aliases: ['g'],
// description: 'Get/set the value in the global configuration (in your home directory).'
// }
];

public run(options: ConfigOptions) {
const config = options.global ? CliConfig.fromGlobal() : CliConfig.fromProject();
const config = (getWorkspace() as {} as { _workspace: WorkspaceJson});

if (config === null) {
throw new SilentError('No config found. If you want to use global configuration, '
+ 'you need the --global argument.');
if (!config) {
throw new SilentError('No config found.');
}

const action = !!options.value ? 'set' : 'get';

if (action === 'get') {
this.get(config, options);
if (options.value == undefined) {
this.get(config._workspace, options);
} else {
this.set(config, options);
this.set(options);
}
}

private get(config: CliConfig, options: ConfigOptions) {
const value = config.get(options.jsonPath);
private get(config: experimental.workspace.WorkspaceJson, options: ConfigOptions) {
const value = options.jsonPath ? getValueFromPath(config as any, options.jsonPath) : config;

if (value === null || value === undefined) {
if (value === undefined) {
throw new SilentError('Value cannot be found.');
} else if (typeof value == 'object') {
this.logger.info(JSON.stringify(value, null, 2));
Expand All @@ -55,80 +155,28 @@ export default class ConfigCommand extends Command {
}
}

private set(config: CliConfig, options: ConfigOptions) {
const type = config.typeOf(options.jsonPath);
let value: any = options.value;
switch (type) {
case 'boolean': value = this.asBoolean(options.value); break;
case 'number': value = this.asNumber(options.value); break;
case 'string': value = options.value; break;

default: value = this.parseValue(options.value, options.jsonPath);
}

if (options.jsonPath.endsWith('.prefix')) {
// update tslint if prefix is updated
this.updateLintForPrefix(this.project.root + '/tslint.json', value);
}
private set(options: ConfigOptions) {
const [config, configPath] = getWorkspaceRaw();

try {
config.set(options.jsonPath, value);
config.save();
} catch (error) {
throw new SilentError(error.message);
}
}
// TODO: Modify & save without destroying comments
const configValue = config.value;

private asBoolean(raw: string): boolean {
if (raw == 'true' || raw == '1') {
return true;
} else if (raw == 'false' || raw == '' || raw == '0') {
return false;
} else {
throw new SilentError(`Invalid boolean value: "${raw}"`);
}
}
const value = parseJson(options.value, JsonParseMode.Loose);
const result = setValueFromPath(configValue, options.jsonPath, value);

private asNumber(raw: string): number {
const val = Number(raw);
if (Number.isNaN(val)) {
throw new SilentError(`Invalid number value: "${raw}"`);
if (result === undefined) {
throw new SilentError('Value cannot be found.');
}
return val;
}

private parseValue(rawValue: string, path: string) {
try {
return JSON.parse(rawValue);
validateWorkspace(configValue);
} catch (error) {
throw new SilentError(`No node found at path ${path}`);
}
}

private updateLintForPrefix(filePath: string, prefix: string): void {
if (!fs.existsSync(filePath)) {
return;
}

const tsLint = JSON.parse(fs.readFileSync(filePath, 'utf8'));

if (Array.isArray(tsLint.rules['component-selector'][2])) {
tsLint.rules['component-selector'][2].push(prefix);
} else {
tsLint.rules['component-selector'][2] = prefix;
this.logger.error(error.message);
throw new SilentError();
}

if (Array.isArray(tsLint.rules['directive-selector'][2])) {
tsLint.rules['directive-selector'][2].push(prefix);
} else {
tsLint.rules['directive-selector'][2] = prefix;
}

fs.writeFileSync(filePath, JSON.stringify(tsLint, null, 2));

this.logger.warn(oneLine`
tslint configuration updated to match new prefix,
you may need to fix any linting errors.
`);
const output = JSON.stringify(configValue);
writeFileSync(configPath, output);
}

}
4 changes: 2 additions & 2 deletions packages/@angular/cli/commands/generate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CommandScope, Option } from '../models/command';
import chalk from 'chalk';
import { CliConfig } from '../models/config';
import { getDefaultSchematicCollection } from '../utilities/config';
import {
getCollection,
getEngineHost
Expand Down Expand Up @@ -68,7 +68,7 @@ export default class GenerateCommand extends SchematicCommand {
}

private parseSchematicInfo(options: any) {
let collectionName: string = CliConfig.getValue('defaults.schematics.collection');
let collectionName = getDefaultSchematicCollection();

let schematicName = options._[0];

Expand Down
9 changes: 3 additions & 6 deletions packages/@angular/cli/commands/new.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommandScope, Option } from '../models/command';
import { CliConfig } from '../models/config';
import { getDefaultSchematicCollection } from '../utilities/config';
import { SchematicCommand } from '../models/schematic-command';


Expand Down Expand Up @@ -34,7 +34,7 @@ export default class NewCommand extends SchematicCommand {
this.initialized = true;

const collectionName = this.parseCollectionName(options);
const schematicName = CliConfig.fromGlobal().get('defaults.schematics.newApp');
const schematicName = 'application';

return this.getOptions({
schematicName,
Expand Down Expand Up @@ -83,10 +83,7 @@ export default class NewCommand extends SchematicCommand {
}

private parseCollectionName(options: any): string {
let collectionName: string =
options.collection ||
options.c ||
CliConfig.getValue('defaults.schematics.collection');
const collectionName = options.collection || options.c || getDefaultSchematicCollection();

return collectionName;
}
Expand Down

0 comments on commit 64ebf9c

Please sign in to comment.