js-yaml#load TypeScript Examples
The following examples show how to use
js-yaml#load.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: utils.ts From yfm-docs with MIT License | 7 votes |
export function compareDirectories(expectedOutputPath: string, outputPath: string): CompareResult {
const filesFromExpectedOutput = walkSync(expectedOutputPath, {
directories: false,
includeBasePath: false,
});
let compareResult: CompareResult = true;
filesFromExpectedOutput.forEach((expectedFilePath) => {
const fileExtension = extname(expectedFilePath);
const expectedContent = getFileContent(resolve(expectedOutputPath, expectedFilePath));
const outputContent = getFileContent(resolve(outputPath, expectedFilePath));
const convertedExpectedContent = convertBackSlashToSlash(expectedContent);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let preparedExpectedContent: any = convertedExpectedContent;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let preparedOutputContent: any = outputContent;
if (fileExtension === '.yaml') {
preparedExpectedContent = load(convertedExpectedContent);
preparedOutputContent = load(outputContent);
}
if (!isEqual(preparedExpectedContent, preparedOutputContent)) {
compareResult = {
expectedContent: preparedExpectedContent,
outputContent: preparedOutputContent,
};
}
});
return compareResult;
}
Example #2
Source File: utils.ts From obsidian-linter with MIT License | 7 votes |
export function loadYAML(yaml_text: string): any {
// replacing tabs at the beginning of new lines with 2 spaces fixes loading yaml that has tabs at the start of a line
// https://github.com/platers/obsidian-linter/issues/157
const parsed_yaml = load(yaml_text.replace(/\n(\t)+/g, '\n ')) as {};
if (!parsed_yaml) {
return {};
}
return parsed_yaml;
}
Example #3
Source File: remove-codegen.ts From amplify-codegen with Apache License 2.0 | 6 votes |
export async function testRemoveCodegen(config: AmplifyFrontendConfig, projectRoot: string, schema: string) {
// init project and add API category
await initProjectWithProfile(projectRoot, { ...config });
const projectName = createRandomName();
await addApiWithoutSchema(projectRoot, { apiName: projectName });
await updateApiSchema(projectRoot, projectName, schema);
const userSourceCodePath = testSetupBeforeAddCodegen(projectRoot, config);
// add codegen succeeds
await expect(addCodegen(projectRoot, { ...config })).resolves.not.toThrow();
// remove codegen
await expect(removeCodegen(projectRoot)).resolves.not.toThrow();
// pre-existing file should still exist
expect(existsSync(userSourceCodePath)).toBe(true);
// previously generated files should still exist
expect(isNotEmptyDir(path.join(projectRoot, config.graphqlCodegenDir))).toBe(true);
// graphql configuration should be updated to remove previous configuration
const generatedConfig = load(readFileSync(getGraphQLConfigFilePath(projectRoot)).toString());
expect(Object.keys(generatedConfig.projects).length).toEqual(0);
}
Example #4
Source File: test-setup.ts From amplify-codegen with Apache License 2.0 | 6 votes |
export async function testValidGraphQLConfig(
projectRoot: string,
config: AmplifyFrontendConfig,
maxDepth?: number,
isConfigured: boolean = false,
region: string = process.env.CLI_REGION || REGION
) {
// graphql codegen configuration should exist
expect(existsSync(getGraphQLConfigFilePath(projectRoot))).toBe(true);
const generatedConfig = load(readFileSync(getGraphQLConfigFilePath(projectRoot)).toString());
Object.keys(generatedConfig.projects).forEach(projectName => {
const projectConfig = generatedConfig.projects[projectName];
const expectedProjectConfig = constructGraphQLConfig(projectName, config, maxDepth, region, isConfigured);
// check if the graphql codegen configuration is valid
expect(projectConfig).toEqual(expectedProjectConfig);
});
}
Example #5
Source File: component-package-loader.ts From malagu with MIT License | 6 votes |
load(nodePackage: NodePackage, mode: string[]): void {
let config: any = {};
const configMap = new Map<string, any>();
const configFiles: string[] = [];
config = this.doLoad(nodePackage, [ ''/* load default config file */, ...mode ], configMap, config, configFiles);
configMap.delete('');
config.mode = Array.from(configMap.keys());
config.configFiles = configFiles;
nodePackage.malaguComponent = config;
}
Example #6
Source File: component-package-loader.ts From malagu with MIT License | 6 votes |
loadConfig(nodePackage: NodePackage, configFiles: string[], mode?: string) {
const configPaths = this.parseConfigPaths(mode);
let fullConfigPath: string | undefined = undefined;
for (const configPath of configPaths) {
try {
if (this.pkg.isRoot(nodePackage)) {
if (existsSync(join(nodePackage.modulePath, configPath))) {
fullConfigPath = join(nodePackage.modulePath, configPath);
}
} else {
fullConfigPath = this.pkg.resolveModule(nodePackage.name + `/${configPath}`);
}
if (fullConfigPath) {
break;
}
} catch (err) {
// noop
}
}
if (fullConfigPath) {
configFiles.push(fullConfigPath);
return load(readFileSync(fullConfigPath, { encoding: 'utf8' }));
}
}
Example #7
Source File: index.tsx From che-dashboard-next with Eclipse Public License 2.0 | 6 votes |
private handleDevfileContent(devfileContent: string, attrs: { stackName?: string, infrastructureNamespace?: string }): Promise<void> {
try {
const devfile = load(devfileContent);
return this.createWorkspace(devfile, attrs.stackName, attrs.infrastructureNamespace);
} catch (e) {
const errorMessage = 'Failed to parse the devfile';
this.showAlert({
key: 'parse-devfile-failed',
variant: AlertVariant.danger,
title: errorMessage + '.'
});
throw new Error(errorMessage + ', \n' + e);
}
}
Example #8
Source File: index.ts From gear-js with GNU General Public License v3.0 | 6 votes |
export async function processPrepare(api: GearApi): Promise<IPrepared> {
const programs = load(readFileSync('./spec/programs.yaml', 'utf8')) as { [program: string]: IProgramSpec };
const uploadedPrograms = await uploadPrograms(api, programs);
const messages = load(readFileSync('./spec/messages.yaml', 'utf8')) as { [program: string]: IMessageSpec[] };
const sentMessages = await sendMessages(api, messages, uploadedPrograms);
return { programs: uploadedPrograms, messages: sentMessages };
}
Example #9
Source File: readFromFile.ts From Nishan with MIT License | 6 votes |
/**
* Reads and extracts data from a local file
* @param file_path Path of the file to read from
* @returns Extracted data from the read file
*/
export async function readFromFile (file_path: string) {
const ext = path.extname(file_path);
let data: INotionSyncFileShape = {} as any;
if (ext === '.json') data = JSON.parse(await fs.promises.readFile(file_path, 'utf-8'));
else if (ext === '.yaml' || ext === '.yml')
data = load(await fs.promises.readFile(file_path, 'utf-8')) as INotionSyncFileShape;
else throw new Error('Unsupported output file extension. Use either json or yaml file when specifying the filepath');
return NotionSync.extractData(data);
}
Example #10
Source File: processServiceFiles.ts From yfm-docs with MIT License | 6 votes |
function preparingPresetFiles(getFilePathsByGlobals: GetFilePathsByGlobalsFunction): void {
const {
input: inputFolderPath,
varsPreset = '',
outputFormat,
applyPresets,
resolveConditions,
} = ArgvService.getConfig();
try {
const presetsFilePaths = getFilePathsByGlobals(['**/presets.yaml']);
for (const path of presetsFilePaths) {
logger.proc(path);
const pathToPresetFile = resolve(inputFolderPath, path);
const content = readFileSync(pathToPresetFile, 'utf8');
const parsedPreset = load(content) as DocPreset;
PresetService.add(parsedPreset, path, varsPreset);
if (outputFormat === 'md' && (!applyPresets || !resolveConditions)) {
// Should save filtered presets.yaml only when --apply-presets=false or --resolve-conditions=false
saveFilteredPresets(path, parsedPreset);
}
}
} catch (error) {
log.error(`Preparing presets.yaml files failed. Error: ${error}`);
throw error;
}
}
Example #11
Source File: submit-form.component.ts From models-web-app with Apache License 2.0 | 5 votes |
submit() {
this.applying = true;
let cr: InferenceServiceK8s = {};
try {
cr = load(this.yaml);
} catch (e) {
let msg = 'Could not parse the provided YAML';
if (e.mark && e.mark.line) {
msg = 'Error parsing the provided YAML in line: ' + e.mark.line;
}
this.snack.open(msg, SnackType.Error, 16000);
this.applying = false;
return;
}
if (!cr.metadata) {
this.snack.open(
'InferenceService must have a metadata field.',
SnackType.Error,
8000,
);
this.applying = false;
return;
}
cr.metadata.namespace = this.namespace;
console.log(cr);
this.backend.postInferenceService(cr).subscribe({
next: () => {
this.navigateBack();
},
error: () => {
this.applying = false;
},
});
}
Example #12
Source File: validator.ts From yfm-docs with MIT License | 5 votes |
export function argvValidator(argv: Arguments<Object>): Boolean {
try {
// Combine passed argv and properties from configuration file.
const pathToConfig = argv.config ? String(argv.config) : join(String(argv.input), YFM_CONFIG_FILENAME);
const content = readFileSync(resolve(pathToConfig), 'utf8');
Object.assign(argv, load(content) || {});
} catch (error) {
if (error.name === 'YAMLException') {
log.error(`Error to parse ${YFM_CONFIG_FILENAME}: ${error.message}`);
}
}
let lintConfig = {};
try {
const pathToConfig = join(String(argv.input), LINT_CONFIG_FILENAME);
const content = readFileSync(resolve(pathToConfig), 'utf8');
lintConfig = load(content) || {};
} catch (error) {
if (error.name === 'YAMLException') {
log.error(`Error to parse ${LINT_CONFIG_FILENAME}: ${error.message}`);
}
} finally {
const preparedLintConfig = merge(lintConfig, {
'log-levels': {
MD033: argv.allowHTML ? 'disabled' : 'error',
},
});
Object.assign(argv, {lintConfig: preparedLintConfig});
}
try {
const pathToRedirects = join(String(argv.input), REDIRECTS_FILENAME);
const redirectsContent = readFileSync(resolve(pathToRedirects), 'utf8');
const redirects = load(redirectsContent);
validateRedirects(redirects as RedirectsConfig, pathToRedirects);
} catch (error) {
if (error.name === 'YAMLException') {
log.error(`Error to parse ${REDIRECTS_FILENAME}: ${error.message}`);
}
if (error.code !== 'ENOENT') {
throw error;
}
}
if (argv.publish) {
for (const [field, validator] of Object.entries(validators)) {
const value = argv[field] ?? validator.defaultValue;
if (!validator) {
continue;
}
const validateFn = validator.validateFn ?? requiredValueValidator;
if (!validateFn(value)) {
throw new Error(validator.errorMessage);
}
argv[field] = value;
}
}
return true;
}
Example #13
Source File: tocs.ts From yfm-docs with MIT License | 5 votes |
/**
* Replaces include fields in toc file by resolved toc.
* @param items
* @param tocDir
* @param sourcesDir
* @param vars
* @return
* @private
*/
function _replaceIncludes(items: YfmToc[], tocDir: string, sourcesDir: string, vars: Record<string, string>): YfmToc[] {
return items.reduce((acc, item) => {
let includedInlineItems: YfmToc[] | null = null;
if (item.name) {
const tocPath = join(tocDir, 'toc.yaml');
item.name = _liquidSubstitutions(item.name, vars, tocPath);
}
if (item.include) {
const {path, mode = IncludeMode.ROOT_MERGE} = item.include;
const includeTocPath = mode === IncludeMode.ROOT_MERGE
? resolve(sourcesDir, path)
: resolve(tocDir, path);
const includeTocDir = dirname(includeTocPath);
try {
const includeToc = load(readFileSync(includeTocPath, 'utf8')) as YfmToc;
// Should ignore included toc with tech-preview stage.
if (includeToc.stage === Stage.TECH_PREVIEW) {
return acc;
}
if (mode === IncludeMode.MERGE || mode === IncludeMode.ROOT_MERGE) {
_copyTocDir(includeTocPath, tocDir);
}
/* Save the path to exclude toc from the output directory in the next step */
includedTocPaths.add(includeTocPath);
let includedTocItems = (item.items || []).concat(includeToc.items);
/* Resolve nested toc inclusions */
const baseTocDir = mode === IncludeMode.LINK ? includeTocDir : tocDir;
includedTocItems = _replaceIncludes(includedTocItems, baseTocDir, sourcesDir, vars);
/* Make hrefs relative to the main toc */
if (mode === IncludeMode.LINK) {
includedTocItems = _replaceIncludesHrefs(includedTocItems, includeTocDir, tocDir);
}
if (item.name) {
item.items = includedTocItems;
} else {
includedInlineItems = includedTocItems;
}
} catch (err) {
const message = `Error while including toc: ${bold(includeTocPath)} to ${bold(join(tocDir, 'toc.yaml'))}`;
console.log(message, err);
log.error(message);
return acc;
} finally {
delete item.include;
}
} else if (item.items) {
item.items = _replaceIncludes(item.items, tocDir, sourcesDir, vars);
}
if (includedInlineItems) {
return acc.concat(includedInlineItems);
} else {
return acc.concat(item);
}
}, [] as YfmToc[]);
}
Example #14
Source File: leading.ts From yfm-docs with MIT License | 5 votes |
function filterFile(path: string) {
const {
input: inputFolderPath,
} = ArgvService.getConfig();
const pathToDir = dirname(path);
const filePath = resolve(inputFolderPath, path);
const content = readFileSync(filePath, 'utf8');
const parsedIndex = load(content) as LeadingPage;
const {vars} = ArgvService.getConfig();
const combinedVars = {
...PresetService.get(pathToDir),
...vars,
};
/* Should remove all links with false expressions */
try {
parsedIndex.title = firstFilterTextItems(
parsedIndex.title,
combinedVars,
{resolveConditions: true},
);
parsedIndex.description = filterTextItems(
parsedIndex.description,
combinedVars,
{resolveConditions: true},
);
if (parsedIndex.meta?.title) {
parsedIndex.meta.title = firstFilterTextItems(
parsedIndex.meta.title,
combinedVars,
{resolveConditions: true},
);
}
if (parsedIndex.nav) {
parsedIndex.nav.title = firstFilterTextItems(
parsedIndex.nav.title,
combinedVars,
{resolveConditions: true},
);
}
parsedIndex.links = filterFiles(parsedIndex.links, 'links', combinedVars, {resolveConditions: true});
writeFileSync(filePath, dump(parsedIndex));
} catch (error) {
log.error(`Error while filtering index file: ${path}. Error message: ${error}`);
}
}
Example #15
Source File: nmstateYaml.ts From assisted-ui-lib with Apache License 2.0 | 5 votes |
yamlToNmstateObject = (yaml: string): Nmstate => {
return load(yaml) as Nmstate;
}
Example #16
Source File: remark-extract-frontmatter.ts From portfolio with MIT License | 5 votes |
export default function extractFrontmatter() {
return (tree: Parent, file: VFile) => {
visit(tree, 'yaml', (node: Parent) => {
//@ts-ignore
file.data.frontmatter = load(node.value);
});
};
}
Example #17
Source File: mappingConfig.ts From actions-mention-to-slack with MIT License | 5 votes |
MappingConfigRepositoryImpl = {
downloadFromUrl: async (url: string) => {
const response = await axios.get<string>(url);
return response.data;
},
loadYaml: (data: string) => {
const configObject = load(data);
if (configObject === undefined) {
throw new Error(
["failed to load yaml", JSON.stringify({ data }, null, 2)].join("\n")
);
}
return configObject as MappingFile;
},
loadFromUrl: async (url: string) => {
const data = await MappingConfigRepositoryImpl.downloadFromUrl(url);
return MappingConfigRepositoryImpl.loadYaml(data);
},
loadFromGithubPath: async (
repoToken: string,
owner: string,
repo: string,
configurationPath: string,
sha: string
) => {
const githubClient = getOctokit(repoToken);
const response = await githubClient.rest.repos.getContent({
owner,
repo,
path: configurationPath,
ref: sha,
});
if (!("content" in response.data)) {
throw new Error(
["Unexpected response", JSON.stringify({ response }, null, 2)].join(
"\n"
)
);
}
const data = Buffer.from(response.data.content, "base64").toString();
return MappingConfigRepositoryImpl.loadYaml(data);
},
}
Example #18
Source File: tocs.ts From yfm-docs with MIT License | 4 votes |
function add(path: string) {
const {
input: inputFolderPath,
output: outputFolderPath,
outputFormat,
ignoreStage,
singlePage,
vars,
resolveConditions,
applyPresets,
removeHiddenTocItems,
} = ArgvService.getConfig();
const pathToDir = dirname(path);
const content = readFileSync(resolve(inputFolderPath, path), 'utf8');
const parsedToc = load(content) as YfmToc;
// Should ignore toc with specified stage.
if (parsedToc.stage === ignoreStage) {
return;
}
const combinedVars = {
...PresetService.get(pathToDir),
...vars,
};
if (parsedToc.title) {
parsedToc.title = firstFilterTextItems(
parsedToc.title,
combinedVars,
{resolveConditions: true},
);
}
/* Should make substitutions in title */
if (applyPresets && typeof parsedToc.title === 'string') {
parsedToc.title = _liquidSubstitutions(parsedToc.title, combinedVars, path);
}
/* Should resolve all includes */
parsedToc.items = _replaceIncludes(
parsedToc.items,
join(inputFolderPath, pathToDir),
resolve(inputFolderPath),
combinedVars,
);
/* Should remove all links with false expressions */
if (resolveConditions || removeHiddenTocItems) {
try {
parsedToc.items = filterFiles(parsedToc.items, 'items', combinedVars, {
resolveConditions,
removeHiddenTocItems,
});
} catch (error) {
log.error(`Error while filtering toc file: ${path}. Error message: ${error}`);
}
}
/* Store parsed toc for .md output format */
storage.set(path, parsedToc);
/* Store path to toc file to handle relative paths in navigation */
parsedToc.base = pathToDir;
if (outputFormat === 'md') {
/* Should copy resolved and filtered toc to output folder */
const outputPath = resolve(outputFolderPath, path);
const outputToc = dump(parsedToc);
shell.mkdir('-p', dirname(outputPath));
writeFileSync(outputPath, outputToc);
if (singlePage) {
const parsedSinglePageToc = _cloneDeep(parsedToc);
const currentPath = resolve(outputFolderPath, path);
parsedSinglePageToc.items = filterFiles(parsedSinglePageToc.items, 'items', {}, {
removeHiddenTocItems: true,
});
prepareTocForSinglePageMode(parsedSinglePageToc, {root: outputFolderPath, currentPath});
const outputSinglePageDir = resolve(dirname(outputPath), SINGLE_PAGE_FOLDER);
const outputSinglePageTocPath = resolve(outputSinglePageDir, 'toc.yaml');
const outputSinglePageToc = dump(parsedSinglePageToc);
shell.mkdir('-p', outputSinglePageDir);
writeFileSync(outputSinglePageTocPath, outputSinglePageToc);
}
}
prepareNavigationPaths(parsedToc, pathToDir);
}