Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): [STENCIL-33] Writing and Reading the Ionic config file #2963

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/cli/ionic-config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { getCompilerSystem } from './state/stencil-cli-config';
import { readJson, uuidv4 } from './telemetry/helpers';

export const DEFAULT_CONFIG = (file: boolean = false) => {
return getCompilerSystem().resolvePath(`${getCompilerSystem().homeDir()}/.ionic${file ? '/config.json' : ''}`);
};
export const default_config = () =>
rwaskiewicz marked this conversation as resolved.
Show resolved Hide resolved
getCompilerSystem().resolvePath(`${getCompilerSystem().homeDir()}/.ionic/config.json`);

export const default_config_directory = () =>
getCompilerSystem().resolvePath(`${getCompilerSystem().homeDir()}/.ionic`);

export interface TelemetryConfig {
'telemetry.stencil'?: boolean;
'tokens.telemetry'?: string;
}

export async function readConfig(): Promise<TelemetryConfig> {
let config: TelemetryConfig = await readJson(DEFAULT_CONFIG(true));
let config: TelemetryConfig = await readJson(default_config());

if (!config) {
config = {
Expand All @@ -27,10 +29,10 @@ export async function readConfig(): Promise<TelemetryConfig> {

export async function writeConfig(config: TelemetryConfig): Promise<void> {
try {
await getCompilerSystem().createDir(DEFAULT_CONFIG(), { recursive: true });
await getCompilerSystem().writeFile(DEFAULT_CONFIG(true), JSON.stringify(config));
await getCompilerSystem().createDir(default_config_directory(), { recursive: true });
await getCompilerSystem().writeFile(default_config(), JSON.stringify(config));
} catch (error) {
console.error(`Stencil Telemetry: couldn't write configuration file to ${DEFAULT_CONFIG(true)} - ${error}.`);
console.error(`Stencil Telemetry: couldn't write configuration file to ${default_config()} - ${error}.`);
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/cli/test/ionic-config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mockLogger } from '@stencil/core/testing';
import { getCompilerSystem, initializeStencilCLIConfig } from '../state/stencil-cli-config';
import { readConfig, writeConfig, updateConfig, DEFAULT_CONFIG } from '../ionic-config';
import { readConfig, writeConfig, updateConfig, default_config } from '../ionic-config';
import { createSystem } from '../../compiler/sys/stencil-sys';

describe('readConfig', () => {
Expand All @@ -11,13 +11,13 @@ describe('readConfig', () => {
});

it('should create a file if it does not exist', async () => {
let result = await getCompilerSystem().stat(DEFAULT_CONFIG(true));
let result = await getCompilerSystem().stat(default_config());

if (result.isFile) {
await getCompilerSystem().removeFile(DEFAULT_CONFIG(true));
await getCompilerSystem().removeFile(default_config());
}

result = await getCompilerSystem().stat(DEFAULT_CONFIG(true));
result = await getCompilerSystem().stat(default_config());

expect(result.isFile).toBe(false);

Expand All @@ -29,7 +29,7 @@ describe('readConfig', () => {
it('should read a file if it exists', async () => {
await writeConfig({ 'telemetry.stencil': true, 'tokens.telemetry': '12345' });

let result = await getCompilerSystem().stat(DEFAULT_CONFIG(true));
let result = await getCompilerSystem().stat(default_config());

expect(result.isFile).toBe(true);

Expand All @@ -51,7 +51,7 @@ describe('updateConfig', () => {
it('should edit a file', async () => {
await writeConfig({ 'telemetry.stencil': true, 'tokens.telemetry': '12345' });

let result = await getCompilerSystem().stat(DEFAULT_CONFIG(true));
let result = await getCompilerSystem().stat(default_config());

expect(result.isFile).toBe(true);

Expand Down