feat: warnOnce & graceful error handling

This commit is contained in:
Igor Randjelovic
2021-04-01 17:55:45 +02:00
parent 7edb1e90b0
commit 1627f52043
4 changed files with 40 additions and 8 deletions

View File

@ -1,11 +1,15 @@
import { env } from '../index'; import { env } from '../index';
import { error } from './log'; import { error, warnOnce } from './log';
function getCLILib() { function getCLILib() {
if (!env.nativescriptLibPath) { if (!env.nativescriptLibPath) {
throw error(` warnOnce(
'getCLILib',
`
Cannot find NativeScript CLI path. Make sure --env.nativescriptLibPath is passed Cannot find NativeScript CLI path. Make sure --env.nativescriptLibPath is passed
`); `
);
return false;
} }
return require(env.nativescriptLibPath); return require(env.nativescriptLibPath);
@ -15,10 +19,15 @@ function getCLILib() {
* Utility to get a value from the nativescript.config.ts file. * Utility to get a value from the nativescript.config.ts file.
* *
* @param {string} key The key to get from the config. Supports dot-notation. * @param {string} key The key to get from the config. Supports dot-notation.
* @param defaultValue The fallback value if the key is not set in the config.
*/ */
export function getValue<T = any>(key: string, defaultValue?: any): T { export function getValue<T = any>(key: string, defaultValue?: any): T {
const lib = getCLILib(); const lib = getCLILib();
if (!lib) {
return defaultValue;
}
return (lib.projectConfigService as { return (lib.projectConfigService as {
getValue(key: string, defaultValue?: any): T; getValue(key: string, defaultValue?: any): T;
}).getValue(key, defaultValue); }).getValue(key, defaultValue);

View File

@ -1,11 +1,15 @@
import { merge } from 'webpack-merge'; import { merge } from 'webpack-merge';
import {
getPackageJson,
getProjectRootPath,
getProjectFilePath,
} from './project';
import { addVirtualEntry, addVirtualModule } from './virtualModules'; import { addVirtualEntry, addVirtualModule } from './virtualModules';
import { getPackageJson, getProjectRootPath } from './project';
import { applyFileReplacements } from './fileReplacements'; import { applyFileReplacements } from './fileReplacements';
import { addCopyRule, removeCopyRule } from './copyRules'; import { addCopyRule, removeCopyRule } from './copyRules';
import { error, info, warn, warnOnce } from './log';
import { determineProjectFlavor } from './flavor'; import { determineProjectFlavor } from './flavor';
import { error, info, warn } from './log';
import { getValue } from './config'; import { getValue } from './config';
import { getIPS } from './host'; import { getIPS } from './host';
import { import {
@ -51,6 +55,7 @@ export default {
error, error,
info, info,
warn, warn,
warnOnce,
}, },
platform: { platform: {
addPlatform, addPlatform,
@ -62,6 +67,7 @@ export default {
getPlatformName, getPlatformName,
}, },
project: { project: {
getProjectFilePath,
getProjectRootPath, getProjectRootPath,
getPackageJson, getPackageJson,
}, },

View File

@ -28,6 +28,16 @@ export function warn(...data: any): void {
console.warn(`[@nativescript/webpack] Warn: \n`, ...cleanup(data)); console.warn(`[@nativescript/webpack] Warn: \n`, ...cleanup(data));
} }
const warnedMap: any = {};
export function warnOnce(key: string, ...data: any): void {
if (warnedMap[key]) {
return;
}
warnedMap[key] = true;
warn(...data);
}
export function info(...data: any): void { export function info(...data: any): void {
if (env.verbose) { if (env.verbose) {
console.log(`[@nativescript/webpack] Info: \n`, ...cleanup(data)); console.log(`[@nativescript/webpack] Info: \n`, ...cleanup(data));

View File

@ -1,7 +1,7 @@
import { dirname, resolve } from 'path'; import { dirname, resolve } from 'path';
import { getPackageJson, getProjectRootPath } from './project'; import { getPackageJson, getProjectRootPath } from './project';
import { error, info } from './log'; import { error, info, warnOnce } from './log';
import { env } from '../'; import { env } from '../';
import AndroidPlatform from '../platforms/android'; import AndroidPlatform from '../platforms/android';
@ -65,13 +65,20 @@ export function getPlatformName(): Platform {
`); `);
} }
throw error(` warnOnce(
'getPlatformName',
`
You need to provide a target platform! You need to provide a target platform!
Available platforms: ${Object.keys(platforms).join(', ')} Available platforms: ${Object.keys(platforms).join(', ')}
Use --env.platform=<platform> or --env.android, --env.ios to specify the target platform. Use --env.platform=<platform> or --env.android, --env.ios to specify the target platform.
`);
Defaulting to "ios".
`
);
return 'ios';
} }
/** /**