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
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions src/cli/ionic-config.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
import { readJSON, writeJSON, mkdirp } from '@ionic/utils-fs';

import fs from 'fs-extra';
splitinfinities marked this conversation as resolved.
Show resolved Hide resolved
import * as os from 'os';
import * as path from 'path';

import { uuidv4 } from './telemetry/helpers';

export const CONFIG_FILE = 'config.json';
const CONFIG_FILE = 'config.json';
export const DEFAULT_CONFIG_DIRECTORY = path.resolve(os.homedir(), '.ionic', CONFIG_FILE);

export interface SystemConfig {
"telemetry"?: boolean,
export interface TelemetryConfig {
"telemetry.stencil"?: boolean,
"tokens.telemetry"?: string,
splitinfinities marked this conversation as resolved.
Show resolved Hide resolved
}

export async function readConfig(): Promise<SystemConfig> {
export async function readConfig(): Promise<TelemetryConfig> {
try {
return await readJSON(DEFAULT_CONFIG_DIRECTORY);

return await fs.readJson(DEFAULT_CONFIG_DIRECTORY);
} catch (e) {
if (e.code !== 'ENOENT') {
throw e;
}

const config: SystemConfig = {
const config: TelemetryConfig = {
"tokens.telemetry": uuidv4(),
"telemetry": true,
"telemetry.stencil": true,
};

Expand All @@ -34,12 +32,15 @@ export async function readConfig(): Promise<SystemConfig> {
}
}

export async function writeConfig(config: SystemConfig): Promise<void> {
await mkdirp(path.dirname(DEFAULT_CONFIG_DIRECTORY));
await writeJSON(DEFAULT_CONFIG_DIRECTORY, config, { spaces: '\t' });
export async function writeConfig(config: TelemetryConfig): Promise<void> {
await fs.mkdirp(path.dirname(DEFAULT_CONFIG_DIRECTORY)).then(async () => {
return await fs.writeJSON(DEFAULT_CONFIG_DIRECTORY, config, { spaces: '\t' });
}).catch(() => {
console.debug(`Couldn't write to ${DEFAULT_CONFIG_DIRECTORY}. `)
splitinfinities marked this conversation as resolved.
Show resolved Hide resolved
});
splitinfinities marked this conversation as resolved.
Show resolved Hide resolved
}

export async function updateConfig(newOptions: SystemConfig): Promise<void> {
export async function updateConfig(newOptions: TelemetryConfig): Promise<void> {
const config = await readConfig();
await writeConfig(Object.assign(config, newOptions));
}
22 changes: 21 additions & 1 deletion src/cli/telemetry/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import { TERMINAL_INFO } from '@ionic/utils-terminal';
interface TerminalInfo {
/**
* Whether this is in CI or not.
*/
readonly ci: boolean;
/**
* Path to the user's shell program.
*/
readonly shell: string;
/**
* Whether the terminal is an interactive TTY or not.
*/
readonly tty: boolean;
/**
* Whether this is a Windows shell or not.
*/
readonly windows: boolean;
}

declare const TERMINAL_INFO: TerminalInfo;

export const tryFn = async <T extends (...args: any[]) => Promise<R>, R>(
fn: T,
Expand All @@ -15,6 +34,7 @@ export const tryFn = async <T extends (...args: any[]) => Promise<R>, R>(

export const isInteractive = (): boolean => TERMINAL_INFO.tty && !TERMINAL_INFO.ci;
splitinfinities marked this conversation as resolved.
Show resolved Hide resolved

// Plucked from https://github.com/ionic-team/capacitor/blob/b893a57aaaf3a16e13db9c33037a12f1a5ac92e0/cli/src/util/uuid.ts
export function uuidv4(): string {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
splitinfinities marked this conversation as resolved.
Show resolved Hide resolved
const r = (Math.random() * 16) | 0;
Expand Down
32 changes: 32 additions & 0 deletions src/cli/telemetry/test/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { tryFn, uuidv4 } from '../helpers';

describe('uuidv4', () => {

it('should output a UUID', async () => {
splitinfinities marked this conversation as resolved.
Show resolved Hide resolved
const pattern = new RegExp(/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i);
const uuid = uuidv4()
expect(typeof uuid).toBe("string")
splitinfinities marked this conversation as resolved.
Show resolved Hide resolved
expect(!!uuid.match(pattern)).toBe(true)
});

});

describe('tryFn', () => {

it('should handle failures correctly', async () => {
const result = await tryFn(async () => {
throw new Error("Uh oh!")
});

expect(result).toBe(null)
});

it('should handle success correctly', async () => {
const result = await tryFn(async () => {
return true
})

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

splitinfinities marked this conversation as resolved.
Show resolved Hide resolved
});
58 changes: 58 additions & 0 deletions src/cli/test/ionic-config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import fs from 'fs-extra';
import { readConfig, writeConfig, updateConfig, DEFAULT_CONFIG_DIRECTORY } from '../ionic-config';

describe('readConfig', () => {

it('should create a file if it does not exist', async () => {
if (await fs.pathExists(DEFAULT_CONFIG_DIRECTORY)) {
fs.rmSync(DEFAULT_CONFIG_DIRECTORY);
}

expect(await fs.pathExists(DEFAULT_CONFIG_DIRECTORY)).toBe(false)

const config = await readConfig();

expect(typeof config).toBe("object");
splitinfinities marked this conversation as resolved.
Show resolved Hide resolved
expect(Object.keys(config).join()).toBe("tokens.telemetry,telemetry.stencil");
});

it('should read a file if it exists', async () => {
await writeConfig({"telemetry.stencil": true, "tokens.telemetry": "12345"})

expect(await fs.pathExists(DEFAULT_CONFIG_DIRECTORY)).toBe(true)

const config = await readConfig();

expect(typeof config).toBe("object");
expect(Object.keys(config).join()).toBe("telemetry.stencil,tokens.telemetry");
expect(config['telemetry.stencil']).toBe(true);
expect(config['tokens.telemetry']).toBe("12345");
});
});

describe('updateConfig', () => {

it('should edit a file', async () => {
await writeConfig({ "telemetry.stencil": true, "tokens.telemetry": "12345" })

expect(await fs.pathExists(DEFAULT_CONFIG_DIRECTORY)).toBe(true)

const configPre = await readConfig();

expect(typeof configPre).toBe("object");
expect(Object.keys(configPre).join()).toBe("telemetry.stencil,tokens.telemetry");
expect(configPre['telemetry.stencil']).toBe(true);
expect(configPre['tokens.telemetry']).toBe("12345");

await updateConfig({ "telemetry.stencil": false, "tokens.telemetry": "67890" });

const configPost = await readConfig();

expect(typeof configPost).toBe("object");
// Should keep the previous order
expect(Object.keys(configPost).join()).toBe("telemetry.stencil,tokens.telemetry");
expect(configPost['telemetry.stencil']).toBe(false);
expect(configPost['tokens.telemetry']).toBe("67890");
});

});