Skip to content

Commit

Permalink
Rename options to es5 and double
Browse files Browse the repository at this point in the history
  • Loading branch information
deadratfink committed Oct 5, 2017
1 parent 195d1fa commit a3d5c57
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 83 deletions.
9 changes: 5 additions & 4 deletions .jestrc.js
Expand Up @@ -20,16 +20,17 @@ module.exports = {
},
mapCoverage: true,
testMatch: [
// '**/test/functional/test-jyt-cli.js',
// '**/test/unit/test-serialize-utils.js',
// '**/test/functional/test-jyt-cli.js',
// '**/test/unit/validation/test-joi-extensions-file-helper.js',
// '**/test/unit/validation/test-joi-extensions-identifier-helper.js',
//'**/test/functional/test-transformer.js',
// '**/test/unit/test-index.js',
//'**/test/unit/test-reader.js',
// '**/test/unit/test-writer.js',
// '**/test/unit/validation/test-options-schema.js',
'**/test/unit/**/*.js',
'**/test/functional/**/*.js',
//'**/test/unit/validation/test-options-schema.js',
'**/test/unit/**/*.js',
'**/test/functional/**/*.js',
//'**/test/unit/validation/test-options-schema-helper.js',


Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -72,7 +72,7 @@
"jsdoc-to-markdown": "~3.0.0",
"nsp": "~2.8.0",
"package-json-to-readme": "~2.0.0",
"winston": "~2.4.0"
"winston": "^2.4.0"
},
"preferGlobal": true,
"bin": {
Expand Down
36 changes: 22 additions & 14 deletions src/cli.js
Expand Up @@ -12,8 +12,8 @@ import {
TYPE_JSON,
TYPE_YAML,
DEFAULT_STRICT,
DEFAULT_NO_ES6,
DEFAULT_NO_SINGLE_QUOTES,
DEFAULT_ES5,
DEFAULT_DOUBLE_QUOTES,
} from './constants';
import { transform } from './transformer';

Expand Down Expand Up @@ -53,24 +53,32 @@ const packagePath = path.join(__dirname, '../package.json');
* @private
*/
const cliOptionsSchema = {
origin: ['o', 'The origin type of INPUT-FILE: [ ' + TYPE_JS + ' | ' + TYPE_JSON + ' | ' + TYPE_YAML + ' ]',
origin: [
'o', 'The origin type of INPUT-FILE: [ ' + TYPE_JS + ' | ' + TYPE_JSON + ' | ' + TYPE_YAML + ' ]',
'string', ORIGIN_DESCRIPTION],
target: ['t', 'The target type of OUTPUT-FILE: [ ' + TYPE_JS + ' | ' + TYPE_JSON + ' | ' + TYPE_YAML + ' ]',
target: [
't', 'The target type of OUTPUT-FILE: [ ' + TYPE_JS + ' | ' + TYPE_JSON + ' | ' + TYPE_YAML + ' ]',
'string', TARGET_DESCRIPTION],
indent: ['i', 'The indention for pretty-print: 1 - 8', 'int', DEFAULT_INDENT],
force: ['f', 'Force overwriting of existing output files on write phase: when files are not overwritten (which' +
force: [
'f', 'Force overwriting of existing output files on write phase: when files are not overwritten (which' +
' is default), then the next transformation with same output file name gets a consecutive number on the base' +
' file name, e.g. in case of foo.yaml it would be foo(1).yaml', 'boolean', DEFAULT_FORCE_FILE_OVERWRITE],
imports: ['m', 'Define an identifier for object (to read as "export const identifier / module.exports[.identifier]"' +
' from JS source file only, must be a valid JS identifier!)', 'string', DEFAULT_JS_IMPORTS_IDENTIFIER],
exports: ['x', 'Define an identifier for object (write to "export const identifier / module.exports[.identifier]"' +
' in JS destination file only, must be a valid JS identifier!)', 'string', DEFAULT_JS_EXPORTS_IDENTIFIER],
strict: ['s', 'Whether to write a "use strict;" in JS type output',
imports: [
'm', 'Define an identifier for object (to read as "export const identifier / module.exports[.identifier]"' +
' from JS source file only, must be a valid JS identifier!)', 'string', DEFAULT_JS_IMPORTS_IDENTIFIER],
exports: [
'x', 'Define an identifier for object (write to "export const identifier / module.exports[.identifier]"' +
' in JS destination file only, must be a valid JS identifier!)', 'string', DEFAULT_JS_EXPORTS_IDENTIFIER],
strict: [
's', 'Whether to write a "use strict;" in JS type output',
'boolean', DEFAULT_STRICT],
'no-es6': [false, 'Whether not to use ECMAScript6 syntax for JS type output like "module.exports" instead of ' +
'"export default", applicable only for JS output', 'boolean', DEFAULT_NO_ES6],
'no-single': [false, 'Whether not to use single-quotes style for values in JS type output (i.e. double-quotes)',
'boolean', DEFAULT_NO_SINGLE_QUOTES],
es5: [
false, 'Whether not to use ECMAScript6 syntax for JS type output like "module.exports" instead of ' +
'"export default", applicable only for JS output', 'boolean', DEFAULT_ES5],
double: [
false, 'Whether not to use single-quotes style for values in JS type output (i.e. double-quotes)',
'boolean', DEFAULT_DOUBLE_QUOTES],
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/constants.js
Expand Up @@ -120,15 +120,15 @@ export const DEFAULT_STRICT = false;
* @constant
* @private
*/
export const DEFAULT_NO_ES6 = false;
export const DEFAULT_ES5 = false;

/**
* Whether _not_ to use single-quotes style for values in JS type output (i.e. double-quotes).
* @type {boolean}
* @constant
* @private
*/
export const DEFAULT_NO_SINGLE_QUOTES = false;
export const DEFAULT_DOUBLE_QUOTES = false;

/**
* The `origin` description value.
Expand Down
16 changes: 8 additions & 8 deletions src/serialize-utils.js
Expand Up @@ -11,17 +11,17 @@ import serializeJs from 'serialize-js';
/**
* Creates a potential named `'module.exports[.exportsTo]'` string.
*
* @param {boolean} es6 - Whether to use ECMAScript6 export syntax.
* @param {boolean} es5 - Whether to use ECMAScript5 export syntax.
* @param {string} [exportsTo] - The export name.
* @returns {Promise.<string>} Resolves with the exports string.
* @private
*/
export async function createExportString(es6, exportsTo) {
let exports = es6 ? 'export' : 'module.exports';
export async function createExportString(es5, exportsTo) {
let exports = es5 ? 'module.exports' : 'export';
if (exportsTo) {
exports += es6 ? ` const ${exportsTo} = ` : '.' + exportsTo + ' = ';
exports += es5 ? '.' + exportsTo + ' = ' : ` const ${exportsTo} = `;
} else {
exports += es6 ? ' default ' : ' = ';
exports += es5 ? ' = ' : ' default ';
}
return exports;
}
Expand All @@ -37,13 +37,13 @@ export async function createExportString(es6, exportsTo) {
export async function serializeJsToString(object, options) {
let useStrict = '';
if (options.strict) {
const quote = options['no-single'] ? '"' : '\'';
const quote = options.double ? '"' : '\'';
useStrict = `${quote}use strict;${quote}${os.EOL}${os.EOL}`;
}
const exportsStr = await createExportString(!options['no-es6'], options.exports);
const exportsStr = await createExportString(options.es5, options.exports);
return `${useStrict}${exportsStr}${serializeJs.serialize(object, {
indent: options.indent,
forceSingleQuotes: !options['no-single'],
forceSingleQuotes: !options.double,
})};${os.EOL}`;
}

Expand Down
70 changes: 36 additions & 34 deletions src/validation/options-schema.js
Expand Up @@ -15,8 +15,8 @@ import {
MIN_YAML_INDENT,
MAX_INDENT,
DEFAULT_STRICT,
DEFAULT_NO_ES6,
DEFAULT_NO_SINGLE_QUOTES,
DEFAULT_ES5,
DEFAULT_DOUBLE_QUOTES,
} from '../constants';

/**
Expand Down Expand Up @@ -48,24 +48,24 @@ const strictSchema = Joi
.description('Whether to write a "use strict;" in JS type output.');

/**
* The `no-es6` option schema.
* The `es5` option schema.
* @type {external:joi.Schema}
* @private
*/
const noES6Schema = Joi
const es5Schema = Joi
.boolean()
.default(DEFAULT_NO_ES6)
.default(DEFAULT_ES5)
.description('Whether not to use ECMAScript6 syntax for JS type output like "module.exports" instead of ' +
'"export default", applicable only for JS output.');

/**
* The `no-single` option schema.
* The `double` option schema.
* @type {external:joi.Schema}
* @private
*/
const noSingleSchema = Joi
const doubleSchema = Joi
.boolean()
.default(DEFAULT_NO_SINGLE_QUOTES)
.default(DEFAULT_DOUBLE_QUOTES)
.description('Whether not to use single-quotes style for values in JS type output (i.e. double-quotes).');

/**
Expand Down Expand Up @@ -193,8 +193,8 @@ export const writeOptionsSchema = Joi.object().keys({
indent: indentSchema,
force: forceSchema,
strict: strictSchema,
'no-es6': noES6Schema,
'no-single': noSingleSchema,
es5: es5Schema,
double: doubleSchema,
}).default()
.required()
.unknown();
Expand All @@ -205,30 +205,32 @@ export const writeOptionsSchema = Joi.object().keys({
* @constant
* @private
*/
export const transformOptionsSchema = readOptionsSchema.concat(Joi.object().keys({
transform: Joi
.func()
.arity(1)
.default(object => object)
.description('The transformation function to alter a read object.'),
dest: Joi
.alternatives().try(
Joi.string()
.min(1)
.label('dest - OUTPUT-FILE'),
Joi.object().type(Stream.Writable),
Joi.object().type(Object),
)
.default(inferDestDefaultFromSrc, 'try dest resolution from src if not set')
.description('The output destination (if string type is treated as a file path).'),
target: targetSchema,
exports: exportsSchema,
indent: indentSchema,
force: forceSchema,
strict: strictSchema,
'no-es6': noES6Schema,
'no-single': noSingleSchema,
}).default()
export const transformOptionsSchema = readOptionsSchema.concat(Joi
.object()
.keys({
transform: Joi
.func()
.arity(1)
.default(object => object)
.description('The transformation function to alter a read object.'),
dest: Joi
.alternatives().try(
Joi.string()
.min(1)
.label('dest - OUTPUT-FILE'),
Joi.object().type(Stream.Writable),
Joi.object().type(Object),
)
.default(inferDestDefaultFromSrc, 'try dest resolution from src if not set')
.description('The output destination (if string type is treated as a file path).'),
target: targetSchema,
exports: exportsSchema,
indent: indentSchema,
force: forceSchema,
strict: strictSchema,
es5: es5Schema,
double: doubleSchema,
}).default()
.required()
);

Expand Down
6 changes: 3 additions & 3 deletions test/functional/test-jyt-cli.js
Expand Up @@ -72,8 +72,8 @@ const CLI_OPTIONS_LONG_TO_SHORT_MAP = {
imports: '-m',
exports: '-x',
strict: '-s',
'no-es6': '--no-es6', // no short available
'no-single': '--no-single', // no short available
es5: '--es5', // no short available
double: '--double', // no short available
};

/**
Expand Down Expand Up @@ -211,7 +211,7 @@ describe(TEST_SUITE_DESCRIPTION_FUNCTIONAL + ' - ./jyt -> ./src/cli.js - ', () =
src: path.resolve(CLI_TEST_BASE_DIR + '/test-data.yaml'),
dest: path.resolve(DEST_NO_ES6),
target: TYPE_JS,
'no-es6': true,
es5: true,
}));
logger.debug(msg);
const stats = fs.statSync(DEST_NO_ES6);
Expand Down
4 changes: 2 additions & 2 deletions test/functional/test-transformer.js
Expand Up @@ -209,7 +209,7 @@ describe(TEST_SUITE_DESCRIPTION_FUNCTIONAL + ' - transformer - ', () => {
transform: transformFunc,
target: TYPE_JS,
strict: true,
'no-single': true,
double: true,
});
logger.debug(msg);
const stats = fs.statSync(DEST_DQ_AND_STRICT);
Expand All @@ -227,7 +227,7 @@ describe(TEST_SUITE_DESCRIPTION_FUNCTIONAL + ' - transformer - ', () => {
dest: path.resolve(DEST_NO_ES6),
transform: transformFunc,
target: TYPE_JS,
'no-es6': true,
es5: true,
});
logger.debug(msg);
const stats = fs.statSync(DEST_NO_ES6);
Expand Down
18 changes: 9 additions & 9 deletions test/unit/test-serialize-utils.js
Expand Up @@ -26,25 +26,25 @@ describe(TEST_SUITE_DESCRIPTION_UNIT + ' - serialize-utils - ', () => {
describe('Function createExportString', () => {
it('should create ES6 default export', async () => {
expect.assertions(1);
const result = await createExportString(true);
const result = await createExportString(false);
expect(result).toBe('export default ');
});

it('should create "module.exports"', async () => {
expect.assertions(1);
const result = await createExportString(false);
const result = await createExportString(true);
expect(result).toBe('module.exports = ');
});

it('should create ES6 default export with named export', async () => {
expect.assertions(1);
const result = await createExportString(true, 'foo');
const result = await createExportString(false, 'foo');
expect(result).toBe(`export const ${namedExport} = `);
});

it('should create "module.exports" with named export', async () => {
expect.assertions(1);
const result = await createExportString(false, 'foo');
const result = await createExportString(true, 'foo');
expect(result).toBe(`module.exports.${namedExport} = `);
});
});
Expand All @@ -55,7 +55,7 @@ describe(TEST_SUITE_DESCRIPTION_UNIT + ' - serialize-utils - ', () => {
const result = await serializeJsToString(toSerializeToJs, {
strict: true,
indent: indent.length,
'no-es6': false,
es5: false,
});
expect(result).toBe(
`'use strict;'${nl}${nl}export default {${nl}${indent}foo: 'bar',${nl}${indent}bar: {bar: 'bar'}${nl}};${nl}`
Expand All @@ -67,7 +67,7 @@ describe(TEST_SUITE_DESCRIPTION_UNIT + ' - serialize-utils - ', () => {
const result = await serializeJsToString(toSerializeToJs, {
strict: false,
indent: indent.length,
'no-es6': false,
// es5: false,
});
expect(result).toBe(`export default {${nl}${indent}foo: 'bar',${nl}${indent}bar: {bar: 'bar'}${nl}};${nl}`);
});
Expand All @@ -77,11 +77,11 @@ describe(TEST_SUITE_DESCRIPTION_UNIT + ' - serialize-utils - ', () => {
const result = await serializeJsToString(toSerializeToJs, {
strict: true,
indent: indent.length,
'no-es6': false,
'no-single': true,
es5: true,
double: true,
});
expect(result).toBe(
`"use strict;"${nl}${nl}export default {${nl}${indent}foo: "bar",${nl}${indent}bar: {bar: "bar"}${nl}};${nl}`
`"use strict;"${nl}${nl}module.exports = {${nl}${indent}foo: "bar",${nl}${indent}bar: {bar: "bar"}${nl}};${nl}`
);
});
});
Expand Down

0 comments on commit a3d5c57

Please sign in to comment.