1
0
Fork 0

feat(type): add env, include, regexp, and stream

This commit is contained in:
ssube 2019-11-13 08:20:34 -06:00
parent 33c2936275
commit 133880c9f8
Signed by: ssube
GPG Key ID: 3EED7B957D362AF1
19 changed files with 168 additions and 7 deletions

View File

@ -2,6 +2,7 @@
"names": [
"async_hooks",
"chai",
"js-yaml",
"sinon"
]
}

View File

@ -2,5 +2,8 @@
"node_modules/chai/index.js": [
"expect",
"use"
],
"node_modules/lodash/lodash.js": [
"isNil"
]
}

12
docs/api/index.md Normal file
View File

@ -0,0 +1,12 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md)
## API Reference
## Packages
| Package | Description |
| --- | --- |
| [@apextoaster/js-yaml-schema](./js-yaml-schema.md) | |

View File

@ -0,0 +1,6 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->
[Home](./index.md) &gt; [@apextoaster/js-yaml-schema](./js-yaml-schema.md)
## js-yaml-schema package

View File

@ -1,10 +1,22 @@
{
"name": "@apextoaster/js-yaml-schema",
"version": "0.1.0",
"description": "Extended JS-YAML schema",
"keywords": [
"js-yaml",
"schema"
],
"main": "out/main.js",
"author": "ssube",
"license": "MIT",
"devDependencies": {
"@istanbuljs/nyc-config-typescript": "^0.1.3",
"@microsoft/api-documenter": "^7.5.8",
"@microsoft/api-extractor": "^7.5.6",
"@types/chai": "^4.2.5",
"@types/chai-as-promised": "^7.1.2",
"@types/js-yaml": "^3.12.1",
"@types/lodash": "^4.14.146",
"@types/mocha": "^5.2.7",
"@types/sinon-chai": "^3.2.3",
"@types/source-map-support": "^0.5.0",
@ -20,7 +32,10 @@
"eslint-plugin-mocha": "^6.2.1",
"eslint-plugin-no-null": "^1.0.2",
"eslint-plugin-sonarjs": "^0.5.0",
"js-yaml": "^3.13.1",
"lodash": "^4.17.15",
"mocha": "^6.2.2",
"noicejs": "^3.0.0",
"nyc": "^14.1.1",
"rollup": "^1.27.0",
"rollup-plugin-commonjs": "^10.1.0",
@ -47,5 +62,11 @@
},
"peerDependencies": {
"js-yaml": "^3.13.1"
},
"nyc": {
"extends": "@istanbuljs/nyc-config-typescript"
},
"publishConfig": {
"registry": "https://registry.npmjs.org"
}
}

7
src/app.ts Normal file
View File

@ -0,0 +1,7 @@
import { VERSION_INFO } from './version';
export async function main(argv: Array<string>): Promise<number> {
/* eslint-disable-next-line no-console */
console.log('Please use this schema with js-yaml.', VERSION_INFO, argv);
return 1;
}

View File

@ -0,0 +1,7 @@
import { BaseError } from 'noicejs';
export class InvalidArgumentError extends BaseError {
constructor(msg = 'invalid argument passed', ...nested: Array<Error>) {
super(msg, ...nested);
}
}

View File

@ -0,0 +1,7 @@
import { BaseError } from 'noicejs';
export class NotFoundError extends BaseError {
constructor(msg = 'value not found', ...nested: Array<Error>) {
super(msg, ...nested);
}
}

View File

@ -1,5 +1,7 @@
import { CONFIG_SCHEMA } from './schema';
import { VERSION_INFO } from './version';
export default {
CONFIG_SCHEMA,
VERSION_INFO,
};

View File

