How to use the @hint/utils-network.asPathString function in @hint/utils-network

To help you get started, we’ve selected a few @hint/utils-network 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 / utils-tests-helpers / src / hint-runner.ts View on Github external
const requestSource = async (url: string, connector: string): Promise => {
    try {
        if (connector === 'local') {
            return await readFileAsync(asPathString(getAsUri(url)!));
        }

        /*
         * Allow us to use our self-signed cert for testing.
         * https://github.com/request/request/issues/418#issuecomment-23058601
         */
        return await requestAsync({
            rejectUnauthorized: false,
            strictSSL: false,
            url
        });
    } catch (e) {
        // Some tests deliberately use invalid URLs (e.g. `test:`).
        return '';
    }
};
github webhintio / hint / packages / parser-webpack-config / src / parser.ts View on Github external
const fileName = path.basename(resource);

        /*
         * In webpack documentation, this is the file name they
         * always use: https://webpack.js.org/configuration/
         */
        if (fileName !== 'webpack.config.js') {
            return;
        }

        this.configFound = true;

        await this.engine.emitAsync(`parse::start::webpack-config`, { resource });

        try {
            const config: webpack.Configuration = await import(asPathString(getAsUri(resource)!)); // `getAsUri(resource)` should not be null as the resource has already been fetched.

            const version = await this.getLocallyInstalledWebpack();

            if (!version) {
                await this.engine.emitAsync('parse::error::webpack-config::not-install', {
                    error: new Error('webpack is not installed'),
                    resource
                });

                return;
            }

            await this.engine.emitAsync('parse::end::webpack-config', {
                config,
                resource,
                version
github webhintio / hint / packages / utils-json / src / final-config.ts View on Github external
export const finalConfig =  (config: T, resource: string): T | IParsingError => {
    if (!config.extends) {
        return config;
    }

    const configIncludes = [];

    // `resource` has already been loaded to provide `config` so `getAsUri` won't be null.
    let configPath = asPathString(getAsUri(resource)!);

    /*
     * `configPath` will have the format c:/path or /path
     * depending on what OS we are running sonar.
     * In case that we are running on Windows, we need
     * to normalize the path to c:\path before continue.
     */
    configIncludes.push(path.normalize(configPath));

    let finalConfigJSON: T = merge({}, config);

    while (finalConfigJSON.extends) {
        const lastPath = configPath;
        const configDir = path.dirname(configPath);

        configPath = path.resolve(configDir, finalConfigJSON.extends);
github webhintio / hint / packages / connector-local / src / connector.ts View on Github external
public async fetchContent(target: string, headers?: object, options?: IFetchOptions): Promise {
        /*
         * target can have one of these forms:
         *   - /path/to/file
         *   - C:/path/to/file
         *   - file:///path/to/file
         *   - file:///C:/path/to/file
         *
         * That's why we need to parse it to an URL
         * and then get the path string.
         */
        const uri = getAsUri(target);
        const filePath: string = uri ? asPathString(uri).replace('%20', ' ') : '';
        const rawContent: Buffer = options && options.content ? Buffer.from(options.content) : await readFileAsBuffer(filePath);
        const contentType = getContentTypeData(null as any, filePath, null, rawContent);
        let content = '';

        if (isTextMediaType(contentType.mediaType || '')) {
            content = rawContent.toString(contentType.charset || undefined);
        }

        // Need to do some magic to create a fetch::end::*
        return {
            request: {} as any,
            response: {
                body: {
                    content,
                    rawContent,
                    rawResponse() {
github webhintio / hint / packages / connector-local / src / connector.ts View on Github external
public async collect(target: URL, options?: IFetchOptions) {
        if (target.protocol !== 'file:') {
            throw new Error('Connector local only works with local files or directories');
        }

        /** The target in string format */
        const href: string = this._href = target.href;
        const initialEvent: Event = { resource: href };

        this.engine.emitAsync('scan::start', initialEvent);

        const pathString = asPathString(target).replace('%20', ' ');
        let files: string[];

        if (isFile(pathString)) {
            await this.engine.emitAsync('fetch::start::target', initialEvent);
            files = [pathString];
        } else {
            files = await globby(this.filesPattern, ({
                absolute: true,
                cwd: pathString,
                dot: true,
                gitignore: true
            }));

            // Ignore options.content when matching multiple files
            if (options && options.content) {
                options.content = undefined;