mirror of
https://github.com/pyscript/pyscript.git
synced 2025-12-21 03:05:38 -05:00
Format the TypeScript files (#877)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import toml from '../src/toml'
|
||||
import toml from '../src/toml';
|
||||
import { getLogger } from './logger';
|
||||
import { version } from './runtime';
|
||||
import { getAttribute, readTextFromPath, showError } from './utils'
|
||||
import { getAttribute, readTextFromPath, showError } from './utils';
|
||||
|
||||
const logger = getLogger('py-config');
|
||||
|
||||
@@ -31,29 +31,30 @@ export type RuntimeConfig = {
|
||||
export type PyScriptMetadata = {
|
||||
version?: string;
|
||||
time?: string;
|
||||
}
|
||||
};
|
||||
|
||||
const allKeys = {
|
||||
"string": ["name", "description", "version", "type", "author_name", "author_email", "license"],
|
||||
"number": ["schema_version"],
|
||||
"boolean": ["autoclose_loader"],
|
||||
"array": ["runtimes", "packages", "paths", "plugins"]
|
||||
string: ['name', 'description', 'version', 'type', 'author_name', 'author_email', 'license'],
|
||||
number: ['schema_version'],
|
||||
boolean: ['autoclose_loader'],
|
||||
array: ['runtimes', 'packages', 'paths', 'plugins'],
|
||||
};
|
||||
|
||||
export const defaultConfig: AppConfig = {
|
||||
"schema_version": 1,
|
||||
"type": "app",
|
||||
"autoclose_loader": true,
|
||||
"runtimes": [{
|
||||
"src": "https://cdn.jsdelivr.net/pyodide/v0.21.3/full/pyodide.js",
|
||||
"name": "pyodide-0.21.3",
|
||||
"lang": "python"
|
||||
}],
|
||||
"packages":[],
|
||||
"paths":[],
|
||||
"plugins": []
|
||||
}
|
||||
|
||||
schema_version: 1,
|
||||
type: 'app',
|
||||
autoclose_loader: true,
|
||||
runtimes: [
|
||||
{
|
||||
src: 'https://cdn.jsdelivr.net/pyodide/v0.21.3/full/pyodide.js',
|
||||
name: 'pyodide-0.21.3',
|
||||
lang: 'python',
|
||||
},
|
||||
],
|
||||
packages: [],
|
||||
paths: [],
|
||||
plugins: [],
|
||||
};
|
||||
|
||||
export function loadConfigFromElement(el: Element): AppConfig {
|
||||
let srcConfig: AppConfig;
|
||||
@@ -61,47 +62,41 @@ export function loadConfigFromElement(el: Element): AppConfig {
|
||||
if (el === null) {
|
||||
srcConfig = {};
|
||||
inlineConfig = {};
|
||||
}
|
||||
else {
|
||||
const configType = getAttribute(el, "type") || "toml";
|
||||
} else {
|
||||
const configType = getAttribute(el, 'type') || 'toml';
|
||||
srcConfig = extractFromSrc(el, configType);
|
||||
inlineConfig = extractFromInline(el, configType);
|
||||
}
|
||||
srcConfig = mergeConfig(srcConfig, defaultConfig);
|
||||
const result = mergeConfig(inlineConfig, srcConfig);
|
||||
result.pyscript = {
|
||||
"version": version,
|
||||
"time": new Date().toISOString()
|
||||
version: version,
|
||||
time: new Date().toISOString(),
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
function extractFromSrc(el: Element, configType: string) {
|
||||
const src = getAttribute(el, "src")
|
||||
const src = getAttribute(el, 'src');
|
||||
if (src) {
|
||||
logger.info('loading ', src)
|
||||
logger.info('loading ', src);
|
||||
return validateConfig(readTextFromPath(src), configType);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
function extractFromInline(el: Element, configType: string) {
|
||||
if (el.innerHTML !== '')
|
||||
{
|
||||
if (el.innerHTML !== '') {
|
||||
logger.info('loading <py-config> content');
|
||||
return validateConfig(el.innerHTML, configType);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
function fillUserData(inputConfig: AppConfig, resultConfig: AppConfig): AppConfig
|
||||
{
|
||||
for (const key in inputConfig)
|
||||
{
|
||||
function fillUserData(inputConfig: AppConfig, resultConfig: AppConfig): AppConfig {
|
||||
for (const key in inputConfig) {
|
||||
// fill in all extra keys ignored by the validator
|
||||
if (!(key in defaultConfig))
|
||||
{
|
||||
if (!(key in defaultConfig)) {
|
||||
resultConfig[key] = inputConfig[key];
|
||||
}
|
||||
}
|
||||
@@ -109,32 +104,22 @@ function fillUserData(inputConfig: AppConfig, resultConfig: AppConfig): AppConfi
|
||||
}
|
||||
|
||||
function mergeConfig(inlineConfig: AppConfig, externalConfig: AppConfig): AppConfig {
|
||||
if (Object.keys(inlineConfig).length === 0 && Object.keys(externalConfig).length === 0)
|
||||
{
|
||||
if (Object.keys(inlineConfig).length === 0 && Object.keys(externalConfig).length === 0) {
|
||||
return defaultConfig;
|
||||
}
|
||||
else if (Object.keys(inlineConfig).length === 0)
|
||||
{
|
||||
} else if (Object.keys(inlineConfig).length === 0) {
|
||||
return externalConfig;
|
||||
}
|
||||
else if(Object.keys(externalConfig).length === 0)
|
||||
{
|
||||
} else if (Object.keys(externalConfig).length === 0) {
|
||||
return inlineConfig;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
let merged: AppConfig = {};
|
||||
|
||||
for (const keyType in allKeys)
|
||||
{
|
||||
for (const keyType in allKeys) {
|
||||
const keys: string[] = allKeys[keyType];
|
||||
keys.forEach(function(item: string){
|
||||
if (keyType === "boolean")
|
||||
{
|
||||
merged[item] = (typeof inlineConfig[item] !== "undefined") ? inlineConfig[item] : externalConfig[item];
|
||||
}
|
||||
else
|
||||
{
|
||||
keys.forEach(function (item: string) {
|
||||
if (keyType === 'boolean') {
|
||||
merged[item] =
|
||||
typeof inlineConfig[item] !== 'undefined' ? inlineConfig[item] : externalConfig[item];
|
||||
} else {
|
||||
merged[item] = inlineConfig[item] || externalConfig[item];
|
||||
}
|
||||
});
|
||||
@@ -149,20 +134,18 @@ function mergeConfig(inlineConfig: AppConfig, externalConfig: AppConfig): AppCon
|
||||
}
|
||||
}
|
||||
|
||||
function parseConfig(configText: string, configType = "toml") {
|
||||
function parseConfig(configText: string, configType = 'toml') {
|
||||
let config: object;
|
||||
if (configType === "toml") {
|
||||
if (configType === 'toml') {
|
||||
try {
|
||||
// TOML parser is soft and can parse even JSON strings, this additional check prevents it.
|
||||
if (configText.trim()[0] === "{")
|
||||
{
|
||||
if (configText.trim()[0] === '{') {
|
||||
const errMessage = `config supplied: ${configText} is an invalid TOML and cannot be parsed`;
|
||||
showError(`<p>${errMessage}</p>`);
|
||||
throw Error(errMessage);
|
||||
}
|
||||
config = toml.parse(configText);
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
const errMessage: string = err.toString();
|
||||
showError(`<p>config supplied: ${configText} is an invalid TOML and cannot be parsed: ${errMessage}</p>`);
|
||||
// we cannot easily just "throw err" here, because for some reason
|
||||
@@ -175,52 +158,42 @@ function parseConfig(configText: string, configType = "toml") {
|
||||
// it's correctly handled by playwright.
|
||||
throw SyntaxError(errMessage);
|
||||
}
|
||||
}
|
||||
else if (configType === "json") {
|
||||
} else if (configType === 'json') {
|
||||
try {
|
||||
config = JSON.parse(configText);
|
||||
}
|
||||
catch (err) {
|
||||
} catch (err) {
|
||||
const errMessage: string = err.toString();
|
||||
showError(`<p>config supplied: ${configText} is an invalid JSON and cannot be parsed: ${errMessage}</p>`);
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
showError(`<p>type of config supplied is: ${configType}, supported values are ["toml", "json"].</p>`);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
function validateConfig(configText: string, configType = "toml") {
|
||||
function validateConfig(configText: string, configType = 'toml') {
|
||||
const config = parseConfig(configText, configType);
|
||||
|
||||
const finalConfig: AppConfig = {}
|
||||
const finalConfig: AppConfig = {};
|
||||
|
||||
for (const keyType in allKeys)
|
||||
{
|
||||
for (const keyType in allKeys) {
|
||||
const keys: string[] = allKeys[keyType];
|
||||
keys.forEach(function(item: string){
|
||||
if (validateParamInConfig(item, keyType, config))
|
||||
{
|
||||
if (item === "runtimes")
|
||||
{
|
||||
keys.forEach(function (item: string) {
|
||||
if (validateParamInConfig(item, keyType, config)) {
|
||||
if (item === 'runtimes') {
|
||||
finalConfig[item] = [];
|
||||
const runtimes = config[item] as object[];
|
||||
runtimes.forEach(function(eachRuntime: object){
|
||||
runtimes.forEach(function (eachRuntime: object) {
|
||||
const runtimeConfig: object = {};
|
||||
for (const eachRuntimeParam in eachRuntime)
|
||||
{
|
||||
if (validateParamInConfig(eachRuntimeParam, "string", eachRuntime))
|
||||
{
|
||||
for (const eachRuntimeParam in eachRuntime) {
|
||||
if (validateParamInConfig(eachRuntimeParam, 'string', eachRuntime)) {
|
||||
runtimeConfig[eachRuntimeParam] = eachRuntime[eachRuntimeParam];
|
||||
}
|
||||
}
|
||||
finalConfig[item].push(runtimeConfig);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
finalConfig[item] = config[item];
|
||||
}
|
||||
}
|
||||
@@ -231,9 +204,8 @@ function validateConfig(configText: string, configType = "toml") {
|
||||
}
|
||||
|
||||
function validateParamInConfig(paramName: string, paramType: string, config: object): boolean {
|
||||
if (paramName in config)
|
||||
{
|
||||
return paramType === "array" ? Array.isArray(config[paramName]) : typeof config[paramName] === paramType;
|
||||
if (paramName in config) {
|
||||
return paramType === 'array' ? Array.isArray(config[paramName]) : typeof config[paramName] === paramType;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user