@ -5,7 +5,6 @@ import { includeType } from './type/Include';
import { regexpType } from './type/Regexp';
import { streamType } from './type/Stream';
export const CONFIG_ENV = 'SALTY_HOME';
export const CONFIG_SCHEMA = Schema.create([DEFAULT_SAFE_SCHEMA], [
envType,
includeType,

View File

@ -1,6 +1,6 @@
import { Type as YamlType } from 'js-yaml';
import { NotFoundError } from '../../error/NotFoundError';
import { NotFoundError } from '../error/NotFoundError';
export const envType = new YamlType('!env', {
kind: 'scalar',

View File

@ -1,9 +1,9 @@
import { existsSync, readFileSync, realpathSync } from 'fs';
import { SAFE_SCHEMA, safeLoad, Type as YamlType } from 'js-yaml';
import { BaseError } from 'noicejs';
import { join } from 'path';
import { NotFoundError } from '../../error/NotFoundError';
import { InvalidArgumentError } from '../error/InvalidArgumentError';
import { NotFoundError } from '../error/NotFoundError';
// work around the circular dependency by setting the schema later
export const includeSchema = {
@ -33,7 +33,7 @@ export const includeType = new YamlType('!include', {
schema: includeSchema.schema,
});
} catch (err) {
throw new BaseError('error including file', err);
throw new InvalidArgumentError('error including file', err);
}
},
});

View File

@ -1,7 +1,7 @@
import { Type as YamlType } from 'js-yaml';
import { isNil } from 'lodash';
import { InvalidArgumentError } from '../../error/InvalidArgumentError';
import { InvalidArgumentError } from '../error/InvalidArgumentError';
export const REGEXP_REGEXP = /^\/(.+)\/([gimsuy]*)$/;

View File

@ -1,6 +1,6 @@
import { Type as YamlType } from 'js-yaml';
import { NotFoundError } from '../../error/NotFoundError';
import { NotFoundError } from '../error/NotFoundError';
const ALLOWED_STREAMS = new Set([
'stdout',

22
test/type/TestEnv.ts Normal file
View File

@ -0,0 +1,22 @@
import { expect } from 'chai';
import { NotFoundError } from '../../src/error/NotFoundError';
import { envType } from '../../src/type/Env';
import { VERSION_INFO } from '../../src/version';
import { describeLeaks, itLeaks } from '../helpers/async';
describeLeaks('env config type', async () => {
itLeaks('should throw on missing variables', async () => {
expect(() => {
envType.resolve('DOES_NOT_EXIST_');
}).to.throw(NotFoundError);
});
itLeaks('should resolve existing variables', async () => {
expect(envType.resolve('CI_COMMIT_SHA')).to.equal(true);
});
itLeaks('should construct a value from variables', async () => {
expect(envType.construct('CI_COMMIT_SHA')).to.equal(VERSION_INFO.git.commit);
});
});

31
test/type/TestInclude.ts Normal file
View File

@ -0,0 +1,31 @@
import { expect } from 'chai';
import { join } from 'path';
import { InvalidArgumentError } from '../../src/error/InvalidArgumentError';
import { NotFoundError } from '../../src/error/NotFoundError';
import { includeType } from '../../src/type/Include';
import { describeLeaks, itLeaks } from '../helpers/async';
const TEST_ROOT = '../test/type';
describeLeaks('include config type', async () => {
itLeaks('should resolve existing files', async () => {
expect(includeType.resolve(join(TEST_ROOT, 'include.yml'))).to.equal(true);
});
itLeaks('should throw when resolving missing files', async () => {
expect(() => {
includeType.resolve(join(TEST_ROOT, 'missing.yml'));
}).to.throw(NotFoundError);
});
itLeaks('should construct data from file', async () => {
expect(includeType.construct(join(TEST_ROOT, 'include.yml'))).to.equal('test');
});
itLeaks('should throw when constructing missing files', async () => {
expect(() => {
includeType.construct(join(TEST_ROOT, 'missing.yml'));
}).to.throw(InvalidArgumentError);
});
});

27
test/type/TestRegexp.ts Normal file
View File

@ -0,0 +1,27 @@
import { expect } from 'chai';
import { regexpType } from '../../src/type/Regexp';
import { describeLeaks, itLeaks } from '../helpers/async';
describeLeaks('regexp config type', async () => {
itLeaks('match slashed strings', async () => {
expect(regexpType.resolve('/foo/')).to.equal(true);
});
itLeaks('should match flags', async () => {
const regexp: RegExp = regexpType.construct('/foo/g');
expect(regexp.flags).to.equal('g');
});
itLeaks('should not match bare strings', async () => {
expect(regexpType.resolve('foo')).to.equal(false);
});
itLeaks('should not match invalid flags', async () => {
expect(regexpType.resolve('/foo/notrealflags')).to.equal(false);
});
itLeaks('should not match regex embedded in a longer string', async () => {
expect(regexpType.resolve('some/regex/with-padding')).to.equal(false);
});
});

1
test/type/include.yml Normal file
View File

@ -0,0 +1 @@
test

View File

@ -239,11 +239,21 @@
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/estree/-/estree-0.0.39.tgz#e177e699ee1b8c22d23174caaa7422644389509f"
integrity sha512-EYNwp3bU+98cpU4lAWYYL7Zz+2gryWH1qbdDTidVd6hkiR6weksdbMadyXKXNPEkQFhXM+hVO9ZygomHXp+AIw==
"@types/js-yaml@^3.12.1":
version "3.12.1"
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/js-yaml/-/js-yaml-3.12.1.tgz#5c6f4a1eabca84792fbd916f0cb40847f123c656"
integrity sha512-SGGAhXLHDx+PK4YLNcNGa6goPf9XRWQNAUUbffkwVGGXIxmDKWyGGL4inzq2sPmExu431Ekb9aEMn9BkPqEYFA==
"@types/json-schema@^7.0.3":
version "7.0.3"
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"
integrity sha512-Il2DtDVRGDcqjDtE+rF8iqg1CArehSK84HZJCT7AMITlyXRBpuPhqGLDQMowraqqu1coEaimg4ZOqggt6L6L+A==
"@types/lodash@^4.14.146":
version "4.14.146"
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/lodash/-/lodash-4.14.146.tgz#de0d2c8610012f12a6a796455054cbc654f8fecf"
integrity sha512-JzJcmQ/ikHSv7pbvrVNKJU5j9jL9VLf3/gqs048CEnBVVVEv4kve3vLxoPHGvclutS+Il4SBIuQQ087m1eHffw==
"@types/mocha@^5.2.7":
version "5.2.7"
resolved "https://artifacts.apextoaster.com/repository/group-npm/@types/mocha/-/mocha-5.2.7.tgz#315d570ccb56c53452ff8638738df60726d5b6ea"
@ -2281,6 +2291,11 @@ node-environment-flags@1.0.5:
object.getownpropertydescriptors "^2.0.3"
semver "^5.7.0"
noicejs@^3.0.0:
version "3.0.0"
resolved "https://artifacts.apextoaster.com/repository/group-npm/noicejs/-/noicejs-3.0.0.tgz#7fca4008c6c9c78ee4ca4b04bb179b424c23cb4c"
integrity sha512-DZFqSAgsh3nxDfHi/ncgLFtN2G2iWTJdOZQU6LQILAVcs7RX57OSR1+FS0WVL8FAZfd47WC4xhN8EGrNzKuSvQ==
normalize-package-data@^2.3.0, normalize-package-data@^2.3.2, normalize-package-data@^2.3.4, normalize-package-data@^2.3.5:
version "2.5.0"
resolved "https://artifacts.apextoaster.com/repository/group-npm/normalize-package-data/-/normalize-package-data-2.5.0.tgz#e66db1838b200c1dfc233225d12cb36520e234a8"