semver#parse TypeScript Examples

The following examples show how to use semver#parse. 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: package.ts    From WebCord with MIT License 6 votes vote down vote up
constructor(keys: T, packageJsonFile?: string) {
        const file = packageJsonFile ?? this.findProjectPackageJson();
        const packageJSON: unknown = JSON.parse(readFileSync(file).toString());
        if (!this.isPackageJsonComplete(packageJSON))
            throw new TypeError("While parsing `package.json`: "+this.checkPackageJsonComplete(packageJSON));
        const newObj = ({} as Pick<PackageJsonProperties, T[number]>)
        for (const key of Array.from(new Set(keys)))
            (newObj as Record<string,unknown>)[key] = packageJSON[key]
        this.data = newObj;
    }
Example #2
Source File: package.ts    From WebCord with MIT License 4 votes vote down vote up
/** A function used to return the details about the `package.json` wrong configuration. */
    private checkPackageJsonComplete(object: unknown): string {
        // Check 1: `package.json` is a JSON object.
        if(!(object instanceof Object))
            return "'object' is actually not an object (but '"+typeof object+"')!"
        else for(const key in object)
            if(typeof key !== "string")
                return "'object' keys are not of the type 'string'."

        // Check 2: 'contributors' is array of 'Person'
        if ((object as PackageJsonProperties).contributors instanceof Object)
            for (const key of (object as Record<string, Array<unknown>>)["contributors"]??[])
                if (!this.isPerson(key)) return "Contributors field is of invalid type.";
    
        // Check 3: 'author' is 'Person' when definied
        if((object as PackageJsonProperties).author !== undefined)
            if (!this.isPerson((object as PackageJsonProperties).author))
                return "Author field is of invalid type.";
    
        // Check 4: 'name', 'description' and 'license' are strings.
        for (const stringKey of ['name', 'description', 'license'])
            if (typeof ((object as { [key: string]: string; })[stringKey]) !== 'string')
                return "'"+stringKey+"' is not assignable to type 'string'.";
        
        // Check 5: 'repository' is either string or object
        if (typeof (object as PackageJsonProperties).repository !== "string" && typeof (object as PackageJsonProperties).repository !== "object")
            return "Repository field is neither of type 'string' nor 'object'.";
    
        // Check 6: As object, 'repository' has 'type' and 'url' keys of type 'string'
        for (const stringKey of ['type', 'url']) {
            const repository = (object as PackageJsonProperties).repository;
            if (typeof (repository) === "object" && typeof ((repository as { [key: string]: string; })[stringKey]) !== "string")
                return "Repository object does not contain a '"+stringKey+"' property.";
        }
    
        // Check 7: `name` field is correct package name.
        if((object as PackageJsonProperties).name.match(/^(?:@[a-z0-9-*~][a-z0-9-*._~]*\/)?[a-z0-9-~][a-z0-9-._~]*$/) === null)
            return "'"+(object as PackageJsonProperties).name+"' is not a valid Node.js package name.";
    
        // Check 8: `version` is a `semver`-parsable string
        if(typeof (object as PackageJsonProperties).version === 'string') {
            if (parse((object as PackageJsonProperties).version) === null)
                return "Version "+(object as PackageJsonProperties).version+" can't be parsed to 'semver'.";
        } else {
            return "Version property is not assignable to type 'string'!"
        }

        // Check 9: `devDependencies` or `dependencies` are either `undefinied` or `Record<string,string>`:
        for(const key of ["dependencies", "devDependencies"]) {
            const testValue = (object as Record<string,unknown|undefined>)[key]
            if (!/undefined|object/.test(typeof testValue))
                return "Property '"+key+"' is of invalid type!"
            else if(testValue instanceof Object) {
                for(const key in testValue)
                    if(typeof key !== "string") {
                        const key2string:unknown = (key as Record<string,unknown>)?.toString();
                        let keyString:string;
                        if(typeof key2string === "string")
                            keyString = key2string
                        else
                            keyString = "[unknown]"
                        return "Package name '"+keyString+"' is not a valid 'string'."
                    } else if (typeof (testValue as Record<string, unknown>)[key] !== "string")
                        return "Version of the package '"+key+"' is not of type 'string'."
            }
        }

        // Check 10: `license` is a valid string:
        if(!/^UNLICEN[SC]ED|SEE LICEN[CS]E IN .+$/.test((object as PackageJsonProperties).license))
            try {
                spdxParse((object as PackageJsonProperties).license)
            } catch {
                return "License field is in wrong format."
            }


        // Check 11: `homepage` is either `undefinied` or `string`
        if((object as PackageJsonProperties).homepage !== undefined && typeof (object as PackageJsonProperties).homepage !== "string")
            return "Homepage property is neither 'string' nor 'undefinied'."
        // All checks passed!
        return "";
    }