-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13994a6
commit 8a40ef6
Showing
6 changed files
with
267 additions
and
278 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,41 +1,40 @@ | ||
import { getCompilerSystem } from './state/stencil-cli-config'; | ||
import { readJson, uuidv4 } from './telemetry/helpers'; | ||
|
||
const CONFIG_FILE = 'config.json'; | ||
export const DEFAULT_CONFIG_DIRECTORY = (file: boolean = false) => { | ||
return getCompilerSystem().resolvePath(`${getCompilerSystem().homeDir()}/.ionic${file ? "/" + CONFIG_FILE : ""}`); | ||
} | ||
export const DEFAULT_CONFIG = (file: boolean = false) => { | ||
return getCompilerSystem().resolvePath(`${getCompilerSystem().homeDir()}/.ionic${file ? '/config.json' : ''}`); | ||
}; | ||
|
||
export interface TelemetryConfig { | ||
"telemetry.stencil"?: boolean, | ||
"tokens.telemetry"?: string, | ||
'telemetry.stencil'?: boolean; | ||
'tokens.telemetry'?: string; | ||
} | ||
|
||
export async function readConfig(): Promise<TelemetryConfig> { | ||
let config: TelemetryConfig = await readJson(DEFAULT_CONFIG_DIRECTORY(true)); | ||
let config: TelemetryConfig = await readJson(DEFAULT_CONFIG(true)); | ||
|
||
if (!config) { | ||
config = { | ||
"tokens.telemetry": uuidv4(), | ||
"telemetry.stencil": true, | ||
}; | ||
if (!config) { | ||
config = { | ||
'tokens.telemetry': uuidv4(), | ||
'telemetry.stencil': true, | ||
}; | ||
|
||
await writeConfig(config); | ||
} | ||
await writeConfig(config); | ||
} | ||
|
||
return config; | ||
return config; | ||
} | ||
|
||
export async function writeConfig(config: TelemetryConfig): Promise<void> { | ||
try { | ||
await getCompilerSystem().createDir(DEFAULT_CONFIG_DIRECTORY(), { recursive: true }); | ||
await getCompilerSystem().writeFile(DEFAULT_CONFIG_DIRECTORY(true), JSON.stringify(config)) | ||
} catch (error) { | ||
console.error(`Stencil Telemetry: couldn't write configuration file to ${DEFAULT_CONFIG_DIRECTORY(true)} - ${error}.`) | ||
}; | ||
try { | ||
await getCompilerSystem().createDir(DEFAULT_CONFIG(), { recursive: true }); | ||
await getCompilerSystem().writeFile(DEFAULT_CONFIG(true), JSON.stringify(config)); | ||
} catch (error) { | ||
console.error(`Stencil Telemetry: couldn't write configuration file to ${DEFAULT_CONFIG(true)} - ${error}.`); | ||
} | ||
} | ||
|
||
export async function updateConfig(newOptions: TelemetryConfig): Promise<void> { | ||
const config = await readConfig(); | ||
await writeConfig(Object.assign(config, newOptions)); | ||
const config = await readConfig(); | ||
await writeConfig(Object.assign(config, newOptions)); | ||
} |
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,107 +1,98 @@ | ||
import type { Logger, CompilerSystem, ConfigFlags } from "../../declarations"; | ||
import type { Logger, CompilerSystem, ConfigFlags } from '../../declarations'; | ||
export type CoreCompiler = typeof import('@stencil/core/compiler'); | ||
|
||
export interface StencilCLIConfigArgs { | ||
task?: string; | ||
args: string[]; | ||
logger: Logger; | ||
sys: CompilerSystem; | ||
flags?: ConfigFlags; | ||
coreCompiler?: CoreCompiler | ||
task?: string; | ||
args: string[]; | ||
logger: Logger; | ||
sys: CompilerSystem; | ||
flags?: ConfigFlags; | ||
coreCompiler?: CoreCompiler; | ||
} | ||
|
||
export default class StencilCLIConfig { | ||
|
||
static instance: StencilCLIConfig; | ||
|
||
private _args: string[]; | ||
private _logger: Logger; | ||
private _sys: CompilerSystem; | ||
private _flags: ConfigFlags; | ||
private _task: string; | ||
private _coreCompiler: CoreCompiler; | ||
|
||
private constructor (options: StencilCLIConfigArgs) { | ||
this._args = options?.args || []; | ||
this._logger = options?.logger; | ||
this._sys = options?.sys; | ||
} | ||
|
||
public static getInstance(options?: StencilCLIConfigArgs): StencilCLIConfig { | ||
if (!StencilCLIConfig.instance && !!options) { | ||
StencilCLIConfig.instance = new StencilCLIConfig(options); | ||
} | ||
|
||
return StencilCLIConfig.instance; | ||
} | ||
|
||
public get logger() { | ||
return this._logger | ||
} | ||
public set logger(logger: Logger) { | ||
this._logger = logger; | ||
} | ||
|
||
|
||
public get sys() { | ||
return this._sys | ||
} | ||
public set sys(sys: CompilerSystem) { | ||
this._sys = sys; | ||
} | ||
|
||
|
||
public get args() { | ||
return this._args | ||
} | ||
public set args(args: string[]) { | ||
this._args = args; | ||
} | ||
|
||
|
||
public get task() { | ||
return this._task | ||
} | ||
public set task(task: string) { | ||
this._task = task; | ||
} | ||
|
||
|
||
public get flags() { | ||
return this._flags | ||
} | ||
public set flags(flags: ConfigFlags) { | ||
this._flags = flags; | ||
} | ||
|
||
|
||
public get coreCompiler() { | ||
return this._coreCompiler | ||
} | ||
public set coreCompiler(coreCompiler: CoreCompiler) { | ||
this._coreCompiler = coreCompiler; | ||
} | ||
static instance: StencilCLIConfig; | ||
|
||
private _args: string[]; | ||
private _logger: Logger; | ||
private _sys: CompilerSystem; | ||
private _flags: ConfigFlags | undefined; | ||
private _task: string | undefined; | ||
private _coreCompiler: CoreCompiler | undefined; | ||
|
||
private constructor(options: StencilCLIConfigArgs) { | ||
this._args = options?.args || []; | ||
this._logger = options?.logger; | ||
this._sys = options?.sys; | ||
} | ||
|
||
public static getInstance(options?: StencilCLIConfigArgs): StencilCLIConfig { | ||
if (!StencilCLIConfig.instance) { | ||
StencilCLIConfig.instance = new StencilCLIConfig(options); | ||
} | ||
|
||
return StencilCLIConfig.instance; | ||
} | ||
|
||
public get logger() { | ||
return this._logger; | ||
} | ||
public set logger(logger: Logger) { | ||
this._logger = logger; | ||
} | ||
|
||
public get sys() { | ||
return this._sys; | ||
} | ||
public set sys(sys: CompilerSystem) { | ||
this._sys = sys; | ||
} | ||
|
||
public get args() { | ||
return this._args; | ||
} | ||
public set args(args: string[]) { | ||
this._args = args; | ||
} | ||
|
||
public get task() { | ||
return this._task; | ||
} | ||
public set task(task: string) { | ||
this._task = task; | ||
} | ||
|
||
public get flags() { | ||
return this._flags; | ||
} | ||
public set flags(flags: ConfigFlags) { | ||
this._flags = flags; | ||
} | ||
|
||
public get coreCompiler() { | ||
return this._coreCompiler; | ||
} | ||
public set coreCompiler(coreCompiler: CoreCompiler) { | ||
this._coreCompiler = coreCompiler; | ||
} | ||
} | ||
|
||
export function initializeStencilCLIConfig(options: StencilCLIConfigArgs): StencilCLIConfig { | ||
return StencilCLIConfig.getInstance(options); | ||
return StencilCLIConfig.getInstance(options); | ||
} | ||
|
||
export function getStencilCLIConfig(): StencilCLIConfig { | ||
return StencilCLIConfig.getInstance(); | ||
return StencilCLIConfig.getInstance(); | ||
} | ||
|
||
export function getCompilerSystem(): CompilerSystem { | ||
const stencilCLIConfig = getStencilCLIConfig(); | ||
return !!stencilCLIConfig && stencilCLIConfig?.sys | ||
return getStencilCLIConfig().sys; | ||
} | ||
|
||
export function getLogger(): Logger { | ||
const stencilCLIConfig = getStencilCLIConfig(); | ||
return !!stencilCLIConfig && stencilCLIConfig?.logger | ||
return getStencilCLIConfig().logger; | ||
} | ||
|
||
export function getCoreCompiler(): CoreCompiler { | ||
const stencilCLIConfig = getStencilCLIConfig(); | ||
return !!stencilCLIConfig && stencilCLIConfig?.coreCompiler | ||
} | ||
return getStencilCLIConfig().coreCompiler; | ||
} |
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,44 +1,48 @@ | ||
import { createLogger } from '../../../compiler/sys/logger/console-logger'; | ||
import { createSystem } from '../../../compiler/sys/stencil-sys'; | ||
import StencilCLIConfig, { getCompilerSystem, getStencilCLIConfig, initializeStencilCLIConfig, getLogger, getCoreCompiler } from '../stencil-cli-config'; | ||
import { | ||
getCompilerSystem, | ||
getStencilCLIConfig, | ||
initializeStencilCLIConfig, | ||
getLogger, | ||
getCoreCompiler, | ||
} from '../stencil-cli-config'; | ||
|
||
describe('StencilCLIConfig', () => { | ||
const config = initializeStencilCLIConfig({ | ||
sys: createSystem(), | ||
args: [], | ||
logger: createLogger() | ||
}) | ||
|
||
it('should behave as a singleton', async () => { | ||
const config2 = initializeStencilCLIConfig({ | ||
sys: createSystem(), | ||
args: [], | ||
logger: createLogger() | ||
}) | ||
|
||
expect(config2).toBe(config); | ||
|
||
const config3 = getStencilCLIConfig(); | ||
|
||
expect(config3).toBe(config); | ||
}); | ||
|
||
it('allows updating any item', async () => { | ||
config.args = ["nice", "awesome"]; | ||
expect(config.args).toEqual(["nice", "awesome"]); | ||
}); | ||
|
||
|
||
it('getCompilerSystem should return a segment of the singleton', async () => { | ||
expect(config.sys).toBe(getCompilerSystem()); | ||
}); | ||
|
||
it('getLogger should return a segment of the singleton', async () => { | ||
expect(config.logger).toBe(getLogger()); | ||
}); | ||
|
||
it('getCoreCompiler should return a segment of the singleton', async () => { | ||
expect(config.coreCompiler).toBe(getCoreCompiler()); | ||
}); | ||
|
||
const config = initializeStencilCLIConfig({ | ||
sys: createSystem(), | ||
args: [], | ||
logger: createLogger(), | ||
}); | ||
|
||
it('should behave as a singleton', () => { | ||
const config2 = initializeStencilCLIConfig({ | ||
sys: createSystem(), | ||
args: [], | ||
logger: createLogger(), | ||
}); | ||
|
||
expect(config2).toBe(config); | ||
|
||
const config3 = getStencilCLIConfig(); | ||
|
||
expect(config3).toBe(config); | ||
}); | ||
|
||
it('allows updating any item', () => { | ||
config.args = ['nice', 'awesome']; | ||
expect(config.args).toEqual(['nice', 'awesome']); | ||
}); | ||
|
||
it('getCompilerSystem should return a segment of the singleton', () => { | ||
expect(config.sys).toBe(getCompilerSystem()); | ||
}); | ||
|
||
it('getLogger should return a segment of the singleton', () => { | ||
expect(config.logger).toBe(getLogger()); | ||
}); | ||
|
||
it('getCoreCompiler should return a segment of the singleton', () => { | ||
expect(config.coreCompiler).toBe(getCoreCompiler()); | ||
}); | ||
}); |
Oops, something went wrong.