How to use the @hint/utils-fs.loadJSONFile function in @hint/utils-fs

To help you get started, we’ve selected a few @hint/utils-fs examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github webhintio / hint / packages / parser-package-json / src / parser.ts View on Github external
public constructor(engine: Engine) {
        super(engine, 'package-json');
        // JSON Schema from http://json.schemastore.org/package
        this.schema = loadJSONFile(path.join(__dirname, 'schema.json'));

        // package.json => type: 'json' (file type from extention).
        engine.on('fetch::end::json', this.parsePackageJson.bind(this));
    }
github webhintio / hint / packages / utils / src / packages / load-resource.ts View on Github external
export const tryToLoadFrom = (resourcePath: string): any => {
    // This is exported so it's easier to stub during tests
    let builder: any = null;

    /*
     * We could be loading a config file that points to a path (thus a JSON).
     * `require` will try to load `.js`, `.json`, `.node` so it will fail and
     * we have to manually do this
     */
    try {
        const resource = loadJSONFile(resourcePath);

        return resource;
    } catch (e) {
        debug(`${resourcePath} is not a JSON file, trying to load it normally`);
    }

    try {
        /*
         * The following link has more info on how `require` resolves modules:
         * http://nodejs.org/dist/latest-v8.x/docs/api/modules.html#modules_all_together
         */

        const resource = requirePackage(resourcePath);

        builder = resource.default || resource;
    } catch (e) {
github webhintio / hint / packages / utils / src / npm.ts View on Github external
if (packages.length === 0) {
        return Promise.resolve(true);
    }

    const hintLocalPath = path.join(currentWorkingDir, 'node_modules', 'hint', 'package.json');

    // Check if hint is installed locally.
    const global: boolean = !fs.existsSync(hintLocalPath); // eslint-disable-line no-sync

    /** package manager to install the packages. */
    const packageManagerChoice = (!global && await hasYarnLock(currentWorkingDir)) ? 'yarn' : 'npm';

    if (!global) {
        try {
            const packagePath = findPackageRoot(currentWorkingDir);
            const jsonContent = loadJSONFile(path.join(packagePath, 'package.json'));

            // If `hint` is a devDependency, then set all packages as devDependencies.
            isDev = jsonContent.devDependencies && jsonContent.devDependencies.hasOwnProperty('hint');
        } catch (err) {
            // Even if `hint` is installed locally, package.json could not exist in the current working directory.
            isDev = false;
        }

    }

    const installCommand = {
        npm: `npm install${global ? ' --global' : ''}${isDev ? ' --save-dev' : ''}`,
        yarn: `yarn add${isDev ? ' --dev' : ''}`
    };

    const command = `${installCommand[packageManagerChoice]} ${packages.join(' ')}`;
github webhintio / hint / packages / formatter-html / src / result.ts View on Github external
import * as path from 'path';

import * as moment from 'moment';

import cloneDeep = require('lodash/cloneDeep');

import { Category, Problem, Severity } from '@hint/utils-types';
import { loadJSONFile } from '@hint/utils-fs';
import { getCategoryName } from '@hint/utils-i18n';
import { FormatterOptions } from 'hint';

const thirdPartyServices = loadJSONFile(path.join(__dirname, 'configs', 'third-party-service-config.json'));
const categoryImages = loadJSONFile(path.join(__dirname, 'configs', 'category-images.json'));
const hintsWithoutDocs = ['optimize-image'];

/** Third party logo type. */
type ThirdPartyLogo = {
    name: string;
    url: string;
    alt: string;
};

/** Third party information. */
type ThirdPartyInfo = {
    logo: ThirdPartyLogo;
    link: string;
    details?: boolean;
};
github webhintio / hint / packages / utils-json / src / final-config.ts View on Github external
configPath = path.resolve(configDir, finalConfigJSON.extends);

        if (configIncludes.includes(configPath)) {

            const error = new Error(`Circular reference found in file ${lastPath}`) as IParsingError;
            const originalPathUri = getAsUri(configIncludes[0]);

            error.resource = originalPathUri && originalPathUri.toString() || lastPath;

            return error;
        }

        delete finalConfigJSON.extends;

        try {
            const extendedConfig = loadJSONFile(configPath);

            configIncludes.push(configPath);

            finalConfigJSON = merge({}, extendedConfig, finalConfigJSON);
        } catch (err) {
            const lastPathUri = getAsUri(lastPath);

            err.resource = lastPathUri && lastPathUri.toString() || lastPath;

            return err;
        }
    }

    return finalConfigJSON;
};
github webhintio / hint / packages / hint / src / lib / config.ts View on Github external
public static loadConfigFile(filePath: string): UserConfig | null {
        let config: UserConfig | null;

        switch (path.extname(filePath)) {
            case '':
            case '.json':
                if (path.basename(filePath) === 'package.json') {
                    config = loadPackageJSONConfigFile(filePath);
                } else {
                    config = loadJSONFile(filePath);
                }
                break;
            case '.js':
                config = loadJSFile(filePath);
                break;

            default:
                config = null;
        }

        config = this.toAbsolutePaths(config, filePath);

        return config;
    }
github webhintio / hint / packages / hint / src / lib / config.ts View on Github external
const loadPackageJSONConfigFile = (filePath: string): UserConfig => {

    debug(`Loading package.json config file: ${filePath}`);

    try {
        return loadJSONFile(filePath).hintConfig || null;
    } catch (e) {
        debug(`Error reading package.json file: ${filePath}`);
        e.message = `Cannot read config file: ${filePath}\nError: ${e.message}`;
        throw e;
    }
};
github webhintio / hint / packages / parser-typescript-config / src / parser.ts View on Github external
public constructor(engine: Engine) {
        super(engine, 'typescript-config');

        this.schema = loadJSONFile(this.schemaPath);

        engine.on('fetch::end::*', this.parseTypeScript.bind(this));
    }
github webhintio / hint / packages / formatter-html / src / result.ts View on Github external
import * as path from 'path';

import * as moment from 'moment';

import cloneDeep = require('lodash/cloneDeep');

import { Category, Problem, Severity } from '@hint/utils-types';
import { loadJSONFile } from '@hint/utils-fs';
import { getCategoryName } from '@hint/utils-i18n';
import { FormatterOptions } from 'hint';

const thirdPartyServices = loadJSONFile(path.join(__dirname, 'configs', 'third-party-service-config.json'));
const categoryImages = loadJSONFile(path.join(__dirname, 'configs', 'category-images.json'));
const hintsWithoutDocs = ['optimize-image'];

/** Third party logo type. */
type ThirdPartyLogo = {
    name: string;
    url: string;
    alt: string;
};

/** Third party information. */
type ThirdPartyInfo = {
    logo: ThirdPartyLogo;
    link: string;
    details?: boolean;
};
github webhintio / hint / packages / parser-babel-config / src / parser.ts View on Github external
public constructor(engine: Engine) {
        super(engine, 'babel-config');
        this.schema = loadJSONFile(path.join(__dirname, 'schema.json'));

        /**
         * package.json => type: 'json' (file type from extention).
         */
        engine.on('fetch::end::json', this.parseBabelConfig.bind(this));
    }